|
@@ -256,24 +256,22 @@ finally:
|
|
|
|
|
|
|
|
|
|
static LSUP_rc
|
|
static LSUP_rc
|
|
-mdbstore_nsm_put (void *h, const LSUP_NSMap *nsm, void *th)
|
|
|
|
|
|
+nsm_put (MDB_env *env, MDB_txn *p_txn, const LSUP_NSMap *nsm)
|
|
{
|
|
{
|
|
- MDBStore *store = h;
|
|
|
|
- MDB_txn *txn;
|
|
|
|
- RCCK (mdb_txn_begin (store->env, (MDB_txn *) th, 0, &txn));
|
|
|
|
-
|
|
|
|
LSUP_rc rc = LSUP_NOACTION;
|
|
LSUP_rc rc = LSUP_NOACTION;
|
|
int db_rc;
|
|
int db_rc;
|
|
|
|
|
|
|
|
+ MDB_txn *txn;
|
|
|
|
+ RCCK (mdb_txn_begin (env, p_txn, 0, &txn));
|
|
|
|
+
|
|
MDB_cursor *dcur = NULL, *icur = NULL;
|
|
MDB_cursor *dcur = NULL, *icur = NULL;
|
|
- if (
|
|
|
|
- mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &dcur) != MDB_SUCCESS
|
|
|
|
- ||
|
|
|
|
- mdb_cursor_open (txn, store->dbi[IDX_NS_PFX], &icur) != MDB_SUCCESS
|
|
|
|
- ) {
|
|
|
|
- mdb_txn_abort (txn);
|
|
|
|
- return LSUP_DB_ERR;
|
|
|
|
- }
|
|
|
|
|
|
+ MDB_dbi ddbi, idbi;
|
|
|
|
+ RCCK (mdb_dbi_open (
|
|
|
|
+ txn, db_labels[IDX_PFX_NS], db_flags[IDX_PFX_NS], &ddbi));
|
|
|
|
+ RCCK (mdb_dbi_open (
|
|
|
|
+ txn, db_labels[IDX_NS_PFX], db_flags[IDX_NS_PFX], &idbi));
|
|
|
|
+ RCCK (mdb_cursor_open (txn, ddbi, &dcur));
|
|
|
|
+ RCCK (mdb_cursor_open (txn, idbi, &icur));
|
|
|
|
|
|
MDB_val pfx_v, ns_v;
|
|
MDB_val pfx_v, ns_v;
|
|
const char ***nsm_data = LSUP_nsmap_dump (nsm);
|
|
const char ***nsm_data = LSUP_nsmap_dump (nsm);
|
|
@@ -320,6 +318,11 @@ loop_end:
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+static LSUP_rc
|
|
|
|
+mdbstore_nsm_put (void *h, const LSUP_NSMap *nsm, void *th)
|
|
|
|
+{ return nsm_put (((MDBStore *)h)->env, (MDB_txn *)th, nsm); }
|
|
|
|
+
|
|
|
|
+
|
|
static const char *
|
|
static const char *
|
|
mdbstore_path_from_id (const char *id)
|
|
mdbstore_path_from_id (const char *id)
|
|
{
|
|
{
|
|
@@ -342,7 +345,7 @@ mdbstore_path_from_id (const char *id)
|
|
|
|
|
|
/** @brief Create the MDB environment and databases on disk.
|
|
/** @brief Create the MDB environment and databases on disk.
|
|
*
|
|
*
|
|
- * This function takes care of creaating the environment path if not existing,
|
|
|
|
|
|
+ * This function takes care of creating the environment path if not existing,
|
|
* and checking that it's a writable directory. If the path is not specified
|
|
* and checking that it's a writable directory. If the path is not specified
|
|
* in the LSUP_MDB_STORE_URN environment variable, a default directory is used.
|
|
* in the LSUP_MDB_STORE_URN environment variable, a default directory is used.
|
|
*/
|
|
*/
|
|
@@ -368,18 +371,50 @@ mdbstore_setup (const char *id, bool clear)
|
|
|
|
|
|
MDB_txn *txn;
|
|
MDB_txn *txn;
|
|
RCCK (mdb_txn_begin (env, NULL, 0, &txn));
|
|
RCCK (mdb_txn_begin (env, NULL, 0, &txn));
|
|
|
|
+ MDB_dbi dbi;
|
|
for (int i = 0; i < N_DB; i++) {
|
|
for (int i = 0; i < N_DB; i++) {
|
|
LOG_TRACE("Creating DB %s", db_labels[i]);
|
|
LOG_TRACE("Creating DB %s", db_labels[i]);
|
|
- MDB_dbi dbi;
|
|
|
|
RCCK (
|
|
RCCK (
|
|
mdb_dbi_open (txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi)
|
|
mdb_dbi_open (txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi)
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Bootstrap the permanent store with initial data.
|
|
|
|
+ MDB_stat stat;
|
|
|
|
+ CHECK (mdb_dbi_open (
|
|
|
|
+ txn, db_labels[IDX_PFX_NS], db_flags[IDX_PFX_NS], &dbi), fail);
|
|
|
|
+ CHECK (mdb_stat (txn, dbi, &stat), fail);
|
|
|
|
+ if (stat.ms_entries == 0) {
|
|
|
|
+ LOG_DEBUG ("Loading initial data into %s", id);
|
|
|
|
+ // Load initial NS map.
|
|
|
|
+ PCHECK (nsm_put (env, txn, LSUP_default_nsm), fail);
|
|
|
|
+
|
|
|
|
+ // Index default context.
|
|
|
|
+ CHECK (mdb_dbi_open (
|
|
|
|
+ txn, db_labels[IDX_T_ST], db_flags[IDX_T_ST], &dbi), fail);
|
|
|
|
+ MDB_cursor *cur;
|
|
|
|
+ CHECK (mdb_cursor_open (txn, dbi, &cur), fail);
|
|
|
|
+ LSUP_Key k = LSUP_buffer_hash (LSUP_default_ctx_buf);
|
|
|
|
+ MDB_val key, data;
|
|
|
|
+ key.mv_data = &k;
|
|
|
|
+ key.mv_size = sizeof (k);
|
|
|
|
+
|
|
|
|
+ data.mv_data = LSUP_default_ctx_buf->addr;
|
|
|
|
+ data.mv_size = LSUP_default_ctx_buf->size;
|
|
|
|
+
|
|
|
|
+ LSUP_rc db_rc = mdb_cursor_put (cur, &key, &data, MDB_NOOVERWRITE);
|
|
|
|
+ LOG_DEBUG ("Add default ctx rc: %i", rc);
|
|
|
|
+ if (db_rc != MDB_KEYEXIST) CHECK (db_rc, fail);
|
|
|
|
+ }
|
|
|
|
+
|
|
mdb_txn_commit (txn);
|
|
mdb_txn_commit (txn);
|
|
mdb_env_close (env);
|
|
mdb_env_close (env);
|
|
|
|
|
|
return clear ? LSUP_OK : rc;
|
|
return clear ? LSUP_OK : rc;
|
|
|
|
+
|
|
|
|
+fail:
|
|
|
|
+ mdb_txn_abort (txn);
|
|
|
|
+ return rc;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -424,18 +459,6 @@ mdbstore_new (const char *id, size_t _unused)
|
|
CHECK (mdb_dbi_open (
|
|
CHECK (mdb_dbi_open (
|
|
txn, db_labels[i], db_flags[i], store->dbi + i), fail);
|
|
txn, db_labels[i], db_flags[i], store->dbi + i), fail);
|
|
|
|
|
|
- // Bootstrap the permanent store with initial data.
|
|
|
|
- MDB_stat stat;
|
|
|
|
- CHECK (mdb_stat (txn, store->dbi[IDX_PFX_NS], &stat), fail);
|
|
|
|
- if (stat.ms_entries == 0) {
|
|
|
|
- LOG_DEBUG("Loading initial data into %s", path);
|
|
|
|
- // Load initial NS map.
|
|
|
|
- mdbstore_nsm_put (store, LSUP_default_nsm, txn);
|
|
|
|
-
|
|
|
|
- // Index default context.
|
|
|
|
- mdbstore_add_term (store, LSUP_default_ctx_buf, txn);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
store->flags |= LSSTORE_OPEN;
|
|
store->flags |= LSSTORE_OPEN;
|
|
mdb_txn_commit (txn);
|
|
mdb_txn_commit (txn);
|
|
txn = NULL;
|
|
txn = NULL;
|
|
@@ -927,7 +950,7 @@ mdbiter_next (
|
|
size_t i = 0;
|
|
size_t i = 0;
|
|
while (it->ck[i++]); // Include sentinel in count.
|
|
while (it->ck[i++]); // Include sentinel in count.
|
|
LSUP_Buffer *ctx;
|
|
LSUP_Buffer *ctx;
|
|
- LOG_TRACE("Allocating %lu context buffers.", i);
|
|
|
|
|
|
+ LOG_TRACE("Allocating %lu context buffers + sentinel.", i - 1);
|
|
ctx = malloc(i * sizeof (*ctx));
|
|
ctx = malloc(i * sizeof (*ctx));
|
|
if (!ctx) return LSUP_MEM_ERR;
|
|
if (!ctx) return LSUP_MEM_ERR;
|
|
|
|
|