Bläddra i källkod

[WIP] More more moving.

Stefano Cossu 5 år sedan
förälder
incheckning
a350b78632

+ 0 - 1
lakesuperior/cy_include/collections.pxd

@@ -19,7 +19,6 @@ cdef extern from "common.h":
         CC_ERR_VALUE_NOT_FOUND
         CC_ERR_OUT_OF_RANGE
         CC_ITER_END
-        #CC_DUP_KEY
 #
 #    int cc_common_cmp_str(const void* key1, const void* key2)
 #

+ 3 - 13
lakesuperior/model/graph/graph.pxd

@@ -6,8 +6,6 @@ from lakesuperior.cy_include cimport collections as cc
 from lakesuperior.model.base cimport Buffer
 from lakesuperior.model.graph.triple cimport BufferTriple
 from lakesuperior.model.structures.keyset cimport Keyset
-from lakesuperior.store.ldp_rs.lmdb_triplestore cimport LmdbTriplestore
-from lakesuperior.store.ldp_rs.lmdb_triplestore cimport TripleKey
 
 # Lookup function that returns whether a triple contains a match pattern.
 ctypedef bint (*lookup_fn_t)(
@@ -27,21 +25,15 @@ cdef class SimpleGraph:
     cdef:
         cc.HashSet *_terms # Set of unique serialized terms.
         cc.HashSet *_triples # Set of unique triples.
-        readonly LmdbTriplestore store
         # Temp data pool. It gets managed with the object lifecycle via cymem.
         Pool _pool
 
         cc.key_compare_ft term_cmp_fn
         cc.key_compare_ft trp_cmp_fn
 
-        void _data_from_lookup(self, tuple trp_ptn, ctx=*) except *
-        void _data_from_keyset(self, Keyset data) except *
-        inline void _add_from_spok(self, const TripleKey spok) except *
-        inline void _add_triple(self, BufferTriple *trp) except *
-        int _remove_triple(self, BufferTriple* trp_buf) except -1
-        bint _trp_contains(self, BufferTriple* btrp)
-        _get_terms(self)
-        set _to_pyset(self)
+        inline void add_triple(self, BufferTriple *trp) except *
+        int remove_triple(self, BufferTriple* trp_buf) except -1
+        bint trp_contains(self, BufferTriple* btrp)
 
         # Basic graph operations.
         void ip_union(self, SimpleGraph other) except *
@@ -56,8 +48,6 @@ cdef class SimpleGraph:
     cpdef xor(self, SimpleGraph other)
     cpdef void set(self, tuple trp) except *
     cpdef void remove_triples(self, pattern) except *
-    cpdef object as_rdflib(self)
-    #cpdef set terms(self, str type)
 
 
 cdef class Imr(SimpleGraph):

+ 13 - 33
lakesuperior/model/graph/graph.pyx

@@ -323,7 +323,7 @@ cdef class SimpleGraph:
         return graph_set
 
     @property
-    def _all_terms(self):
+    def stored_terms(self):
         """
         All terms in the graph with their memory address.
 
@@ -446,7 +446,7 @@ cdef class SimpleGraph:
         term.serialize_from_rdflib(p, &sp)
         term.serialize_from_rdflib(o, &so)
 
-        return self._trp_contains(&btrp)
+        return self.trp_contains(&btrp)
 
 
     def __iter__(self):
@@ -618,7 +618,7 @@ cdef class SimpleGraph:
             bt = <BufferTriple*>cur
             #print('Checking: <0x{:02x}> <0x{:02x}> <0x{:02x}>'.format(
             #    <size_t>bt.s, <size_t>bt.p, <size_t>bt.o))
-            if other._trp_contains(bt):
+            if other.trp_contains(bt):
                 #print('Adding.')
                 new_gr.add_triple(bt)
 
@@ -643,7 +643,7 @@ cdef class SimpleGraph:
         cc.hashset_iter_init(&it, self._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if not other._trp_contains(bt):
+            if not other.trp_contains(bt):
                 self.remove_triple(bt)
 
 
@@ -670,7 +670,7 @@ cdef class SimpleGraph:
             bt = <BufferTriple*>cur
             #print('Checking: <0x{:02x}> <0x{:02x}> <0x{:02x}>'.format(
             #    <size_t>bt.s, <size_t>bt.p, <size_t>bt.o))
-            if not other._trp_contains(bt):
+            if not other.trp_contains(bt):
                 #print('Adding.')
                 new_gr.add_triple(bt)
 
@@ -694,7 +694,7 @@ cdef class SimpleGraph:
         cc.hashset_iter_init(&it, self._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if other._trp_contains(bt):
+            if other.trp_contains(bt):
                 self.remove_triple(bt)
 
 
@@ -718,14 +718,14 @@ cdef class SimpleGraph:
         cc.hashset_iter_init(&it, self._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if not other._trp_contains(bt):
+            if not other.trp_contains(bt):
                 new_gr.add_triple(bt)
 
         # Other way around.
         cc.hashset_iter_init(&it, other._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if not self._trp_contains(bt):
+            if not self.trp_contains(bt):
                 new_gr.add_triple(bt)
 
         return new_gr
@@ -753,14 +753,14 @@ cdef class SimpleGraph:
         cc.hashset_iter_init(&it, other._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if not self._trp_contains(bt):
+            if not self.trp_contains(bt):
                 tmp.add_triple(bt)
 
         # Remove triples in common.
         cc.hashset_iter_init(&it, self._triples)
         while cc.hashset_iter_next(&it, &cur) != cc.CC_ITER_END:
             bt = <BufferTriple*>cur
-            if other._trp_contains(bt):
+            if other.trp_contains(bt):
                 print(self.remove_triple(bt))
 
         self |= tmp
@@ -800,7 +800,7 @@ cdef class SimpleGraph:
         return cc.hashset_remove(self._triples, btrp, NULL)
 
 
-    cdef bint _trp_contains(self, BufferTriple* btrp):
+    cdef bint trp_contains(self, BufferTriple* btrp):
         cdef:
             cc.HashSetIter it
             void* cur
@@ -820,7 +820,7 @@ cdef class SimpleGraph:
         """
         if None in trp:
             raise ValueError(f'Invalid triple: {trp}')
-        self.remove_triples((trp[0], trp[1], None))
+        self.remove((trp[0], trp[1], None))
         self.add((trp,))
 
 
@@ -836,7 +836,7 @@ cdef class SimpleGraph:
             self.data.remove(match)
 
 
-    cpdef object as_rdflib(self):
+    def as_rdflib(self):
         """
         Return the data set as an RDFLib Graph.
 
@@ -979,26 +979,6 @@ cdef class Imr(SimpleGraph):
         #super().__init(*args, **kwargs)
 
 
-    @property
-    def identifier(self):
-        """
-        IMR URI. For compatibility with RDFLib Resource.
-
-        :rtype: string
-        """
-        return self.uri
-
-
-    @property
-    def graph(self):
-        """
-        Return a SimpleGraph with the same data.
-
-        :rtype: SimpleGraph
-        """
-        raise NotImplementedError() # TODO
-
-
     def __repr__(self):
         """
         String representation of an Imr.

+ 37 - 7
lakesuperior/store/ldp_rs/lmdb_triplestore.pyx

@@ -693,6 +693,39 @@ cdef class LmdbTriplestore(BaseLmdbStore):
             self._cur_close(cur)
 
 
+    cpdef SimpleGraph graph(self, triple_pattern, context=None):
+        """
+        Create a SimpleGraph instance from "borrowed" buffers from the store.
+
+        The instance is only valid within the LMDB transaction that created it.
+
+        :param tuple triple_pattern: 3 RDFLib terms
+        :param context: Context graph, if available.
+        :type context: rdflib.Graph or None
+
+        :rtype: Iterator
+        :return: Generator over triples and contexts in which each result has
+            the following format::
+
+                (s, p, o), generator(contexts)
+
+        Where the contexts generator lists all context that the triple appears
+        in.
+        """
+        cdef:
+            unsigned char spok[TRP_KLEN]
+            lmdb.MDB_val key_v, data_v
+            SimpleGraph gr = SimpleGraph()
+
+        logger.debug(
+                'Getting triples for: {}, {}'.format(triple_pattern, context))
+
+        for spok in self.triple_keys(triple_pattern, context):
+            gr.add(self.lookup_term(spok))
+
+        return gr
+
+
     cdef Keyset triple_keys(self, tuple triple_pattern, context=None):
         """
         Top-level lookup method.
@@ -1327,12 +1360,12 @@ cdef class LmdbTriplestore(BaseLmdbStore):
         """
         cdef Buffer pk_t
 
-        self.lookup_term(key, &pk_t)
+        #self.lookup_term(key, &pk_t)
 
-        return deserialize_to_rdflib(&pk_t)
+        return deserialize_to_rdflib(self.lookup_term(key))
 
 
-    cdef inline int lookup_term(self, const Key key, Buffer *data) except -1:
+    cdef inline Buffer lookup_term(self, const Key key, Buffer *data):
         """
         look up a term by key.
 
@@ -1349,10 +1382,7 @@ cdef class LmdbTriplestore(BaseLmdbStore):
                 lmdb.mdb_get(self.txn, self.get_dbi('t:st'), &key_v, &data_v),
                 f'Error getting data for key \'{key}\'.')
 
-        data[0].addr = data_v.mv_data
-        data[0].sz = data_v.mv_size
-
-        return 0
+        return <Buffer>data
 
 
     cdef tuple from_trp_key(self, TripleKey key):

+ 0 - 27
tests/0_data_structures/test_graph.py

@@ -66,33 +66,6 @@ class TestGraphInit:
             assert t in gr
 
 
-    @pytest.mark.skip(reason='TODO')
-    def test_init_keyset(self):
-        """
-        Test creation using a keyset.
-
-        TODO
-        """
-        pass
-
-
-    def test_init_lookup(self, trp, store):
-        """
-        Test creation by store lookup.
-        """
-        with store.txn_ctx(True):
-            for t in trp:
-                store.add(t)
-
-        with store.txn_ctx():
-            gr1 = SimpleGraph(store=store, lookup=((None, None, None)))
-        assert len(gr1) == 6
-
-        with store.txn_ctx():
-            gr2 = SimpleGraph(store=store, lookup=((URIRef('urn:s:1'), None, None)))
-        assert len(gr1) == 2
-
-
 @pytest.mark.usefixtures('trp')
 class TestGraphLookup:
     """