Stefano Cossu пре 2 година
родитељ
комит
b30e85ee5b
5 измењених фајлова са 56 додато и 33 уклоњено
  1. 5 4
      src/environment.c
  2. 11 6
      src/graph.c
  3. 32 15
      src/store_htable.c
  4. 5 7
      src/term.c
  5. 3 1
      test/test_graph.c

+ 5 - 4
src/environment.c

@@ -23,20 +23,21 @@ LSUP_Env *LSUP_default_env = NULL;
 
 static uint64_t term_cache_hash_fn (
         const void *item, uint64_t seed0, uint64_t seed1)
-{ return LSUP_term_hash (((LSUP_KeyedTerm *) item)->term); }
+{ return ((const LSUP_KeyedTerm *) item)->key; }
 
 
 static int term_cache_cmp_fn (const void *a, const void *b, void *udata)
 {
-    return LSUP_term_equals (
-            ((const LSUP_KeyedTerm *) a)->term,
-            ((const LSUP_KeyedTerm *) b)->term);
+    return 
+            ((const LSUP_KeyedTerm *) a)->key -
+            ((const LSUP_KeyedTerm *) b)->key;
 }
 
 
 static void term_cache_free_fn (void *item)
 {
     LSUP_KeyedTerm *kterm = item;
+    log_trace ("Freeing term %s", kterm->term->data);
 
     LSUP_term_free (kterm->term);
 }

+ 11 - 6
src/graph.c

@@ -368,9 +368,13 @@ LSUP_graph_add (Graph *gr, const LSUP_Triple trp[], size_t *ct)
 
         LSUP_rc db_rc = LSUP_graph_add_iter (it, trp + i);
 
-        if (db_rc == LSUP_OK) rc = LSUP_OK;
+        if (db_rc == LSUP_OK) {
+            rc = LSUP_OK;
+            if (ct) (*ct)++;
+            // A duplicate will return LSUP_NOACTION and not increment the
+            // counter.
+        }
         if (UNLIKELY (db_rc < 0)) return db_rc;
-        if (ct && db_rc == LSUP_OK) (*ct)++;
     }
 
     LSUP_graph_add_done (it);
