Browse Source

Update to latest LSUP_rdf version.

scossu 1 year ago
parent
commit
aa22a928fd
8 changed files with 178 additions and 171 deletions
  1. 2 1
      .gitignore
  2. 6 5
      Makefile
  3. 0 0
      bin/.keep
  4. 8 4
      include/core.h
  5. 5 5
      include/desc.h
  6. 28 3
      src/core.c
  7. 110 137
      src/desc.c
  8. 19 16
      test/test_desc.c

+ 2 - 1
.gitignore

@@ -103,8 +103,9 @@ venv.bak/
 ## LOCAL ##
 
 sandbox.c
-mdb_store/
+bin/*
 doc/
+mdb_store/
 
 # IDE
 .syntastic*

+ 6 - 5
Makefile

@@ -5,6 +5,7 @@
 CC = gcc
 AR = ar
 
+VALGRIND_DUMP = /tmp/lsr_valgrind.log
 
 ## Paths.
 
@@ -15,10 +16,10 @@ includedir = $(PREFIX)/include/lsup
 
 INCLUDE = -I. -Iinclude -I$(includedir)
 # Link all ext libraries statically.
-CFLAGS += -Wall -fPIC -MMD $(INCLUDE)
+CFLAGS += -std=gnu11 -Wall -fPIC -MMD $(INCLUDE)
 DBG_CFLAGS = -Itest -O0 -g3 -DDEBUG
-LDFLAGS = -L. -L$(libdir) -llsuprdf -luuid
-DBG_LDFLAGS = -L. -L$(libdir) -llsuprdf_dbg -luuid
+LDFLAGS = -L. -L$(libdir) -llsuprdf
+DBG_LDFLAGS = -L. -L$(libdir) -llsuprdf_dbg
 
 SRC = $(wildcard src/*.c)
 OBJ = $(SRC:.c=.o)
@@ -119,9 +120,9 @@ gdb_test: bin/test ## Run a test suite within gdb.
 memcheck:
 	valgrind \
 		--leak-check=full --show-leak-kinds=all --track-origins=yes \
-		--log-file=/tmp/lsup_valgrind.log \
+		--log-file=$(VALGRIND_DUMP) \
 		./bin/test
-	@echo "Memcheck complete. Valgrind log is at /tmp/lsup_valgrind.log"
+	@echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
 
 
 memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.

+ 0 - 0
bin/.keep


+ 8 - 4
include/core.h

@@ -15,7 +15,8 @@
  */
 
 /// URI prefix for all managed resources.
-#define LSR_RSRC_PFX "urn:lsres:"
+#define LSR_RSRC_PFX "rsrc"
+#define LSR_RSRC_NS "urn:lsres:"
 
 /// Label for descriptive resource type.
 #define LSR_TYPE_DESC "DESC_R"
@@ -26,7 +27,7 @@
 
 /// Determine if an IRI is an LSR resource.
 #define LSR_IS_RSRC_IRI(term) \
