ソースを参照

Remove all namespace handling in store interfaces.

scossu 7 時間 前
コミット
c5fff8f966
6 ファイル変更31 行追加213 行削除
  1. 1 1
      Makefile
  2. 4 0
      include/lsup/graph.h
  3. 1 47
      include/lsup/store_interface.h
  4. 7 16
      src/graph.c
  5. 10 134
      src/store_mdb.c
  6. 8 15
      test/test_graph.c

+ 1 - 1
Makefile

@@ -61,7 +61,7 @@ CODEC_OBJ = $(patsubst $(CODEC_DIR)/%.c, $(BUILDDIR)/%.o, $(CODEC_SRC))
 CODEC_DBG_OBJ = $(CODEC_OBJ:.o=_dbg.o)
 LOCAL_OBJ = $(LSUP_SRC:src/%.c=$(BUILDDIR)/%.o)
 OBJ = $(EXT_OBJ) $(LOCAL_OBJ)
-DBG_OBJ = $(EXT_OBJ) $(LOCAL_OBJ)
+DBG_OBJ = $(EXT_OBJ) $(LOCAL_OBJ:.o=_dbg.o)
 STATIC_LIB = $(OUTDIR)/liblsuprdf.a
 DYN_LIB = $(STATIC_LIB:.a=.so)
 STATIC_DBG_LIB = $(STATIC_LIB:.a=_dbg.a)

+ 4 - 0
include/lsup/graph.h

@@ -246,6 +246,10 @@ LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
 
 
 /** @brief Number of triples in a graph.
+ *
+ * @param gr Graph handle.
+ *
+ * @return Number of triples, or a negative value on error.
  */
 size_t
 LSUP_graph_size (const LSUP_Graph *gr);

+ 1 - 47
include/lsup/store_interface.h

@@ -58,7 +58,6 @@ typedef enum {
     LSUP_STORE_IDX      = 1<<2,   ///< Store is fully SPO(C)-indexed.
     LSUP_STORE_TXN      = 1<<3,   ///< Supports transaction handling.
     LSUP_STORE_COW      = 1<<4,   ///< Copy on write. @sa #iter_next_fn_t()
-    LSUP_STORE_OWN_NSM  = 1<<5,   ///< Store has its own namespace map.
 } LSUP_StoreFeature;
 
 
@@ -364,38 +363,6 @@ typedef LSUP_rc (*store_remove_fn_t)(
         const LSUP_Buffer *sc, void *udata, size_t *ct);
 
 
-/** @brief Update the namespace map of a store.
- *
- * This is only available in stores with the LSUP_STORE_OWN_NSM feature.
- *
- * @param[in] store Store back end to update.
- *
- * @param[in] pfx Prefix to update. If the key is already existing, it is
- *  updated or deleted (see `ns` parameter).
- *
- * @param[in] ns Namespace to relate to the prefix. If this value is NULL, the
- *  prefix mapping is removed, if existing, or otherwise quietly skipped.
- *
- * @param[in] udata Opaque implementation-specific data handler. Consult each
- *  implementation for details.
- *
- * @return LSUP_OK if a mapping was added, updated, or deleted; LSUP_NOACTION
- * if NULL was passed for `ns` and no corresponding prefix was found to delete;
- * <0 if an error occurred.
- */
-typedef LSUP_rc (*store_nsm_update_fn_t)(
-        void *store, const char *pfx, const char *ns, void *udata);
-
-
-/** @brief Get the store's namespace prefix map.
- *
- * @param[in] store MDB store to query.
- *
- * @return NS map or NULL on error.
- */
-typedef LSUP_NSMap * (*store_nsm_get_fn_t)(void *store);
-
-
 /** @brief Prototype: yield the matching triples and advance the iterator.
  *
  * @note Iterators keep transactions open. Don't hold on to them longer than
@@ -521,17 +488,7 @@ typedef struct store_if_t {
     // Removal.
     store_remove_fn_t   remove_fn;      ///< Remove triples by pattern.
 
-    // Namespace prefix mapping.
-    store_nsm_update_fn_t  nsm_update_fn; ///< Add a ns/pfx pair to the map.
-                                        ///<
-                                        ///< Only available (and mandatory)
-                                        ///< in stores with the
-                                        ///< #LSUP_STORE_OWN_NSM feature.
-    store_nsm_get_fn_t  nsm_get_fn;     ///< Get the store's namespace map.
-                                        ///<
-                                        ///< Only available (and mandatory)
-                                        ///< in stores with the
-                                        ///< #LSUP_STORE_OWN_NSM feature.
+    // Context.
     store_ctx_list_fn_t ctx_list_fn;    ///< Get all context URIs in a store.
                                         ///<
                                         ///< Only applicable to stores with
@@ -573,9 +530,6 @@ const LSUP_StoreInt my_store_int = {
 
     .remove_fn      = my_remove_fn,
 
-    .nsm_update_fn  = my_nsm_update_fn,
-    .nsm_get_fn     = my_nsm_get_fn,
-
     .ctx_list_fn   = my_ctx_list_fn,
 };
 */

