Bläddra i källkod

Add partial lookup tests; fix some meory errors.

Stefano Cossu 3 år sedan
förälder
incheckning
fe29a544f7
4 ändrade filer med 124 tillägg och 13 borttagningar
  1. 17 9
      include/core.h
  2. 3 1
      src/store_htable.c
  3. 1 2
      src/store_mdb.c
  4. 103 1
      test/test_graph.c

+ 17 - 9
include/core.h

@@ -14,19 +14,27 @@
 #include <sys/stat.h>
 #include <uuid/uuid.h>
 
+
 #ifdef DEBUG
 #define DEBUG_TEST 1
-#else
-#define DEBUG_TEST 0
-#endif
-#define STR "%s"
+// GDB breakpoints.
+#include <signal.h>
+#define BREAKPOINT if (DEBUG_TEST) raise (SIGINT)
+
 #define TRACE(fmt, ...) \
-        do {\
-            if (DEBUG_TEST) \
-                fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
-                        __FILE__,  __LINE__, __func__, __VA_ARGS__); \
-        } while (0)
+    do {\
+        fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
+                __FILE__,  __LINE__, __func__, __VA_ARGS__); \
+    } while (0)
 
+#else  /* DEBUG */
+#define DEBUG_TEST 0
+#define BREAKPOINT
+#define TRACE(fmt, ...)
+
+#endif  /* DEBUG */
+
+#define STR "%s"
 #define LIKELY(x)       __builtin_expect(!!(x), true)
 #define UNLIKELY(x)     __builtin_expect(!!(x), false)
 

+ 3 - 1
src/store_htable.c

