Explorar o código

Complete context list.

scossu hai 3 días
pai
achega
c7d620253f
Modificáronse 7 ficheiros con 104 adicións e 54 borrados
  1. 2 2
      Makefile
  2. 1 1
      include/lsup/store_interface.h
  3. 1 0
      src/core.c
  4. 11 4
      src/graph.c
  5. 1 1
      src/store.c
  6. 25 22
      src/store_mdb.c
  7. 63 24
      test/test_graph.c

+ 2 - 2
Makefile

@@ -20,7 +20,7 @@ VALGRIND_DUMP = /tmp/lsup_valgrind.log
 CALLGRIND_DUMP = /tmp/lsup_callgrind.out
 MASSIF_DUMP = /tmp/lsup_massif.out
 
-INCLUDE_BASE := . -Iinclude -I/opt/local/include -Iext/tpl/src -Iext/hashmap -Iext/log/src
+INCLUDE_BASE := . -Iinclude -Iext/tpl/src -Iext/hashmap -Iext/log/src
 INCLUDE := -I$(INCLUDE_BASE)
 _CFLAGS := -std=gnu11 -Wall -fPIC $(INCLUDE)
 CFLAGS = $(_CFLAGS) -O3
@@ -29,7 +29,7 @@ DBG_CFLAGS = $(_CFLAGS) -Itest -O0 -ggdb -DDEBUG
 #$(info DBG_CFLAGS: $(DBG_CFLAGS))
 # NOTE: -luuid is a Linux system library. Other OS's might need a different
 # link or a non-system library built.
-LDFLAGS := -L$(libdir) -L$(OUTDIR) -L. -llmdb -lxxhash -luuid
+LDFLAGS := -L$(OUTDIR) -L$(libdir) -L. -llmdb -lxxhash -luuid
 
 PARSER = bin/lemon
 LEMON_SRC = ext/sqlite/tool/lemon.c

+ 1 - 1
include/lsup/store_interface.h

@@ -454,7 +454,7 @@ typedef void (*iter_free_fn_t)(void * it);
  *
  * @return Set of all context URIs.
  */
-typedef LSUP_Buffer ** (*store_ctx_list_fn_t)(void *store);
+typedef LSUP_Buffer ** (*store_ctx_list_fn_t)(void *store, void *txn);
 
 
 

+ 1 - 0
src/core.c

@@ -1,3 +1,4 @@
+#define _XOPEN_SOURCE 500
 #include <errno.h>
 #include <ftw.h>
 #include <string.h>

+ 11 - 4
src/graph.c

@@ -253,6 +253,9 @@ LSUP_graph_free (LSUP_Graph *gr)
 
     LSUP_term_free (gr->uri);
     free (gr->store->id);