+ 7 - 16
src/graph.c

@@ -51,9 +51,7 @@ LSUP_graph_new (LSUP_Store *store, const char *uri_str, LSUP_NSMap *nsm)
     gr->store = (store) ? store : LSUP_store_new (
             LSUP_STORE_HTABLE, NULL, 0, false);
 
-    if (gr->store->sif->features & LSUP_STORE_OWN_NSM)
-        gr->nsm = gr->store->sif->nsm_get_fn (gr->store->data);
-    else gr->nsm = nsm ? nsm : LSUP_default_nsm;
+    gr->nsm = nsm ? nsm : LSUP_default_nsm;
 
     gr->uri =
         uri_str? LSUP_iriref_new (uri_str, gr->nsm) :
@@ -253,9 +251,6 @@ LSUP_graph_free (LSUP_Graph *gr)
     if (UNLIKELY (!gr)) return;
 
     LSUP_term_free (gr->uri);
-    // 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.
     // TODO Use feature flags. Need some specs around this.
@@ -319,20 +314,12 @@ finally:
 
 LSUP_NSMap *
 LSUP_graph_namespace (const LSUP_Graph *gr)
-{
-    // If nsm_get_fn is not defined, the store has no own NS map.
-    if (!gr->store->sif->nsm_get_fn) return gr->nsm;
-
-    return gr->store->sif->nsm_get_fn (gr->store->data);
-}
+{ return gr->nsm; }
 
 
 void
 LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm)
-{
-    if (!gr->store->sif->nsm_get_fn) gr->nsm = nsm;
-    else log_warn ("Graph back end has a stored NS map.");
-}
+{ gr->nsm = nsm; }
 
 
 size_t
@@ -342,6 +329,10 @@ LSUP_graph_size (const LSUP_Graph *gr)
     LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
     void *it = gr->store->sif->lookup_fn (
         gr->store->data, NULL, NULL, NULL, sc, NULL, &ct);
+    if (!it) {
+        log_error ("Error initializing lookup.");
+        ct = -1;
+    }
     gr->store->sif->lu_free_fn (it);
 
     LSUP_buffer_free (sc);

+ 10 - 134
src/store_mdb.c

@@ -3,7 +3,7 @@
 /**
  * Number of DBs defined. See MAIN_TABLE and LOOKUP_TABLE defines below.
  */
-#define N_DB 12
+#define N_DB 10
 
 /**
  * Memory map size.
@@ -112,7 +112,6 @@ typedef struct mdbstore_iter_t {
 /*          #ID pfx #DB label   #Flags  */ \
     ENTRY(  T_ST,   "t:st",     0               )   /* Key to ser. term */  \
     ENTRY(  SPO_C,  "spo:c",    DUPFIXED_MASK   )   /* Triple to context */ \
-    ENTRY(  PFX_NS, "pfx:ns",   0               )   /* Prefix to NS */      \
     ENTRY(  IDK_ID, "idk:id",   0               )   /* ID key to ID */      \
 
 /**
@@ -127,7 +126,6 @@ typedef struct mdbstore_iter_t {
     ENTRY(  SO_P,    "so:p",   DUPFIXED_MASK    )   /* 2-bound lookup */    \
     ENTRY(  SP_O,    "sp:o",   DUPFIXED_MASK    )   /* 2-bound lookup */    \
     ENTRY(  C_SPO,   "c:spo",  DUPFIXED_MASK    )   /* Context lookup */    \
