Browse Source

Cleaner initialization and teardown of MDB stores.

Stefano Cossu 4 years ago
parent
commit
f094f7d676
2 changed files with 40 additions and 17 deletions
  1. 33 15
      src/graph.c
  2. 7 2
      src/store_mdb.c

+ 33 - 15
src/graph.c

@@ -26,6 +26,7 @@ typedef enum KSetFlag {
  */
 static const char *default_ctx_label = "urn:lsup:default";
 static LSUP_Buffer *default_ctx = NULL;
+static LSUP_MDBStore *default_store = NULL, *default_tmp_store = NULL;
 
 
 typedef struct Graph {
@@ -75,7 +76,16 @@ graph_iter_next_buffer (GraphIterator *it, LSUP_SerTriple *sspo);
 
 
 /* Atexit functions. */
-void ctx_cleanup() { LSUP_buffer_free (default_ctx); }
+void ctx_cleanup()
+{
+/*@ @brief Close LMDB environment.
+ *
+ * Run at exit.
+ */
+    LSUP_mdbstore_free (default_store);
+    LSUP_mdbstore_free (default_tmp_store);
+    LSUP_buffer_free (default_ctx);
+}
 
 
 static inline bool is_null_trp (const LSUP_TripleKey *trp)
@@ -106,21 +116,17 @@ LSUP_graph_new (const LSUP_store_type store_type)
     gr->uri = LSUP_uri_new (NULL);
     gr->store_type = store_type;
 
-    if (mdbstore_init() != LSUP_OK) return NULL;
+    if (UNLIKELY (mdbstore_init() != LSUP_OK)) return NULL;
 
     if (gr->store_type == LSUP_STORE_MEM) {
         // TODO uncomment gr->ht_store = LSUP_htstore_new (0);
         // if (!gr->ht_store) return NULL;
 
     } else if (gr->store_type == LSUP_STORE_MDB) {
-        gr->mdb_store = LSUP_mdbstore_new(
-                getenv ("LSUP_MDB_STORE_PATH"), default_ctx);
-        if (!gr->mdb_store) return NULL;
+        gr->mdb_store = default_store;
 
     } else {
-        gr->mdb_store = LSUP_mdbstore_new(
-                MDB_RAMDISK_PATH, default_ctx);
-        if (!gr->mdb_store) return NULL;
+        gr->mdb_store = default_tmp_store;
     }
 
     return gr;
@@ -202,10 +208,12 @@ LSUP_graph_free (LSUP_Graph *gr)
     if (LIKELY (gr != NULL)) {
         LSUP_term_free (gr->uri);
 
+        /*
         if (gr->store_type == LSUP_STORE_MEM)
             NULL;// TODO uncomment LSUP_htstore_free (gr->ht_store);
         else
             LSUP_mdbstore_free (gr->mdb_store);
+        */
 
         free (gr);
     }
@@ -517,13 +525,19 @@ LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
 static inline LSUP_rc
 mdbstore_init()
 {
-    if (UNLIKELY (!default_ctx)) {
-        // RAM disk store.
+    char *path;
+
+    // RAM disk store.
+    if (UNLIKELY (!default_tmp_store)) {
         printf("Initilalizing RAM disk back end.\n");
-        char *path = MDB_RAMDISK_PATH;
+        path = MDB_RAMDISK_PATH;
         if (LSUP_mdbstore_setup (path, true) != LSUP_OK) return LSUP_DB_ERR;
+        default_tmp_store = LSUP_mdbstore_new (path, default_ctx);
+        if (UNLIKELY (!default_tmp_store)) return LSUP_DB_ERR;
+    }
 
-        // Permanent store.
+    // Permanent store.
+    if (UNLIKELY (!default_store)) {
         printf("Initilalizing persistent back end.\n");
         // NOTE This method will only allow one persistent disk back end per
         // application. TODO maybe later allow multiple backends if useful.
@@ -532,13 +546,17 @@ mdbstore_init()
             path = DEFAULT_ENV_PATH;
             fprintf(
                 stderr,
-                "WARNING: `LSUP_STORE_PATH' environment variable is not set. "
-                "The default location %s will be used as the graph store.\n",
-                path
+                "WARNING: `LSUP_MDB_STORE_PATH' environment variable is not "
+                "set. The default location %s will be used as the graph "
+                "store.\n", path
             );
         }
         if (LSUP_mdbstore_setup (path, false) != LSUP_OK) return LSUP_DB_ERR;
+        default_store = LSUP_mdbstore_new (path, default_ctx);
+        if (UNLIKELY (!default_store)) return LSUP_DB_ERR;
+    }
 
+    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);

+ 7 - 2
src/store_mdb.c

@@ -196,6 +196,7 @@ inline static LSUP_rc lookup_2bound(
 inline static LSUP_rc lookup_3bound(
         MDBStore *store, MDBIterator *it, size_t *ct);
 
+
 /**
  * API.
  */
@@ -256,7 +257,9 @@ LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx)
     char *env_mapsize = getenv ("LSUP_MDB_MAPSIZE");
     if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
     else sscanf (env_mapsize, "%lu", &mapsize);
-    printf ("Setting environment map size to %lu bytes.\n", mapsize);
+    TRACE(
+            "Setting environment map size at %s to %lu bytes.\n",
+            path, mapsize);
     db_rc = mdb_env_set_mapsize (store->env, mapsize);
 
     db_rc = mdb_env_set_maxdbs (store->env, N_DB);
@@ -289,7 +292,9 @@ void
 LSUP_mdbstore_free (LSUP_MDBStore *store)
 {
     if (store->state & LSSTORE_OPEN) {
-        TRACE (STR, "Closing MDB env.\n");
+        const char *path;
+        mdb_env_get_path (store->env, &path);
+        TRACE ("Closing MDB env at %s.\n", path);
         mdb_env_close (store->env);
     }