浏览代码

WIP temporarily remove htable back end.

Stefano Cossu 3 年之前
父节点
当前提交
a6777091bd
共有 9 个文件被更改,包括 128 次插入213 次删除
  1. 3 0
      include/core.h
  2. 11 3
      include/graph.h
  3. 45 19
      src/graph.c
  4. 0 0
      src/store_htable.c.disabled
  5. 2 1
      src/store_mdb.c
  6. 0 2
      test.c
  7. 65 26
      test/assets.h
  8. 2 2
      test/test_graph.c
  9. 0 160
      test/test_htable.c

+ 3 - 0
include/core.h

@@ -31,6 +31,9 @@
 // TODO Handle memory errors better.
 #define CRITICAL(exp)   if (UNLIKELY ((exp) == NULL)) abort()
 
+// TODO Cross-platform ramdisk path.
+#define TMPDIR /tmp
+
 #define KLEN sizeof(LSUP_Key)
 #define DBL_KLEN sizeof(LSUP_DoubleKey)
 #define TRP_KLEN sizeof(LSUP_TripleKey)

+ 11 - 3
include/graph.h

@@ -1,13 +1,21 @@
 #ifndef _LSUP_GRAPH_H
 #define _LSUP_GRAPH_H
 
-#include "store_htable.h"
 #include "store_mdb.h"
 
+/*
+ * Define backend types and checks.
+ */
+
+#define BACKEND_TBL                                                         \
+    ENTRY(  LSUP_STORE_MEM         )/* Memory backend, hash map. */         \
+    ENTRY(  LSUP_STORE_MDB         )/* LMDB back end on persistent disk. */ \
+    ENTRY(  LSUP_STORE_MDB_TMP     )/* LMDB back end on RAM disk. */        \
 
 typedef enum LSUP_store_type {
-    LSUP_STORE_MEM,
-    LSUP_STORE_MDB
+#define ENTRY(x) x,
+    BACKEND_TBL
+#undef ENTRY
 } LSUP_store_type;
 
 

+ 45 - 19
src/graph.c

@@ -1,4 +1,3 @@
-#include "htable.h"
 #include "graph.h"
 
 // Initial size of lookup graph. It will double each time capacity is reached.
@@ -31,7 +30,7 @@ typedef struct Graph {
     LSUP_store_type         store_type;     // In-memory or MDB-backed
     LSUP_Term               *uri;                 // Graph "name" (URI)
     union {
-        LSUP_HTStore *      ht_store;
+        //LSUP_HTStore *      ht_store;
         LSUP_MDBStore *     mdb_store;
     };
 } Graph;
