Browse Source

Do not use triples where a term may be NULL.

Stefano Cossu 4 years ago
parent
commit
186558ebe9
10 changed files with 78 additions and 65 deletions
  1. 5 2
      include/graph.h
  2. 4 2
      include/store_htable.h
  3. 4 4
      include/store_mdb.h
  4. 1 2
      src/codec_nt.c
  5. 25 14
      src/graph.c
  6. 8 6
      src/store_htable.c
  7. 8 8
      src/store_mdb.c
  8. 16 21
      test/test_graph.c
  9. 3 2
      test/test_store_ht.c
  10. 4 4
      test/test_store_mdb.c

+ 5 - 2
include/graph.h

@@ -228,7 +228,9 @@ LSUP_graph_add(
  *  deleted.
  */
 LSUP_rc
-LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
+LSUP_graph_remove (
+        LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
+        const LSUP_Term *o, size_t *ct);
 
 
 /** Look up triples by a matching pattern and yield an iterator.
@@ -242,7 +244,8 @@ LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  *  freed with #LSUP_graph_iter_free after use.
  */
 LSUP_GraphIterator *
-LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
+LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
+        const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
 
 
 /** @brief Advance a cursor obtained by a lookup and return a matching triple.

+ 4 - 2
include/store_htable.h

@@ -89,11 +89,13 @@ LSUP_htstore_add_done (LSUP_HTIterator *it);
 
 LSUP_rc
 LSUP_htstore_remove(
-        LSUP_HTStore *store, const LSUP_SerTriple *sspo, size_t *ct);
+        LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so, size_t *ct);
 
 LSUP_HTIterator *
 LSUP_htstore_lookup(
-        LSUP_HTStore *store, const LSUP_SerTriple *sspo);
+        LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so);
 
 size_t
 LSUP_htstore_size (LSUP_HTStore *ht);

+ 4 - 4
include/store_mdb.h

@@ -200,8 +200,8 @@ LSUP_rc LSUP_mdbstore_add(
 
 LSUP_rc
 LSUP_mdbstore_remove(
-        LSUP_MDBStore *store, const LSUP_SerTriple *sspo,
-        const LSUP_Buffer *sc, size_t *ct);
+        LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct);
 
 
 /** @brief Look up matching triples and optional context.
@@ -235,8 +235,8 @@ LSUP_mdbstore_remove(
  */
 LSUP_MDBIterator *
 LSUP_mdbstore_lookup(
-        LSUP_MDBStore *store, const LSUP_SerTriple *sspo,
-        const LSUP_Buffer *sc, size_t *ct);
+        LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct);
 
 
 /** @brief Yield the matching triples and advance the iterator.

+ 1 - 2
src/codec_nt.c

@@ -180,10 +180,9 @@ gr_to_nt_init (const LSUP_Graph *gr)
 {
     LSUP_CodecIterator *it;
     MALLOC_GUARD (it, NULL);
-    LSUP_Triple lut = {NULL, NULL, NULL};
 
     it->codec = &nt_codec;
-    it->gr_it = LSUP_graph_lookup(gr, &lut, &it->cur);
+    it->gr_it = LSUP_graph_lookup(gr, NULL, NULL, NULL, &it->cur);
     it->nsm = LSUP_graph_namespace (gr);
     it->cur = 0;
     it->trp = LSUP_triple_new (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY);

+ 25 - 14
src/graph.c

@@ -153,9 +153,8 @@ static LSUP_rc
 graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
 {
     LSUP_rc rc = LSUP_NOACTION;
-    const LSUP_Triple trp = {NULL, NULL, NULL};
 
-    GraphIterator *it = LSUP_graph_lookup (src, &trp, NULL);
+    GraphIterator *it = LSUP_graph_lookup (src, NULL, NULL, NULL, NULL);
 
     LSUP_SerTriple sspo;
 
@@ -261,7 +260,7 @@ LSUP_graph_size (const Graph *gr)
     else {
         LSUP_Triple *spo = TRP_DUMMY;
         size_t ct;
-        LSUP_GraphIterator *it = LSUP_graph_lookup (gr, spo, &ct);
+        LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, &ct);
 
         LSUP_graph_iter_free (it);
         LSUP_triple_free (spo);
@@ -381,19 +380,25 @@ LSUP_graph_add (
 
 
 LSUP_rc
-LSUP_graph_remove (Graph *gr, const LSUP_Triple *spo, size_t *ct)
+LSUP_graph_remove (
+        LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
+        const LSUP_Term *o, size_t *ct)
 {
     LSUP_rc rc;
 
-    LSUP_SerTriple *sspo = LSUP_striple_new_from_triple (spo);
+    LSUP_Buffer *ss = LSUP_buffer_new_from_term (s);
+    LSUP_Buffer *sp = LSUP_buffer_new_from_term (p);
+    LSUP_Buffer *so = LSUP_buffer_new_from_term (o);
     LSUP_Buffer *sc = LSUP_buffer_new_from_term (gr->uri);
 
     if (gr->store_type == LSUP_STORE_MEM)
-        rc = LSUP_htstore_remove (gr->ht_store, sspo, ct);
+        rc = LSUP_htstore_remove (gr->ht_store, ss, sp, so, ct);
     else
-        rc = LSUP_mdbstore_remove (gr->mdb_store, sspo, sc, ct);
+        rc = LSUP_mdbstore_remove (gr->mdb_store, ss, sp, so, sc, ct);
 
-    LSUP_striple_free (sspo);
+    LSUP_buffer_free (ss);
+    LSUP_buffer_free (sp);
+    LSUP_buffer_free (so);
     LSUP_buffer_free (sc);
 
     return rc;
@@ -401,24 +406,29 @@ LSUP_graph_remove (Graph *gr, const LSUP_Triple *spo, size_t *ct)
 
 
 GraphIterator *
-LSUP_graph_lookup (const Graph *gr, const LSUP_Triple *spo, size_t *ct)
+LSUP_graph_lookup (const Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
+        const LSUP_Term *o, size_t *ct)
 {
     GraphIterator *it;
     MALLOC_GUARD (it, NULL);
 
     it->graph = gr;
 
-    LSUP_SerTriple *sspo = LSUP_striple_new_from_triple (spo);
+    LSUP_Buffer *ss = LSUP_buffer_new_from_term (s);
+    LSUP_Buffer *sp = LSUP_buffer_new_from_term (p);
+    LSUP_Buffer *so = LSUP_buffer_new_from_term (o);
     LSUP_Buffer *sc = LSUP_buffer_new_from_term (gr->uri);
 
     if (it->graph->store_type == LSUP_STORE_MEM) {
-        it->ht_iter = LSUP_htstore_lookup (it->graph->ht_store, sspo);
+        it->ht_iter = LSUP_htstore_lookup (it->graph->ht_store, ss, sp, so);
         if (ct) *ct = it->ct;
 
     } else it->mdb_iter = LSUP_mdbstore_lookup (
-            it->graph->mdb_store, sspo, sc, ct);
+            it->graph->mdb_store, ss, sp, so, sc, ct);
 
-    LSUP_striple_free (sspo);
+    LSUP_buffer_free (ss);
+    LSUP_buffer_free (sp);
+    LSUP_buffer_free (so);
     LSUP_buffer_free (sc);
 
     return it;
@@ -502,7 +512,8 @@ LSUP_graph_iter_cur (GraphIterator *it)
 bool
 LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
 {
-    GraphIterator *it = LSUP_graph_lookup (gr, spo, NULL);
+    GraphIterator *it = LSUP_graph_lookup (
+            gr, spo->s, spo->p, spo->o, NULL);
     LSUP_Triple *tmp_spo = LSUP_triple_new (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY);
     bool rc = LSUP_graph_iter_next (it, tmp_spo) != LSUP_END;
 

+ 8 - 6
src/store_htable.c

@@ -349,9 +349,10 @@ LSUP_htstore_add_done (HTIterator *it)
 
 LSUP_rc
 LSUP_htstore_remove(
-        LSUP_HTStore *store, const LSUP_SerTriple *sspo, size_t *ct)
+        LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so,  size_t *ct)
 {
-    LSUP_HTIterator *it = LSUP_htstore_lookup (store, sspo);
+    LSUP_HTIterator *it = LSUP_htstore_lookup (store, ss, sp, so);
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
 
     if (ct) *ct = 0;
@@ -375,7 +376,8 @@ LSUP_htstore_remove(
 
 
 HTIterator *
-LSUP_htstore_lookup (HTStore *store, const LSUP_SerTriple *sspo)
+LSUP_htstore_lookup (HTStore *store, const LSUP_Buffer *ss,
+        const LSUP_Buffer *sp, const LSUP_Buffer *so)
 {
     HTIterator *it;
     MALLOC_GUARD (it, NULL);
@@ -387,9 +389,9 @@ LSUP_htstore_lookup (HTStore *store, const LSUP_SerTriple *sspo)
     if (HASH_COUNT (store->keys) == 0) return it;
 
     LSUP_TripleKey spok = {
-        LSUP_buffer_hash (sspo->s),
-        LSUP_buffer_hash (sspo->p),
-        LSUP_buffer_hash (sspo->o),
+        LSUP_buffer_hash (ss),
+        LSUP_buffer_hash (sp),
+        LSUP_buffer_hash (so),
     };
 
     // s p o

+ 8 - 8
src/store_mdb.c

@@ -540,13 +540,13 @@ key_to_sterm(
 
 MDBIterator *
 LSUP_mdbstore_lookup(
-        LSUP_MDBStore *store, const LSUP_SerTriple *sspo,
-        const LSUP_Buffer *sc, size_t *ct)
+        LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct)
 {
     LSUP_TripleKey spok = {
-        LSUP_buffer_hash (sspo->s),
-        LSUP_buffer_hash (sspo->p),
-        LSUP_buffer_hash (sspo->o),
+        LSUP_buffer_hash (ss),
+        LSUP_buffer_hash (sp),
+        LSUP_buffer_hash (so),
     };
 
     LSUP_MDBIterator *it;
@@ -727,8 +727,8 @@ LSUP_mdbiter_free (MDBIterator *it)
 
 LSUP_rc
 LSUP_mdbstore_remove(
-        MDBStore *store, const LSUP_SerTriple *sspo,
-        const LSUP_Buffer *sc, size_t *ct)
+        LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
+        const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct)
 {
     LSUP_rc rc = LSUP_NOACTION;
 
@@ -753,7 +753,7 @@ LSUP_mdbstore_remove(
     ck_v.mv_size = KLEN;
     ck_v.mv_data = &ck;
 
-    LSUP_MDBIterator *it = LSUP_mdbstore_lookup (store, sspo, sc, ct);
+    LSUP_MDBIterator *it = LSUP_mdbstore_lookup (store, ss, sp, so, sc, ct);
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
     if (ct) TRACE ("Found %lu triples to remove.", *ct);
 

+ 16 - 21
test/test_graph.c

@@ -63,19 +63,19 @@ _graph_lookup (LSUP_store_type type)
     LSUP_Triple *trp = create_triples();
 
     // Lookup triples.
-    LSUP_Triple *lu_trp[N_LUT] = {
-        LSUP_triple_new (trp[0].s, NULL, NULL),         // 5 matches
-        LSUP_triple_new (NULL, trp[2].p, NULL),         // 3 matches
-        LSUP_triple_new (NULL, NULL, trp[5].o),         // 2 matches
-        LSUP_triple_new (trp[0].s, trp[0].p, NULL),     // 1 match
-        LSUP_triple_new (NULL, trp[0].p, trp[0].o),     // 1 match
-        LSUP_triple_new (trp[0].s, trp[2].p, trp[5].o), // 1 match
-        LSUP_triple_new (trp[0].p, NULL, NULL),         // 0 matches
-        LSUP_triple_new (NULL, trp[2].s, NULL),         // 0 matches
-        LSUP_triple_new (NULL, NULL, trp[5].p),         // 0 matches
-        LSUP_triple_new (trp[2].s, trp[6].p, NULL),     // 0 matches
-        LSUP_triple_new (NULL, trp[1].p, trp[5].o),     // 0 matches
-        LSUP_triple_new (trp[2].s, trp[2].p, trp[5].o), // 0 matches
+    LSUP_Term *lu_trp[N_LUT][3] = {
+        {trp[0].s, NULL, NULL},             // 5 matches
+        {NULL, trp[2].p, NULL},             // 3 matches
+        {NULL, NULL, trp[5].o},             // 2 matches
+        {trp[0].s, trp[0].p, NULL},         // 1 match
+        {NULL, trp[0].p, trp[0].o},         // 1 match
+        {trp[0].s, trp[2].p, trp[5].o},     // 1 match
+        {trp[0].p, NULL, NULL},             // 0 matches
+        {NULL, trp[2].s, NULL},             // 0 matches
+        {NULL, NULL, trp[5].p},             // 0 matches
+        {trp[2].s, trp[6].p, NULL},         // 0 matches
+        {NULL, trp[1].p, trp[5].o},         // 0 matches
+        {trp[2].s, trp[2].p, trp[5].o},     // 0 matches
     };
 
     // Lookup result counts.
@@ -115,8 +115,8 @@ _graph_lookup (LSUP_store_type type)
 
     for (int i = 0; i < N_LUT; i++) {
         printf ("Checking tiple #%d on %d... ", i, type);
-        spo = lu_trp[i];
-        LSUP_GraphIterator *it = LSUP_graph_lookup (gr, spo, &ct);
+        LSUP_GraphIterator *it = LSUP_graph_lookup (
+                gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
         if (type != LSUP_STORE_MEM) // TODO not implemented in htable.
             EXPECT_INT_EQ (ct, lu_ct[i]);
         printf ("done.\n");
@@ -135,10 +135,7 @@ _graph_lookup (LSUP_store_type type)
         LSUP_graph_iter_free (it);
     };
 
-    //LSUP_triple_free (spo);
-    for (int i = 0; i < N_LUT; i++) free (lu_trp[i]);
     free_triples (trp);
-
     LSUP_graph_free (gr);
 
     return 0;
@@ -158,8 +155,7 @@ _graph_remove (LSUP_store_type type)
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
-    LSUP_Triple *spo = LSUP_triple_new (trp[0].s, NULL, NULL);
-    LSUP_graph_remove (gr, spo, &ct);
+    LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
 
     ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
     ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
@@ -171,7 +167,6 @@ _graph_remove (LSUP_store_type type)
     ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
     EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
 
-    free (spo);
     free_triples (trp); // gr copied data.
 
     LSUP_graph_free (gr);

+ 3 - 2
test/test_store_ht.c

@@ -30,7 +30,7 @@ static int test_htstore()
     LSUP_htstore_add_done (it);
 
     // Test lookups.
-    LSUP_SerTriple lut[12] = {
+    LSUP_Buffer *lut[12][3] = {
         {NULL, NULL, NULL},
 
         {ser_trp[0]->s, NULL, NULL},
@@ -60,7 +60,8 @@ static int test_htstore()
         size_t ct = 0;
         TRACE ("Testing triple lookup #%d.", i);
 
-        LSUP_HTIterator *it = LSUP_htstore_lookup(store, lut + i);
+        LSUP_HTIterator *it = LSUP_htstore_lookup(
+                store, lut[i][0], lut[i][1], lut[i][2]);
         while (LSUP_htiter_next (it, sspo) != LSUP_END) {
             // Verify that the internal counter aligns with an external one.
             ct ++;

+ 4 - 4
test/test_store_mdb.c

@@ -30,7 +30,7 @@ static int test_triple_store()
     EXPECT_INT_EQ (LSUP_mdbstore_size (store), 8);
 
     // Test lookups.
-    LSUP_SerTriple lut[12] = {
+    LSUP_Buffer *lut[12][3] = {
         {NULL, NULL, NULL},
 
         {ser_trp[0].s, NULL, NULL},
@@ -78,7 +78,7 @@ static int test_triple_store()
         TRACE ("Testing triple lookup #%d.\n", i);
 
         LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
-                store, lut + i, luc[i], &ct);
+                store, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
         EXPECT_INT_EQ (ct, results[i]);
 
         LSUP_mdbiter_free (it);
@@ -139,7 +139,7 @@ static int test_quad_store()
     LSUP_Buffer *sc3 = LSUP_buffer_new_from_term (ctx3);
 
     // Test lookups.
-    LSUP_SerTriple lut[41] = {
+    LSUP_Buffer *lut[41][3] = {
         // Any context
         {NULL, NULL, NULL},                                 // #0
 
@@ -281,7 +281,7 @@ static int test_quad_store()
 
         printf ("Checking triple #%d...", i);
         LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
-                store, lut + i, luc[i], &ct);
+                store, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
         ASSERT (it != NULL, "Lookup error!");
         EXPECT_INT_EQ (ct, results[i]);
         printf ("OK.\n");