-    ENTRY(  NS_PFX,  "ns:pfx", DUPSORT_MASK     )   /* NS to prefix */      \
 
 /**
  * DB labels. They are prefixed with DB_
@@ -211,108 +209,6 @@ inline static LSUP_rc lookup_2bound (
 inline static LSUP_rc lookup_3bound(MDBIterator *it, size_t *ct);
 
 
-/**
- * Store interface.
- */
-
-static LSUP_NSMap *
-mdbstore_nsm_get (void *h)
-{
-    MDBStore *store = h;
-    LSUP_NSMap *nsm = LSUP_nsmap_new();
-    if (UNLIKELY (!nsm)) return NULL;
-
-    MDB_txn *txn;
-    mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
-
-    MDB_cursor *cur;
-    if (mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &cur) != MDB_SUCCESS) {
-        mdb_txn_abort (txn);
-        return NULL;
-    }
-
-    MDB_val ns_v, pfx_v;
-    if (mdb_cursor_get (cur, &pfx_v, &ns_v, MDB_FIRST) != MDB_SUCCESS)
-        goto finally;
-
-    do {
-        LSUP_ns_pfx pfx;
-        char *ns = malloc (ns_v.mv_size);
-
-        strncpy (pfx, pfx_v.mv_data, pfx_v.mv_size);
-        strncpy (ns, ns_v.mv_data, ns_v.mv_size);
-        LSUP_nsmap_add (nsm, pfx, ns);
-
-        free (ns);
-    } while (mdb_cursor_get (
-                cur, &pfx_v, &ns_v, MDB_NEXT_NODUP) == MDB_SUCCESS);
-
-finally:
-    mdb_cursor_close (cur);
-    mdb_txn_abort (txn);
-
-    return nsm;
-}
-
-
-static LSUP_rc
-nsm_update (MDB_env *env, const char *pfx, const char *ns, MDB_txn *p_txn)
-{
-    LSUP_rc rc = LSUP_NOACTION;
-    int db_rc;
-
-    MDB_txn *txn;
-    RCCK (mdb_txn_begin (env, p_txn, 0, &txn));
-
-    MDB_cursor *dcur = NULL, *icur = NULL;
-    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;
-
-    pfx_v.mv_data = (void *) pfx;
-    pfx_v.mv_size = strlen (pfx) + 1;
-
-    if (ns) {
-        // Add or update prefix mapping.
-        ns_v.mv_data = (void *) ns;
-        ns_v.mv_size = strlen (ns) + 1;
-
-        CHECK ((rc = mdb_cursor_put (dcur, &pfx_v, &ns_v, 0)), fail);
-        CHECK ((rc = mdb_cursor_put (icur, &ns_v, &pfx_v, 0)), fail);
-
-    } else {
-        // Delete prefix.
-        if (mdb_cursor_get (dcur, &pfx_v, &ns_v, MDB_SET_KEY) == MDB_NOTFOUND)
-            goto success;
-        CHECK ((rc = mdb_cursor_del (dcur, 0)), fail);
-        if (mdb_cursor_get (icur, &ns_v, &pfx_v, MDB_GET_BOTH) == MDB_NOTFOUND)
-            goto success;
-        CHECK ((rc = mdb_cursor_del (icur, 0)), fail);
-    }
-
-success:
-    CHECK (mdb_txn_commit (txn), fail);
-
-    return rc;
-
-fail:
-    mdb_txn_abort (txn);
-
-    return rc;
-}
-
-
-static LSUP_rc
-mdbstore_nsm_update (void *h, const char *pfx, const char *ns, void *th)
-{ return nsm_update (((MDBStore *)h)->env, pfx, ns, (MDB_txn *)th); }
-
-
 static const char *
 mdbstore_path_from_id (const char *id)
 {
@@ -333,6 +229,10 @@ mdbstore_path_from_id (const char *id)
 }
 
 
