Browse Source

WIP delete triples in update.

scossu 8 months ago
parent
commit
c5053f2387
4 changed files with 103 additions and 54 deletions
  1. 19 28
      include/desc.h
  2. 43 26
      src/desc.c
  3. 2 0
      test.c
  4. 39 0
      test/test_desc.c

+ 19 - 28
include/desc.h

@@ -121,16 +121,26 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc);
 
 
 
 
 /** @brief Update an existing resource.
 /** @brief Update an existing resource.
+ *
+ * This operation works by first removing triples by graph and term matching,
+ * if provided, then adding triples grouped by graphs. Both steps are
+ * optional.
+ *
+ * Updates to triples containing #LSR_managed_preds and #LSR_managed_types are
+ * ignored.
  *
  *
  * @param[in] rsrc Resource handle.
  * @param[in] rsrc Resource handle.
  *
  *
- * @param[in] rm_data NULL-terminated array of graph handles. Each graph
- * contains triples to be removed from graphs with a matching URI in the
- * resource. If no matching graph is found, that graph has no effect.
- * Note that some internal functions may pass NULL to this parameter. This
- * should not be done externally because it assumes that the resource is
- * new and may leave it in an inconsistent state. This is different from
- * passing `{NULL}`.
+ * @param[in] rm_data `{NULL}`-terminated array of term arrays. Each term array
+ * contains, in the following order: graph URI, s, p, o. Any and all terms in
+ * each array may be NULL, acting as wildcards. This function calls
+ * #LSUP_graph_remove_txn() on each matching graph, or on all graphs if the
+ * first term in an array is NULL.
+ *
+ * @important Passing `NULL` (note: not `{NULL}`) as an argument to `rm_data`
+ * indicates that the resource is new. Some internal functions use this
+ * feature. This should not be done by an API consumer because it may leave the
+ * resource an inconsistent state.
  *
  *
  * @param[in] add_data NULL-terminated array of graph handles to add. If the
  * @param[in] add_data NULL-terminated array of graph handles to add. If the
  * resource has already a matching graph for a graph being added, the triples
  * resource has already a matching graph for a graph being added, the triples
@@ -139,8 +149,7 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc);
  * @return LSUP_OK on successful update; TODO
  * @return LSUP_OK on successful update; TODO
  */
  */
 LSUP_rc
 LSUP_rc
-LSR_desc_update (
-        LSR_Desc *rsrc, LSUP_Graph *const *rm_data,
+LSR_desc_update ( LSR_Desc *rsrc, LSUP_Term *const rm_data[][4],
         LSUP_Graph *const *add_data);
         LSUP_Graph *const *add_data);
 
 
 
 
@@ -191,24 +200,6 @@ LSUP_rc
 LSR_desc_store (const LSR_Desc *rsrc);
 LSR_desc_store (const LSR_Desc *rsrc);
 
 
 
 
-/** @brief Perform a delta update on a stored resource.
- *
- * This function operates directly on a stored resource without the need to
- * provide an in-memory DESC-R. It first deletes triples by given patterns,
- * then adds triples. Both steps are optional.
- *
- * @param[in] id ID of the resource to be modified, without the namespace.
- *
- * @param[in] remove Array of 3-member array of terms. Each set of
- *  terms represents the s, p, o bound terms. Any and all can be NULL, as in
- *  #LSUP_graph_remove(). The array is terminated by a `{NULL}` array.
- *
- * @param[in] add Array of triples to be added, terminated by a NULL.
- */
-LSUP_rc
-LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add);
-
-
 /** @brief Delete a DESC-R.
 /** @brief Delete a DESC-R.
  *
  *
  * TODO Soft-deletes to be implemented with versioning.
  * TODO Soft-deletes to be implemented with versioning.
@@ -219,6 +210,6 @@ LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add);
  *  resource was found for the given ID.
  *  resource was found for the given ID.
  */
  */
 LSUP_rc
 LSUP_rc