-    (LSUP_IS_IRI (term) && strstr ((term)->data, LSR_RSRC_PFX) == (term)->data)
+    (LSUP_IS_IRI (term) && strstr ((term)->data, LSR_RSRC_NS) == (term)->data)
 
 
 /*
@@ -57,6 +58,8 @@ extern bool LSR_is_init;
 extern LSR_TermMap *LSR_managed_preds;
 /// Managed RDF types.
 extern LSR_TermMap *LSR_managed_types;
+/// Default back end store.
+extern LSUP_Store *LSR_store;
 
 
 /*
@@ -95,14 +98,15 @@ inline LSUP_Term *LSR_id_to_urn (const uuid_t id, const char *frag)
     char *urn_str;
 
     if (frag) {
+        // +2 for the : and #, NUL is included in UUID_STR_LEN.
         urn_str = malloc (
                 strlen (LSR_RSRC_PFX) + UUID_STR_LEN + strlen (frag) + 2);
         if (! urn_str) return NULL;
-        sprintf (urn_str, "%s%s#%s", LSR_RSRC_PFX, id_str, frag);
+        sprintf (urn_str, "%s:%s#%s", LSR_RSRC_PFX, id_str, frag);
     } else {
         urn_str = malloc (strlen (LSR_RSRC_PFX) + UUID_STR_LEN + 1);
         if (! urn_str) return NULL;
-        sprintf (urn_str, "%s%s", LSR_RSRC_PFX, id_str);
+        sprintf (urn_str, "%s:%s", LSR_RSRC_PFX, id_str);
     }
 
     LSUP_Term *urn = LSUP_iriref_new (urn_str, LSUP_default_nsm);

+ 5 - 5
include/desc.h

@@ -30,9 +30,9 @@
  * absolute URLs based on the server root.
  *
  * 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.
+ * graph handles.  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
@@ -40,7 +40,7 @@
  * List).
  *
  * Relationships between these graphs are expressed by triples stored in the
- * default graph.
+ * `main_data` graph.
  */
 typedef struct desc_t {
     LSUP_Graph **           user_data;      ///< User-defined data graphs.
@@ -70,7 +70,7 @@ typedef struct desc_t {
  *  reference the resource itself, an IRIRef with an empty string as data may
  *  be used. The graph URI is maintained within the resource. As a convention,
  *  it is recommended to use a fragment URI with a short label, e.g.
- *  `#set37205`, `#~alice`, or `#ancillary`. Fragments beginning with two
+ *  `#changeset37205`, `#~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 pointer. The handle should be freed with

+ 28 - 3
src/core.c

@@ -7,9 +7,9 @@ bool LSR_is_init = false;
  * Initial namespace map.
  */
 static char *nsm_str[][2] = {
+    {LSR_RSRC_PFX, LSR_RSRC_NS},
     {"ebucore", "http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#"},
-    {"rsrc", LSR_RSRC_PFX},
-    {"lsup", "http://data.knowledgetx.com/onto/lsup#"},
+    {"ore", "http://www.openarchives.org/ore/terms/"},
     {"premis", "http://www.loc.gov/premis/rdf/v1#"},
     {NULL, NULL}
 };
@@ -50,6 +50,7 @@ static char *mgd_type_str[] = {
 
 
 LSR_TermMap *LSR_managed_preds, *LSR_managed_types;
+LSUP_Store *LSR_store = NULL;
 
 
 /*
@@ -71,7 +72,12 @@ static int tmap_cmp_fn (const void *a, const void *b, void *udata)
 
 /// Free element within the term map.
 static void tmap_free_fn (void *item)
-{ LSUP_term_free ((LSUP_Term *) item); }
+{
+    // Partly reimplement LSUP_term_free() without freeing the term handle.
+    LSUP_Term *term = item;
+    if (LSUP_IS_IRI (term)) free (term->iri_info);
+    free (term->data);
+}
 
 
 LSUP_rc LSR_init (void)
@@ -81,6 +87,18 @@ LSUP_rc LSR_init (void)
     LSUP_rc rc = LSUP_init();
     if (rc < 0) return rc;
 
+    // Set up default back end.
+    // If LSR_BACKEND_URN is not defined in env, LSUP_MDB_STORE_URN is used;
+    // if that is also not set, lsup_rdf will use its default—which may be in
+    // a NON-PERSISTENT volume!
+    const char *store_urn = getenv ("LSR_BACKEND_URN");
+
+    const LSUP_StoreInt *sif = LSUP_store_int (LSR_DEFAULT_BACKEND);
+    if (sif->setup_fn) {
+        sif->setup_fn (store_urn, false);
+    }
+    LSR_store = LSUP_store_new (LSR_DEFAULT_BACKEND, store_urn, 0);
+
     // Load additional LSUP_repo namespaces into store and memory.
     LSUP_NSMap *nsm = LSUP_default_nsm;
 
@@ -95,9 +113,11 @@ LSUP_rc LSR_init (void)
             sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
             tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
     for (int i = 0; mgd_pred_str[i] != NULL; i++) {
+        log_trace ("Storing managed predicate: %s", mgd_pred_str[i]);
         LSUP_Term *uri = LSUP_iriref_new (mgd_pred_str[i], LSUP_default_nsm);
 
         hashmap_set (LSR_managed_preds, uri);
+        free (uri);
     }
 
     // Cache managed types.
@@ -105,10 +125,12 @@ LSUP_rc LSR_init (void)
             sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
             tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
     for (int i = 0; mgd_type_str[i] != NULL; i++) {
+        log_trace ("Storing managed type: %s", mgd_type_str[i]);
         LSUP_Term *uri = LSUP_iriref_new (
                 mgd_type_str[i], LSUP_default_nsm);
 
         hashmap_set (LSR_managed_types, uri);
+        free (uri);
     }
     LSR_is_init = true;
 
@@ -124,6 +146,9 @@ void LSR_done (void)
 
     log_info ("Tearing down LSUP repo environment.");
 
+    LSUP_store_free (LSR_store);
+    LSR_store = NULL;
+
     hashmap_free (LSR_managed_preds);
     hashmap_free (LSR_managed_types);
 

+ 110 - 137
src/desc.c

@@ -12,9 +12,15 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
 
     // Default context graph.
     rsrc->main_data = LSUP_graph_new (
-            LSUP_default_ctx, LSUP_STORE_HTABLE, NULL, NULL, 0);
+            NULL, LSUP_term_copy (LSUP_default_ctx), NULL);
+    log_debug (
+            "Main data graph: %s",
+            LSUP_graph_uri(rsrc->main_data)->data);
 
-    LSUP_GraphIterator *lu_it, *add_it, *admin_add_it = NULL;
+    LSUP_GraphIterator
+        *lu_it,                 // Lookup iterator.
+        *add_it,                // Main graph add iterator.
+        *admin_add_it = NULL;   // Admin graph add iterator.
 
     LSUP_Term *dest_s, *dest_p, *dest_o;
     LSUP_Triple src_spo_s, dest_spo_s;
@@ -35,7 +41,7 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
 
     // Loop over input graphs.
     for (size_t i = 0; i < ct; i++) {
-        LSUP_Term *gr_uri = LSUP_term_copy (LSUP_graph_uri (data[i]));
+        LSUP_Term *gr_uri = LSUP_graph_uri (data[i]);
 
         LSUP_Term *rel_uri = LSUP_iriref_relative (rsrc_uri, gr_uri);
         if (strstr (rel_uri->data, "#__") == rel_uri->data) {
@@ -46,7 +52,11 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
         if (rc < 0) goto finally;
 
         rsrc->user_data[i] = LSUP_graph_new (
-                gr_uri, LSUP_STORE_HTABLE, NULL, NULL, 0);
+                NULL, LSUP_term_copy (gr_uri), NULL);
+        log_debug (
+                "User data graph (@%p): %s",
+                LSUP_graph_uri(rsrc->user_data[i]),
+                LSUP_graph_uri(rsrc->user_data[i])->data);
 
         add_it = LSUP_graph_add_init (rsrc->user_data[i]);
         lu_it = LSUP_graph_lookup (rsrc->user_data[i], NULL, NULL, NULL, NULL);
@@ -76,24 +86,24 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
             LSUP_rc tmp_rc;
             // Check subject.
             if (LSR_IS_RSRC_IRI (dest_s)) {
-                uuid_parse (dest_s->data + strlen (LSR_RSRC_PFX), id_tmp);
+                uuid_parse (dest_s->data + strlen (LSR_RSRC_NS), id_tmp);
                 tmp_rc = LSR_desc_get (id_tmp, NULL);
                 if (tmp_rc != LSUP_OK) {
                     log_error (
                             "Referenced subject does not exist: %s",
-                            dest_s->data + strlen (LSR_RSRC_PFX));
+                            dest_s->data + strlen (LSR_RSRC_NS));
                     rc = LSUP_VALUE_ERR;
                     goto finally;
                 }
             }
             // Check object.
             if (LSR_IS_RSRC_IRI (dest_o)) {
-                uuid_parse (dest_o->data + strlen (LSR_RSRC_PFX), id_tmp);
+                uuid_parse (dest_o->data + strlen (LSR_RSRC_NS), id_tmp);
                 tmp_rc = LSR_desc_get (id_tmp, NULL);
                 if (tmp_rc != LSUP_OK) {
                     log_error (
                             "Referenced object does not exist: %s",
-                            dest_o->data + strlen (LSR_RSRC_PFX));
+                            dest_o->data + strlen (LSR_RSRC_NS));
                     rc = LSUP_VALUE_ERR;
                     goto finally;
                 }
@@ -152,8 +162,11 @@ loop_end:
     /* BEGIN adding managed (admin) data. */
 
     LSUP_Term *gr_uri = LSR_id_to_urn (rsrc->id, "__admin");
-    rsrc->admin_data = LSUP_graph_new (
-            gr_uri, LSUP_STORE_HTABLE, NULL, NULL, 0);
+    rsrc->admin_data = LSUP_graph_new (NULL, gr_uri, NULL);
+    log_debug (
+            "Admin data graph (@%p): %s",
+            LSUP_graph_uri(rsrc->admin_data),
+            LSUP_graph_uri(rsrc->admin_data)->data);
 
     admin_add_it = LSUP_graph_add_init (rsrc->admin_data);
     dest_s = LSUP_iriref_new("", NULL); // Relative to resource URI.
@@ -252,12 +265,15 @@ 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;
+    PRCCK (LSR_desc_get (rsrc->id, &old_rsrc));
 
+    /*
+     * BEGIN txn
+     */
+    void *txn;
+    LSUP_store_begin (LSR_store, 0, &txn);
     // Remove all existing user graphs.
     if (old_rsrc) {
-        LSUP_Term *main_data_urn = LSUP_graph_uri (old_rsrc->main_data);
         // TODO Handle managed preds and types.
         // TODO Handle conflict between disjoint managed types.
         // TODO Retain created and created_by.
@@ -265,32 +281,79 @@ LSR_desc_store (const LSR_Desc *rsrc)
             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);
+            PCHECK (LSUP_graph_remove_txn (
+                        txn, old_rsrc->user_data[i],
+                        NULL, NULL, NULL, &ct), fail);
+            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);
+            PCHECK (LSUP_graph_remove_txn (
+                        txn, old_rsrc->main_data, gr_uri,
+                        NULL, NULL, NULL), fail);
+            PCHECK (LSUP_graph_remove_txn (
+                        txn, old_rsrc->main_data,
+                        NULL, NULL, gr_uri, NULL), fail);
         }
     }
 
+    LSUP_Graph *tmp_gr;
     // Add new triples.
+    LSUP_rc rc = LSUP_NOACTION;
     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);
+        tmp_gr = LSUP_graph_new (
+                LSR_store,
+                LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
+        if (UNLIKELY (!tmp_gr)) {
+            rc = LSUP_MEM_ERR;
+            goto fail;
+        }
+        PCHECK (LSUP_graph_copy_contents_txn (
+                    txn, rsrc->user_data[i], tmp_gr), fail);
+        LSUP_graph_free (tmp_gr);
     }
 
     // Update admin data.
-    LSUP_graph_store (rsrc->admin_data, NULL, NULL);
+    tmp_gr = LSUP_graph_new (
+                LSR_store,
+                LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)), NULL);
+    if (UNLIKELY (!tmp_gr)) {
+        rc = LSUP_MEM_ERR;
+        goto fail;
+    }
+    PCHECK (LSUP_graph_copy_contents_txn (
+                txn, rsrc->admin_data, tmp_gr), fail);
+    LSUP_graph_free (tmp_gr);
+
     // Update graph metadata.
-    LSUP_graph_store (rsrc->main_data, NULL, NULL);
+    tmp_gr = LSUP_graph_new (
+                LSR_store,
+                LSUP_term_copy (LSUP_graph_uri (rsrc->main_data)), NULL);
+    if (UNLIKELY (!tmp_gr)) {
+        rc = LSUP_MEM_ERR;
+        goto fail;
+    }
+    PCHECK (LSUP_graph_copy_contents_txn (
+                txn, rsrc->main_data, tmp_gr), fail);
+    LSUP_graph_free (tmp_gr);
+
+    PCHECK (LSUP_store_commit (LSR_store, txn), fail);
+    /*
+     * END txn
+     */
+
+    return LSUP_OK;
+
+fail:
+    LSUP_store_abort (LSR_store, txn);
+
+    return rc;
 }
 
 
 LSUP_rc
 LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
 {
+    return LSUP_OK;
 }
 
 
@@ -300,39 +363,38 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
     LSUP_rc rc = LSUP_OK;
 
     LSUP_Graph *main_gr = LSUP_graph_new (
-            LSUP_default_ctx, LSR_DEFAULT_BACKEND, NULL, NULL, 0);
-
-    LSUP_Term
-        *s = LSR_id_to_urn (id, NULL),
-        *p = LSUP_iriref_new ("rdf:type", LSUP_default_nsm),
-        *o = LSUP_iriref_new ("lsup:Resource", LSUP_default_nsm);
+            LSR_store, LSUP_term_copy (LSUP_default_ctx), NULL);
+    if (!main_gr) return LSUP_DB_ERR;
 
-    LSUP_Triple *spo = LSUP_triple_new (s, p, o);
-    LSUP_triple_free (spo);
+    LSUP_Triple *spo = LSUP_triple_new (
+        LSR_id_to_urn (id, NULL),
+        LSUP_iriref_new ("rdf:type", LSUP_default_nsm),
+        LSUP_iriref_new ("lsup:Resource", LSUP_default_nsm)
+    );
 
     if (!LSUP_graph_contains (main_gr, spo)) {
         rc = LSUP_NORESULT;
         goto finally;
     }
 
-    LSUP_term_free (o);
-    o = LSUP_iriref_new ("lsup:DescriptiveResource", LSUP_default_nsm);
-    spo = LSUP_triple_new (s, p, o);
+    LSUP_term_free (spo->o);
+    spo->o = LSUP_iriref_new ("lsup:DescriptiveResource", LSUP_default_nsm);
     if (!LSUP_graph_contains (main_gr, spo)) {
-        log_error ("%s is not a descriptive resource.", o->data);
+        log_error ("%s is not a descriptive resource.", spo->o->data);
         rc = LSUP_NORESULT;
         goto finally;
     }
 
-    LSUP_term_free (p);
-    p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
+    LSUP_term_free (spo->p);
+    spo->p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
     size_t ct = 0, i = 0;
 
     // Find all graphs making up the resource.
-    LSUP_GraphIterator *it = LSUP_graph_lookup (main_gr, NULL, p, s, &ct);
+    LSUP_GraphIterator *it = LSUP_graph_lookup (
+            main_gr, NULL, spo->p, spo->s, &ct);
     LSUP_Graph **data = calloc (sizeof (*data), ct + 1);
     while (LSUP_graph_iter_next (it, &spo)) {
-        data[i] = LSUP_graph_new (spo->s, LSR_DEFAULT_BACKEND, NULL, NULL, 0);
+        data[i] = LSUP_graph_new (LSR_store, LSUP_term_copy (spo->s), NULL);
         if (! data[i++]) break; // Last slot remains NULL (sentinel).
     }
     LSUP_graph_iter_free (it);
@@ -345,9 +407,6 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
 
 finally:
     LSUP_triple_free (spo);
-    LSUP_term_free (s);
-    LSUP_term_free (p);
-    LSUP_term_free (o);
     LSUP_graph_free (main_gr);
 
     return rc;
@@ -357,29 +416,10 @@ finally:
 LSUP_Graph *
 LSR_desc_metadata (const LSR_Desc *rsrc)
 {
-    uuid_t uuid;
-    char 
-        *rsrc_uri_str = LSUP_graph_uri (rsrc->admin_data)->data,
-        *frag = "#metadata-",
-        uuid_str [UUID_STR_LEN],
-        id_str [8],
-        *uri_str = malloc (
-                strlen (frag) + strlen (rsrc_uri_str) + sizeof (id_str));
-    if (UNLIKELY (!uri_str)) return NULL;
-
-    uuid_generate_random (uuid);
-    uuid_unparse_lower (uuid, uuid_str);
-    strncpy (id_str, uuid_str, sizeof (id_str) - 1);
-
-    sprintf (uri_str, "%s%s%s", rsrc_uri_str, frag, id_str);
-
-    LSUP_Graph *res = LSUP_graph_copy (rsrc->admin_data);
-    if (LIKELY (res))
-        LSUP_graph_set_uri (res, LSUP_iriref_new (uri_str, NULL));
-
-    free (uri_str);
-    free (rsrc_uri_str);
-    free (uuid);
+    LSUP_Graph *res = LSUP_graph_new (
+            LSR_store, LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)),
+            NULL);
+    LSUP_graph_copy_contents (rsrc->admin_data, res);
 
     return res;
 }
@@ -388,39 +428,20 @@ LSR_desc_metadata (const LSR_Desc *rsrc)
 LSUP_Graph **
 LSR_desc_user_data (const LSR_Desc *rsrc)
 {
-    uuid_t uuid;
-    char 
-        *rsrc_uri_str = LSUP_graph_uri (rsrc->admin_data)->data,
-        *frag = "#metadata-",
-        uuid_str [UUID_STR_LEN],
-        id_str [8],
-        *uri_str = malloc (
-                strlen (frag) + strlen (rsrc_uri_str) + sizeof (id_str));
-    if (UNLIKELY (!uri_str)) return NULL;
-
     size_t ct = 0;
-    while (rsrc->user_data[ct]) ct ++;
-    LSUP_Graph **res = malloc (sizeof *res * (ct + 1));
+    while (rsrc->user_data[ct++]);
+    LSUP_Graph **res = malloc (sizeof (*res) * (ct + 1));
     if (UNLIKELY (!res)) return NULL;
 
     for (size_t i = 0; i < ct; i++) {
-        uuid_generate_random (uuid);
-        uuid_unparse_lower (uuid, uuid_str);
-        strncpy (id_str, uuid_str, sizeof (id_str) - 1);
-
-        sprintf (uri_str, "%s%s%s", rsrc_uri_str, frag, id_str);
-
-        res[i] = LSUP_graph_copy (rsrc->user_data[i]);
-        if (LIKELY (res))
-            LSUP_graph_set_uri (res[i], LSUP_iriref_new (uri_str, NULL));
+        res[i] = LSUP_graph_new (
+                LSR_store,
+                LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
+        LSUP_graph_copy_contents (rsrc->user_data[i], res[i]);
     }
 
     res[ct] = NULL;
 
-    free (uri_str);
-    free (rsrc_uri_str);
-    free (uuid);
-
     return res;
 }
 
@@ -438,51 +459,3 @@ void LSR_desc_free (LSR_Desc *rsrc)
 }
 
 
-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 (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;
-            // 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);
-        }
-    } 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]);
-
-        log_trace ("Storing data graph #%lu", 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);
-
-    return rc;
-}
-
-
-LSUP_rc
-LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
-{
-    LSUP_rc rc = LSUP_OK;
-
-    return rc;
-}

+ 19 - 16
test/test_desc.c

@@ -5,20 +5,20 @@ static int
 test_desc_create()
 {
     LSUP_Term *terms1[] = {
-        LSUP_iriref_new ("usn:s:1", NULL),
-        LSUP_iriref_new ("usn:s:2", NULL),
-        LSUP_iriref_new ("usn:p:1", NULL),
-        LSUP_iriref_new ("usn:p:2", NULL),
-        LSUP_iriref_new ("usn:o:1", NULL),
-        LSUP_iriref_new ("usn:o:2", NULL),
+        LSUP_iriref_new ("urn:s:1", NULL),
+        LSUP_iriref_new ("urn:s:2", NULL),
+        LSUP_iriref_new ("urn:p:1", NULL),
+        LSUP_iriref_new ("urn:p:2", NULL),
+        LSUP_iriref_new ("urn:o:1", NULL),
+        LSUP_iriref_new ("urn:o:2", NULL),
     };
     LSUP_Term *terms2[] = {
-        LSUP_iriref_new ("usn:s:10", NULL),
-        LSUP_iriref_new ("usn:s:20", NULL),
-        LSUP_iriref_new ("usn:p:10", NULL),
-        LSUP_iriref_new ("usn:p:20", NULL),
-        LSUP_iriref_new ("usn:o:10", NULL),
-        LSUP_iriref_new ("usn:o:20", NULL),
+        LSUP_iriref_new ("urn:s:10", NULL),
+        LSUP_iriref_new ("urn:s:20", NULL),
+        LSUP_iriref_new ("urn:p:10", NULL),
+        LSUP_iriref_new ("urn:p:20", NULL),
+        LSUP_iriref_new ("urn:o:10", NULL),
+        LSUP_iriref_new ("urn:o:20", NULL),
     };
 
     LSUP_Triple trp1[] = {
@@ -37,11 +37,14 @@ test_desc_create()
     };
 
     LSUP_Graph *gr1 = LSUP_graph_new (
-            LSUP_iriref_new ("#usr1", NULL), LSUP_STORE_MEM);
-    LSUP_graph_add (gr1, trp1, NULL);
+            NULL, LSUP_iriref_new ("#usr1", NULL), NULL);
+    ASSERT (gr1, "Error creating graph!");
+    EXPECT_PASS (LSUP_graph_add (gr1, trp1, NULL));
+
     LSUP_Graph *gr2 = LSUP_graph_new (
-            LSUP_iriref_new ("#usr2", NULL), LSUP_STORE_MEM);
-    LSUP_graph_add (gr2, trp2, NULL);
+            NULL, LSUP_iriref_new ("#usr2", NULL), NULL);
+    ASSERT (gr2, "Error creating graph!");
+    EXPECT_PASS (LSUP_graph_add (gr2, trp2, NULL));
 
     LSUP_Graph *data[] = {gr1, gr2, NULL};
     LSR_Desc *rsrc;