Bladeren bron

Fix contexts() mess.

Stefano Cossu 6 jaren geleden
bovenliggende
commit
b7f1faa4c0
2 gewijzigde bestanden met toevoegingen van 53 en 40 verwijderingen
  1. 46 33
      lakesuperior/store/ldp_rs/lmdb_triplestore.pyx
  2. 7 7
      tests/1_store/test_lmdb_store.py

+ 46 - 33
lakesuperior/store/ldp_rs/lmdb_triplestore.pyx

@@ -1316,53 +1316,66 @@ cdef class LmdbTriplestore(BaseLmdbStore):
         """
         cdef:
             size_t ct
-            lmdb.MDB_cursor_op seek_op, scan_op
             lmdb.MDB_stat stat
-            lmdb.MDB_val key_v
+            lmdb.MDB_val key_v, data_v
             TripleKey spok
 
         cur = (
                 self._cur_open('spo:c') if triple and all(triple)
                 else self._cur_open('c:'))
-
-        key_v.mv_data = &spok
-        if triple and all(triple):
-            lmdb_seek_op = lmdb.MDB_SET_KEY
-            lmdb_scan_op = lmdb.MDB_NEXT_DUP
-            spok = [
-                self._to_key_idx(triple[0]),
-                self._to_key_idx(triple[1]),
-                self._to_key_idx(triple[2]),
-            ]
-            key_v.mv_size = TRP_KLEN
-        else:
-            lmdb_seek_op = lmdb.MDB_FIRST
-            lmdb_scan_op = lmdb.MDB_NEXT
-            key_v.mv_size = 0
-
         try:
-            _check(lmdb.mdb_stat(
-                self.txn, lmdb.mdb_cursor_dbi(cur), &stat))
+            if triple and all(triple):
+                _check(lmdb.mdb_stat(
+                    self.txn, lmdb.mdb_cursor_dbi(cur), &stat))
+
+                spok = [
+                    self._to_key_idx(triple[0]),
+                    self._to_key_idx(triple[1]),
+                    self._to_key_idx(triple[2]),
+                ]
+                key_v.mv_data = spok
+                key_v.mv_size = TRP_KLEN
 
-            try:
-                _check(lmdb.mdb_cursor_get(
-                        cur, &key_v, &data_v, seek_op))
-            except KeyNotFoundError:
-                ctx[0] = NULL
-                return
+                try:
+                    _check(lmdb.mdb_cursor_get(
+                            cur, &key_v, &data_v, lmdb.MDB_SET_KEY))
+                except KeyNotFoundError:
+                    ctx[0] = NULL
+                    return
 
-            ctx[0] = <Key*>malloc(stat.ms_entries * KLEN)
-            sz[0] = 0
+                ctx[0] = <Key*>malloc(stat.ms_entries * KLEN)
+                sz[0] = 0
+
+                while True:
+                    ctx[0][sz[0]] = (<Key*>data_v.mv_data)[0]
+                    sz[0] += 1
+                    try:
+                        _check(lmdb.mdb_cursor_get(
+                            cur, &key_v, &data_v, lmdb.MDB_NEXT_DUP))
+                    except KeyNotFoundError:
+                        break
+            else:
+                _check(lmdb.mdb_stat(
+                    self.txn, lmdb.mdb_cursor_dbi(cur), &stat))
 
-            while True:
-                ctx[0][sz[0]] = (<Key*>data_v.mv_data)[0]
                 try:
                     _check(lmdb.mdb_cursor_get(
-                        cur, &key_v, &data_v, scan_op))
+                            cur, &key_v, &data_v, lmdb.MDB_FIRST))
                 except KeyNotFoundError:
-                    break
+                    ctx[0] = NULL
+                    return
+
+                ctx[0] = <Key*>malloc(stat.ms_entries * KLEN)
+                sz[0] = 0
 
-                sz[0] += 1
+                while True:
+                    ctx[0][sz[0]] = (<Key*>key_v.mv_data)[0]
+                    sz[0] += 1
+                    try:
+                        _check(lmdb.mdb_cursor_get(
+                            cur, &key_v, NULL, lmdb.MDB_NEXT))
+                    except KeyNotFoundError:
+                        break
 
         finally:
             self._cur_close(cur)

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

@@ -649,7 +649,7 @@ class TestContext:
 
         with store.txn_ctx(True):
             store.add_graph(gr_uri)
-            assert gr_uri in {gr.uri for gr in store.contexts()}
+            assert gr_uri in store.contexts()
 
 
     def test_add_graph_with_triple(self, store):
@@ -664,7 +664,7 @@ class TestContext:
             store.add(trp, ctx_uri)
 
         with store.txn_ctx():
-            assert ctx_uri in {gr.uri for gr in store.contexts(trp)}
+            assert ctx_uri in store.contexts(trp)
 
 
     def test_empty_context(self, store):
@@ -675,10 +675,10 @@ class TestContext:
 
         with store.txn_ctx(True):
             store.add_graph(gr_uri)
-            assert gr_uri in {gr.uri for gr in store.contexts()}
+            assert gr_uri in store.contexts()
         with store.txn_ctx(True):
             store.remove_graph(gr_uri)
-            assert gr_uri not in {gr.uri for gr in store.contexts()}
+            assert gr_uri not in store.contexts()
 
 
     def test_context_ro_txn(self, store):
@@ -698,10 +698,10 @@ class TestContext:
         # allow a lookup in the same transaction, but this does not seem to be
         # possible.
         with store.txn_ctx():
-            assert gr_uri in {gr.uri for gr in store.contexts()}
+            assert gr_uri in store.contexts()
         with store.txn_ctx(True):
             store.remove_graph(gr_uri)
-            assert gr_uri not in {gr.uri for gr in store.contexts()}
+            assert gr_uri not in store.contexts()
 
 
     def test_add_trp_to_ctx(self, store):
@@ -732,7 +732,7 @@ class TestContext:
             assert len(set(store.triples((None, None, None), gr_uri))) == 3
             assert len(set(store.triples((None, None, None), gr2_uri))) == 1
 
-            assert gr2_uri in {gr.uri for gr in store.contexts()}
+            assert gr2_uri in store.contexts()
             assert trp1 in _clean(store.triples((None, None, None)))
             assert trp1 not in _clean(store.triples((None, None, None),
                     RDFLIB_DEFAULT_GRAPH_URI))