Bläddra i källkod

Fix mdb lookup_0bound; fix profiler tool.

Stefano Cossu 4 år sedan
förälder
incheckning
c67d41ad1f
5 ändrade filer med 62 tillägg och 66 borttagningar
  1. 1 1
      Makefile
  2. 30 32
      profile.c
  3. 1 1
      src/environment.c
  4. 6 10
      src/store_mdb.c
  5. 24 22
      test/test_graph.c

+ 1 - 1
Makefile

@@ -61,7 +61,7 @@ memcheck: test valgrind
 
 profile: build_parsers
 	$(CC) \
-		$(CFLAGS) -DDEBUG -g \
+		$(CFLAGS) -g -DTESTING \
 		$(INCLUDE) \
 		$(LIB) \
 		$(SRC) profile.c \

+ 30 - 32
profile.c

@@ -2,59 +2,51 @@
 #include "graph.h"
 
 #ifndef NT
-#define NT 1000
+#define NT 100000
 #endif
 
 static LSUP_Triple *
-generate_triples()
+generate_triples(size_t nt)
 {
     LSUP_Triple *trp;
-    trp = malloc((NT  + 1) * sizeof(LSUP_Triple));
+    trp = malloc((nt  + 1) * sizeof(LSUP_Triple));
     if (!trp) exit (-1);
 
-    for (size_t i = 0; i < NT; i++) {
+    for (size_t i = 0; i < nt; i++) {
         char sstr[32], pstr[32], ostr[32];
 
-        sprintf(sstr, "urn:s:%lu", i % (NT / 100));
-        sprintf(pstr, "urn:p:%lu", i % (NT / 1000));
+        sprintf(sstr, "urn:s:%lu", i % (nt / 100));
+        sprintf(pstr, "urn:p:%lu", i % (nt / 1000));
         sprintf(ostr, "urn:o:%lu", i);
         LSUP_triple_init(
                 trp + i, LSUP_uri_new (sstr),
                 LSUP_uri_new (pstr), LSUP_uri_new (ostr));
     }
-    LSUP_triple_init (trp + NT, NULL, NULL, NULL);
+    LSUP_triple_init (trp + nt, NULL, NULL, NULL);
     log_info ("Triples generated.");
 
     return trp;
 }
 
-static LSUP_rc
-insert_triples (LSUP_Graph *gr, LSUP_Triple *trp)
-{
-    size_t ct;
-    LSUP_rc rc = LSUP_graph_add_trp(gr, trp, &ct);
-    if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
-    else log_info ("Graph populated with %lu triples.", ct);
-
-    return rc;
-}
 
-
-int main()
+int main(int argc, char *argv[])
 {
+    size_t nt = (argc > 1) ? atoi (argv[1]) : NT;
     // Set env variable to test path.
     putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_profile_mdb");
     // Clear out database from previous test.
     rm_r (getenv ("LSUP_MDB_STORE_PATH"));
-    LSUP_init();
+
+    if (LSUP_init() != LSUP_OK) abort();
 
     int rc;
     clock_t start, tc1, tc2, end;
     double wallclock, rate;
 
-    log_info ("Generating triples.");
+    log_info ("Generating %lu triples.", nt);
     start = clock();
-    LSUP_Triple *trp = generate_triples();
+
+    LSUP_Triple *trp = generate_triples(nt);
     tc1 = clock();
     wallclock = (tc1 - start) / CLOCKS_PER_SEC;
     log_info ("Time elapsed: %lf s", wallclock);
@@ -65,9 +57,13 @@ int main()
         log_error ("Error creating graph!");
         return -1;
     }
-    rc = insert_triples (gr, trp);
 
-    for (size_t i = 0; i < NT; i++) {
+    size_t ct;
+    rc = LSUP_graph_add_trp(gr, trp, &ct);
+    if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
+    else log_info ("Graph populated with %lu triples.", ct);
+
+    for (size_t i = 0; i < nt; i++) {
         LSUP_term_free (trp[i].s);
         LSUP_term_free (trp[i].p);
         LSUP_term_free (trp[i].o);
@@ -80,25 +76,27 @@ int main()
     log_info ("Graph size: %lu", LSUP_graph_size (gr));
 
     log_info ("Lookup...");
-    size_t ct = 0;
+    ct = 0;
     LSUP_Triple *spo = TRP_DUMMY;
-    LSUP_Term *s = LSUP_uri_new ("urn:s:0");
-    LSUP_Term *p = LSUP_uri_new ("urn:p:3200");
-    LSUP_Term *o = LSUP_uri_new ("urn:o:3200");
-    LSUP_GraphIterator *it = LSUP_graph_lookup(gr, NULL, NULL, NULL, NULL);
+    LSUP_Term *s = LSUP_uri_new ("urn:s:8");
+    LSUP_Term *p = LSUP_uri_new ("urn:p:0");
+    LSUP_Term *o = LSUP_uri_new ("urn:o:300");
+    LSUP_GraphIterator *it = LSUP_graph_lookup(gr, s, NULL, NULL, &ct);
+    log_info ("Found triples by count: %lu", ct);
+    ct = 0;
     while (LSUP_graph_iter_next (it, spo) != LSUP_END)
         ct ++;
-    log_info ("Found triples per subject: %lu", ct);
+    log_info ("Found triples by iteration: %lu", ct);
     LSUP_graph_iter_free (it);
     end = clock();
     wallclock = (end - tc2) / CLOCKS_PER_SEC;
     log_info ("Time elapsed: %lf s", wallclock);
 
     wallclock = (end - start) / CLOCKS_PER_SEC;
-    rate = NT / wallclock;
+    rate = nt / wallclock;
     log_info (
             "%d triples created and inserted in %lf s (%lf triples/s)",
-            NT, wallclock, rate);
+            nt, wallclock, rate);
 
     LSUP_term_free (s);
     LSUP_term_free (p);

+ 1 - 1
src/environment.c

@@ -65,7 +65,7 @@ LSUP_init (void)
         int loglevel = LOG_TRACE;
 #else
         char *_loglevel = getenv ("LSUP_LOGLEVEL");
-        int loglevel = (_loglevel == NULL) ? LOG_WARN : atoi (_loglevel);
+        int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
 #endif
         log_set_level (loglevel);
 

+ 6 - 10
src/store_mdb.c

@@ -88,9 +88,6 @@ typedef struct MDBIterator {
  */
 #define DUPSORT_MASK        MDB_DUPSORT
 #define DUPFIXED_MASK       MDB_DUPSORT | MDB_DUPFIXED
-#define INT_KEY_MASK        MDB_INTEGERKEY
-#define INT_DUP_KEY_MASK    MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERKEY
-#define INT_DUPDATA_MASK    MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERDUP
 
 /**
  * Main DBs. These are the master information containers.
@@ -627,7 +624,7 @@ mdbiter_next_key (LSUP_MDBIterator *it)
      * it->rc is set to the result of the next iteration.
      */
     it->iter_op_fn (it);
-    log_debug (
+    log_trace (
             "Found  spok: {%lx, %lx, %lx}",
             it->spok[0], it->spok[1], it->spok[2]);
 
@@ -661,11 +658,11 @@ mdbiter_next_key (LSUP_MDBIterator *it)
 
             if (db_rc == MDB_SUCCESS) {
                 rc = LSUP_OK;
-                //log_debug ("Triple found for context.");
+                log_trace ("Triple found for context.");
             }
 
             else if (db_rc == MDB_NOTFOUND) {
-                //log_debug ("No triples found for context.");
+                log_trace ("No triples found for context.");
                 if (it->rc == MDB_NOTFOUND) rc = LSUP_END;
                 else it->iter_op_fn (it);
 
@@ -1042,9 +1039,9 @@ index_triple(
 inline static void
 it_next_0bound (MDBIterator *it)
 {
-    memcpy (it->spok, it->data.mv_data, sizeof (LSUP_TripleKey));
+    memcpy (it->spok, it->key.mv_data, sizeof (LSUP_TripleKey));
 
-    it->rc = mdb_cursor_get (it->cur, &it->key, NULL, MDB_NEXT);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT);
 }
 
 
@@ -1130,7 +1127,6 @@ inline static LSUP_rc
 lookup_0bound (MDBIterator *it, size_t *ct)
 {
     log_debug ("Looking up 0 bound terms.");
-    log_debug ("Lookup context: %lx", it->ck);
 
     if (it->store->txn) it->txn = it->store->txn;
     else {
@@ -1150,7 +1146,7 @@ lookup_0bound (MDBIterator *it, size_t *ct)
             it->key.mv_data = &it->ck;
             it->key.mv_size = KLEN;
 
-            it->rc = mdb_cursor_get (it->cur, &it->key, NULL, MDB_SET);
+            it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
             if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
 
             mdb_cursor_close (it->cur);

+ 24 - 22
test/test_graph.c

@@ -80,31 +80,31 @@ _graph_lookup (LSUP_store_type type)
     };
 
     // Lookup result counts.
-    size_t lu_ct[N_LUT] = { 8, 5, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
+    size_t lu_ct[N_LUT] = {
+        8,
+        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] = {
+    size_t lu_match[N_LUT][8] = {
         {0, 1, 2, 3, 4, 5, 6, 7},
-        {0, 3, 4, 5, 7},
-        {2, 4, 7},
-        {5, 7},
+        {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},
+        {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6},
+        {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6},
+        {0, 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}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
     };
-    */
 
     LSUP_Graph *gr = LSUP_graph_new (type);
 
@@ -123,16 +123,18 @@ _graph_lookup (LSUP_store_type type)
         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!");
+        for (int j = 0; j < 8; j++) {
+            for (int k = 0; LSUP_graph_iter_next(it) != LSUP_END; k++) {
+                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);
     };