@@ -386,10 +390,11 @@ LSUP_graph_remove (
 {
     LSUP_rc rc;
 
-    LSUP_Buffer *ss = LSUP_term_serialize (s);
-    LSUP_Buffer *sp = LSUP_term_serialize (p);
-    LSUP_Buffer *so = LSUP_term_serialize (o);
-    LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
+    LSUP_Buffer
+        *ss = LSUP_term_serialize (s),
+        *sp = LSUP_term_serialize (p),
+        *so = LSUP_term_serialize (o),
+        *sc = LSUP_term_serialize (gr->uri);
 
     if (gr->store_type == LSUP_STORE_MEM)
         rc = LSUP_htstore_remove (gr->ht_store, ss, sp, so, ct);

+ 32 - 15
src/store_htable.c

@@ -97,7 +97,11 @@ uint64_t trp_key_hash_fn (
 
 
 int trp_key_cmp_fn (const void *a, const void *b, void *udata)
-{ return memcmp (a, b, TRP_KLEN); }
+{
+    const LSUP_Key *aa = a, *bb = b;
+    log_trace ("cmp a: {%lx, %lx, %lx}", aa[0], aa[1], aa[2]);
+    log_trace ("cmp b: {%lx, %lx, %lx}", bb[0], bb[1], bb[2]);
+    return memcmp (a, b, TRP_KLEN); }
 
 
 /**
@@ -330,24 +334,39 @@ LSUP_htstore_add_done (HTIterator *it)
 LSUP_rc
 LSUP_htstore_remove(
         LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
-        const LSUP_Buffer *so,  size_t *ct)
+        const LSUP_Buffer *so,  size_t *ct_p)
 {
-    LSUP_rc rc = LSUP_NOACTION;
+    LSUP_rc rc;
+    size_t i, ct;
 
-    LSUP_HTIterator *it = LSUP_htstore_lookup (store, ss, sp, so, ct);
+    LSUP_HTIterator *it = LSUP_htstore_lookup (store, ss, sp, so, &ct);
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
 
-    while (htiter_next_key (it)) {
-        if (it->rc == LSUP_OK) {
-            LSUP_TripleKey *del = hashmap_delete (store->keys, it->entry);
-            free (del);
-            rc = LSUP_OK;
-        }
+    if (ct == 0) {
+        rc = LSUP_NOACTION;
+        goto finally;
+    }
+    // Preallocate delete list.
+    LSUP_TripleKey **del_list = malloc (sizeof (*del_list) * ct);
+    if (UNLIKELY (!del_list)) {
+        rc = LSUP_MEM_ERR;
+        goto finally;
     }
 
+    // Gather delete list first. Looping over hashmap while deleting elements
+    // won't work.
+    for (i = 0; htiter_next_key (it) == LSUP_OK; i++)
+        del_list[i] = it->entry;
+
+    for (i = 0; i < ct; i++)
+        hashmap_delete (store->keys, del_list[i]);
+
+finally:
     LSUP_htiter_free (it);
 
-    return rc;
+    if (ct_p) *ct_p = ct;
+
+    return LSUP_OK;
 }
 
 
@@ -406,10 +425,8 @@ LSUP_htstore_lookup (HTStore *store, const LSUP_Buffer *ss,
 
     if (ct) {
         // Loop over results to determine count.
-        while (htiter_next_key (it) == LSUP_OK) {
-            (*ct)++;
-            log_trace ("ct: %lu", *ct);
-        }
+        while (htiter_next_key (it) == LSUP_OK) (*ct)++;
+
         // Reposition cursor to the hashtable beginning.
         it->cur = 0;
         it->rc = LSUP_OK;

+ 5 - 7
src/term.c

@@ -439,8 +439,6 @@ LSUP_tcache_add (const LSUP_Key key, const LSUP_Term *term)
     // Many calls will likely attempt inserting duplicates after the first one.
     if (LIKELY (hashmap_get (LSUP_term_cache, &entry_s))) return LSUP_NOACTION;
 
-    // If we really need to add the term, make a copy.
-    entry_s.term = LSUP_term_copy (term);
     hashmap_set (LSUP_term_cache, &entry_s);
 
     return LSUP_OK;
@@ -452,8 +450,8 @@ LSUP_tcache_get (LSUP_Key key)
 {
     LSUP_KeyedTerm *entry = hashmap_get (
             LSUP_term_cache, &(LSUP_KeyedTerm){.key=key});
-    if (entry) log_trace ("ID found for key %u: %s", key, entry->term->data);
-    else log_trace ("No ID found for key %u.", 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);
 
     return (entry) ? entry->term : NULL;
 }
@@ -596,12 +594,12 @@ term_init (
             term->datatype = (LSUP_Term *)tmp;
         }
 
-        log_trace ("Datatype address: %p", term->datatype);
-        log_trace ("Datatype hash: %lu", LSUP_term_hash (term->datatype));
+        //log_trace ("Datatype address: %p", term->datatype);
+        log_trace ("Datatype hash: %lx", LSUP_term_hash (term->datatype));
 
     } else if (term->type == LSUP_TERM_BNODE) {
         // TODO This is not usable for global skolemization.
-        term->bnode_id = XXH64 (
+        term->bnode_id = LSUP_HASH (
                 term->data, strlen (term->data) + 1, LSUP_HASH_SEED);
     }
 

+ 3 - 1
test/test_graph.c

@@ -157,7 +157,10 @@ _graph_remove (LSUP_store_type type)
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
+    // Triples 0, 3, 4, 5, 7 will be removed.
     LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
+    EXPECT_INT_EQ (ct, 5);
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
 
     ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
     ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
@@ -167,7 +170,6 @@ _graph_remove (LSUP_store_type type)
     ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
     ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
     ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
-    EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
 
     free_triples (trp); // gr copied data.