+/**
+ * Store interface.
+ */
+
 /** @brief Create the MDB environment and databases on disk.
  *
  * This function takes care of creating the environment path if not existing,
@@ -372,28 +272,12 @@ mdbstore_setup (const char *id, bool clear)
     // 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);
+                txn, db_labels[IDX_T_ST], db_flags[IDX_T_ST], &dbi), fail);
     CHECK (mdb_stat (txn, dbi, &stat), fail);
 
-    const char ***nsm_data = NULL;
     if (stat.ms_entries == 0) {
         LOG_DEBUG ("Loading initial data into %s", path);
-        // Load initial NS map.
-        nsm_data = LSUP_nsmap_dump (LSUP_default_nsm);
-        if (UNLIKELY (!nsm_data)) {
-            rc = LSUP_ERROR;
-            goto loop_fail;
-        }
-        for (size_t i = 0; nsm_data[i] != NULL; i++)
-            PCHECK (nsm_update (
-                        env, nsm_data[i][0], nsm_data[i][1], txn), loop_fail);
-        for (size_t i = 0; nsm_data[i] != NULL; i++)
-            free (nsm_data[i]);
-        free (nsm_data);
-
         // 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);
@@ -404,9 +288,8 @@ mdbstore_setup (const char *id, bool clear)
         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);
+        LSUP_rc db_rc = mdb_cursor_put (cur, &key, &data, 0);
+        CHECK (db_rc, fail);
     }
 
     mdb_txn_commit (txn);
@@ -414,10 +297,6 @@ mdbstore_setup (const char *id, bool clear)
 
     return clear ? LSUP_OK : rc;
 
-loop_fail:
-    for (size_t i = 0; nsm_data[i] != NULL; i++)
-        free (nsm_data[i]);
-    free (nsm_data);
 fail:
     if (rc >= 0) rc = LSUP_DB_ERR;
     mdb_txn_abort (txn);
@@ -1344,7 +1223,7 @@ fail:
 const LSUP_StoreInt mdbstore_int = {
     .name           = "MDB Store",
     .features       = LSUP_STORE_PERM | LSUP_STORE_CTX | LSUP_STORE_IDX
-                      | LSUP_STORE_TXN | LSUP_STORE_COW | LSUP_STORE_OWN_NSM,
+                      | LSUP_STORE_TXN | LSUP_STORE_COW,
 
     .setup_fn       = mdbstore_setup,
     .new_fn         = mdbstore_new,
@@ -1371,9 +1250,6 @@ const LSUP_StoreInt mdbstore_int = {
 
     .remove_fn      = mdbstore_remove,
 
-    .nsm_update_fn  = mdbstore_nsm_update,
-    .nsm_get_fn     = mdbstore_nsm_get,
-
     .ctx_list_fn    = mdbstore_ctx_list,
 };
 
@@ -1747,7 +1623,7 @@ lookup_2bound(uint8_t idx0, uint8_t idx1, MDBIterator *it, size_t *ct)
                 luk2_offset = 0;
             }
             dbi = it->store->dbi[lookup_indices[i + 3]];
-            LOG_DEBUG(
+            LOG_DEBUG (
                     "Looking up 2 bound in %s",
                     db_labels[lookup_indices[i + 3]]);
 

+ 8 - 15
test/test_graph.c

@@ -20,16 +20,16 @@ _graph_new (LSUP_StoreType type)
     EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1"));
     EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
 
-    // Check that setup function is idempotent with clear == false.
-    const LSUP_StoreInt *sif = LSUP_store_int (type);
-    if (sif->setup_fn) EXPECT_INT_EQ (
-            sif->setup_fn (NULL, false), LSUP_NOACTION);
-
     ASSERT (
             strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0,
             "Graph URI mismatch!");
     EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
 
+    // Check that setup function is idempotent with clear == false.
+    const LSUP_StoreInt *sif = LSUP_store_int (type);
+    if (sif->setup_fn) EXPECT_INT_EQ (
+            sif->setup_fn (NULL, false), LSUP_NOACTION);
+
     LSUP_graph_free (gr);
     LSUP_store_free (store);
 
@@ -57,16 +57,9 @@ _graph_ns_uri (LSUP_StoreType type)
     log_info("Testing NSM for graph store type: %d", type);
 
     LSUP_NSMap *nsm = LSUP_nsmap_new();
-    if (sif->features & LSUP_STORE_OWN_NSM) {
-        for (int i = 0; nsdata[i][0]; i++) {
-            log_debug ("ns i: %d", i);
-            EXPECT_PASS (store->sif->nsm_update_fn (
-                    store->data, nsdata[i][0], nsdata[i][1], NULL));
-        }
-    } else {
-        for (int i = 0; nsdata[i][0]; i++)
-            EXPECT_PASS (LSUP_nsmap_add (nsm, nsdata[i][0], nsdata[i][1]));
-    }
+    for (int i = 0; nsdata[i][0]; i++)
+        EXPECT_PASS (LSUP_nsmap_add (nsm, nsdata[i][0], nsdata[i][1]));
+
     LSUP_Graph *gr = LSUP_graph_new (store, "ns1:gr1", nsm);
     ASSERT (gr != NULL, "Error creating graph!");