Procházet zdrojové kódy

Remove some legacy constructs.

Stefano Cossu před 5 roky
rodič
revize
1e32195ce8

+ 2 - 2
lakesuperior/api/resource.py

@@ -274,8 +274,8 @@ def update_delta(uid, remove_trp, add_trp):
         add, as 3-tuples of RDFLib terms.
     """
     rsrc = LdpFactory.from_stored(uid)
-    remove_trp = rsrc.check_mgd_terms(SimpleGraph(remove_trp))
-    add_trp = rsrc.check_mgd_terms(SimpleGraph(add_trp))
+    remove_trp = rsrc.check_mgd_terms(remove_trp)
+    add_trp = rsrc.check_mgd_terms(add_trp)
 
     return rsrc.modify(RES_UPDATED, remove_trp, add_trp)
 

+ 4 - 2
lakesuperior/model/graph/graph.pxd

@@ -29,8 +29,10 @@ cdef class SimpleGraph:
         cc.key_compare_ft term_cmp_fn
         cc.key_compare_ft trp_cmp_fn
 
-        inline BufferTriple* store_triple(self, const BufferTriple* strp)
-        inline void add_triple(self, const BufferTriple *trp, bint add=*) except *
+        BufferTriple* store_triple(self, const BufferTriple* strp)
+        void add_triple(
+            self, const BufferTriple *trp, bint copy=*
+        ) except *
         int remove_triple(self, const BufferTriple* trp_buf) except -1
         bint trp_contains(self, const BufferTriple* btrp)
 

+ 6 - 4
lakesuperior/model/graph/graph.pyx

@@ -601,7 +601,7 @@ cdef class SimpleGraph:
 
 
     cdef inline void add_triple(
-        self, const BufferTriple* trp, bint add=False
+        self, const BufferTriple* trp, bint copy=False
     ) except *:
         """
         Add a triple from 3 (TPL) serialized terms.
@@ -610,10 +610,10 @@ cdef class SimpleGraph:
         also is only added if not existing.
 
         :param BufferTriple* trp: The triple to add.
-        :param bint add: if ``True``, the triple and term data will be
+        :param bint copy: if ``True``, the triple and term data will be
             allocated and copied into the graph memory pool.
         """
-        if add:
+        if copy:
             trp = self.store_triple(trp)
 
         logger.info('Inserting terms.')
@@ -886,7 +886,9 @@ cdef class Imr(SimpleGraph):
             the first found result is returned.
         :rtype: rdflib.term.Node
         """
