Browse Source

Set up RAMdisk store on first graph.

Stefano Cossu 4 years ago
parent
commit
e046f64390
2 changed files with 50 additions and 11 deletions
  1. 23 5
      src/graph.c
  2. 27 6
      test/test_graph.c

+ 23 - 5
src/graph.c

@@ -11,6 +11,8 @@
 // factor low.
 #define IDX_SIZE_RATIO 1.7
 
+// RAMdisk path for MDB volatile store.
+#define MDB_RAMDISK_PATH TMPDIR "/lsup_mem_graph"
 
 typedef enum KSetFlag {
     LSUP_KS_NONE        = 0,
@@ -55,7 +57,7 @@ size_t LSUP_graph_size(const LSUP_Graph *gr);
 
 /* * * Static prototypes. * * */
 
-static inline void default_ctx_init();
+static inline void mdbstore_init();
 
 
 /* * * Post-lookup callback prototypes * * */
@@ -103,18 +105,21 @@ LSUP_graph_new(const LSUP_store_type store_type)
     CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
     gr->uri = LSUP_uri_new(NULL);
 
-    default_ctx_init();
+    mdbstore_init();
 
     if (store_type == LSUP_STORE_MEM) {
         // TODO uncomment gr->ht_store = LSUP_htstore_new(0);
+        // if (!gr->ht_store) return NULL;
 
     } else if (store_type == LSUP_STORE_MDB) {
         gr->mdb_store = LSUP_mdbstore_new(
                 getenv("LSUP_MDB_STORE_PATH"), default_ctx);
+        if (!gr->mdb_store) return NULL;
 
     } else {
         gr->mdb_store = LSUP_mdbstore_new(
-                TMPDIR "/lsup_mem_graph", default_ctx);
+                MDB_RAMDISK_PATH, default_ctx);
+        if (!gr->mdb_store) return NULL;
     }
 
     return gr;
@@ -218,6 +223,7 @@ LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri)
 LSUP_rc
 LSUP_graph_resize(LSUP_Graph *gr, size_t size)
 {
+    return LSUP_OK; // TODO remove line.
     if (gr->store_type == LSUP_STORE_MEM)
         return 0;// TODO uncomment LSUP_htstore_resize(gr->ht_store, size);
 
@@ -464,10 +470,22 @@ LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo)
 
 /* * * Static functions * * */
 
-static inline void default_ctx_init()
+/** @brief Initialize default context and ramdisk only once per process.
+ *
+ * The ramdisk store persists after the application is closed, but will be
+ * wiped clean the next time this function is called.
+ */
+static inline void mdbstore_init()
 {
-    // Initialize default context only once per process.
     if(UNLIKELY(!default_ctx)) {
+        // RAM disk store.
+        char *path = MDB_RAMDISK_PATH;
+        LSUP_mdbstore_setup (&path, true);
+
+        // Permanent store.
+        //path = getenv ("LSUP_MDB_STORE_PATH");
+        //LSUP_mdbstore_setup (&path);
+
         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);

+ 27 - 6
test/test_graph.c

@@ -2,10 +2,12 @@
 #include "graph.h"
 #include "assets.h"
 
-static int test_graph_new()
+/* TODO Not yet implemented. */
+static int __attribute__ ((unused))
+test_graph_mem_new()
 {
     LSUP_Graph *gr;
-    gr = LSUP_graph_new(LSUP_STORE_MDB);
+    gr = LSUP_graph_new(LSUP_STORE_MEM);
 
     EXPECT_PASS(LSUP_graph_set_uri(gr, "urn:gr:1"));
     EXPECT_STR_EQ(LSUP_graph_uri(gr)->data, "urn:gr:1");
@@ -22,12 +24,31 @@ static int test_graph_new()
 }
 
 
-static int test_graph_add()
+static int
+test_graph_mdb_new()
+{
+    LSUP_Graph *gr;
+    gr = LSUP_graph_new(LSUP_STORE_MDB);
+    ASSERT (gr != NULL, "Error creating graph!");
+
+    EXPECT_PASS(LSUP_graph_set_uri(gr, "urn:gr:1"));
+    EXPECT_STR_EQ(LSUP_graph_uri(gr)->data, "urn:gr:1");
+
+    ASSERT(strcmp(LSUP_graph_uri(gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
+    EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
+
+    LSUP_graph_free(gr);
+
+    return 0;
+}
+
+
+static int
+test_graph_mdb_add()
 {
     LSUP_Triple *trp = create_triples();
 
     LSUP_Graph *gr = LSUP_graph_new(LSUP_STORE_MDB);
-    LSUP_graph_resize(gr, NUM_TRP + 2);
 
     size_t ct;
     LSUP_graph_add_trp(gr, trp, NUM_TRP, &ct);
@@ -50,8 +71,8 @@ static int test_graph_add()
 
 int graph_tests()
 {
-    RUN(test_graph_new);
-    RUN(test_graph_add);
+    RUN(test_graph_mdb_new);
+    RUN(test_graph_mdb_add);
     return 0;
 }