-LSUP_desc_delete (LSR_id id);
+LSR_desc_delete (LSR_id id);
 
 
 #endif /* _LSR_DESC_H */
 #endif /* _LSR_DESC_H */

+ 43 - 26
src/desc.c

@@ -129,8 +129,8 @@ LSUP_rc
 LSR_desc_store (const LSR_Desc *rsrc)
 LSR_desc_store (const LSR_Desc *rsrc)
 {
 {
     LSR_Desc *old_rsrc;
     LSR_Desc *old_rsrc;
-    LSUP_rc rc = LSR_desc_get (rsrc->id, &old_rsrc);
-    PRCCK (rc);
+    LSUP_rc get_rc = LSR_desc_get (rsrc->id, &old_rsrc);
+    PRCCK (get_rc);
 
 
     /*
     /*
      * BEGIN txn
      * BEGIN txn
@@ -138,12 +138,11 @@ LSR_desc_store (const LSR_Desc *rsrc)
     void *txn;
     void *txn;
     LSUP_store_begin (LSR_store, 0, &txn);
     LSUP_store_begin (LSR_store, 0, &txn);
     // Remove all existing user graphs.
     // Remove all existing user graphs.
-    if (rc == LSUP_OK) {
-        // TODO Handle managed preds and types.
-        // TODO Handle conflict between disjoint managed types.
-        // TODO Retain created and created_by.
+    if (get_rc == LSUP_OK) {
         for (size_t i = 0; old_rsrc->user_data[i] != NULL; i++) {
         for (size_t i = 0; old_rsrc->user_data[i] != NULL; i++) {
+
             LSUP_Term *gr_uri = LSUP_graph_uri (old_rsrc->user_data[i]);
             LSUP_Term *gr_uri = LSUP_graph_uri (old_rsrc->user_data[i]);
+
             size_t ct;
             size_t ct;
             // Remove triples from user graph.
             // Remove triples from user graph.
             PCHECK (LSUP_graph_remove_txn (
             PCHECK (LSUP_graph_remove_txn (
@@ -158,12 +157,14 @@ LSR_desc_store (const LSR_Desc *rsrc)
             PCHECK (LSUP_graph_remove_txn (
             PCHECK (LSUP_graph_remove_txn (
                         txn, old_rsrc->main_data,
                         txn, old_rsrc->main_data,
                         NULL, NULL, gr_uri, NULL), fail);
                         NULL, NULL, gr_uri, NULL), fail);
+
+            LSUP_term_free (gr_uri);
         }
         }
     }
     }
 
 
     LSUP_Graph *tmp_gr;
     LSUP_Graph *tmp_gr;
     // Add new triples.
     // Add new triples.
-    rc = LSUP_NOACTION;
+    LSUP_rc rc = LSUP_NOACTION;
     for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
     for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
         tmp_gr = LSUP_graph_new (
         tmp_gr = LSUP_graph_new (
                 LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
                 LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
@@ -212,13 +213,6 @@ fail:
 }
 }
 
 
 
 
-LSUP_rc
-LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
-{
-    return LSUP_OK;
-}
-
-
 LSUP_rc
 LSUP_rc
 LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
 LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
 {
 {
@@ -307,27 +301,50 @@ LSR_desc_user_data (const LSR_Desc *rsrc)
 
 
 LSUP_rc
 LSUP_rc
 LSR_desc_update (
 LSR_desc_update (
-        LSR_Desc *rsrc, LSUP_Graph *const *rm_data,
+        LSR_Desc *rsrc, LSUP_Term *const rm_data[][4],
         LSUP_Graph *const *add_data)
         LSUP_Graph *const *add_data)
 {
 {
     LSUP_rc rc = LSUP_NOACTION;
     LSUP_rc rc = LSUP_NOACTION;
     LSUP_Term *rsrc_uri = LSR_id_to_urn (rsrc->id, NULL);
     LSUP_Term *rsrc_uri = LSR_id_to_urn (rsrc->id, NULL);
 
 
-    size_t ct;
+    size_t ct = 0;
 
 
     /*
     /*
      * REMOVE user data.
      * REMOVE user data.
      */
      */
 
 
     if (rm_data) {
     if (rm_data) {
-        // Count graphs to be removed.
-        ct = 0;
-        while (add_data[ct]) ct++;
-        for (size_t i = 0; i < ct; i++) {
-            LSUP_Term *gr_uri = LSUP_graph_uri (rm_data[i]);
-            // TODO remove ops.
+        size_t ct_loop;
+        for (size_t i = 0; rm_data[i] != NULL; i++) {
+            LSUP_Buffer
+                *ss = NULL,
+                *sp = NULL,
+                *so = NULL,
+                *sc = NULL;
+
+            if (rm_data[i][1])
+                ss = LSUP_term_serialize (rm_data[i][1]);
+            if (rm_data[i][2])
+                sp = LSUP_term_serialize (rm_data[i][2]);
+            if (rm_data[i][3])
+                so = LSUP_term_serialize (rm_data[i][3]);
+            if (rm_data[i][0])
+                sc = LSUP_term_serialize (rm_data[i][0]);
+
+            LSR_store->sif->remove_fn (
+                    LSR_store->data, ss, sp, so, sc, NULL, &ct_loop);
+
+            LSUP_buffer_free (ss);
+            LSUP_buffer_free (sp);
+            LSUP_buffer_free (so);
+            LSUP_buffer_free (sc);
+
+            log_debug (
+                    "Removed %lu triples for %s",
+                    ct_loop, rm_data[i][0]->data);
+
+            ct += ct_loop;
             // TODO if graph is empty after removal, remove it.
             // TODO if graph is empty after removal, remove it.
-            LSUP_term_free (gr_uri);
         }
         }
     }
     }
 
 
@@ -335,7 +352,7 @@ LSR_desc_update (
      * ADD user data.
      * ADD user data.
      */
      */
 
 
-    // Count graphs inserted and allocate space.
+    // Count graphs to be inserted and allocate space.
     ct = 0;
     ct = 0;
     while (add_data[ct]) ct++;
     while (add_data[ct]) ct++;
     rsrc->user_data = calloc (sizeof (*rsrc->user_data), ct + 1);
     rsrc->user_data = calloc (sizeof (*rsrc->user_data), ct + 1);
@@ -370,7 +387,7 @@ LSR_desc_update (
         add_it = LSUP_graph_add_init (rsrc->user_data[i]);
         add_it = LSUP_graph_add_init (rsrc->user_data[i]);
         lu_it = LSUP_graph_lookup (rsrc->user_data[i], NULL, NULL, NULL, NULL);
         lu_it = LSUP_graph_lookup (rsrc->user_data[i], NULL, NULL, NULL, NULL);
 
 
-        // Loop over graph triples.
+        // Loop over source graph triples.
         LSUP_Term *dest_s, *dest_p, *dest_o;
         LSUP_Term *dest_s, *dest_p, *dest_o;
         LSUP_Triple *src_spo;
         LSUP_Triple *src_spo;
         while (LSUP_graph_iter_next (lu_it, &src_spo) == LSUP_OK) {
         while (LSUP_graph_iter_next (lu_it, &src_spo) == LSUP_OK) {
@@ -382,7 +399,7 @@ LSR_desc_update (
             LSUP_triple_init (spo, dest_s, dest_p, dest_o);
             LSUP_triple_init (spo, dest_s, dest_p, dest_o);
 
 
             // if the pred is managed, ignore the triple and send a warning.
             // if the pred is managed, ignore the triple and send a warning.
-            if (hashmap_get(LSR_managed_preds, spo->p)) {
+            if (hashmap_get (LSR_managed_preds, spo->p)) {
                 log_warn (
                 log_warn (
                         "Predicate %s is managed. Skipping triple.",
                         "Predicate %s is managed. Skipping triple.",
                         dest_p->data);
                         dest_p->data);

+ 2 - 0
test.c

@@ -33,6 +33,8 @@ int main(int argc, char **argv) {
         rc = 0;
         rc = 0;
     }
     }
 
 
+    LSR_done();
+
     end = clock();
     end = clock();
     wallclock = (end - start) * 1000 / CLOCKS_PER_SEC;
     wallclock = (end - start) * 1000 / CLOCKS_PER_SEC;
 
 

+ 39 - 0
test/test_desc.c

@@ -4,6 +4,7 @@
 static int
 static int
 test_desc_create()
 test_desc_create()
 {
 {
+    /*
     LSUP_Term *terms1[] = {
     LSUP_Term *terms1[] = {
         LSUP_iriref_new ("urn:s:1", NULL),
         LSUP_iriref_new ("urn:s:1", NULL),
         LSUP_iriref_new ("urn:s:2", NULL),
         LSUP_iriref_new ("urn:s:2", NULL),
@@ -51,8 +52,44 @@ test_desc_create()
     ASSERT (gr2, "Error creating graph!");
     ASSERT (gr2, "Error creating graph!");
     EXPECT_PASS (LSUP_graph_add (gr2, trp2, NULL));
     EXPECT_PASS (LSUP_graph_add (gr2, trp2, NULL));
 
 
+    char *tmp = NULL;
+    void *it;
+    it = ttl_codec.encode_graph_init(gr1);
+    while (ttl_codec.encode_graph_iter (it, &tmp) != LSUP_END)
+        log_info ("gr1: %s\n", tmp);
+    ttl_codec.encode_graph_done (it);
+
+    it = ttl_codec.encode_graph_init(gr2);
+    while (ttl_codec.encode_graph_iter (it, &tmp) != LSUP_END)
+        log_info ("gr2: %s\n", tmp);
+    ttl_codec.encode_graph_done (it);
+
+    free (tmp);
+
     for (size_t i = 0; trp2[i]; i++)
     for (size_t i = 0; trp2[i]; i++)
         free (trp2[i]);
         free (trp2[i]);
+    */
+    char
+        *ttl1 = "<urn:s:1> <urn:p:2> <urn:o:2> , <urn:o:1> ; "
+        "<urn:p:1> <urn:o:1> . "
+        "<urn:s:2> <urn:p:1> <urn:o:1> . ",
+        *ttl2 = "<urn:s:20> <urn:p:10> <urn:o:10> . "
+        "<urn:s:10> <urn:p:10> <urn:o:10> ; "
+        "<urn:p:20> <urn:o:20> , <urn:o:10> . ";
+
+    LSUP_Term
+        *usr1_uri = LSUP_iriref_new ("#usr1", NULL),
+        *usr2_uri = LSUP_iriref_new ("#usr2", NULL);
+
+    LSUP_Graph
+        *gr1 = LSUP_graph_new(NULL, usr1_uri, NULL),
+        *gr2 = LSUP_graph_new(NULL, usr2_uri, NULL);
+
+    LSUP_term_free (usr1_uri);
+    LSUP_term_free (usr2_uri);
+
+    ttl_codec.
+
 
 
     LSUP_Graph *data[] = {gr1, gr2, NULL};
     LSUP_Graph *data[] = {gr1, gr2, NULL};
     LSR_Desc *rsrc;
     LSR_Desc *rsrc;
@@ -63,10 +100,12 @@ test_desc_create()
     LSUP_graph_free (gr1);
     LSUP_graph_free (gr1);
     LSUP_graph_free (gr2);
     LSUP_graph_free (gr2);
 
 
+    /*
     for (int i = 0; i < 6; i++) {
     for (int i = 0; i < 6; i++) {
         LSUP_term_free (terms1[i]);
         LSUP_term_free (terms1[i]);
         LSUP_term_free (terms2[i]);
         LSUP_term_free (terms2[i]);
     }
     }
+    */
 
 
     // TODO more action
     // TODO more action