[PATCH 5 of 6 saem_ref] [rdf] Prefetch ORM cache for activities when exporting ConceptScheme

Denis Laxalde denis.laxalde at logilab.fr
Thu Mar 16 13:57:13 CET 2017


Sylvain Thenault a écrit :
> # HG changeset patch
> # User Sylvain Thénault <sylvain.thenault at logilab.fr>
> # Date 1489576155 -3600
> #      Wed Mar 15 12:09:15 2017 +0100
> # Node ID a6ecc94cec9d3d91868ab20f911b9557b13ba998
> # Parent  06537e435c3d0c4281597b0311430ed17b5aadf0
> [rdf] Prefetch ORM cache for activities when exporting ConceptScheme
>
> has this may take age on medium sized scheme.
>
> Related to #12175187
>
> diff --git a/cubicweb_saem_ref/entities/rdf.py b/cubicweb_saem_ref/entities/rdf.py
> --- a/cubicweb_saem_ref/entities/rdf.py
> +++ b/cubicweb_saem_ref/entities/rdf.py
> @@ -279,10 +279,40 @@ class ConceptSchemeRDFPrimaryAdapter(sko
>
>      def accept(self, entity):
>          """Return True if the entity should be recursivly added to the graph."""
>          return entity.cw_etype == 'Activity'
>
> +    def warm_caches(self):
> +        super(ConceptSchemeRDFPrimaryAdapter, self).warm_caches()
> +        scheme = self.entity
> +        cnx = self._cw
> +
> +        activities, rset = _warm_activity_cache(cnx, scheme, 'CS identity X, A generated X')
> +        scheme._cw_related_cache['generated_object'] = (rset, activities)
> +        scheme._cw_related_cache['used_object'] = (rset, activities)
> +
> +        activities, rset = _warm_activity_cache(cnx, scheme, 'X in_scheme CS, A generated X')
> +        concepts = tuple(rset.entities(1))
> +        skos.cache_entities_relations(concepts, rset, 'generated', 'object',
> +                                      entity_col=0, target_col=1)
> +        skos.cache_entities_relations(concepts, rset, 'used', 'object',
> +                                      entity_col=0, target_col=1)
> +
> +
> +def _warm_activity_cache(cnx, scheme, rql_expr):
> +    activities_rql = skos._select_attributes(
> +        'Any X,A WHERE CS eid %(cs)s,' + rql_expr,
> +        'A', _ACTIVITY_ATTRIBUTES)
> +    rset = cnx.execute(activities_rql, {'cs': scheme.eid})
> +    activities = tuple(rset.entities(1))
> +    skos.cache_entities_relations(activities, rset, 'generated', 'subject')
> +    skos.cache_entities_relations(activities, rset, 'used', 'subject')
> +    user_rset = cnx.execute('Any U,A WHERE A associated_with U, CS eid %(cs)s, ' + rql_expr,
> +                            {'cs': scheme.eid})
> +    skos.cache_entities_relations(activities, user_rset, 'associated_with', 'subject')
> +    return activities, rset
> +
>
>  class ConceptRDFPrimaryAdapter(skos.ConceptRDFPrimaryAdapter):
>
>      def register_rdf_mapping(self, reg):
>          super(ConceptRDFPrimaryAdapter, self).register_rdf_mapping(reg)
> diff --git a/test/test_entities_rdf.py b/test/test_entities_rdf.py
> --- a/test/test_entities_rdf.py
> +++ b/test/test_entities_rdf.py
> @@ -15,15 +15,18 @@
>  # with this program. If not, see <http://www.gnu.org/licenses/>.
>  """Tests for agent entities"""
>
>  import datetime
>
> +from logilab.common.decorators import monkeypatch
> +
>  from cubicweb.devtools.testlib import CubicWebTC
>
>  from cubes.skos.rdfio import RDFLibRDFGraph, RDFRegistry
>
>  from cubicweb_saem_ref import permanent_url
> +from cubicweb_saem_ref.entities import rdf
>
>  import testutils
>
>
>  class RDFExportTC(testutils.XmlTestMixin, CubicWebTC):
> @@ -227,11 +230,22 @@ class RDFExportTC(testutils.XmlTestMixin
>          with self.admin_access.client_cnx() as cnx:
>              scheme = testutils.setup_scheme(cnx, u'some vocab')
>              cnx.commit()
>              concept = scheme.add_concept(u'some concept')
>              cnx.commit()
> -            scheme.cw_clear_all_caches()
> +
> +        with self.admin_access.client_cnx() as cnx:
> +            scheme = cnx.entity_from_eid(scheme.eid)
> +            concept = cnx.entity_from_eid(concept.eid)
> +
> +            orig_warm_caches = rdf.ConceptSchemeRDFPrimaryAdapter.warm_caches
> +
> +            @monkeypatch(rdf.ConceptSchemeRDFPrimaryAdapter)
> +            def warm_caches(self):
> +                orig_warm_caches(self)
> +                # ensure no RQL further query is done
> +                self._cw.execute = lambda rql, args=None: 1 / 0
>

Could you explain this change in the commit message? If the intent is to
make sure some function is not called, a mock seems like the way to go.



More information about the saem-devel mailing list