Forráskód Böngészése

Hashmap update:

* Update hashmap.c
* Fix incompatibilities with const members
* Fix Makefile not cleaning up ext artifacts.
scossu 3 napja
szülő
commit
78103c0918
9 módosított fájl, 57 hozzáadás és 49 törlés
  1. 12 3
      Makefile
  2. 6 2
      README.md
  3. 1 1
      ext/hashmap
  4. 1 1
      ext/tpl
  5. 2 2
      src/environment.c
  6. 25 30
      src/namespace.c
  7. 4 4
      src/store_htable.c
  8. 3 3
      src/term.c
  9. 3 3
      test.c

+ 12 - 3
Makefile

@@ -49,7 +49,7 @@ LSUP_SRC = $(wildcard src/*.c)
 SRC = $(EXT_SRC) $(LSUP_SRC)
 TEST_SRC = $(wildcard test/*.c) test.c
 
-EXT_OBJ := $(EXT_SRC:.c=.o)
+EXT_OBJ := $(EXT_SRC:%.c=$(BUILDDIR)/%.o)
 # TODO This is extremely convoluted, simplify if possible.
 CODEC_SRC := $(wildcard $(CODEC_DIR)/codec_*.c)
 CODEC_REL_SRC := $(CODEC_SRC:$(CODEC_DIR)/%=%)
@@ -70,6 +70,10 @@ DYN_DBG_LIB = $(DYN_LIB:.so=_dbg.so)
 LIBS = $(STATIC_LIB) $(DYN_LIB)
 DBG_LIBS = $(STATIC_DBG_LIB) $(DYN_DBG_LIB)
 
+$(info EXT_SRC: $(EXT_SRC))
+$(info EXT_OBJ: $(EXT_OBJ))
+$(info OBJ: $(OBJ))
+$(info DBG_OBJ: $(DBG_OBJ))
 #$(info LIBS: $(LIBS))
 #$(info DBG_LIBS: $(DBG_LIBS))
 
@@ -132,6 +136,12 @@ $(DYN_DBG_LIB): $(DBG_OBJ)
 	$(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_DBG_OBJ)
 
 
+# External libraries.
+$(BUILDDIR)/ext/%.o: ext/%.c
+	mkdir -p $(dir $@)
+	$(CC) $(CFLAGS) -c $^ -o $@
+
+
 # Standard objects.
 $(BUILDDIR)/%.o: src/%.c
 	$(CC) $(CFLAGS) -c $^ -o $@
@@ -175,12 +185,11 @@ debug_install: debug ## Install debug libraries.
 
 .PHONY: clean
 clean: ## Clean up artifacts, including language parsers.
-	rm -f ./*.[aod] bin/* src/codec/*.out
 	rm -rf $(BUILDDIR)
+	rm -f bin/*
 	rm -f $(LIBS) $(DBG_LIBS)
 	rm -f include/codec/grammar_*.h
 	rm -f src/codec/grammar_*.c src/codec/parser_*.c
-	rm -rf venv/
 
 
 .PHONY: uninstall ## Uninstall library (not the dependencies).

+ 6 - 2
README.md

@@ -138,6 +138,10 @@ and other targets in the current Makefile.
 
 ### Environment Variables
 
+`LSUP_DEFAULT_CTX`: string to derive the default context URI for context-aware
+stores when the environment is initialized. This must be a fully qualified URI.
+If unspecified, the value of `LSUP_DEFAULT_CTX_LABEL` is used.
+
 `LSUP_MDB_STORE_PATH`: The file path for the persistent store back end. For
 production use it is strongly recommended to set this to a permanent location
 on the fastest storage volume available. If unset, the current directory will
@@ -154,8 +158,8 @@ be used. The directory must exist.
 
 If unspecified, it is set to 3.
 
-`LSUP_MDB_MAPSIZE` Virtual memory map size. It is recommended to leave this
-alone, unless you are running Valgrind or other tools that limit memory usage.
+`LSUP_MDB_MAPSIZE`: Virtual memory map size. It is recommended to leave this
+alone, except when running Valgrind or other tools that limit memory usage.
 The map size by itself does not preallocate any resources and is safe to
 increase beyond the physical capacity of the host system. By default, it is set
 to 1Tb for 64-bit systems and 4Gb for 32-bit systems.

+ 1 - 1
ext/hashmap

@@ -1 +1 @@
-Subproject commit d9b23a5a4df7e572ec897357292cf377eca9b229
+Subproject commit e6669b5e5d9c6a58af09c3795738120fe5db740e

+ 1 - 1
ext/tpl

@@ -1 +1 @@
-Subproject commit f8138ad393f4b1985c916029ab6d703e4e7a1c4c
+Subproject commit bfe4c3e1ea5f912e6a7240d851952dd4a335cb03

+ 2 - 2
src/environment.c

@@ -46,8 +46,8 @@ LSUP_init (void)
         LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]);
 
     // Default context URI.
-    char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
-    if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
+    const char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
+    if (!default_ctx_str) default_ctx_str = DEFAULT_CTX_LABEL;
     LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL);
     if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
     LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);

+ 25 - 30
src/namespace.c

@@ -9,8 +9,6 @@ typedef struct ns_entry_t {
     char *              ns;         // Fully qualified NS.
 } NSEntry;
 
-typedef struct hashmap NSMap;
-
 /** @brief Iterator for dumping NS map.
  */
 struct dump_iter_t {
@@ -58,7 +56,7 @@ static bool nsmap_dump_ns_iter_fn (const void *item, void *udata)
  * API.
  */
 
-NSMap *
+LSUP_NSMap *
 LSUP_nsmap_new (void)
 {
     return hashmap_new (
@@ -68,12 +66,12 @@ LSUP_nsmap_new (void)
 
 
 void
-LSUP_nsmap_free (NSMap *map)
+LSUP_nsmap_free (LSUP_NSMap *map)
 { hashmap_free (map); }
 
 
 LSUP_rc
-LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
+LSUP_nsmap_add (LSUP_NSMap *map, const char *pfx, const char *nsstr)
 {
     NSEntry entry_s = {};
 
@@ -82,33 +80,30 @@ LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
                 "Prefix `%s` is longer than the maximum allowed size "
                 "(%d characters). Truncating.", pfx, PFX_LEN - 1);
     strncpy (entry_s.pfx, pfx, PFX_LEN -1);
-    char *ns = strdup (nsstr);
-
-    NSEntry *ret = hashmap_get (map, &entry_s);
-    if (!ret) {
-        entry_s.ns = ns;
-        hashmap_set (map, &entry_s);
-        if (hashmap_oom (map)) return LSUP_MEM_ERR;
-        LOG_DEBUG("Added prefix '%s' to NS map @%p.", entry_s.pfx, map);
-    } else {
+    entry_s.ns = strdup (nsstr);
+
+    const NSEntry *ret = hashmap_delete (map, &entry_s);
+    if (!ret) LOG_DEBUG("Adding prefix '%s' to NS map @%p.", entry_s.pfx, map);
+    else {
         LOG_DEBUG(
                 "Replacing NS '%s' with '%s' for prefix '%s'.",
-                ret->ns, ns, entry_s.pfx);
+                ret->ns, entry_s.ns, entry_s.pfx);
+        // Free replaced NS string.
         free (ret->ns);
-        ret->ns = ns;
     }
-    // Free replaced NS string.
+    hashmap_set (map, &entry_s);
+    if (hashmap_oom (map)) return LSUP_MEM_ERR;
 
     return LSUP_OK;
 }
 
 
 LSUP_rc
-LSUP_nsmap_remove (NSMap *map, const char *pfx)
+LSUP_nsmap_remove (LSUP_NSMap *map, const char *pfx)
 {
     NSEntry entry_s = {};
     strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
-    NSEntry *entry = hashmap_delete (map, &entry_s);
+    const NSEntry *entry = hashmap_delete (map, &entry_s);
 
     if (!entry) return LSUP_NOACTION;
 
@@ -119,22 +114,22 @@ LSUP_nsmap_remove (NSMap *map, const char *pfx)
 
 
 const char *
-LSUP_nsmap_get_ns (const NSMap *map, const char *pfx)
+LSUP_nsmap_get_ns (const LSUP_NSMap *map, const char *pfx)
 {
     NSEntry entry_s = {};
     strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
-    NSEntry *entry = hashmap_get ((NSMap *)map, &entry_s);
+    const NSEntry *entry = hashmap_get ((LSUP_NSMap *)map, &entry_s);
 
     return (entry) ? entry->ns : NULL;
 }
 
 
 const char *
-LSUP_nsmap_get_pfx (const NSMap *map, const char *ns)
+LSUP_nsmap_get_pfx (const LSUP_NSMap *map, const char *ns)
 {
     const NSEntry *entry;
     size_t i = 0;
-    while (hashmap_iter ((NSMap *)map, &i, (void **) &entry)) {
+    while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
         if (strncmp (entry->ns, ns, strlen (ns)) == 0)
             return entry->pfx;
     }
@@ -145,7 +140,7 @@ LSUP_nsmap_get_pfx (const NSMap *map, const char *ns)
 
 LSUP_rc
 LSUP_nsmap_normalize_uri (
-        const NSMap *map, const char *pfx_uri, char **fq_uri_p)
+        const LSUP_NSMap *map, const char *pfx_uri, char **fq_uri_p)
 {
     LSUP_rc rc;
     char *fq_uri = NULL;
@@ -162,7 +157,7 @@ LSUP_nsmap_normalize_uri (
     strncpy (pfx, pfx_uri, pfx_len);
     pfx[pfx_len] = '\0';
 
-    const char *ns = LSUP_nsmap_get_ns ((NSMap *)map, pfx);
+    const char *ns = LSUP_nsmap_get_ns ((LSUP_NSMap *)map, pfx);
 
     if (ns) {
         // -1 for :, +1 for terminator.
@@ -192,7 +187,7 @@ no_prefix:
 
 LSUP_rc
 LSUP_nsmap_denormalize_uri (
-        const NSMap *map, const char *fq_uri, char **pfx_uri_p)
+        const LSUP_NSMap *map, const char *fq_uri, char **pfx_uri_p)
 {
     /*
      * This is different from LSUP_nsmap_get_ns, in that the URI being looked
@@ -205,7 +200,7 @@ LSUP_nsmap_denormalize_uri (
     const char *pfx = NULL;
     size_t i = 0, offset;
 
-    while (hashmap_iter ((NSMap *)map, &i, (void **) &entry)) {
+    while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
         offset = strlen (entry->ns);
         if (strncmp (entry->ns, fq_uri, offset) == 0) {
             pfx = entry->pfx;
@@ -235,9 +230,9 @@ LSUP_nsmap_denormalize_uri (
 
 
 const char ***
-LSUP_nsmap_dump (const NSMap *map)
+LSUP_nsmap_dump (const LSUP_NSMap *map)
 {
-    size_t i = hashmap_count ((NSMap *) map);
+    size_t i = hashmap_count ((LSUP_NSMap *) map);
 
     const char ***data = malloc (2 * (i + 1) * sizeof (char *));
     if (UNLIKELY (!data)) return NULL;
@@ -248,7 +243,7 @@ LSUP_nsmap_dump (const NSMap *map)
     }
 
     struct dump_iter_t cur = {.i = 0, .data = data};
-    hashmap_scan ((NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
+    hashmap_scan ((LSUP_NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
     data[i] = NULL; // Sentinel
 
     return data;

+ 4 - 4
src/store_htable.c

@@ -131,7 +131,7 @@ tkey_to_strp (
 
 
 static LSUP_rc
-htstore_add_key_iter (HTIterator *it, const LSUP_Key *spok);
+add_key_iter (HTIterator *it, const LSUP_Key *spok);
 
 
 /** @brief Advance iterator by next key.
@@ -262,7 +262,7 @@ htstore_add_iter (void *h, const LSUP_BufferTriple *sspo)
         LSUP_buffer_hash (sspo->o),
     };
 
-    LSUP_rc rc = htstore_add_key_iter (it, spok);
+    LSUP_rc rc = add_key_iter (it, spok);
 
     if (rc != LSUP_OK) return rc;
 
@@ -460,7 +460,7 @@ tkey_to_strp (
         const HTStore *store, const LSUP_Key *spok, LSUP_BufferTriple *sspo)
 {
     // Data owned by the store.
-    IndexEntry *tmp;
+    const IndexEntry *tmp;
 
     for (int i = 0; i < 3; i++) {
         tmp = hashmap_get (store->idx, spok + i);
@@ -475,7 +475,7 @@ tkey_to_strp (
 
 
 static LSUP_rc
-htstore_add_key_iter (HTIterator *it, const LSUP_Key *spok)
+add_key_iter (HTIterator *it, const LSUP_Key *spok)
 {
     // Add triple.
     LOG_TRACE("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);

+ 3 - 3
src/term.c

@@ -563,7 +563,7 @@ LSUP_term_set_add (LSUP_TermSet *ts, LSUP_Term *term, LSUP_Term **existing)
     LSUP_Hash key = LSUP_term_hash (term);
     KeyedTerm entry_s = {.key=key, .term=term};
 
-    KeyedTerm *ex = hashmap_get (ts, &entry_s);
+    const KeyedTerm *ex = hashmap_get (ts, &entry_s);
     if (ex) {
         if (existing) *existing = ex->term;
         return LSUP_NOACTION;
@@ -579,7 +579,7 @@ LSUP_term_set_add (LSUP_TermSet *ts, LSUP_Term *term, LSUP_Term **existing)
 const LSUP_Term *
 LSUP_term_set_get (LSUP_TermSet *ts, LSUP_Key key)
 {
-    KeyedTerm *entry = hashmap_get (ts, &(KeyedTerm){.key=key});
+    const KeyedTerm *entry = hashmap_get (ts, &(KeyedTerm){.key=key});
     if (entry) LOG_TRACE("ID found for key %lx: %s", key, entry->term->data);
     else LOG_TRACE("No ID found for key %lx.", key);
 
@@ -654,7 +654,7 @@ LSUP_link_map_add (
     // Keyed term to look up the link term and insert it, if necessary.
     KeyedTerm entry_s = {.key=LSUP_term_hash (term), .term=term};
 
-    Link *ex = hashmap_get (lmap->links, &(Link){.term=&entry_s});
+    const Link *ex = hashmap_get (lmap->links, &(Link){.term=&entry_s});
     if (ex) {
         // Add terms one by one to the existing term set.
         LOG_TRACE(

+ 3 - 3
test.c

@@ -22,8 +22,8 @@ int main(int argc, char **argv) {
 
     start = clock();
 
-    int rc = LSUP_init();
-    if (rc != LSUP_OK) return rc;
+    LSUP_rc rc = LSUP_init();
+    RCCK (rc);
 
     if (
         term_tests() ||
@@ -51,6 +51,6 @@ int main(int argc, char **argv) {
     log_info ("");
     log_info ("Run %d tests in %lu ms.", tests_run, (size_t) wallclock);
 
-    return rc;
+    return (int)rc;
 }