@@ -40,7 +39,7 @@ typedef struct Graph {
 typedef struct GraphIterator {
     const Graph *           graph;      // Parent graph.
     union {                             // Internal store iterator.
-        LSUP_HTIterator *   ht_iter;
+        //LSUP_HTIterator *   ht_iter;
         LSUP_MDBIterator *  mdb_iter;
     };
     size_t                  ct;         // Total matches.
@@ -54,6 +53,11 @@ typedef struct GraphIterator {
 size_t LSUP_graph_size(const LSUP_Graph *gr);
 
 
+/* * * Static prototypes. * * */
+
+static inline void default_ctx_init();
+
+
 /* * * Post-lookup callback prototypes * * */
 
 int match_add_fn(
@@ -81,25 +85,27 @@ static inline bool is_null_trp(const LSUP_TripleKey *trp)
             *trp[2] == NULL_KEY);
 }
 
+#define ENTRY(x) (be) == (x) ||
+static inline bool
+check_backend (LSUP_store_type be)
+{ return (BACKEND_TBL false); }
+#undef ENTRY
+
 
 /* * * GRAPH * * */
 
 Graph *
 LSUP_graph_new(const LSUP_store_type store_type)
 {
+    if (!check_backend(store_type)) return NULL;
+
     LSUP_Graph *gr;
     CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
 
-    // Initialize default context only once per process.
-    if(UNLIKELY(!default_ctx)) {
-        LSUP_Term *default_ctx_uri = LSUP_uri_new(default_ctx_label);
-        LSUP_term_serialize(default_ctx_uri, default_ctx);
-        LSUP_term_free(default_ctx_uri);
-        atexit(ctx_cleanup);
-    }
+    default_ctx_init();
 
     if (store_type == LSUP_STORE_MEM) {
-        gr->ht_store = LSUP_htstore_new(0);
+        // TODO uncomment gr->ht_store = LSUP_htstore_new(0);
 
     } else if (store_type == LSUP_STORE_MDB) {
         gr->mdb_store = LSUP_mdbstore_new(
@@ -150,6 +156,7 @@ LSUP_graph_copy(const Graph *src)
 }
 
 
+/* TODO uncomment 
 Graph *
 LSUP_graph_bool_op(
         const LSUP_bool_op op, const Graph *gr1, const Graph *gr2)
@@ -176,6 +183,7 @@ LSUP_graph_bool_op(
 
     return res;
 }
+*/
 
 
 void
@@ -185,7 +193,7 @@ LSUP_graph_free(LSUP_Graph *gr)
         LSUP_term_free(gr->uri);
 
         if (gr->store_type == LSUP_STORE_MEM)
-            LSUP_htstore_free(gr->ht_store);
+            NULL;// TODO uncomment LSUP_htstore_free(gr->ht_store);
         else
             LSUP_mdbstore_free(gr->mdb_store);
 
@@ -207,7 +215,7 @@ LSUP_rc
 LSUP_graph_resize(LSUP_Graph *gr, size_t size)
 {
     if (gr->store_type == LSUP_STORE_MEM)
-        return LSUP_htstore_resize(gr->ht_store, size);
+        return 0;// TODO uncomment LSUP_htstore_resize(gr->ht_store, size);
 
     return LSUP_VALUE_ERR;
 }
@@ -217,7 +225,7 @@ size_t
 LSUP_graph_capacity(const Graph *gr)
 {
     if(gr->store_type == LSUP_STORE_MEM)
-        return LSUP_htstore_capacity(gr->ht_store);
+        return 0;// TODO uncomment LSUP_htstore_capacity(gr->ht_store);
 
     return LSUP_NOT_IMPL_ERR;
 }
@@ -227,7 +235,7 @@ size_t
 LSUP_graph_size(const Graph *gr)
 {
     if(gr->store_type == LSUP_STORE_MEM)
-        return LSUP_htstore_size(gr->ht_store);
+        return 0;// TODO uncomment LSUP_htstore_size(gr->ht_store);
 
     return LSUP_mdbstore_size(gr->mdb_store);
 }
@@ -247,6 +255,7 @@ LSUP_graph_add(
      * NOTE It is possible to pass both sets of RDF triples and buffer triples.
      */
 
+    /* TODO uncomment
     if (gr->store_type == LSUP_STORE_MEM) {
         // Resize all at once if needed.
         htsize_t prealloc = LSUP_htstore_size(gr->ht_store) + trp_ct + strp_ct;
@@ -291,6 +300,7 @@ LSUP_graph_add(
 
         return rc;
     }
+    */
 
     if (gr->store_type == LSUP_STORE_MDB) {
         rc = LSUP_NOACTION;
@@ -334,6 +344,8 @@ LSUP_graph_add(
         }
 
         LSUP_mdbstore_add_done(it, NULL);
+
+        return rc;
     }
 
     return LSUP_VALUE_ERR;
@@ -350,7 +362,7 @@ LSUP_graph_remove(Graph *gr, const LSUP_Triple *spo, size_t *ct)
     LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
 
     if (gr->store_type == LSUP_STORE_MEM)
-        rc = LSUP_htstore_remove(gr->ht_store, &sspo, ct);
+        rc = 0;// TODO uncomment LSUP_htstore_remove(gr->ht_store, &sspo, ct);
     else
         rc = LSUP_mdbstore_remove(gr->mdb_store, &sspo, sc, ct);
 
@@ -374,7 +386,7 @@ LSUP_graph_lookup(const Graph *gr, const LSUP_Triple *spo)
     LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
 
     if (gr->store_type == LSUP_STORE_MEM) {
-        it->ht_iter = LSUP_htstore_lookup(gr->ht_store, &sspo, &it->ct);
+        // TODO uncomment it->ht_iter = LSUP_htstore_lookup(gr->ht_store, &sspo, &it->ct);
 
     } else {
         it->mdb_iter = LSUP_mdbstore_lookup(gr->mdb_store, &sspo, sc, &it->ct);
@@ -398,7 +410,7 @@ graph_iter_next_buffer(GraphIterator *it, LSUP_SerTriple *sspo)
     LSUP_rc rc;
 
     if (it->graph->store_type == LSUP_STORE_MEM)
-        rc = LSUP_htiter_next(it->ht_iter, sspo);
+        rc = 0;// TODO uncomment LSUP_htiter_next(it->ht_iter, sspo);
     else rc = LSUP_mdbiter_next(it->mdb_iter, sspo);
 
     return rc;
@@ -427,7 +439,7 @@ void
 LSUP_graph_iter_free(GraphIterator *it)
 {
     if (it->graph->store_type == LSUP_STORE_MEM)
-        LSUP_htiter_free(it->ht_iter);
+        NULL;// TODO uncomment LSUP_htiter_free(it->ht_iter);
     else
         LSUP_mdbiter_free(it->mdb_iter);
 
@@ -445,3 +457,17 @@ LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo)
 
     return rc;
 }
+
+
+/* * * Static functions * * */
+
+static inline void default_ctx_init()
+{
+    // Initialize default context only once per process.
+    if(UNLIKELY(!default_ctx)) {
+        LSUP_Term *default_ctx_uri = LSUP_uri_new(default_ctx_label);
+        default_ctx = LSUP_buffer_new_from_term(default_ctx_uri);
+        LSUP_term_free(default_ctx_uri);
+        atexit(ctx_cleanup);
+    }
+}

+ 0 - 0
src/store_htable.c → src/store_htable.c.disabled


+ 2 - 1
src/store_mdb.c

@@ -382,12 +382,13 @@ LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc)
      */
     MDBIterator *it;
     CRITICAL (it = malloc (sizeof (*it)));
+    it->store = store;
     it->i = 0;
 
     if (!store->txn) {
         mdb_txn_begin(store->env, NULL, 0, &store->txn);
         // We are starting the main DB txn and we need to close it afterwards.
-        it->state = LSSTORE_DIRTY_TXN;
+        it->state |= LSSTORE_DIRTY_TXN;
     }
 
     // Take care of context first.

+ 0 - 2
test.c

@@ -1,5 +1,4 @@
 #include "test_term.c"
-#include "test_htable.c"
 #include "test_store_mdb.c"
 #include "test_graph.c"
 
@@ -7,7 +6,6 @@ int main(int argc, char **argv) {
 
     int result = (
         term_tests() |
-        htable_tests() |
         store_mdb_test() |
         graph_tests() |
         0);

+ 65 - 26
test/assets.h

@@ -12,35 +12,74 @@ LSUP_Triple *create_triples()
 
     // These constitute overall 10 individual triples, 8 unique.
 
-    LSUP_uri_init(trp[0].s, "urn:s:0");
-    LSUP_uri_init(trp[0].p, "urn:p:0");
-    LSUP_uri_init(trp[0].o, "urn:o:0");
+    /*
+    LSUP_uri_init(&terms[0][0], "urn:s:0");
+    LSUP_uri_init(&terms[0][1], "urn:p:0");
+    LSUP_uri_init(&terms[0][2], "urn:o:0");
+    trp[0] = {terms[1][0], terms[1][1], terms[1][2]};
+
+    LSUP_uri_init(&terms[1][0], "urn:s:1");
+    LSUP_uri_init(&terms[1][1], "urn:p:1");
+    LSUP_uri_init(&terms[1][2], "urn:o:1");
+    trp[1] = {terms[1][0], terms[1][1], terms[1][2]};
+
+    LSUP_uri_init(&terms[2][0], "urn:s:2");
+    LSUP_uri_init(&terms[2][1], "urn:p:2");
+    LSUP_uri_init(&terms[2][2], "urn:o:2");
+    trp[2] = {terms[2][0], terms[2][1], terms[2][2]};
+
+    LSUP_uri_init(&terms[3][0], "urn:s:0");
+    LSUP_uri_init(&terms[3][1], "urn:p:1");
+    LSUP_uri_init(&terms[3][2], "urn:o:2");
+    trp[3] = {terms[3][0], terms[3][1], terms[3][2]};
+
+    LSUP_uri_init(&terms[4][0], "urn:s:0");
+    LSUP_uri_init(&terms[4][1], "urn:p:2");
+    LSUP_term_init(&terms[4][2], LSUP_TERM_LITERAL, "String 1", NULL, NULL);
+    trp[4] = {terms[4][0], terms[4][1], terms[4][2]};
+
+    LSUP_uri_init(&terms[5][0], "urn:s:0");
+    LSUP_uri_init(&terms[5][1], "urn:p:5");
+    LSUP_term_init(
+            &terms[5][2], LSUP_TERM_LITERAL, "String 1", "xsd:string", NULL);
+    trp[5] = {terms[5][0], terms[5][1], terms[5][2]};
 
-    LSUP_uri_init(trp[1].s, "urn:s:1");
-    LSUP_uri_init(trp[1].p, "urn:p:1");
-    LSUP_uri_init(trp[1].o, "urn:o:1");
+    LSUP_uri_init(&terms[6][0], "urn:s:1");
+    LSUP_uri_init(&terms[6][1], "urn:p:6");
+    LSUP_term_init(
+            &terms[6][2], LSUP_TERM_LITERAL, "String 1", "xsd:string", "es-ES");
+    trp[6] = {terms[6][0], terms[6][1], terms[6][2]};
+    */
 
-    LSUP_uri_init(trp[2].s, "urn:s:2");
-    LSUP_uri_init(trp[2].p, "urn:p:2");
-    LSUP_uri_init(trp[2].o, "urn:o:2");
+    trp[0].s = LSUP_uri_new("urn:s:0"),
+    trp[0].p = LSUP_uri_new("urn:p:0"),
+    trp[0].o = LSUP_uri_new("urn:o:0"),
 
-    LSUP_uri_init(trp[3].s, "urn:s:0");
-    LSUP_uri_init(trp[3].p, "urn:p:1");
-    LSUP_uri_init(trp[3].o, "urn:o:2");
+    trp[1].s = LSUP_uri_new("urn:s:1");
+    trp[1].p = LSUP_uri_new("urn:p:1");
+    trp[1].o = LSUP_uri_new("urn:o:1");
 
-    LSUP_uri_init(trp[4].s, "urn:s:0");
-    LSUP_uri_init(trp[4].p, "urn:p:2");
-    LSUP_term_init(trp[4].o, LSUP_TERM_LITERAL, "String 1", NULL, NULL);
+    trp[2].s = LSUP_uri_new("urn:s:2");
+    trp[2].p = LSUP_uri_new("urn:p:2");
+    trp[2].o = LSUP_uri_new("urn:o:2");
 
-    LSUP_uri_init(trp[5].s, "urn:s:0");
-    LSUP_uri_init(trp[5].p, "urn:p:5");
-    LSUP_term_init(
-            trp[5].o, LSUP_TERM_LITERAL, "String 1", "xsd:string", NULL);
+    trp[3].s = LSUP_uri_new("urn:s:0");
+    trp[3].p = LSUP_uri_new("urn:p:1");
+    trp[3].o = LSUP_uri_new("urn:o:2");
 
-    LSUP_uri_init(trp[6].s, "urn:s:1");
-    LSUP_uri_init(trp[6].p, "urn:p:6");
-    LSUP_term_init(
-            trp[6].o, LSUP_TERM_LITERAL, "String 1", "xsd:string", "es-ES");
+    trp[4].s = LSUP_uri_new("urn:s:0");
+    trp[4].p = LSUP_uri_new("urn:p:2");
+    trp[4].o = LSUP_term_new(LSUP_TERM_LITERAL, "String 1", NULL, NULL);
+
+    trp[5].s = LSUP_uri_new("urn:s:0");
+    trp[5].p = LSUP_uri_new("urn:p:5");
+    trp[5].o = LSUP_term_new(
+            LSUP_TERM_LITERAL, "String 1", "xsd:string", NULL);
+
+    trp[6].s = LSUP_uri_new("urn:s:1");
+    trp[6].p = LSUP_uri_new("urn:p:6");
+    trp[6].o = LSUP_term_new(
+            LSUP_TERM_LITERAL, "String 1", "xsd:string", "es-ES");
 
     // Let's reuse pointers. Do not double-free.
     trp[7].s = trp[0].s; // <urn:s:0>
@@ -66,9 +105,9 @@ void free_triples(LSUP_Triple *trp)
 {
     // Last three triples are second pointers.
     for(int i=0; i < NUM_TRP - 3; i++) {
-        LSUP_term_done(trp[i].s);
-        LSUP_term_done(trp[i].p);
-        LSUP_term_done(trp[i].o);
+        LSUP_term_free(trp[i].s);
+        LSUP_term_free(trp[i].p);
+        LSUP_term_free(trp[i].o);
     }
 
     free(trp);

+ 2 - 2
test/test_graph.c

@@ -5,7 +5,7 @@
 static int test_graph_new()
 {
     LSUP_Graph *gr;
-    gr = LSUP_graph_new(LSUP_STORE_MEM);
+    gr = LSUP_graph_new(LSUP_STORE_MDB);
 
     EXPECT_PASS(LSUP_graph_set_uri(gr, "urn:gr:1"));
     EXPECT_STR_EQ(LSUP_graph_uri(gr)->data, "urn:gr:1");
@@ -26,7 +26,7 @@ static int test_graph_add()
 {
     LSUP_Triple *trp = create_triples();
 
-    LSUP_Graph *gr = LSUP_graph_new(LSUP_STORE_MEM);
+    LSUP_Graph *gr = LSUP_graph_new(LSUP_STORE_MDB);
     LSUP_graph_resize(gr, NUM_TRP + 2);
 
     size_t ct;

+ 0 - 160
test/test_htable.c

@@ -1,160 +0,0 @@
-#include "test.h"
-#include "htable.h"
-
-#define _CT 8
-
-static inline uint64_t id_hash_fn(const void *key, ksize_t size, uint64_t seed)
-{ return *(uint64_t*)key; }
-
-static inline bool buffer_eq_fn(const void *a, const void *b, ksize_t size)
-{ return memcmp(a, b, size) == 0; }
-
-
-static int htable_idx()
-{
-    LSUP_Key keys[_CT] = {5, 8, 13, 21, 34, 55, 89, 5};
-
-    LSUP_HTable *ht = LSUP_htable_new(
-            _CT, sizeof(LSUP_Key), sizeof(LSUP_Buffer),
-            id_hash_fn, buffer_eq_fn);
-
-    LSUP_Buffer *sterm = BUF_DUMMY;
-
-    for (int i = 0; i < _CT; i++) {
-        char tmp[64];
-        sprintf(tmp, "<%lu>", keys[i]);
-        LSUP_buffer_reset(sterm, strlen(tmp) + 1, tmp);
-        printf("Buffer to insert: ");
-        LSUP_buffer_print(sterm);
-
-        if (LSUP_htable_put(ht, keys + i, sterm) != LSUP_OK)
-            LSUP_buffer_done(sterm);
-    }
-
-    LSUP_buffer_free(sterm);
-
-    EXPECT_INT_EQ(LSUP_htable_size(ht), 7);
-
-    for (int i = 0; i < _CT; i++) {
-        LSUP_Buffer* vtmp;
-        char ptmp[64];
-        LSUP_htable_get(ht, keys + i, (void**)&vtmp);
-
-        printf("Key in get: <%lu>: ", keys[i]);
-        LSUP_buffer_print(vtmp);
-
-        sprintf(ptmp, "<%lu>", keys[i]);
-
-        EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
-    }
-
-    LSUP_Key *ktmp;
-    LSUP_Buffer *vtmp;
-    htsize_t cur = 0;
-
-    while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
-        printf("Key in iter: <%lu>: ", *ktmp);
-        LSUP_buffer_print(vtmp);
-
-        char ptmp[64];
-        sprintf(ptmp, "<%lu>", *ktmp);
-
-        EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
-    }
-
-    cur = 0;
-    while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
-        LSUP_buffer_done(vtmp);
-    }
-
-    printf("Freeing hash table.\n");
-    LSUP_htable_free(ht);
-
-    return 0;
-}
-
-
-static int htable_keys()
-{
-    LSUP_TripleKey keys[_CT] = {
-        {1, 1, 1},
-        {2, 1, 1},
-        {1, 2, 3},
-        {1, 9, 9},
-        {5, 6, 7},
-        {7, 6, 5},
-        {1, 1, 1}, // Duplicate.
-        {2, 1, 1}, // Duplicate.
-    };
-
-    LSUP_HTable *ht = LSUP_htable_new(
-            _CT, sizeof(LSUP_TripleKey), sizeof(LSUP_Buffer),
-            id_hash_fn, buffer_eq_fn);
-
-    LSUP_Buffer *sterm = BUF_DUMMY;
-
-    for (int i = 0; i < _CT; i++) {
-        char tmp[64];
-        sprintf(tmp, "<%lu : %lu : %lu>", keys[i][0], keys[i][1], keys[i][2]);
-        LSUP_buffer_reset(sterm, strlen(tmp) + 1, tmp);
-        TRACE(STR, "Buffer to insert: ");
-        LSUP_buffer_print(sterm);
-
-        if (LSUP_htable_put(ht, keys + i, sterm) != LSUP_OK)
-            LSUP_buffer_done(sterm);
-    }
-
-    LSUP_buffer_free(sterm);
-
-    EXPECT_INT_EQ(LSUP_htable_size(ht), 6);
-
-    for (int i = 0; i < _CT; i++) {
-        LSUP_Buffer* vtmp;
-        char ptmp[64];
-        LSUP_htable_get(ht, keys[i], (void**)&vtmp);
-
-        printf(
-                "Key in get: <%lu : %lu : %lu>: ",
-                keys[i][0], keys[i][1], keys[i][2]);
-        LSUP_buffer_print(vtmp);
-
-        sprintf(ptmp, "<%lu : %lu : %lu>", keys[i][0], keys[i][1], keys[i][2]);
-
-        EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
-    }
-
-    LSUP_TripleKey *ktmp;
-    LSUP_Buffer *vtmp;
-    htsize_t cur = 0;
-
-    while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
-        printf(
-                "Key in iter: <%lu : %lu : %lu>: ",
-                (*ktmp)[0], (*ktmp)[1], (*ktmp)[2]);
-        LSUP_buffer_print(vtmp);
-
-        char ptmp[64];
-        sprintf(ptmp, "<%lu : %lu : %lu>", (*ktmp)[0], (*ktmp)[1], (*ktmp)[2]);
-
-        EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
-    }
-
-    cur = 0;
-    while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
-        LSUP_buffer_done(vtmp);
-    }
-
-    printf("Freeing hash table.\n");
-    LSUP_htable_free(ht);
-
-    return 0;
-}
-
-
-int htable_tests()
-{
-    RUN(htable_idx);
-    RUN(htable_keys);
-
-    return 0;
-}