Browse Source

Use sentinel for adding triples to a graph.

Stefano Cossu 4 years ago
parent
commit
b6ce0ca597
4 changed files with 44 additions and 24 deletions
  1. 19 5
      include/graph.h
  2. 21 16
      src/graph.c
  3. 2 1
      test/assets.h
  4. 2 2
      test/test_graph.c

+ 19 - 5
include/graph.h

@@ -193,15 +193,29 @@ LSUP_graph_add_done (LSUP_GraphIterator *it);
  *
  * For API users it may be more convenient to use the more specialized
  * #LSUP_graph_add_trp.
+ *
+ * @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[out] inserted This will be filled with the total number of triples
+ *  inserted.
  */
 LSUP_rc
 LSUP_graph_add(
-        LSUP_Graph *gr,
-        const LSUP_Triple trp[], size_t trp_ct,
-        const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
+        LSUP_Graph *gr, const LSUP_Triple trp[],
+        const LSUP_SerTriple strp[], size_t *inserted);
 
-#define LSUP_graph_add_trp(gr, trp, ct, ins) \
-    LSUP_graph_add (gr, trp, ct, NULL, 0, ins)
+
+/** @brief Insert RDF triples into a graph.
+ *
+ * This is a convenience method for external callers which most likely have
+ * non-serialized triples at hand.
+ */
+#define LSUP_graph_add_trp(gr, trp, ins) \
+    LSUP_graph_add (gr, trp, NULL, ins)
 
 
 /** @brief Delete triples by a matching pattern.

+ 21 - 16
src/graph.c

@@ -159,12 +159,14 @@ graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
 
     LSUP_SerTriple sspo;
 
+    LSUP_GraphIterator *add_it = LSUP_graph_add_init (dest);
     while (graph_iter_next_buffer (it, &sspo) != LSUP_END) {
         TRACE ("Inserting triple #%lu\n", LSUP_graph_iter_cur (it));
-        LSUP_rc add_rc = LSUP_graph_add (dest, NULL, 0, &sspo, 1, NULL);
+        LSUP_rc add_rc = LSUP_graph_add_iter (add_it, &sspo);
         if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
         else if (add_rc < 0) return add_rc;
     }
+    LSUP_graph_add_done (it);
 
     return rc;
 }
@@ -316,10 +318,9 @@ LSUP_graph_add_done (LSUP_GraphIterator *it)
 
 
 LSUP_rc
-LSUP_graph_add(
-        LSUP_Graph *gr,
-        const LSUP_Triple trp[], size_t trp_ct,
-        const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted)
+LSUP_graph_add (
+        Graph *gr, const LSUP_Triple trp[],
+        const LSUP_SerTriple strp[], size_t *inserted)
 {
     /*
      * NOTE It is possible to pass both sets of RDF triples and buffer triples.
@@ -334,24 +335,28 @@ LSUP_graph_add(
     LSUP_SerTriple *sspo = LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
     if (UNLIKELY (!sspo)) return LSUP_MEM_ERR;
 
-    for (size_t i = 0; i < trp_ct; i++) {
-        TRACE ("Inserting triple #%lu\n", i);
+    if (trp) {
+        for (size_t i = 0; trp[i].s != NULL; i++) {
+            TRACE ("Inserting triple #%lu\n", i);
 
-        LSUP_triple_serialize (trp + i, sspo);
-        LSUP_rc db_rc = LSUP_graph_add_iter (it, sspo);
+            LSUP_triple_serialize (trp + i, sspo);
+            LSUP_rc db_rc = LSUP_graph_add_iter (it, sspo);
 
-        if (db_rc == LSUP_OK) rc = LSUP_OK;
-        if (UNLIKELY (db_rc < 0)) return db_rc;
+            if (db_rc == LSUP_OK) rc = LSUP_OK;
+            if (UNLIKELY (db_rc < 0)) return db_rc;
+        }
     }
     LSUP_striple_free (sspo);
 
     // Insert serialized triples.
-    for (size_t i = 0; i < strp_ct; i++) {
-        TRACE ("Inserting serialized triple #%lu\n", i);
-        LSUP_rc db_rc = LSUP_graph_add_iter (it, strp + i);
+    if (strp) {
+        for (size_t i = 0; strp[i].s != NULL; i++) {
+            TRACE ("Inserting serialized triple #%lu\n", i);
+            LSUP_rc db_rc = LSUP_graph_add_iter (it, strp + i);
 
-        if (db_rc == LSUP_OK) rc = LSUP_OK;
-        if (UNLIKELY (db_rc < 0)) return db_rc;
+            if (db_rc == LSUP_OK) rc = LSUP_OK;
+            if (UNLIKELY (db_rc < 0)) return db_rc;
+        }
     }
 
     if (inserted) {

+ 2 - 1
test/assets.h

@@ -8,7 +8,8 @@
 LSUP_Triple *create_triples()
 {
     LSUP_Triple *trp;
-    trp = malloc (NUM_TRP * sizeof (LSUP_Triple));
+    // Leave 1 spare NULL as a sentinel
+    trp = calloc (NUM_TRP + 1, sizeof (LSUP_Triple));
     if (!trp) abort();
 
     trp[0].s = LSUP_uri_new ("urn:s:0");

+ 2 - 2
test/test_graph.c

@@ -29,7 +29,7 @@ test_graph_mem_add()
     ASSERT (gr != NULL, "Error creating graph!");
 
     size_t ct;
-    LSUP_graph_add_trp (gr, trp, NUM_TRP, &ct);
+    LSUP_graph_add_trp (gr, trp, &ct);
 
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
@@ -78,7 +78,7 @@ test_graph_mdb_add()
     ASSERT (gr != NULL, "Error creating graph!");
 
     size_t ct;
-    LSUP_graph_add_trp (gr, trp, NUM_TRP, &ct);
+    LSUP_graph_add_trp (gr, trp, &ct);
 
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);