Browse Source

Pass array of triple handles to LSUP_graph_add_txn().

scossu 1 year ago
parent
commit
7782524770
7 changed files with 114 additions and 119 deletions
  1. 2 4
      include/graph.h
  2. 4 5
      src/graph.c
  3. 50 57
      test/assets/triples.h
  4. 15 14
      test/test_codec_nt.c
  5. 4 1
      test/test_codec_ttl.c
  6. 31 30
      test/test_graph.c
  7. 8 8
      test/test_store.c

+ 2 - 4
include/graph.h

@@ -225,16 +225,14 @@ LSUP_graph_add_done (LSUP_GraphIterator *it);
  *
  * @param[in] gr Graph to add triples to.
  *
- * @param[in] trp Array of triples to add. The last triple must be NULL.
- *
- * @param[in] strp Array of buffer triples to add. The last one must be NULL.
+ * @param[in] trp NULL-terminated array of triple handles to add.
  *
  * @param[out] ct This will be filled with the total number of triples
  *  inserted.
  */
 LSUP_rc
 LSUP_graph_add_txn (
-        void *txn, LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct);
+        void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct);
 
 
 /// Non-transactional version of #LSUP_graph_add_txn.

+ 4 - 5
src/graph.c

@@ -282,7 +282,7 @@ LSUP_graph_add_done (LSUP_GraphIterator *it)
 
 LSUP_rc
 LSUP_graph_add_txn (
-        void *txn, LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct)
+        void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct)
 {
     LSUP_rc rc = LSUP_NOACTION;
 
@@ -291,16 +291,15 @@ LSUP_graph_add_txn (
 
     if (ct) *ct = 0;
     // Serialize and insert RDF triples.
-    for (size_t i = 0; trp[i].s != NULL; i++) {
+    for (size_t i = 0; trp[i] != NULL; i++) {
         log_trace ("Inserting triple #%lu", i);
 
-        LSUP_rc db_rc = LSUP_graph_add_iter (it, trp + i);
+        LSUP_rc db_rc = LSUP_graph_add_iter (it, trp[i]);
 
         if (db_rc == LSUP_OK) {
             rc = LSUP_OK;
             if (ct) (*ct)++;
-            // A duplicate will return LSUP_NOACTION and not increment the
-            // counter.
+            // A duplicate will return LSUP_NOACTION and not increment ct.
         }
         if (UNLIKELY (db_rc < 0)) {
             rc = db_rc;

+ 50 - 57
test/assets/triples.h

@@ -5,78 +5,71 @@
 
 #define NUM_TRP 10
 
-LSUP_Triple *create_triples()
+LSUP_Triple **create_triples()
 {
-    LSUP_NSMap *nsm = LSUP_nsmap_new();
+    LSUP_NSMap *nsm = LSUP_nsmap_new ();
     LSUP_nsmap_add (nsm, "ns1", "urn:s:");
     LSUP_nsmap_add (nsm, "ns2", "urn:p:");
 
-    LSUP_Triple *trp;
-    // Leave 1 spare NULL as a sentinel
-    trp = calloc (NUM_TRP + 1, sizeof (LSUP_Triple));
-    if (!trp) abort();
-
-    trp[0].s = LSUP_iriref_new("urn:s:0", NULL);
-    trp[0].p = LSUP_iriref_new("urn:p:0", NULL);
-    trp[0].o = LSUP_iriref_new("urn:o:0", NULL);
-
-    trp[1].s = LSUP_iriref_new("urn:s:1", NULL);
-    trp[1].p = LSUP_iriref_new("urn:p:1", NULL);
-    trp[1].o = LSUP_iriref_new("urn:o:1", NULL);
-
-    trp[2].s = LSUP_iriref_new("urn:s:2", NULL);
-    trp[2].p = LSUP_iriref_new("urn:p:2", NULL);
-    trp[2].o = LSUP_iriref_new("urn:o:2", NULL);
-
-    trp[3].s = LSUP_iriref_new("urn:s:0", NULL);
-    trp[3].p = LSUP_iriref_new("urn:p:1", NULL);
-    trp[3].o = LSUP_iriref_new("urn:o:2", NULL);
-
-    trp[4].s = LSUP_iriref_new("urn:s:0", NULL);
-    trp[4].p = LSUP_iriref_new("ns2:2", nsm);
-    trp[4].o = LSUP_literal_new ("String 1", NULL);
-
-    trp[5].s = LSUP_iriref_new("ns1:0", nsm);
-    trp[5].p = LSUP_iriref_new("urn:p:5", NULL);
-    trp[5].o = LSUP_literal_new(
-            "String 1", LSUP_iriref_new ("urn:mydatatype:string", NULL));
-
-    trp[6].s = LSUP_iriref_new("urn:s:1", NULL);
-    trp[6].p = LSUP_iriref_new("urn:p:6", NULL);
-    trp[6].o = LSUP_lt_literal_new("String 1", "es-ES");
-
+    LSUP_Term
+        *s0 = LSUP_iriref_new ("urn:s:0", NULL),
+        *s5 = LSUP_iriref_new ("ns1:0", nsm),
+        *p2 = LSUP_iriref_new ("urn:p:2", NULL),
+        *p4 = LSUP_iriref_new ("ns2:2", nsm),
+        *o5 = LSUP_literal_new (
+                    "String 1",
+                    LSUP_iriref_new ("urn:mydatatype:string", NULL));
+
+    LSUP_Triple **trp = calloc (sizeof (LSUP_Triple), NUM_TRP + 1);
+    trp[0] = LSUP_triple_new (
+            s0,
+            LSUP_iriref_new ("urn:p:0", NULL),
+            LSUP_iriref_new ("urn:o:0", NULL));
+    trp[1] = LSUP_triple_new (
+            LSUP_iriref_new ("urn:s:1", NULL),
+            LSUP_iriref_new ("urn:p:1", NULL),
+            LSUP_iriref_new ("urn:o:1", NULL));
+    trp[2] = LSUP_triple_new (
+            LSUP_iriref_new ("urn:s:2", NULL),
+            p2,
+            LSUP_iriref_new ("urn:o:2", NULL));
+    trp[3] = LSUP_triple_new (
+            LSUP_iriref_new ("urn:s:0", NULL),
+            LSUP_iriref_new ("urn:p:1", NULL),
+            LSUP_iriref_new ("urn:o:2", NULL));
+    trp[4] = LSUP_triple_new (
+            LSUP_iriref_new ("urn:s:0", NULL),
+            p4,
+            LSUP_literal_new ("String 1", NULL));
+    trp[5] = LSUP_triple_new (s5, LSUP_iriref_new ("urn:p:5", NULL), o5);
+    trp[6] = LSUP_triple_new (
+            LSUP_iriref_new ("urn:s:1", NULL),
+            LSUP_iriref_new ("urn:p:6", NULL),
+            LSUP_lt_literal_new ("String 1", "es-ES"));
     // Unique triple from reused pointers. Do not double-free.
-    trp[7].s = trp[0].s; // <urn:s:0>
-    trp[7].p = trp[2].p; // <urn:p:2>
-    trp[7].o = trp[5].o; // "String 1"^^xsd:string
-
+    trp[7] = LSUP_triple_new (s0, p2, o5);
     // Duplicate of trp[7]. Do not double-free.
-    trp[8].s = trp[0].s;
-    trp[8].p = trp[2].p;
-    trp[8].o = trp[5].o;
-
-    // Duplicate of trp[7] from different term type but semantically identical.
-    // Do not double-free.
-    trp[9].s = trp[5].s;
-    trp[9].p = trp[4].p;
-    trp[9].o = trp[5].o;
+    trp[8] = LSUP_triple_new (s0, p2, o5);
+    // Duplicate of trp[7] from different terms but semantically
+    // identical. Do not double-free.
+    trp[9] = LSUP_triple_new (s5, p4, o5);
 
     return trp;
 }
 
 
-void free_triples (LSUP_Triple *trp)
+void free_triples (LSUP_Triple **trp)
 {
-    LSUP_nsmap_free (LSUP_iriref_nsm (trp[4].p));
+    LSUP_nsmap_free (LSUP_iriref_nsm (trp[4]->p));
 
     // Last three triples are second pointers.
-    for (int i=0; i < NUM_TRP - 3; i++) {
-        LSUP_term_free (trp[i].s);
-        LSUP_term_free (trp[i].p);
-        LSUP_term_free (trp[i].o);
-    }
+    for (int i=0; i < NUM_TRP - 3; i++)
+        LSUP_triple_free (trp[i]);
+    free (trp[7]);
+    free (trp[8]);
+    free (trp[9]);
 
-    free (trp);
+    free(trp);
 }
 #endif  /* _TEST_ASSETS_H */
 

+ 15 - 14
test/test_codec_nt.c

@@ -2,6 +2,7 @@
 #include "codec/codec_nt.h"
 
 #define TERM_CT 11
+#define TRP_CT 8
 
 typedef char nt_str[64];
 
@@ -99,21 +100,20 @@ char *end_nt_doc[7] = {
 };
 
 
-static LSUP_Triple trp[8];
+static LSUP_Triple *trp[TRP_CT];
 
 
 void
 init_triples (LSUP_Term **terms)
 {
-    memset (trp, 0, sizeof (LSUP_Triple) * 8); // Last NULL is a sentinel
-
-    LSUP_triple_init (trp + 0, terms[0], terms[1], terms[3]);
-    LSUP_triple_init (trp + 1, terms[0], terms[2], terms[4]);
-    LSUP_triple_init (trp + 2, terms[0], terms[2], terms[5]);
-    LSUP_triple_init (trp + 3, terms[0], terms[2], terms[8]);
-    LSUP_triple_init (trp + 4, terms[8], terms[1], terms[5]);
-    LSUP_triple_init (trp + 5, terms[8], terms[1], terms[6]);
-    LSUP_triple_init (trp + 6, terms[8], terms[1], terms[7]);
+    trp[0] = LSUP_triple_new (terms[0], terms[1], terms[3]);
+    trp[1] = LSUP_triple_new (terms[0], terms[2], terms[4]);
+    trp[2] = LSUP_triple_new (terms[0], terms[2], terms[5]);
+    trp[3] = LSUP_triple_new (terms[0], terms[2], terms[8]);
+    trp[4] = LSUP_triple_new (terms[8], terms[1], terms[5]);
+    trp[5] = LSUP_triple_new (terms[8], terms[1], terms[6]);
+    trp[6] = LSUP_triple_new (terms[8], terms[1], terms[7]);
+    trp[7] = NULL;
 }
 
 
@@ -122,7 +122,6 @@ free_terms (LSUP_Term **terms)
 {
     for (int i = 0; i < TERM_CT; i++)
         LSUP_term_free (terms[i]);
-
     free (terms);
 }
 
@@ -154,7 +153,7 @@ test_encode_nt_term()
 
     free (out);
     LSUP_nsmap_free (nsm);
-    free_terms(terms);
+    free_terms (terms);
 
     return 0;
 }
@@ -244,7 +243,7 @@ test_decode_nt_graph()
 
     for (int i = 0; i < 7; i++) {
         log_info ("Checking triple #%d.", i);
-        EXPECT_INT_EQ (LSUP_graph_contains (gr, trp + i), 1);
+        EXPECT_INT_EQ (LSUP_graph_contains (gr, trp[i]), 1);
     }
 
     LSUP_graph_free (gr);
@@ -291,7 +290,9 @@ int codec_nt_tests()
     RUN (test_decode_nt_graph);
     RUN (test_decode_nt_bad_graph);
 
-    free_terms(terms);
+    free_terms (terms);
+    for (int i = 0; i < TRP_CT; i++)
+        free (trp[i]);
 
     return 0;
 }

+ 4 - 1
test/test_codec_ttl.c

@@ -6,7 +6,8 @@
 #define W3C_TEST_BASE "http://www.w3.org/2001/sw/DataAccess/df1/tests/"
 
 
-static LSUP_Triple trp[8];
+#define TRP_CT 8
+static LSUP_Triple *trp[TRP_CT];
 
 
 /// Encode a graph as TTL.
@@ -125,6 +126,8 @@ int codec_ttl_tests()
     RUN (test_w3c_neg);
 
     free_terms(terms);
+    for (int i = 0; i < TRP_CT; i++)
+        free (trp[i]);
 
     return 0;
 }

+ 31 - 30
test/test_graph.c

@@ -45,7 +45,7 @@ _graph_add (LSUP_StoreType type)
     const LSUP_StoreInt *sif = LSUP_store_int (type);
     if (sif->setup_fn) sif->setup_fn (NULL, true);
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
 
     LSUP_Graph *gr;
     LSUP_Store *store;
@@ -65,10 +65,11 @@ _graph_add (LSUP_StoreType type)
 
     for (int i = 0; i < sizeof (trp); i++) {
         log_info ("checking triple #%d.", i);
-        ASSERT (LSUP_graph_contains (gr, trp + i), "Triple not in graph!");
+        ASSERT (LSUP_graph_contains (gr, trp[i]), "Triple not in graph!");
     }
 
-    LSUP_Triple *missing_trp = LSUP_triple_new (trp[1].s, trp[6].p, trp[4].o);
+    LSUP_Triple *missing_trp = LSUP_triple_new (
+            trp[1]->s, trp[6]->p, trp[4]->o);
     ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
     free (missing_trp);
 
@@ -85,23 +86,23 @@ static int
 _graph_lookup (LSUP_StoreType type)
 {
     const LSUP_StoreInt *sif = LSUP_store_int (type);
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
 
     // Lookup triples.
     LSUP_Term *lu_trp[N_LUT][3] = {
         {NULL, NULL, NULL},                 // 8 matches
-        {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
+        {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.
@@ -198,7 +199,7 @@ _graph_remove (LSUP_StoreType type)
     const LSUP_StoreInt *sif = LSUP_store_int (type);
     if (sif->setup_fn) sif->setup_fn (NULL, true);
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
 
     LSUP_Graph *gr;
     LSUP_Store *store;
@@ -216,18 +217,18 @@ _graph_remove (LSUP_StoreType type)
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
     // Triples 0, 3, 4, 5, 7 will be removed.
-    LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
+    LSUP_graph_remove (gr, trp[0]->s, NULL, NULL, &ct);
     EXPECT_INT_EQ (ct, 5);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
 
-    ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
-    ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
-    ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
-    ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
-    ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
-    ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
-    ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
-    ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
+    ASSERT (!LSUP_graph_contains (gr, trp[0]), "Unexpected triple found!");
+    ASSERT (LSUP_graph_contains (gr, trp[1]), "Triple not in graph!");
+    ASSERT (LSUP_graph_contains (gr, trp[2]), "Triple not in graph!");
+    ASSERT (!LSUP_graph_contains (gr, trp[3]), "Unexpected triple found!");
+    ASSERT (!LSUP_graph_contains (gr, trp[4]), "Unexpected triple found!");
+    ASSERT (!LSUP_graph_contains (gr, trp[5]), "Unexpected triple found!");
+    ASSERT (LSUP_graph_contains (gr, trp[6]), "Triple not in graph!");
+    ASSERT (!LSUP_graph_contains (gr, trp[7]), "Unexpected triple found!");
 
     free_triples (trp); // gr copied data.
 
@@ -249,7 +250,7 @@ _graph_txn (LSUP_StoreType type)
 
     if (sif->setup_fn) sif->setup_fn (NULL, true);
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
 
     LSUP_Graph *gr;
     LSUP_Store *store =
@@ -365,7 +366,7 @@ BACKEND_TBL
 
 static int test_graph_copy()
 {
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
 
     LSUP_Graph *gr1 = LSUP_graph_new (NULL, NULL, NULL);
     ASSERT (gr1 != NULL, "Error creating graph!");
@@ -380,7 +381,7 @@ static int test_graph_copy()
     for (int i = 0; i < sizeof (trp); i++) {
         log_info ("checking triple #%d.", i);
         ASSERT (
-                LSUP_graph_contains (gr2, trp + i),
+                LSUP_graph_contains (gr2, trp[i]),
                 "Triple not in copied graph!");
     }
 
@@ -392,7 +393,7 @@ static int test_graph_copy()
     for (int i = 0; i < sizeof (trp); i++) {
         log_info ("checking triple #%d.", i);
         ASSERT (
-                LSUP_graph_contains (gr3, trp + i),
+                LSUP_graph_contains (gr3, trp[i]),
                 "Triple not in copied graph!");
     }
 

+ 8 - 8
test/test_store.c

@@ -24,11 +24,11 @@ static int test_triple_store()
     store->data = store->sif->new_fn (store->id, 0);
     ASSERT (store->data != NULL, "Error creating store data back end!");
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
     LSUP_BufferTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
+        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
         ser_trp[i] = *tmp;
         free (tmp);
     }
@@ -141,11 +141,11 @@ static int test_quad_store()
     store->data = store->sif->new_fn (store->id, 0);
     ASSERT (store->data != NULL, "Error creating store data back end!");
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
     LSUP_BufferTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
+        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
         ser_trp[i] = *tmp;
         free (tmp);
     }
@@ -395,11 +395,11 @@ static int test_txn_commit (void)
     store->data = store->sif->new_fn (store->id, 0);
     ASSERT (store->data != NULL, "Error creating store data back end!");
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
     LSUP_BufferTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
+        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
         ser_trp[i] = *tmp;
         free (tmp);
     }
@@ -468,11 +468,11 @@ static int test_txn_abort (void)
     store->data = store->sif->new_fn (store->id, 0);
     ASSERT (store->data != NULL, "Error creating store data back end!");
 
-    LSUP_Triple *trp = create_triples();
+    LSUP_Triple **trp = create_triples();
     LSUP_BufferTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
+        LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
         ser_trp[i] = *tmp;
         free (tmp);
     }