@@ -356,7 +356,7 @@ LSUP_htstore_remove(
 
     if (ct) *ct = 0;
 
-    TripleEntry *tmp = malloc (sizeof (*tmp));
+    TripleEntry *tmp;
     HASH_ITER (hh, store->keys, it->entry, tmp) {
         if (it->eq_fn (it->entry->key, it->luk)) {
             HASH_DEL (store->keys, it->entry);
@@ -366,6 +366,8 @@ LSUP_htstore_remove(
         }
     }
 
+    LSUP_htiter_free (it);
+
     // TODO clean up orphan indices in separate (async, scheduled) function.
 
     return LSUP_OK;

+ 1 - 2
src/store_mdb.c

@@ -643,8 +643,7 @@ mdbiter_next_key (LSUP_MDBIterator *it)
         MDB_val key, data;
 
         int db_rc;
-        db_rc = mdb_cursor_open
-            (mdb_cursor_txn (it->cur), it->store->dbi[IDX_SPO_C], &cur);
+        db_rc = mdb_cursor_open (it->txn, it->store->dbi[IDX_SPO_C], &cur);
         if (UNLIKELY (db_rc != MDB_SUCCESS)) {
             fprintf (
                     stderr, "%s:%d [%s]: Database error: %s\n",

+ 103 - 1
test/test_graph.c

@@ -2,6 +2,8 @@
 #include "graph.h"
 #include "assets/triples.h"
 
+#define N_LUT 12
+
 static int
 _graph_new (LSUP_store_type type)
 {
@@ -55,6 +57,94 @@ _graph_add (LSUP_store_type type)
 }
 
 
+static int
+_graph_lookup (LSUP_store_type type)
+{
+    LSUP_Triple *trp = create_triples();
+
+    // Lookup triples.
+    LSUP_Triple *lu_trp[N_LUT] = {
+        LSUP_triple_new (trp[0].s, NULL, NULL),         // 5 matches
+        LSUP_triple_new (NULL, trp[2].p, NULL),         // 3 matches
+        LSUP_triple_new (NULL, NULL, trp[5].o),         // 2 matches
+        LSUP_triple_new (trp[0].s, trp[0].p, NULL),     // 1 match
+        LSUP_triple_new (NULL, trp[0].p, trp[0].o),     // 1 match
+        LSUP_triple_new (trp[0].s, trp[2].p, trp[5].o), // 1 match
+        LSUP_triple_new (trp[0].p, NULL, NULL),         // 0 matches
+        LSUP_triple_new (NULL, trp[2].s, NULL),         // 0 matches
+        LSUP_triple_new (NULL, NULL, trp[5].p),         // 0 matches
+        LSUP_triple_new (trp[2].s, trp[6].p, NULL),     // 0 matches
+        LSUP_triple_new (NULL, trp[1].p, trp[5].o),     // 0 matches
+        LSUP_triple_new (trp[2].s, trp[2].p, trp[5].o), // 0 matches
+    };
+
+    // Lookup result counts.
+    size_t lu_ct[N_LUT] = { 5, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
+
+    /* TODO
+    // Index of lookup matches from trp.
+    size_t lu_match[N_LUT][5] = {
+        {0, 3, 4, 5, 7},
+        {2, 4, 7},
+        {5, 7},
+        {0}, {0}, {7},
+        {}, {}, {},
+    };
+
+    // Index of lookup non-matches from trp.
+    size_t lu_no_match[N_LUT][8] = {
+        {1, 2, 6},
+        {0, 1, 3, 5, 6},
+        {0, 1, 2, 3, 4, 6},
+        {1, 2, 3, 4, 5, 6, 7},
+        {0, 1, 2, 3, 4, 5, 6, 7},
+        {0, 1, 2, 3, 4, 5, 6, 7},
+        {0, 1, 2, 3, 4, 5, 6, 7},
+    };
+    */
+
+    LSUP_Graph *gr = LSUP_graph_new (type);
+
+    size_t ct;
+    LSUP_graph_add_trp (gr, trp, &ct);
+
+    EXPECT_INT_EQ (ct, 8);
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
+
+    LSUP_Triple *spo;
+
+    for (int i = 0; i < N_LUT; i++) {
+        printf ("Checking tiple #%d on %d... ", i, type);
+        spo = lu_trp[i];
+        LSUP_GraphIterator *it = LSUP_graph_lookup (gr, spo, &ct);
+        if (type != LSUP_STORE_MEM) // TODO not implemented in htable.
+            EXPECT_INT_EQ (ct, lu_ct[i]);
+        printf ("done.\n");
+
+        /* TODO
+        for (int j = 0; LSUP_graph_iter_next != LSUP_END; j++) {
+            ASSERT (
+                    LSUP_graph_contains (trp[lu_match[j]]),
+                    "Triple not found!");
+            ASSERT (
+                    !(LSUP_graph_contains (trp[lu_no_match[j]])),
+                    "Unexpected triple found!");
+        }
+
+        */
+        LSUP_graph_iter_free (it);
+    };
+
+    //LSUP_triple_free (spo);
+    for (int i = 0; i < N_LUT; i++) free (lu_trp[i]);
+    free_triples (trp);
+
+    LSUP_graph_free (gr);
+
+    return 0;
+}
+
+
 static int
 _graph_remove (LSUP_store_type type)
 {
@@ -81,11 +171,14 @@ _graph_remove (LSUP_store_type type)
     ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
     EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
 
-    LSUP_triple_free (spo);
+    free (spo);
     free_triples (trp); // gr copied data.
 
     LSUP_graph_free (gr);
 
+    // TODO Test complete removal of triples from index when they are not
+    // in another context.
+
     return 0;
 }
 
@@ -106,6 +199,14 @@ static int test_graph_add() {
 }
 
 
+static int test_graph_lookup() {
+    if (_graph_lookup (LSUP_STORE_MEM) != 0) return -1;
+    if (_graph_lookup (LSUP_STORE_MDB_TMP) != 0) return -1;
+
+    return 0;
+}
+
+
 static int test_graph_remove() {
     if (_graph_remove (LSUP_STORE_MEM) != 0) return -1;
     if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1;
@@ -118,6 +219,7 @@ int graph_tests()
 {
     RUN (test_graph_new);
     RUN (test_graph_add);
+    RUN (test_graph_lookup);
     RUN (test_graph_remove);
 
     return 0;