Browse Source

Store DESC-R. Basic tests.

Stefano Cossu 2 years ago
parent
commit
c0f01ca2e6
4 changed files with 31 additions and 12 deletions
  1. 11 4
      include/desc.h
  2. 1 1
      src/core.c
  3. 14 7
      src/desc.c
  4. 5 0
      test/test_desc.c

+ 11 - 4
include/desc.h

@@ -73,7 +73,10 @@ typedef struct desc_t {
  *  `#set37205`, `#~alice`, or `#ancillary`. Fragments beginning with two
  *  underscores are reserved for internal use and are not allowed in the input.
  *
- * @param[out] rsrc Resource handle. It must be freed with #LSR_desc_free().
+ * @param[out] rsrc Resource handle pointer. The handle should be freed with
+ *  #LSR_desc_free(). On a non-success return code, the handle may be garbage.
+ *
+ * @return LSUP_OK on success; < 0 on error.
  */
 LSUP_rc
 LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc);
@@ -86,7 +89,10 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc);
  *
  * @param[in] data Single graph to insert.
  *
- * @param[out] rsrc Resource handle. It must be freed with #LSR_desc_free().
+ * @param[out] rsrc Resource handle pointer. The handle should be freed with
+ *  #LSR_desc_free(). On a non-success return code, the handle may be garbage.
+ *
+ * @return LSUP_OK on success; < 0 on error.
  */
 LSUP_rc
 LSR_desc_new (const LSUP_Graph *data, LSR_Desc **rsrc);
@@ -102,8 +108,9 @@ LSR_desc_new (const LSUP_Graph *data, LSR_Desc **rsrc);
  * @param[in] id ID of the resource to be retrieved, without the namespace.
  *
  * @param[out] rsrc Resource handle to be populated with the found resource. It
- *  must be freed with #LSR_desc_free(). If NULL, the resource is only checked
- *  for existence (much faster).
+ *  should be freed with #LSR_desc_free(). If NULL, the resource is only
+ *  checked for existence (much faster). On a non-success return code, the
+ *  handle may be garbage.
  *
  * @return LSUP_OK if the resource is found; LSUP_NORESULT if not found; <0
  *  on error.

+ 1 - 1
src/core.c

@@ -102,7 +102,7 @@ void LSR_done (void)
 {
     if (!LSR_is_init) return;
 
-    log_info ("Tering down LSUP repo environment.");
+    log_info ("Tearing down LSUP repo environment.");
 
     LSR_TermMap *entry, *tmp;
     HASH_ITER (hh, LSR_managed_preds, entry, tmp) {

+ 14 - 7
src/desc.c

@@ -252,8 +252,8 @@ LSR_desc_store (const LSR_Desc *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);
+    if (rc == LSUP_OK) {
+        //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;
@@ -265,12 +265,13 @@ LSR_desc_store (const LSR_Desc *rsrc)
             LSUP_graph_remove (old_rsrc->main_data, gr_uri, NULL, NULL, NULL);
             LSUP_graph_remove (old_rsrc->main_data, NULL, NULL, gr_uri, NULL);
         }
-    }
+    } else rc = LSUP_OK;
 
     // 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_Term *gr_uri = LSUP_graph_uri (rsrc->user_data[i]);
 
+        log_trace ("Storing data graph #%lu", i);
         LSUP_graph_store (rsrc->user_data[i], NULL, NULL);
     }
 
@@ -278,17 +279,22 @@ LSR_desc_store (const LSR_Desc *rsrc)
     LSUP_graph_store (rsrc->admin_data, NULL, NULL);
     // Update graph metadata.
     LSUP_graph_store (rsrc->main_data, NULL, NULL);
+
+    return rc;
 }
 
 
 LSUP_rc
 LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
 {
+    LSUP_rc rc = LSUP_OK;
+
+    return rc;
 }
 
 
 LSUP_rc
-LSR_desc_get (const uuid_t id, LSR_Desc **rsrc)
+LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
 {
     LSUP_rc rc = LSUP_OK;
 
@@ -307,7 +313,8 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc)
     LSUP_term_free (p);
     LSUP_term_free (o);
 
-    if (rsrc && rc == LSUP_OK) {
+    if (rsrc_p)
+        if(rc == LSUP_OK) {
         p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_DEF_NSM);
         size_t ct = 0, i = 0;
 
@@ -320,7 +327,7 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc)
         }
         LSUP_graph_iter_free (it);
 
-        rc = LSR_desc_new_multi (data, rsrc);
+        rc = LSR_desc_new_multi (data, rsrc_p);
 
         i = 0;
         while (i < ct) LSUP_graph_free (data[i++]);

+ 5 - 0
test/test_desc.c

@@ -47,6 +47,8 @@ test_desc_create()
     LSR_Desc *rsrc;
     EXPECT_PASS (LSR_desc_new_multi (data, &rsrc));
 
+    // Free input handles before using the resource to ensure that these
+    // pointers are not being referenced by the resource.
     LSUP_graph_free (gr1);
     LSUP_graph_free (gr2);
 
@@ -57,6 +59,9 @@ test_desc_create()
 
     // TODO more action
 
+    // Store for next test before freeing.
+    EXPECT_PASS (LSR_desc_store (rsrc));
+
     LSR_desc_free (rsrc);
 
     return 0;