-        values = self[p]
+        # TODO use slice.
+        values = {trp[2] for trp in self.lookup((self.uri, p, None))}
+        logger.info(f'Values found: {values}')
 
         if strict and len(values) > 1:
             raise RuntimeError('More than one value found for {}, {}.'.format(

+ 28 - 22
lakesuperior/model/ldp/ldpr.py

@@ -593,39 +593,43 @@ class Ldpr(metaclass=ABCMeta):
         return self.create_or_replace(create_only=False)
 
 
-    def check_mgd_terms(self, gr):
+    def check_mgd_terms(self, trp):
         """
         Check whether server-managed terms are in a RDF payload.
 
-        :param SimpleGraph gr: The graph to validate.
+        :param SimpleGraph trp: The graph to validate.
         """
-        offending_subjects = gr.terms_by_type('s') & srv_mgd_subjects
+        offending_subjects = (
+            {t[0] for t in trp if t[0] is not None} & srv_mgd_subjects
+        )
         if offending_subjects:
             if self.handling == 'strict':
                 raise ServerManagedTermError(offending_subjects, 's')
             else:
-                gr_set = set(gr)
                 for s in offending_subjects:
                     logger.info('Removing offending subj: {}'.format(s))
-                    for t in gr_set:
+                    for t in trp:
                         if t[0] == s:
-                            gr.remove(t)
+                            trp.remove(t)
 
-        offending_predicates = gr.terms_by_type('p') & srv_mgd_predicates
+        offending_predicates = (
+            {t[1] for t in trp if t[1] is not None} & srv_mgd_predicates
+        )
         # Allow some predicates if the resource is being created.
         if offending_predicates:
             if self.handling == 'strict':
                 raise ServerManagedTermError(offending_predicates, 'p')
             else:
-                gr_set = set(gr)
                 for p in offending_predicates:
                     logger.info('Removing offending pred: {}'.format(p))
-                    for t in gr_set:
+                    for t in trp:
                         if t[1] == p:
-                            gr.remove(t)
+                            trp.remove(t)
 
-        types = {t[2] for t in gr if t[1] == RDF.type}
-        offending_types = types & srv_mgd_types
+        offending_types = (
+            {t[2] for t in trp if t[1] == RDF.type and t[2] is not None}
+            & srv_mgd_types
+        )
         if not self.is_stored:
             offending_types -= self.smt_allow_on_create
         if offending_types:
@@ -634,11 +638,11 @@ class Ldpr(metaclass=ABCMeta):
             else:
                 for to in offending_types:
                     logger.info('Removing offending type: {}'.format(to))
-                    gr.remove_triples((None, RDF.type, to))
+                    trp.remove_triples((None, RDF.type, to))
 
-        #logger.debug('Sanitized graph: {}'.format(gr.serialize(
+        #logger.debug('Sanitized graph: {}'.format(trp.serialize(
         #    format='turtle').decode('utf-8')))
-        return gr
+        return trp
 
 
     def sparql_delta(self, qry_str):
@@ -671,16 +675,15 @@ class Ldpr(metaclass=ABCMeta):
         qry_str = (
                 re.sub('<#([^>]+)>', '<{}#\\1>'.format(self.uri), qry_str)
                 .replace('<>', '<{}>'.format(self.uri)))
-        pre_gr = self.imr.graph
-        post_rdfgr = Graph(identifier=self.uri)
-        post_rdfgr += pre_gr
+        pre_gr = self.imr.as_rdflib().graph
+        post_gr = Graph(identifier=self.uri)
+        post_gr |= pre_gr
 
-        post_rdfgr.update(qry_str)
-        post_gr = SimpleGraph(data=set(post_rdfgr))
+        post_gr.update(qry_str)
 
         # FIXME Fix and  use SimpleGraph's native subtraction operation.
-        remove_gr = self.check_mgd_terms(SimpleGraph(pre_gr.data - post_gr.data))
-        add_gr = self.check_mgd_terms(SimpleGraph(post_gr.data - pre_gr.data))
+        remove_gr = self.check_mgd_terms(SimpleGraph(set(pre_gr - post_gr)))
+        add_gr = self.check_mgd_terms(SimpleGraph(set(post_gr - pre_gr)))
 
         return remove_gr, add_gr
 
@@ -718,6 +721,9 @@ class Ldpr(metaclass=ABCMeta):
         rdfly.modify_rsrc(self.uid, remove_trp, add_trp)
 
         self._clear_cache()
+        # Reload IMR because if we exit the LMDB txn we lose access to stored
+        # memory locations.
+        self.imr
 
         if (
                 ev_type is not None and

+ 3 - 1
lakesuperior/store/ldp_rs/lmdb_triplestore.pxd

@@ -63,7 +63,9 @@ cdef class LmdbTriplestore(BaseLmdbStore):
     cpdef void _remove_graph(self, object gr_uri) except *
     cpdef tuple all_namespaces(self)
     cpdef tuple all_contexts(self, triple=*)
-    cpdef SimpleGraph graph_lookup(self, triple_pattern, context=*, uri=*)
+    cpdef SimpleGraph graph_lookup(
+        self, triple_pattern, context=*, uri=*, copy=*
+    )
 
     cdef:
         void _add_graph(self, Buffer *pk_gr) except *

+ 2 - 2
lakesuperior/store/ldp_rs/lmdb_triplestore.pyx

@@ -697,7 +697,7 @@ cdef class LmdbTriplestore(BaseLmdbStore):
 
 
     cpdef SimpleGraph graph_lookup(
-            self, triple_pattern, context=None, uri=None
+            self, triple_pattern, context=None, uri=None, copy=False
     ):
         """
         Create a SimpleGraph or Imr instance from buffers from the store.
@@ -751,7 +751,7 @@ cdef class LmdbTriplestore(BaseLmdbStore):
             self.lookup_term(spok + DBL_KLEN, buffers + cur * 3 + 2)
             #logger.info(f'Found triple o: {buffer_dump(btrp[cur].o)}')
 
-            gr.add_triple(btrp + cur)
+            gr.add_triple(btrp + cur, copy)
             cur += 1
 
         return gr

+ 8 - 5
lakesuperior/store/ldp_rs/rsrc_centric_layout.py

@@ -252,7 +252,7 @@ class RsrcCentricLayout:
 
         :rtype: SimpleGraph
         """
-        return self.store.graph_lookup((subject, None, None), ctx)
+        return self.store.graph_lookup((subject, None, None), ctx, copy=True)
 
 
     def count_rsrc(self):
@@ -294,7 +294,7 @@ class RsrcCentricLayout:
         imr = Imr(uri=nsc['fcres'][uid])
 
         for ctx in contexts:
-            gr = self.store.graph_lookup((None, None, None), ctx)
+            gr = self.store.graph_lookup((None, None, None), ctx, copy=True)
             imr |= gr
 
         # Include inbound relationships.
@@ -335,7 +335,8 @@ class RsrcCentricLayout:
         imr = self.store.graph_lookup(
             (None, None, None),
             context=nsc['fcadmin'][uid],
-            uri=nsc['fcres'][uid]
+            uri=nsc['fcres'][uid],
+            copy=True
         )
 
         if strict:
@@ -358,7 +359,8 @@ class RsrcCentricLayout:
         userdata = self.store.graph_lookup(
             (None, None, None),
             context=nsc['fcmain'][uid],
-            uri=uri
+            uri=uri,
+            copy=True
         )
 
         return userdata
@@ -464,7 +466,8 @@ class RsrcCentricLayout:
         else:
             #return ds.graph(ctx_uri)[subj_uri : cont_p : ])
             return self.store.graph_lookup(
-                (subj_uri, cont_p, None), ctx_uri
+                (subj_uri, cont_p, None), ctx_uri,
+                copy=True
             )[subj_uri : cont_p]