Browse Source

Invert insertion and index of ht triples.

Stefano Cossu 4 years ago
parent
commit
0cdf81248a
1 changed files with 21 additions and 19 deletions
  1. 21 19
      src/store_htable.c

+ 21 - 19
src/store_htable.c

@@ -285,31 +285,15 @@ LSUP_htstore_add_init (HTStore *store)
 LSUP_rc
 LSUP_htstore_add_iter (HTIterator *it, const LSUP_SerTriple *sspo)
 {
-    LSUP_rc rc = LSUP_NOACTION;
     LSUP_TripleKey spok = NULL_TRP;
 
-    // Add terms to index.
-    for (int i = 0; i < 3; i++) {
-        spok[i] = LSUP_buffer_hash (LSUP_striple_pos (sspo, i));
-        TRACE ("Indexing term key %lu\n", spok[i]);
-
-        IndexEntry *ins = NULL;
-        HASH_FIND (hh, it->store->idx, spok + i, KLEN, ins);
-        if (ins == NULL) {
-            ins = malloc (sizeof (*ins));
-            if (UNLIKELY (!ins)) return LSUP_MEM_ERR;
-            ins->key = spok[i];
-            ins->sterm = LSUP_striple_pos (sspo, i);
-            HASH_ADD (hh, it->store->idx, key, KLEN, ins);
-        }
-    }
-
     // Add triple.
     TRACE ("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);
 
     TripleEntry *k_ins = NULL;
     HASH_FIND (hh, it->store->keys, spok, TRP_KLEN, k_ins);
     if (k_ins == NULL) {
+        TRACE (STR, "Triple not found, inserting.\n");
         k_ins = malloc (sizeof (*k_ins));
         if (UNLIKELY (!k_ins)) return LSUP_MEM_ERR;
 
@@ -317,10 +301,28 @@ LSUP_htstore_add_iter (HTIterator *it, const LSUP_SerTriple *sspo)
         HASH_ADD (hh, it->store->keys, key, TRP_KLEN, k_ins);
 
         it->i++;
-        rc = LSUP_OK;
+    } else {
+        TRACE (STR, "Triple found. Skipping.\n");
+        return LSUP_NOACTION;
     }
 
-    return rc;
+    // Add terms to index.
+    for (int i = 0; i < 3; i++) {
+        spok[i] = LSUP_buffer_hash (LSUP_striple_pos (sspo, i));
+        TRACE ("Indexing term key %lx\n", spok[i]);
+
+        IndexEntry *ins = NULL;
+        HASH_FIND (hh, it->store->idx, spok + i, KLEN, ins);
+        if (ins == NULL) {
+            ins = malloc (sizeof (*ins));
+            if (UNLIKELY (!ins)) return LSUP_MEM_ERR;
+            ins->key = spok[i];
+            ins->sterm = LSUP_striple_pos (sspo, i);
+            HASH_ADD (hh, it->store->idx, key, KLEN, ins);
+        }
+    }
+
+    return LSUP_OK;
 }