Browse Source

WIP store DESC-R.

Stefano Cossu 2 years ago
parent
commit
efdacd685d
3 changed files with 72 additions and 7 deletions
  1. 19 6
      include/desc.h
  2. 44 0
      src/desc.c
  3. 9 1
      test/test_desc.c

+ 19 - 6
include/desc.h

@@ -29,10 +29,10 @@
  * they are portable and, in a Linked Data server, can be easily replaced with
  * absolute URLs based on the server root.
  *
- * The `user_data` handle is a NULL-terminated array of user-defined graphs.
- * Each graph is given a user-defined label that is tacked on to the resource
- * URN and may or may not be consistent across resources, depending on how
- * repository managers organize their knowledge.
+ * The `user_data` handle is a {NULL}-terminated array of user-defined graphs.
+ * Each graph URI is considered relative to the resource URN and may or may not
+ * be consistent across resources, depending on how repository managers
+ * organize their knowledge.
  *
  * The `admin_data` graph contains exclusively triples managed by the
  * repository.  They may have special functionality attached and may be created
@@ -129,10 +129,10 @@ LSR_desc_free (LSR_Desc *rsrc);
  * @param[in] rsrc Resource to be created or overwritten.
  */
 LSUP_rc
-LSR_desc_store (LSR_Desc *rsrc);
+LSR_desc_store (const LSR_Desc *rsrc);
 
 
-/** Perform a delta update on a stored resource.
+/** @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,
@@ -149,4 +149,17 @@ LSR_desc_store (LSR_Desc *rsrc);
 LSUP_rc
 LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add);
 
+
+/** @brief Delete a DESC-R.
+ *
+ * TODO Soft-deletes to be implemented with versioning.
+ *
+ * @param[in] id Resource ID.
+ *
+ * @return LSUP_OK if the resource was found and deleted; LSUP_NOACTION if no
+ *  resource was found for the given ID.
+ */
+LSUP_rc
+LSUP_desc_delete (LSR_id id);
+
 #endif /* _LSR_DESC_H */

+ 44 - 0
src/desc.c

@@ -243,6 +243,50 @@ fail:
 }
 
 
+LSUP_rc
+LSR_desc_store (const LSR_Desc *rsrc)
+{
+    // TODO Make atomic. Needs to implement transactions in backend.
+    LSR_Desc *old_rsrc;
+    LSUP_rc rc = LSR_desc_get (rsrc->id, &old_rsrc);
+    if (UNLIKELY (rc < 0)) return rc;
+
+    // Remove all existing user graphs.
+    if (old_rsrc) {
+        LSUP_Term *main_data_urn = LSUP_graph_uri (old_rsrc->main_data);
+        for (size_t i = 0; old_rsrc->user_data[i] != NULL; i++) {
+            LSUP_Term *gr_uri = LSUP_graph_uri (old_rsrc->user_data[i]);
+            size_t ct;
+            // Remove triples from user graph.
+            LSUP_graph_remove (old_rsrc->user_data[i], NULL, NULL, NULL, &ct);
+            log_debug ( "Removed %lu triples from graph %s", ct, gr_uri->data);
+
+            // Remove user graph metadata.
+            LSUP_graph_remove (old_rsrc->main_data, gr_uri, NULL, NULL, NULL);
+            LSUP_graph_remove (old_rsrc->main_data, NULL, NULL, gr_uri, NULL);
+        }
+    }
+
+    // Add new triples.
+    for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
+        LSUP_Term *gr_uri = LSUP_graph_uri (rsrc->user_data[i]);
+
+        LSUP_graph_store (rsrc->user_data[i], NULL, NULL);
+    }
+
+    // Update admin data.
+    LSUP_graph_store (rsrc->admin_data, NULL, NULL);
+    // Update graph metadata.
+    LSUP_graph_store (rsrc->main_data, NULL, NULL);
+}
+
+
+LSUP_rc
+LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
+{
+}
+
+
 LSUP_rc
 LSR_desc_get (const uuid_t id, LSR_Desc **rsrc)
 {

+ 9 - 1
test/test_desc.c

@@ -42,8 +42,8 @@ test_desc_create()
     LSUP_Graph *gr2 = LSUP_graph_new (
             LSUP_iriref_new ("#usr2", NULL), LSUP_STORE_MEM);
     LSUP_graph_add (gr2, trp2, NULL);
-    LSUP_Graph *data[] = {gr1, gr2, NULL};
 
+    LSUP_Graph *data[] = {gr1, gr2, NULL};
     LSR_Desc *rsrc;
     EXPECT_PASS (LSR_desc_new_multi (data, &rsrc));
 
@@ -62,8 +62,16 @@ test_desc_create()
     return 0;
 }
 
+
+static int
+test_desc_get ()
+{
+    return 0;
+}
+
 int desc_tests()
 {
     RUN (test_desc_create);
+    RUN (test_desc_get);
     return 0;
 }