Browse Source

Convenience function for random URN string; perf test for 100K triples.

Stefano Cossu 4 years ago
parent
commit
463e5642c3
6 changed files with 69 additions and 11 deletions
  1. 2 0
      Makefile
  2. 3 0
      include/graph.h
  3. 9 0
      include/term.h
  4. 9 10
      src/graph.c
  5. 16 0
      src/term.c
  6. 30 1
      test/test_graph.c

+ 2 - 0
Makefile

@@ -12,6 +12,7 @@ build:
 	gcc -g -Wall \
 		-std=c99 \
 		-Iinclude -Iext/xxHash \
+		-luuid \
 		ext/xxHash/xxhash.c src/*.c \
 		-o bin/lsup_rdf.so
 
@@ -20,5 +21,6 @@ test:
 		-std=c99 \
 		-DDEBUG \
 		-Iinclude -Iext/xxHash -Itest \
+		-luuid \
 		ext/xxHash/xxhash.c src/*.c test.c \
 		-o bin/test

+ 3 - 0
include/graph.h

@@ -43,6 +43,9 @@ LSUP_graph_capacity(LSUP_Graph *gr);
 size_t
 LSUP_graph_size(LSUP_Graph *gr);
 
+char *
+LSUP_graph_uri(LSUP_Graph *gr);
+
 bool
 LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *t);
 

+ 9 - 0
include/term.h

@@ -48,9 +48,18 @@ LSUP_term_init(
         LSUP_Term *term, LSUP_term_type type,
         char *data, char *datatype, char *lang);
 
+
 LSUP_Term *
 LSUP_term_new(LSUP_term_type type, char *data, char *datatype, char *lang);
 
+
+/**
+ * Generate a random URN with the format: `urn:lsup:<uuid4>`.
+ */
+char *
+LSUP_term_gen_random_str();
+
+
 /** Simple ad-hoc serialization function.
  *
  * This function allocates and returns the following byte sequence:

+ 9 - 10
src/graph.c

@@ -310,7 +310,7 @@ static int index_add_pair(Index *idx, LSUP_Key key, LSUP_SerTerm *sterm)
         memcpy(entry->val->addr, sterm->addr, sterm->size);
 
         idx->free_i ++;
-        TRACE("Size now at %lu\n", idx->free_i);
+        // TRACE("Size now at %lu\n", idx->free_i);
 
     }
 
@@ -346,15 +346,8 @@ LSUP_graph_init(
         LSUP_store_type store_type)
 {
     if (uri_str == NULL) {
-        char gr_name[UUIDSTR_SIZE + 9];
-
-        uuid_t uuid;
-        uuid_generate_random(uuid);
-
-        uuid_str_t uuid_str;
-        uuid_unparse_lower(uuid, uuid_str);
-        sprintf(gr_name, "urn:lsup:%s", uuid_str);
-        gr->uri = LSUP_term_new(LSUP_TERM_URI, gr_name, NULL, NULL);
+        gr->uri = LSUP_term_new(
+                LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
     } else {
         gr->uri = LSUP_term_new(LSUP_TERM_URI, uri_str, NULL, NULL);
     }
@@ -420,6 +413,10 @@ size_t
 LSUP_graph_capacity(LSUP_Graph *gr) { return gr->keys->capacity; }
 
 
+char *
+LSUP_graph_uri(LSUP_Graph *gr) { return gr->uri->data; }
+
+
 size_t
 LSUP_graph_size(LSUP_Graph *gr) { return gr->keys->free_i; }
 
@@ -700,4 +697,6 @@ int match_add_fn(LSUP_Graph *src, LSUP_Graph *dest, void *ctx)
 int match_rm_fn(LSUP_Graph *src, LSUP_Graph *dest, void *ctx)
 {
     memcpy(keyset_peek(src->keys), &NULL_TRP, TRP_KLEN);
+
+    return LSUP_OK;
 }

+ 16 - 0
src/term.c

@@ -67,6 +67,22 @@ LSUP_Term
 }
 
 
+char *
+LSUP_term_gen_random_str()
+{
+    uuid_t uuid;
+    uuid_generate_random(uuid);
+
+    uuid_str_t uuid_str;
+    uuid_unparse_lower(uuid, uuid_str);
+
+    static char uri[UUIDSTR_SIZE + 9];
+    sprintf(uri, "urn:lsup:%s", uuid_str);
+
+    return uri;
+}
+
+
 int
 LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
 {

+ 30 - 1
test/test_graph.c

@@ -75,7 +75,7 @@ static int test_graph_heap()
 {
     LSUP_Graph *gr = LSUP_graph_new(10, "urn:gr:1", LSUP_STORE_MEM);
 
-    ASSERT(strcmp(gr->uri->data, "urn:gr:1") == 0, "Graph URI mismatch!");
+    ASSERT(strcmp(LSUP_graph_uri(gr), "urn:gr:1") == 0, "Graph URI mismatch!");
     EXPECT_INT_EQ(LSUP_graph_capacity(gr), 10);
     EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
 
@@ -105,10 +105,39 @@ static int test_graph_add()
 }
 
 
+static int test_graph_add_100k()
+{
+    size_t nt = 100000;
+
+    LSUP_Triple *trp = malloc(nt * sizeof(LSUP_Triple));
+    for (size_t i; i < nt; i++) {
+        trp[i].s = LSUP_term_new(
+                LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
+        trp[i].p = LSUP_term_new(
+                LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
+        trp[i].o = LSUP_term_new(
+                LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
+    }
+    TRACE(STR, "Triples generated.");
+
+    LSUP_Graph *gr = LSUP_graph_new(nt, NULL, LSUP_STORE_MEM);
+
+    LSUP_graph_add(gr, trp, nt);
+    TRACE(STR, "Graph populated.");
+
+    _free_triples(trp); // gr takes ownership of data.
+
+    LSUP_graph_free(gr);
+
+    return 0;
+}
+
+
 int graph_tests()
 {
     RUN(test_graph_heap);
     RUN(test_graph_add);
+    RUN(test_graph_add_100k);
     return 0;
 }