+    // If the store has the nsm_get_fn, it's been uniquely created for this
+    // graph and it's safe to free.
+    if (gr->store->sif->nsm_get_fn) LSUP_nsmap_free (gr->nsm);
     // If the store is a HTable, it means it has been created with the graph
     // and must go with it.
     if (gr->store->type == LSUP_STORE_HTABLE) {
@@ -640,23 +643,27 @@ LSUP_graph_iter_free (LSUP_GraphIterator *it)
 LSUP_TermSet *
 LSUP_graph_list_txn (void *txn, LSUP_Store *store)
 {
-    LSUP_Term **tdata = store->sif->ctx_ls_fn (store->data, txn);
+    LSUP_Buffer **tdata = store->sif->ctx_list_fn (store->data, txn);
     if (UNLIKELY (!tdata)) return NULL;
 
-    LSUP_TermSet ts = LSUP_term_set_new();
+    LSUP_TermSet *ts = LSUP_term_set_new();
 
     size_t i = 0;
     while (tdata[i]) {
-        LSUP_Term *t = LSUP_term_new_from_buffer (tdata[i++]);
+        LSUP_Term *t = LSUP_term_new_from_buffer (tdata[i]);
         LSUP_rc rc = LSUP_term_set_add (ts, t, NULL);
+        LSUP_buffer_free (tdata[i]);
         if (UNLIKELY (rc != LSUP_OK)) {
             LSUP_term_free (t);
             LSUP_term_set_free (ts);
 
             return NULL;
         }
+        i++;
     }
-    return tdata;
+    free (tdata);
+
+    return ts;
 }
 
 

+ 1 - 1
src/store.c

@@ -55,7 +55,7 @@ LSUP_store_new (
 void
 LSUP_store_free (LSUP_Store *store)
 {
-    if (!UNLIKELY (!store)) return;
+    if (UNLIKELY (!store)) return;
 
     store->sif->free_fn (store->data);
     if (store->id) free (store->id);

+ 25 - 22
src/store_mdb.c

@@ -690,7 +690,8 @@ mdbstore_add_abort (void *h)
 
 
 static LSUP_rc
-key_to_sterm (MDBIterator *it, const LSUP_Key key, LSUP_Buffer *sterm)
+key_to_sterm (
+        MDBStore *store, MDB_txn *txn, const LSUP_Key key, LSUP_Buffer *sterm)
 {
     LSUP_rc rc = LSUP_NORESULT;
     int db_rc;
@@ -699,7 +700,7 @@ key_to_sterm (MDBIterator *it, const LSUP_Key key, LSUP_Buffer *sterm)
     key_v.mv_data = (void*)&key;
     key_v.mv_size = KLEN;
 
-    db_rc = mdb_get (it->txn, it->store->dbi[IDX_T_ST], &key_v, &data_v);
+    db_rc = mdb_get (txn, store->dbi[IDX_T_ST], &key_v, &data_v);
 
     sterm->flags |= LSUP_BUF_BORROWED;
     if (db_rc == MDB_SUCCESS) {
@@ -915,9 +916,9 @@ mdbiter_next (
 
     if (rc == LSUP_OK) {
         if (sspo) {
-            key_to_sterm (it, it->spok[0], sspo->s);
-            key_to_sterm (it, it->spok[1], sspo->p);
-            key_to_sterm (it, it->spok[2], sspo->o);
+            key_to_sterm (it->store, it->txn, it->spok[0], sspo->s);
+            key_to_sterm (it->store, it->txn, it->spok[1], sspo->p);
+            key_to_sterm (it->store, it->txn, it->spok[2], sspo->o);
 
             // TODO error handling.
         }
@@ -933,7 +934,7 @@ mdbiter_next (
             if (!ctx) return LSUP_MEM_ERR;
 
             for (i = 0; it->ck[i]; i++)
-                key_to_sterm (it, it->ck[i], ctx + i);
+                key_to_sterm (it->store, it->txn, it->ck[i], ctx + i);
             memset (ctx + i, 0, sizeof (*ctx)); // Sentinel
 
             // TODO error handling.
@@ -1275,29 +1276,31 @@ LSUP_Buffer **
 mdbstore_ctx_list (void *h, void *th)
 {
     MDBStore *store = h;
-    LSUP_rc rc;
-    mdb_txn *txn;
+    LSUP_rc db_rc;
+    MDB_txn *txn;
     if (th) txn = th;
     else CHECK (mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn), fail);
-    MDB_cursor *cur;
 
-    CHECK (mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &cur), fail);
 
-    MDB_stat stat;
-    mdb_stat (txn, store->dbi[IDX_C_SPO], &stat);
-    size_t ct = stat.ms_entries;
-    LSUP_Buffer **tdata = malloc ((ct + 1) * sizeof(*data));
-    if (!UNLIKELY (!tdata)) goto fail;
+    MDB_cursor *cur;
+    CHECK (mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &cur), fail);
+    MDB_val key, data;
+    db_rc = mdb_cursor_get (cur, &key, &data, MDB_FIRST);
 
-    mdb_value key, data;
+    LSUP_Buffer **tdata = NULL;
     size_t i = 0;
-    rc = mdb_cursor_get (cur, &key, &data, MDB_FIRST);
-    while (rc == MDB_SUCCESS) {
-I       CHECK (key_to_sterm (&key, &tdata[i++]), fail);
-        CHECK (mdb_cursor_get (cur, &key, &data, MDB_NEXT), fail);
+    while (db_rc == MDB_SUCCESS) {
+        tdata = realloc (tdata, (i + 1) * sizeof (*tdata));
+        if (UNLIKELY (!tdata)) goto fail;
+        tdata[i] = BUF_DUMMY;
+        LSUP_Key tkey = *(LSUP_Key*)key.mv_data;
+        CHECK (key_to_sterm (store, txn, tkey, tdata[i]), fail);
+        db_rc = mdb_cursor_get (cur, &key, &data, MDB_NEXT_NODUP);
+        i++;
     }
 
-    tdata[ct] = NULL;  // Sentinel
+    tdata = realloc (tdata, i * sizeof (data));
+    tdata[i] = NULL;  // Sentinel
     mdb_cursor_close (cur);
     if (txn != th && txn != NULL) mdb_txn_abort (txn);
 
@@ -1343,7 +1346,7 @@ const LSUP_StoreInt mdbstore_int = {
     .nsm_put_fn     = mdbstore_nsm_put,
     .nsm_get_fn     = mdbstore_nsm_get,
 
-    .ctx_index_fn   = mdbsore_ctx_index,
+    .ctx_list_fn   = mdbstore_ctx_list,
 };
 
 

+ 63 - 24
test/test_graph.c

@@ -11,7 +11,7 @@ _graph_new (LSUP_StoreType type)
     if (sif->setup_fn) sif->setup_fn (NULL, true);
 
     LSUP_Graph *gr;
-    LSUP_Store *store;
+    LSUP_Store *store = NULL;
     if (type == LSUP_STORE_HTABLE) {
         gr = LSUP_graph_new (NULL, NULL, NULL);
     } else {
@@ -33,7 +33,7 @@ _graph_new (LSUP_StoreType type)
     EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
 
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -71,7 +71,8 @@ _graph_ns_uri (LSUP_StoreType type)
     LSUP_term_free (comp);
 
     LSUP_graph_free (gr);
-    if (store) LSUP_store_free (store);
+    LSUP_nsmap_free (nsm);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -87,7 +88,6 @@ _graph_add (LSUP_StoreType type)
 
     LSUP_Graph *gr;
     LSUP_Store *store;
-
     if (type == LSUP_STORE_HTABLE) store = NULL;
     else store = LSUP_store_new (type, NULL, 0);
 
@@ -123,7 +123,7 @@ _graph_add (LSUP_StoreType type)
 
     LSUP_graph_free (gr);
     LSUP_graph_free (gr2);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -140,7 +140,9 @@ _graph_get (LSUP_StoreType type)
 
     LSUP_Triple **trp = create_triples();
 
-    LSUP_Store *store = LSUP_store_new (type, NULL, 0);
+    LSUP_Store *store;
+    if (type == LSUP_STORE_HTABLE) store = NULL;
+    else store = LSUP_store_new (type, NULL, 0);
     LSUP_Graph
         *gr1 = LSUP_graph_new (store, NULL, NULL),
         *gr2 = LSUP_graph_new (store, NULL, NULL);
@@ -184,7 +186,7 @@ _graph_get (LSUP_StoreType type)
     LSUP_graph_free (gr2);
     LSUP_graph_free (gr3);
     LSUP_graph_free (gr4);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -199,7 +201,9 @@ _graph_link_map (LSUP_StoreType type)
 
     LSUP_Triple **trp = create_triples();
 
-    LSUP_Store *store = LSUP_store_new (type, NULL, 0);
+    LSUP_Store *store;
+    if (type == LSUP_STORE_HTABLE) store = NULL;
+    else store = LSUP_store_new (type, NULL, 0);
     LSUP_Graph *gr = LSUP_graph_new (store, NULL, NULL);
 
     size_t ct;
@@ -297,7 +301,7 @@ _graph_link_map (LSUP_StoreType type)
 
     free_triples (trp);
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -314,7 +318,9 @@ _graph_bool_ops (LSUP_StoreType type)
 
     LSUP_Triple **trp = create_triples();
 
-    LSUP_Store *store = LSUP_store_new (type, NULL, 0);
+    LSUP_Store *store;
+    if (type == LSUP_STORE_HTABLE) store = NULL;
+    else store = LSUP_store_new (type, NULL, 0);
     LSUP_Graph
         *gr1 = LSUP_graph_new (store, NULL, NULL),
         *gr2 = LSUP_graph_new (store, NULL, NULL),
@@ -402,7 +408,7 @@ _graph_bool_ops (LSUP_StoreType type)
     LSUP_graph_free (gr1);
     LSUP_graph_free (gr2);
     free_triples (trp);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -463,7 +469,7 @@ _graph_lookup (LSUP_StoreType type)
     if (sif->setup_fn) sif->setup_fn (NULL, true);
 
     LSUP_Graph *gr;
-    LSUP_Store *store;
+    LSUP_Store *store = NULL;
     if (type == LSUP_STORE_HTABLE) {
         gr = LSUP_graph_new (NULL, NULL, NULL);
     } else {
@@ -519,7 +525,7 @@ _graph_lookup (LSUP_StoreType type)
 
     free_triples (trp);
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -534,7 +540,7 @@ _graph_remove (LSUP_StoreType type)
     LSUP_Triple **trp = create_triples();
 
     LSUP_Graph *gr;
-    LSUP_Store *store;
+    LSUP_Store *store = NULL;
     if (type == LSUP_STORE_HTABLE) {
         gr = LSUP_graph_new (NULL, NULL, NULL);
     } else {
@@ -565,7 +571,7 @@ _graph_remove (LSUP_StoreType type)
     free_triples (trp); // gr copied data.
 
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     // TODO Test complete removal of triples from index when they are not
     // in another context.
@@ -611,7 +617,7 @@ _graph_txn (LSUP_StoreType type)
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     free_triples (trp); // gr copied data.
 
@@ -672,7 +678,7 @@ _graph_relative (LSUP_StoreType type)
     LSUP_term_free (rel_o);
     free (rel_spo);
     LSUP_graph_free (gr);
-    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -688,18 +694,36 @@ _graph_list (LSUP_StoreType type)
 
     LSUP_Store *store = LSUP_store_new (type, NULL, 0);
 
-    LSUP_Graph
-        *gr1 = LSUP_graph_new (store,  "urn:gr:1", NULL),
-        *gr2 = LSUP_graph_new (store,  "urn:gr:2", NULL),
-        *gr3 = LSUP_graph_new (store,  "urn:gr:3", NULL);
+    LSUP_Graph *gg[3] = {
+        LSUP_graph_new (store,  "urn:gr:1", NULL),
+        LSUP_graph_new (store,  "urn:gr:2", NULL),
+        LSUP_graph_new (store,  "urn:gr:3", NULL),
+    };
 
     LSUP_Triple **trp = create_triples();
-    LSUP_graph_add (gr1, trp, NULL);
+    LSUP_graph_add (gg[0], trp, NULL);  // Add some triples to the 1st graph.
+    LSUP_graph_add (gg[1], trp + 4, NULL);  // Same with the 2nd graph.
+    free_triples (trp);
 
     LSUP_TermSet *ts = LSUP_graph_list (store);
-    //TODO
+    ASSERT (ts != NULL, "Error creating context list!");
 
-    free_triples (trp);
+    EXPECT_INT_EQ (hashmap_count (ts), 2);
+
+    // Check if first 2 graphs (with triples) are in the context set.
+    LSUP_Hash key;
+    key = LSUP_term_hash (LSUP_graph_uri (gg[0]));
+    ASSERT (LSUP_term_set_get (ts, key) != NULL, "Context  #1 not found!");
+    key = LSUP_term_hash (LSUP_graph_uri (gg[1]));
+    ASSERT (LSUP_term_set_get (ts, key) != NULL, "Context #2 not found!");
+    key = LSUP_term_hash (LSUP_graph_uri (gg[2]));
+    ASSERT (
+            LSUP_term_set_get (ts, key) == NULL,
+            "Empty context shoud not be here!");
+
+    for (size_t i = 0; i < 3; i++) LSUP_graph_free (gg[i]);
+    LSUP_term_set_free (ts);
+    LSUP_store_free (store);
 
     return 0;
 }
@@ -836,6 +860,20 @@ BACKEND_TBL
 }
 
 
+static int test_graph_list()
+{
+    /*
+     * Test relative URIs in graphs.
+     */
+#define ENTRY(a, b) \
+    if (_graph_list (LSUP_STORE_##a) != 0) return -1;
+BACKEND_TBL
+#undef ENTRY
+
+    return 0;
+}
+
+
 static int test_graph_copy()
 {
     LSUP_Triple **trp = create_triples();
@@ -892,6 +930,7 @@ int graph_tests()
     RUN (test_graph_copy);
     RUN (test_graph_txn);
     RUN (test_graph_relative);
+    RUN (test_graph_list);
 
     return 0;
 }