Parcourir la source

Implement rich comparators; include module in __repr__; fix some more tests.

Stefano Cossu il y a 5 ans
Parent
commit
f3553ebbca

+ 1 - 1
lakesuperior/model/ldp/ldpr.py

@@ -26,7 +26,7 @@ from lakesuperior.dictionaries.srv_mgd_terms import (
 from lakesuperior.exceptions import (
     InvalidResourceError, RefIntViolationError, ResourceNotExistsError,
     ServerManagedTermError, TombstoneError)
-from lakesuperior.model.rdf.graph import Graph as Graph
+from lakesuperior.model.rdf.graph import Graph
 from lakesuperior.store.ldp_rs.rsrc_centric_layout import VERS_CONT_LABEL
 from lakesuperior.toolbox import Toolbox
 

+ 19 - 7
lakesuperior/model/rdf/graph.pyx

@@ -4,6 +4,7 @@ import rdflib
 
 from lakesuperior import env
 
+from cpython.object cimport Py_LT, Py_EQ, Py_GT, Py_LE, Py_NE, Py_GE
 from libc.string cimport memcpy
 from libc.stdlib cimport free
 
@@ -148,10 +149,21 @@ cdef class Graph:
         return self.keys.size()
 
 
-    def __eq__(self, other):
-        """ Equality operator between ``Graph`` instances. """
-        # TODO Use __richcmp__()
-        return len(self & other) == 0
+    def __richcmp__(self, other, int op):
+        """ Comparators between ``Graph`` instances. """
+        if op == Py_LT:
+            raise NotImplementedError()
+        elif op == Py_EQ:
+            logger.info('Comparing for equality.')
+            return len(self ^ other) == 0
+        elif op == Py_GT:
+            raise NotImplementedError()
+        elif op == Py_LE:
+            raise NotImplementedError()
+        elif op == Py_NE:
+            return len(self ^ other) != 0
+        elif op == Py_GE:
+            raise NotImplementedError()
 
 
     def __repr__(self):
@@ -161,10 +173,10 @@ cdef class Graph:
         This includes the subject URI, number of triples contained and the
         memory address of the instance.
         """
-        id_repr = f' uri={self.uri},' if self.uri else ''
+        uri_repr = f', uri={self.uri}' if self.uri else ''
         return (
-                f'<{self.__class__.__name__} @0x{id(self):02x}{id_repr} '
-            f'length={len(self)}>'
+            f'<{self.__class__.__module__}.{self.__class__.__qualname__} '
+            f'@0x{id(self):02x} length={len(self)}{uri_repr}>'
         )
 
 

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

@@ -283,7 +283,6 @@ cdef class LmdbTriplestore(BaseLmdbStore):
 
         :param rdflib.URIRef graph: URI of the named graph to add.
         """
-        logger.exception('Called add_graph.')
         cdef:
             lmdb.MDB_txn *_txn
             Buffer _sc
@@ -293,17 +292,17 @@ cdef class LmdbTriplestore(BaseLmdbStore):
             c = c.identifier
 
         ck = self.to_key(c)
-        if not self._key_exists(<unsigned char*>ck, KLEN, b'c:'):
+        if not self._key_exists(<unsigned char*>&ck, KLEN, b'c:'):
             # Insert context term if not existing.
             if self.is_txn_rw:
+                #logger.debug('Working in existing RW transaction.')
                 _txn = self.txn
             else:
+                #logger.debug('Opening a temporary RW transaction.')
                 _check(lmdb.mdb_txn_begin(self.dbenv, NULL, 0, &_txn))
                 # Open new R/W transactions.
-                #logger.debug('Opening a temporary RW transaction.')
 
             try:
-                #logger.debug('Working in existing RW transaction.')
                 # Add to list of contexts.
                 key_v.mv_data = &ck
                 key_v.mv_size = KLEN
@@ -314,6 +313,9 @@ cdef class LmdbTriplestore(BaseLmdbStore):
                 ))
                 if not self.is_txn_rw:
                     _check(lmdb.mdb_txn_commit(_txn))
+                    # Kick the main transaction to see the new terms.
+                    lmdb.mdb_txn_reset(self.txn)
+                    _check(lmdb.mdb_txn_renew(self.txn))
             except:
                 if not self.is_txn_rw:
                     lmdb.mdb_txn_abort(_txn)
@@ -343,7 +345,7 @@ cdef class LmdbTriplestore(BaseLmdbStore):
         try:
             spok_v.mv_size = TRP_KLEN
             # If context was specified, remove only associations with that context.
-            match_set.seek()
+            match_set.keys.seek()
             if context is not None:
                 ck_v.mv_data = &ck
                 ck_v.mv_size = KLEN
@@ -622,7 +624,9 @@ cdef class LmdbTriplestore(BaseLmdbStore):
                 _check(lmdb.mdb_cursor_get(cur, &key_v, &data_v, lmdb.MDB_SET))
                 while True:
                     c_uri = self.from_key((<Key*>data_v.mv_data)[0])
-                    contexts.append(rdflib.Graph(self, uri=c_uri, store=self))
+                    contexts.append(
+                        Graph(self, uri=c_uri)
+                    )
                     try:
                         _check(lmdb.mdb_cursor_get(
                             cur, &key_v, &data_v, lmdb.MDB_NEXT_DUP))
@@ -739,7 +743,7 @@ cdef class LmdbTriplestore(BaseLmdbStore):
                     data_v.mv_size = TRP_KLEN
 
                     flt_res = Graph(self, res.capacity, uri=uri)
-                    res.seek()
+                    res.keys.seek()
                     while res.keys.get_next(&spok):
                         data_v.mv_data = spok
                         try:

+ 2 - 1
tests/1_store/test_lmdb_store.py

@@ -1,9 +1,10 @@
+import pdb
 import pytest
 
 from os import path
 from shutil import rmtree
 
-from rdflib import Graph, Namespace, URIRef
+from rdflib import Namespace, URIRef
 from rdflib.graph import DATASET_DEFAULT_GRAPH_ID as RDFLIB_DEFAULT_GRAPH_URI
 from rdflib.namespace import RDF, RDFS