소스 검색

Redesign buffer and term API.

Stefano Cossu 3 년 전
부모
커밋
6411db4085
16개의 변경된 파일577개의 추가작업 그리고 569개의 파일을 삭제
  1. 17 36
      include/buffer.h
  2. 1 0
      include/core.h
  3. 14 14
      include/graph.h
  4. 11 11
      include/store_mdb.h
  5. 22 22
      include/term.h
  6. 29 8
      include/triple.h
  7. 7 6
      src/buffer.c
  8. 20 17
      src/graph.c
  9. 198 197
      src/store_mdb.c
  10. 48 45
      src/term.c
  11. 72 43
      src/triple.c
  12. 5 0
      test.c
  13. 45 45
      test/assets.h
  14. 27 27
      test/test_graph.c
  15. 60 96
      test/test_store_mdb.c
  16. 1 2
      test/test_term.c

+ 17 - 36
include/buffer.h

@@ -10,7 +10,7 @@
  * performed by simply using the addr and size attributes.
  *
  * A buffer can be initialized once and reused multiple times, e.g. in a loop,
- * without being freed between iterations, by using #LSUP_buffer_reset.
+ * without being freed between iterations, by using #LSUP_buffer_init.
  */
 typedef struct LSUP_Buffer {
     /*@null@*/ void *addr;
@@ -18,14 +18,12 @@ typedef struct LSUP_Buffer {
 } LSUP_Buffer;
 
 
-/** Reuse an initialized buffer handle.
+/** Initialize or reuse a buffer handle.
  *
- * The data block is resized without being freed (with realloc). If the new
- * size is not smaller than the old one and the data parameter is NULL, the
- * existing data are preserved. If the size is increased, the new memory is not
- * initialized.
+ * The handle must have been created with #LSUP_buffer_new*().
  *
- * The handle must be freed with #LSUP_buffer_done after use.
+ * The data block is resized without being freed first. The handle must be
+ * eventually freed with #LSUP_buffer_done() after use.
  *
  * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  *  allocation.
@@ -33,34 +31,16 @@ typedef struct LSUP_Buffer {
  * @param size[in] New size.
  *
  * @param data[in] If not NULL, data to replace the existing ones. The size
- * of the data to be copied is determined by the size parameter.
+ *  of the data to be copied is determined by the size parameter. If NULL, the
+ *  existing data are preserved as with a normal realloc().
  */
 LSUP_rc
-LSUP_buffer_reset (LSUP_Buffer *buf, const size_t size, const void *data);
-
-
-/** Initialize an allocated buffer handle.
- *
- * The handle must be freed with #LSUP_buffer_done after use.
- *
- * @param buf[in] A manually (stack or heap) allocated buffer handle.
- *
- * @param size[in] New size.
- *
- * @param data[in] If not NULL, data to replace the existing ones. The size
- * of the data to be copied is determined by the size parameter.
- */
-inline LSUP_rc
-LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data)
-{
-    buf->addr = NULL;
-    return LSUP_buffer_reset(buf, size, data);
-}
+LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data);
 
 
 /** @brief Create a new buffer and optionally populate it with data.
  *
- * To change the buffer size later call #LSUP_buffer_reset.
+ * To change the buffer size and/or data later call #LSUP_buffer_init.
  *
  * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  *
@@ -74,10 +54,11 @@ LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data)
 inline LSUP_Buffer *
 LSUP_buffer_new (const size_t size, const void *data)
 {
-    LSUP_Buffer *buf;
-    CRITICAL (buf = malloc (sizeof (*buf)));
+    LSUP_Buffer *buf = calloc (1, sizeof (*buf));
+    if (!buf) return NULL;
 
     if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
+        free (buf->addr);
         free (buf);
         return NULL;
     }
@@ -86,14 +67,14 @@ LSUP_buffer_new (const size_t size, const void *data)
 }
 
 
-/** @brief Dummy buffer to be used with #LSUP_buffer_reset.
+/** @brief Dummy buffer to be used with #LSUP_buffer_init.
  */
-#define BUF_DUMMY LSUP_buffer_new(0, NULL);
+#define BUF_DUMMY LSUP_buffer_new (0, NULL)
 
 
 /** @brief Free the content of a buffer.
  */
-void LSUP_buffer_done(LSUP_Buffer *buf);
+void LSUP_buffer_done (LSUP_Buffer *buf);
 
 
 /** @brief Free a buffer.
@@ -118,8 +99,8 @@ inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
 
 /** @brief Return whether two buffers are equal.
  *
- * This may be faster than #LSUP_buffer_cmp because it returns immediately if
- * the sizes of the buffers differ.
+ * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
+ * first.
  */
 inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
 {

+ 1 - 0
include/core.h

@@ -70,6 +70,7 @@ typedef enum {
     LSUP_DB_ERR         = -88805,
     LSUP_NOT_IMPL_ERR   = -88806,
     LSUP_IO_ERR         = -88807,
+    LSUP_MEM_ERR        = -88808,
 } LSUP_rc;
 
 typedef enum {

+ 14 - 14
include/graph.h

@@ -46,7 +46,7 @@ typedef struct GraphIterator LSUP_GraphIterator;
  * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  */
 LSUP_Graph *
-LSUP_graph_new(const LSUP_store_type store_type);
+LSUP_graph_new (const LSUP_store_type store_type);
 
 
 /** @brief copy a graph into a new one.
@@ -63,7 +63,7 @@ LSUP_graph_new(const LSUP_store_type store_type);
  * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  */
 LSUP_Graph *
-LSUP_graph_copy(const LSUP_Graph *src);
+LSUP_graph_copy (const LSUP_Graph *src);
 
 
 /** Perform a boolean operation between two graphs.
@@ -87,7 +87,7 @@ LSUP_graph_bool_op(
 /** @brief Free a graph.
  */
 void
-LSUP_graph_free(LSUP_Graph *gr);
+LSUP_graph_free (LSUP_Graph *gr);
 
 
 /** @brief Number of triples that can be stored without resizing the graph.
@@ -96,13 +96,13 @@ LSUP_graph_free(LSUP_Graph *gr);
  *  an MDB graph.
  */
 size_t
-LSUP_graph_capacity(const LSUP_Graph *gr);
+LSUP_graph_capacity (const LSUP_Graph *gr);
 
 
 /** @brief Number of triples in a graph.
  */
 size_t
-LSUP_graph_size(const LSUP_Graph *gr);
+LSUP_graph_size (const LSUP_Graph *gr);
 
 
 /** @brief Change the capacity of an in-memory graph.
@@ -121,7 +121,7 @@ LSUP_graph_size(const LSUP_Graph *gr);
  *  resizing.
  */
 LSUP_rc
-LSUP_graph_resize(LSUP_Graph *gr, size_t size);
+LSUP_graph_resize (LSUP_Graph *gr, size_t size);
 
 
 /** @brief Read-only graph URI.
@@ -129,7 +129,7 @@ LSUP_graph_resize(LSUP_Graph *gr, size_t size);
  * To change the graph URI, use #LSUP_graph_set_uri.
  */
 LSUP_Term *
-LSUP_graph_uri(const LSUP_Graph *gr);
+LSUP_graph_uri (const LSUP_Graph *gr);
 
 
 /** Set the URI of a graph.
@@ -139,10 +139,10 @@ LSUP_graph_uri(const LSUP_Graph *gr);
  * @param uri[in] New URI as a string. If NULL, a UUID4 URN is generated.
  */
 LSUP_rc
-LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri);
+LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri);
 
 bool
-LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo);
+LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
 
 
 /** @brief Add triples and/or serialized triples to a graph.
@@ -157,7 +157,7 @@ LSUP_graph_add(
         const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
 
 #define LSUP_graph_add_trp(gr, trp, ct, ins) \
-    LSUP_graph_add(gr, trp, ct, NULL, 0, ins)
+    LSUP_graph_add (gr, trp, ct, NULL, 0, ins)
 
 
 /** @brief Delete triples by a matching pattern.
@@ -170,7 +170,7 @@ LSUP_graph_add(
  *  deleted.
  */
 LSUP_rc
-LSUP_graph_remove(LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
+LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
 
 
 /** Look up triples by a matching pattern and yield an iterator.
@@ -184,7 +184,7 @@ LSUP_graph_remove(LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  *  freed with #LSUP_graph_iter_free after use.
  */
 LSUP_GraphIterator *
-LSUP_graph_lookup(const LSUP_Graph *gr, const LSUP_Triple *spo);
+LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Triple *spo);
 
 
 /** @brief Advance a cursor obtained by a lookup and return a matching triple.
@@ -198,12 +198,12 @@ LSUP_graph_lookup(const LSUP_Graph *gr, const LSUP_Triple *spo);
  *  was reached.
  */
 LSUP_rc
-LSUP_graph_iter_next(LSUP_GraphIterator *it, LSUP_Triple *spo);
+LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
 
 
 /** @brief Free a graph iterator.
  */
 void
-LSUP_graph_iter_free(LSUP_GraphIterator *it);
+LSUP_graph_iter_free (LSUP_GraphIterator *it);
 
 #endif

+ 11 - 11
include/store_mdb.h

@@ -50,7 +50,7 @@ typedef LSUP_rc (*store_match_fn_t)(const LSUP_TripleKey spok, void *data);
  *  in which case it will be set either to the environment variable
  *  LSUP_STORE_PATH, or if that is not set, a default local path.
  */
-LSUP_rc LSUP_mdbstore_setup(char **path, bool clear);
+LSUP_rc LSUP_mdbstore_setup (char **path, bool clear);
 
 
 /** @brief Open an MDB store.
@@ -73,7 +73,7 @@ LSUP_rc LSUP_mdbstore_setup(char **path, bool clear);
  *  in triple mode.
  */
 LSUP_MDBStore *
-LSUP_mdbstore_new(const char *path, const LSUP_Buffer *default_ctx);
+LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx);
 
 
 /** @brief Close a store and free its handle.
@@ -81,7 +81,7 @@ LSUP_mdbstore_new(const char *path, const LSUP_Buffer *default_ctx);
  * @param[in] store Store pointer.
  *
  */
-void LSUP_mdbstore_free(LSUP_MDBStore *store);
+void LSUP_mdbstore_free (LSUP_MDBStore *store);
 
 
 /** @brief Print stats about a store and its databases.
@@ -90,7 +90,7 @@ void LSUP_mdbstore_free(LSUP_MDBStore *store);
  *
  * @param store[in] The store to get stats for.
  */
-LSUP_rc LSUP_mdbstore_stats(LSUP_MDBStore *store);
+LSUP_rc LSUP_mdbstore_stat (LSUP_MDBStore *store, MDB_stat *stat);
 
 
 /** @brief Store size.
@@ -99,7 +99,7 @@ LSUP_rc LSUP_mdbstore_stats(LSUP_MDBStore *store);
  *
  * @return Number of stored SPO triples across all contexts.
  */
-size_t LSUP_mdbstore_size(LSUP_MDBStore *store);
+size_t LSUP_mdbstore_size (LSUP_MDBStore *store);
 
 
 /** @brief Initialize bulk triple load.
@@ -119,7 +119,7 @@ size_t LSUP_mdbstore_size(LSUP_MDBStore *store);
  *  load steps.
  */
 LSUP_MDBIterator *
-LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc);
+LSUP_mdbstore_add_init (LSUP_MDBStore *store, const LSUP_Buffer *sc);
 
 
 /** @brief Add one triple into the store.
@@ -142,7 +142,7 @@ LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc);
  *  already existed; LSUP_DB_ERR if an MDB error occurred.
  */
 LSUP_rc
-LSUP_mdbstore_add_iter(struct MDBIterator *it, const LSUP_SerTriple *sspo);
+LSUP_mdbstore_add_iter (struct MDBIterator *it, const LSUP_SerTriple *sspo);
 
 
 /** @brief Finalize an add loop and free iterator.
@@ -258,7 +258,7 @@ LSUP_mdbstore_lookup(
  * @return LSUP_OK if results were found; LSUP_END if no (more) results were
  * found; LSUP_DB_ERR if a MDB_* error occurred.
  */
-LSUP_rc LSUP_mdbiter_next(LSUP_MDBIterator *it, LSUP_SerTriple *sspo);
+LSUP_rc LSUP_mdbiter_next (LSUP_MDBIterator *it, LSUP_SerTriple *sspo);
 
 
 /** @brief Iterator's internal counter.
@@ -278,13 +278,13 @@ LSUP_mdbiter_i (LSUP_MDBIterator *it);
  *
  * @param it[in] Iterator pointer. It will be set to NULL after freeing.
  */
-void LSUP_mdbiter_free(struct MDBIterator *it);
+void LSUP_mdbiter_free (struct MDBIterator *it);
 
 
 /** @brief Contexts that a triple key appears in.
  *
- * This function is most conveniently used by a callback to #LSUP_mdbstore_lookup
- * because it handles triple keys.
+ * This function is most conveniently used by a callback to
+ * #LSUP_mdbstore_lookup because it handles triple keys.
  *
  * @param store[in] The store to be queried.
  *

+ 22 - 22
include/term.h

@@ -69,7 +69,7 @@ LSUP_term_new(
 
 /** @brief Placeholder term to use with LSUP_term_reset.
  */
-#define TERM_DUMMY LSUP_term_new(LSUP_TERM_UNDEFINED, NULL, NULL, NULL)
+#define TERM_DUMMY LSUP_term_new (LSUP_TERM_UNDEFINED, NULL, NULL, NULL)
 
 
 /** @brief Shortcut to create a URI.
@@ -83,22 +83,22 @@ LSUP_term_new(
  * @return LSUP_OK if successful, LSUP_VALUE_ERR if validation fails.
  */
 inline LSUP_Term *
-LSUP_uri_new(const char *data)
+LSUP_uri_new (const char *data)
 {
     if (!data) {
         uuid_t uuid;
-        uuid_generate_random(uuid);
+        uuid_generate_random (uuid);
 
         uuid_str_t uuid_str;
-        uuid_unparse_lower(uuid, uuid_str);
+        uuid_unparse_lower (uuid, uuid_str);
 
         char uri[UUID4_URN_SIZE];
-        snprintf(uri, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
+        snprintf (uri, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
 
         data = uri;
     }
 
-    return LSUP_term_new(LSUP_TERM_URI, data, NULL, NULL);
+    return LSUP_term_new (LSUP_TERM_URI, data, NULL, NULL);
 }
 
 
@@ -115,33 +115,33 @@ LSUP_term_init(
 
 
 LSUP_Term *
-LSUP_term_new_from_buffer(const LSUP_Buffer *sterm);
+LSUP_term_new_from_buffer (const LSUP_Buffer *sterm);
 
 
 LSUP_Buffer *
-LSUP_buffer_new_from_term(const LSUP_Term *term);
+LSUP_buffer_new_from_term (const LSUP_Term *term);
 
 
 /**
  * @brief Shortcut to initialize a URI.
  */
 inline LSUP_rc
-LSUP_uri_init(LSUP_Term *term, const char *data)
+LSUP_uri_init (LSUP_Term *term, const char *data)
 {
     if (!data) {
         uuid_t uuid;
-        uuid_generate_random(uuid);
+        uuid_generate_random (uuid);
 
         uuid_str_t uuid_str;
-        uuid_unparse_lower(uuid, uuid_str);
+        uuid_unparse_lower (uuid, uuid_str);
 
         char uri[UUIDSTR_SIZE + 10];
-        sprintf(uri, "urn:uuid4:%s", uuid_str);
+        sprintf (uri, "urn:uuid4:%s", uuid_str);
 
         data = uri;
     }
 
-    return LSUP_term_init(term, LSUP_TERM_URI, data, NULL, NULL);
+    return LSUP_term_init (term, LSUP_TERM_URI, data, NULL, NULL);
 }
 
 
@@ -150,7 +150,7 @@ LSUP_uri_init(LSUP_Term *term, const char *data)
  * The resulting term must be freed with #LSUP_term_free after use.
  */
 LSUP_rc
-LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm);
+LSUP_term_serialize (const LSUP_Term *term, LSUP_Buffer *sterm);
 
 
 /** @brief Deserialize a buffer into a term.
@@ -159,11 +159,11 @@ LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm);
  * by #LSUP_term_serialize.
  */
 LSUP_rc
-LSUP_term_deserialize(const LSUP_Buffer *sterm, LSUP_Term *term);
+LSUP_term_deserialize (const LSUP_Buffer *sterm, LSUP_Term *term);
 
 
 inline LSUP_Key
-LSUP_sterm_to_key(const LSUP_Buffer *sterm)
+LSUP_sterm_to_key (const LSUP_Buffer *sterm)
 {
     if (UNLIKELY (sterm == NULL)) return NULL_KEY;
 
@@ -176,14 +176,14 @@ LSUP_sterm_to_key(const LSUP_Buffer *sterm)
  * If NULL is passed, the result is NULL_KEY.
  */
 inline LSUP_Key
-LSUP_term_to_key(const LSUP_Term *term)
+LSUP_term_to_key (const LSUP_Term *term)
 {
     if (UNLIKELY (term == NULL)) return NULL_KEY;
 
-    LSUP_Buffer *sterm = LSUP_buffer_new_from_term(term);
+    LSUP_Buffer *sterm = LSUP_buffer_new_from_term (term);
     LSUP_Key key = XXH64(sterm->addr, sterm->size, SEED);
 
-    LSUP_buffer_free(sterm);
+    LSUP_buffer_free (sterm);
 
     return key;
 }
@@ -191,7 +191,7 @@ LSUP_term_to_key(const LSUP_Term *term)
 /**
  * Compare two terms.
  */
-bool LSUP_term_equals(const LSUP_Term *term1, const LSUP_Term *term2);
+bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
 
 /*
 // TODO Implement when xxhash v0.8 is released with stable xxhash128 function.
@@ -200,9 +200,9 @@ LSUP_term_hash128(const LSUP_Term *term);
 */
 
 void
-LSUP_term_done(LSUP_Term *term);
+LSUP_term_done (LSUP_Term *term);
 
 void
-LSUP_term_free(LSUP_Term *term);
+LSUP_term_free (LSUP_Term *term);
 
 #endif

+ 29 - 8
include/triple.h

@@ -23,19 +23,40 @@ typedef enum {
 
 
 LSUP_Triple *
-LSUP_triple_new();
+LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
 
 
 LSUP_SerTriple *
-LSUP_striple_new();
+LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
 
 
 LSUP_Triple *
-LSUP_triple_new_from_striple(const LSUP_SerTriple *sspo);
+LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo);
 
 
 LSUP_SerTriple *
-LSUP_striple_new_from_triple(const LSUP_Triple *spo);
+LSUP_striple_new_from_triple (const LSUP_Triple *spo);
+
+
+/** @brief Initialize internal term pointers in a heap-allocated triple.
+ *
+ * The triple must be freed with #LSUP_triple_free().
+ *
+ * @param spo[in] Triple pointer to initialize.
+ */
+LSUP_rc
+LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
+
+
+/** @brief Initialize internal term pointers in a heap-allocated buffer triple.
+ *
+ * The triple must be freed with #LSUP_striple_free().
+ *
+ * @param sspo[in] Serialized triple pointer to initialize.
+ */
+LSUP_rc
+LSUP_striple_init (
+        LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
 
 
 /** @brief Serialize a RDF triple into a buffer triple.
@@ -49,7 +70,7 @@ LSUP_striple_new_from_triple(const LSUP_Triple *spo);
  * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  */
 LSUP_rc
-LSUP_triple_serialize(const LSUP_Triple *spo, LSUP_SerTriple *sspo);
+LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo);
 
 
 /** @brief Deserialize a buffer triple into a RDF triple.
@@ -63,7 +84,7 @@ LSUP_triple_serialize(const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  */
 LSUP_rc
-LSUP_triple_deserialize(const LSUP_SerTriple *sspo, LSUP_Triple *spo);
+LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo);
 
 
 /** @brief Free the internal pointers of a triple.
@@ -116,7 +137,7 @@ LSUP_striple_free (LSUP_SerTriple *sspo);
  * @return Corresponding triple term or NULL if n is out of range.
  */
 inline LSUP_Term *
-LSUP_triple_pos(const LSUP_Triple *trp, LSUP_TriplePos n)
+LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
 { _FN_BODY }
 
 
@@ -131,7 +152,7 @@ LSUP_triple_pos(const LSUP_Triple *trp, LSUP_TriplePos n)
  * @return Corresponding serialized term or NULL if n is out of range.
  */
 inline LSUP_Buffer *
-LSUP_striple_pos(const LSUP_SerTriple *trp, LSUP_TriplePos n)
+LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n)
 { _FN_BODY }
 #undef _FN_BODY
 

+ 7 - 6
src/buffer.c

@@ -12,10 +12,11 @@ int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2);
 /* * * API * * */
 
 LSUP_rc
-LSUP_buffer_reset (LSUP_Buffer *buf, const size_t size, const void *data)
+LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data)
 {
+    // If size is zero, addr becomes NULL.
     void *tmp = realloc (buf->addr, size);
-    if (UNLIKELY (!tmp)) return LSUP_ERROR;
+    if (UNLIKELY (size > 0 && tmp == NULL)) return LSUP_MEM_ERR;
 
     buf->addr = tmp;
     buf->size = size;
@@ -36,18 +37,18 @@ void LSUP_buffer_print (const LSUP_Buffer *buf)
             printf ("\\x%02x", chr);
         }
     }
-    printf("\n");
+    printf ("\n");
 }
 
 
-void LSUP_buffer_done(LSUP_Buffer *buf)
+void LSUP_buffer_done (LSUP_Buffer *buf)
 {
-    if (LIKELY (buf)) free(buf->addr);
+    if (LIKELY (buf)) free (buf->addr);
 }
 
 void LSUP_buffer_free (LSUP_Buffer *buf)
 {
     LSUP_buffer_done (buf);
-    free(buf);
+    free (buf);
 }
 

+ 20 - 17
src/graph.c

@@ -104,14 +104,15 @@ LSUP_graph_new (const LSUP_store_type store_type)
     LSUP_Graph *gr;
     CRITICAL (gr = malloc (sizeof (LSUP_Graph)));
     gr->uri = LSUP_uri_new (NULL);
+    gr->store_type = store_type;
 
     if (mdbstore_init() != LSUP_OK) return NULL;
 
-    if (store_type == LSUP_STORE_MEM) {
+    if (gr->store_type == LSUP_STORE_MEM) {
         // TODO uncomment gr->ht_store = LSUP_htstore_new (0);
         // if (!gr->ht_store) return NULL;
 
-    } else if (store_type == LSUP_STORE_MDB) {
+    } else if (gr->store_type == LSUP_STORE_MDB) {
         gr->mdb_store = LSUP_mdbstore_new(
                 getenv ("LSUP_MDB_STORE_PATH"), default_ctx);
         if (!gr->mdb_store) return NULL;
@@ -244,6 +245,7 @@ LSUP_graph_capacity (const Graph *gr)
 size_t
 LSUP_graph_size (const Graph *gr)
 {
+    TRACE ("Store type: %d\n", gr->store_type);
     if (gr->store_type == LSUP_STORE_MEM)
         return 0;// TODO uncomment LSUP_htstore_size (gr->ht_store);
 
@@ -314,11 +316,9 @@ LSUP_graph_add(
     } else {
         rc = LSUP_NOACTION;
 
-        LSUP_Buffer sc;
-        LSUP_term_serialize (gr->uri, &sc);
-
-        LSUP_MDBIterator *it = LSUP_mdbstore_add_init (gr->mdb_store, &sc);
-        LSUP_buffer_done (&sc);
+        LSUP_Buffer *sc = LSUP_buffer_new_from_term (gr->uri);
+        LSUP_MDBIterator *it = LSUP_mdbstore_add_init (gr->mdb_store, sc);
+        LSUP_buffer_done (sc);
 
         // Serialize and insert RDF triples.
         if (trp_ct > 0) {
@@ -365,16 +365,15 @@ LSUP_graph_remove (Graph *gr, const LSUP_Triple *spo, size_t *ct)
 {
     LSUP_rc rc;
 
-    LSUP_SerTriple sspo;
-    LSUP_triple_serialize (spo, &sspo);
+    LSUP_SerTriple *sspo = LSUP_striple_new_from_triple (spo);
     LSUP_Buffer *sc = LSUP_buffer_new_from_term (gr->uri);
 
     if (gr->store_type == LSUP_STORE_MEM)
         rc = 0;// TODO uncomment LSUP_htstore_remove (gr->ht_store, &sspo, ct);
     else
-        rc = LSUP_mdbstore_remove (gr->mdb_store, &sspo, sc, ct);
+        rc = LSUP_mdbstore_remove (gr->mdb_store, sspo, sc, ct);
 
-    LSUP_striple_done (&sspo);
+    LSUP_striple_free (sspo);
     LSUP_buffer_free (sc);
 
     return rc;
@@ -411,7 +410,7 @@ LSUP_graph_lookup (const Graph *gr, const LSUP_Triple *spo)
  * This is an internal function to pass raw buffers between higher-level
  * functions without serializing and deserializing triples.
  */
-static LSUP_rc
+inline static LSUP_rc
 graph_iter_next_buffer (GraphIterator *it, LSUP_SerTriple *sspo)
 {
     LSUP_rc rc;
@@ -427,7 +426,9 @@ graph_iter_next_buffer (GraphIterator *it, LSUP_SerTriple *sspo)
 LSUP_rc
 LSUP_graph_iter_next (GraphIterator *it, LSUP_Triple *spo)
 {
-    LSUP_SerTriple *sspo = LSUP_striple_new();
+    LSUP_SerTriple *sspo = NULL;
+    if (spo) sspo = LSUP_striple_new(BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
+
     LSUP_rc rc = graph_iter_next_buffer (it, sspo);
 
     if (rc == LSUP_OK) {
@@ -441,10 +442,12 @@ LSUP_graph_iter_next (GraphIterator *it, LSUP_Triple *spo)
     }
 
     // Addresses of triple buffers are owned by the DB. Do not free.
-    free (sspo->s);
-    free (sspo->p);
-    free (sspo->o);
-    free (sspo);
+    if (sspo) {
+        free (sspo->s);
+        free (sspo->p);
+        free (sspo->o);
+        free (sspo);
+    }
 
     return rc;
 }

+ 198 - 197
src/store_mdb.c

@@ -220,7 +220,7 @@ LSUP_mdbstore_setup (char **path, bool clear)
     int rc;
 
     // Set environment path.
-    if (path == NULL && (*path = getenv("LSUP_STORE_PATH")) == NULL) {
+    if (path == NULL && (*path = getenv ("LSUP_STORE_PATH")) == NULL) {
         // FIXME This won't work for multiple graphs with different disk
         // back ends. A random path generator needs to be used.
         *path = DEFAULT_ENV_PATH;
@@ -239,65 +239,65 @@ LSUP_mdbstore_setup (char **path, bool clear)
 
     // Open a temporary environment and txn to create the DBs.
     MDB_env *env;
-    mdb_env_create(&env);
+    mdb_env_create (&env);
 
-    mdb_env_set_maxdbs(env, N_DB);
-    mdb_env_open(env, *path, 0, ENV_FILE_MODE);
+    mdb_env_set_maxdbs (env, N_DB);
+    mdb_env_open (env, *path, 0, ENV_FILE_MODE);
 
     MDB_txn *txn;
-    mdb_txn_begin(env, NULL, 0, &txn);
+    mdb_txn_begin (env, NULL, 0, &txn);
     for (int i = 0; i < N_DB; i++) {
-        TRACE("Creating DB %s", db_labels[i]);
+        TRACE ("Creating DB %s", db_labels[i]);
         MDB_dbi dbi;
-        rc = mdb_dbi_open(txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi);
+        rc = mdb_dbi_open (txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi);
         if (rc != MDB_SUCCESS) return rc;
     }
 
-    mdb_txn_commit(txn);
-    mdb_env_close(env);
+    mdb_txn_commit (txn);
+    mdb_env_close (env);
 
     return rc;
 }
 
 
 MDBStore *
-LSUP_mdbstore_new(const char *path, const LSUP_Buffer *default_ctx)
+LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx)
 {
     int db_rc;
     LSUP_MDBStore *store;
-    CRITICAL(store = malloc(sizeof(LSUP_MDBStore)));
+    CRITICAL (store = malloc (sizeof (LSUP_MDBStore)));
 
-    db_rc = mdb_env_create(&store->env);
-    TRACE("create rc: %d", db_rc);
+    db_rc = mdb_env_create (&store->env);
+    TRACE ("create rc: %d", db_rc);
 
     store->default_ctx = (
             default_ctx ?
-            LSUP_buffer_new(default_ctx->size, default_ctx->addr) : NULL);
+            LSUP_buffer_new (default_ctx->size, default_ctx->addr) : NULL);
 
     // Set map size.
     size_t mapsize;
-    char *env_mapsize = getenv("LSUP_MDB_MAPSIZE");
+    char *env_mapsize = getenv ("LSUP_MDB_MAPSIZE");
     if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
-    else sscanf(env_mapsize, "%lu", &mapsize);
+    else sscanf (env_mapsize, "%lu", &mapsize);
 
-    db_rc = mdb_env_set_maxdbs(store->env, N_DB);
+    db_rc = mdb_env_set_maxdbs (store->env, N_DB);
     if (UNLIKELY (db_rc != MDB_SUCCESS)) return NULL;
 
-    db_rc = mdb_env_open(store->env, path, 0, ENV_FILE_MODE);
+    db_rc = mdb_env_open (store->env, path, 0, ENV_FILE_MODE);
     if (UNLIKELY (db_rc != MDB_SUCCESS)) return NULL;
 
     // Assign DB handles to store->dbi.
     MDB_txn *txn;
-    mdb_txn_begin(store->env, NULL, 0, &txn);
+    mdb_txn_begin (store->env, NULL, 0, &txn);
     for (int i = 0; i < N_DB; i++) {
-        db_rc = mdb_dbi_open(txn, db_labels[i], db_flags[i], store->dbi + i);
+        db_rc = mdb_dbi_open (txn, db_labels[i], db_flags[i], store->dbi + i);
         if (UNLIKELY (db_rc != MDB_SUCCESS)) {
-            mdb_txn_abort(txn);
+            mdb_txn_abort (txn);
             return NULL;
         }
     }
 
-    mdb_txn_commit(txn);
+    mdb_txn_commit (txn);
 
     store->state |= LSSTORE_OPEN;
     store->txn = NULL;
@@ -307,54 +307,52 @@ LSUP_mdbstore_new(const char *path, const LSUP_Buffer *default_ctx)
 
 
 void
-LSUP_mdbstore_free(LSUP_MDBStore *store)
+LSUP_mdbstore_free (LSUP_MDBStore *store)
 {
     if (store->state & LSSTORE_OPEN) {
-        TRACE(STR, "Closing MDB env.\n");
-        mdb_env_close(store->env);
+        TRACE (STR, "Closing MDB env.\n");
+        mdb_env_close (store->env);
     }
 
     if (store->default_ctx) {
-        LSUP_buffer_done(store->default_ctx);
-        free(store->default_ctx);
+        LSUP_buffer_done (store->default_ctx);
+        free (store->default_ctx);
     }
 
-    free(store);
+    free (store);
 }
 
 
 LSUP_rc
-LSUP_mdbstore_stats(LSUP_MDBStore *store)
+LSUP_mdbstore_stat (LSUP_MDBStore *store, MDB_stat *stat)
 {
-    // TODO
-    // MDB_stat env_stat, db_stats[N_DB];
-    return 0;
+    if (!(store->state & LSSTORE_INIT)) return 0;
+
+    MDB_txn *txn;
+    mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
+
+    if (mdb_stat (txn, store->dbi[IDX_SPO_C], stat) != MDB_SUCCESS)
+        return LSUP_DB_ERR;
+    mdb_txn_abort (txn);
+
+    return LSUP_OK;
 }
 
 
 size_t
-LSUP_mdbstore_size(LSUP_MDBStore *store)
+LSUP_mdbstore_size (LSUP_MDBStore *store)
 {
     // Size is calculated outside of any pending write txn.
 
-    if(!(store->state & LSSTORE_INIT)) return 0;
-
-    MDB_txn *txn;
-    mdb_txn_begin(store->env, NULL, MDB_RDONLY, &txn);
-
     MDB_stat stat;
-    int rc = mdb_stat(txn, store->dbi[IDX_SPO_C], &stat);
-    TRACE("Stat rc: %d\n", rc);
-    // TODO error handling.
-
-    mdb_txn_abort(txn);
+    if (LSUP_mdbstore_stat (store, &stat) != LSUP_OK) return 0;
 
     return stat.ms_entries;
 }
 
 
 MDBIterator *
-LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc)
+LSUP_mdbstore_add_init (LSUP_MDBStore *store, const LSUP_Buffer *sc)
 {
     /* An iterator is used here. Some members are a bit misused but it does
      * its job without having to define a very similar struct.
@@ -366,7 +364,7 @@ LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc)
     it->i = 0;
 
     // No other write transaction may be open.
-    mdb_txn_begin(store->env, NULL, 0, &it->store->txn);
+    mdb_txn_begin (store->env, NULL, 0, &it->store->txn);
 
     // Take care of context first.
     // Serialize and hash.
@@ -375,10 +373,10 @@ LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc)
     if (store->default_ctx != NULL) {
         if (sc == NULL) sc = store->default_ctx;
 
-        it->ck = LSUP_sterm_to_key(sc);
+        it->ck = LSUP_sterm_to_key (sc);
 
         // Insert t:st for context.
-        //TRACE("Adding context: %s", sc);
+        //TRACE ("Adding context: %s", sc);
         it->key.mv_data = &it->ck;
         it->key.mv_size = KLEN;
         it->data.mv_data = sc->addr;
@@ -395,7 +393,7 @@ LSUP_mdbstore_add_init(LSUP_MDBStore *store, const LSUP_Buffer *sc)
 
 
 LSUP_rc
-LSUP_mdbstore_add_iter(MDBIterator *it, const LSUP_SerTriple *sspo)
+LSUP_mdbstore_add_iter (MDBIterator *it, const LSUP_SerTriple *sspo)
 {
     int db_rc;
     LSUP_rc rc;
@@ -403,13 +401,13 @@ LSUP_mdbstore_add_iter(MDBIterator *it, const LSUP_SerTriple *sspo)
 
     // Add triple.
     for (int i = 0; i < 3; i++) {
-        LSUP_Buffer *st = LSUP_striple_pos(sspo, i);
+        LSUP_Buffer *st = LSUP_striple_pos (sspo, i);
 
-        printf("Inserting term: ");
-        LSUP_buffer_print(st);
-        printf("\n");
+        printf ("Inserting term: ");
+        LSUP_buffer_print (st);
+        printf ("\n");
 
-        spok[i] = LSUP_sterm_to_key(st);
+        spok[i] = LSUP_sterm_to_key (st);
 
         it->key.mv_data = spok + i;
         it->key.mv_size = KLEN;
@@ -424,7 +422,7 @@ LSUP_mdbstore_add_iter(MDBIterator *it, const LSUP_SerTriple *sspo)
         }
     }
 
-    TRACE("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);
+    TRACE ("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);
 
     // Insert spo:c.
     it->key.mv_data = spok;
@@ -483,7 +481,7 @@ LSUP_mdbstore_add (
         LSUP_MDBStore *store, const LSUP_Buffer *sc,
         const LSUP_SerTriple strp[], const size_t ct, size_t *inserted)
 {
-    MDBIterator *it = LSUP_mdbstore_add_init(store, sc);
+    MDBIterator *it = LSUP_mdbstore_add_init (store, sc);
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
 
     for (size_t i = 0; i < ct; i++) {
@@ -495,7 +493,7 @@ LSUP_mdbstore_add (
     }
     *inserted = it->i;
 
-    return LSUP_mdbstore_add_done(it);
+    return LSUP_mdbstore_add_done (it);
 }
 
 
@@ -504,7 +502,7 @@ sterm_to_key (
         LSUP_MDBStore *store, const LSUP_Buffer *sterm)
 {
     // TODO this will be replaced by a lookup when 128-bit hash is introduced.
-    return LSUP_sterm_to_key(sterm);
+    return LSUP_sterm_to_key (sterm);
 }
 
 
@@ -518,7 +516,7 @@ key_to_sterm(
     bool txn_dirty = false;
 
     if (!txn) {
-        db_rc = mdb_txn_begin(store->env, NULL, MDB_RDONLY, &txn);
+        db_rc = mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
         if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
         txn_dirty = true;
     }
@@ -527,16 +525,19 @@ key_to_sterm(
     key_v.mv_data = (void*)&key;
     key_v.mv_size = KLEN;
 
-    db_rc = mdb_get(txn, store->dbi[IDX_T_ST], &key_v, &data_v);
+    db_rc = mdb_get (txn, store->dbi[IDX_T_ST], &key_v, &data_v);
 
     if (db_rc == MDB_SUCCESS) {
         sterm->addr = data_v.mv_data;
         sterm->size = data_v.mv_size;
         rc = LSUP_OK;
-    }
-    else if (UNLIKELY(db_rc != MDB_NOTFOUND)) rc = LSUP_ERROR;
+    } else if (db_rc == MDB_NOTFOUND) {
+        free (sterm->addr);
+        sterm->addr = NULL;
+        sterm->size = 0;
+    } else rc = LSUP_ERROR;
 
-    if (txn_dirty) mdb_txn_abort(txn);
+    if (txn_dirty) mdb_txn_abort (txn);
 
     return rc;
 }
@@ -548,18 +549,18 @@ LSUP_mdbstore_lookup(
         const LSUP_Buffer *sc, size_t *ct)
 {
     LSUP_TripleKey spok = {
-        LSUP_sterm_to_key(sspo->s),
-        LSUP_sterm_to_key(sspo->p),
-        LSUP_sterm_to_key(sspo->o),
+        LSUP_sterm_to_key (sspo->s),
+        LSUP_sterm_to_key (sspo->p),
+        LSUP_sterm_to_key (sspo->o),
     };
 
     LSUP_MDBIterator *it;
-    CRITICAL(it = malloc (sizeof (*it)));
+    CRITICAL (it = malloc (sizeof (*it)));
 
     it->store = store;
-    it->ck = store->default_ctx ? LSUP_sterm_to_key(sc) : NULL_KEY;
+    it->ck = store->default_ctx ? LSUP_sterm_to_key (sc) : NULL_KEY;
 
-    if(ct) *ct = 0;
+    if (ct) *ct = 0;
 
     uint8_t idx0, idx1;
 
@@ -606,31 +607,31 @@ LSUP_mdbstore_lookup(
     } else if (spok[2] != NULL_KEY) {
         it->luk[0] = spok[2];
         idx0 = 2;
-        RCNL (lookup_1bound(store, idx0, it, ct));
+        RCNL (lookup_1bound (store, idx0, it, ct));
 
     // ? ? ? (all terms unbound)
-    } else RCNL (lookup_0bound(store, it, ct));
+    } else RCNL (lookup_0bound (store, it, ct));
 
     return it;
 }
 
 
-LSUP_rc
-mdbiter_next_key(LSUP_MDBIterator *it)
+inline static LSUP_rc
+mdbiter_next_key (LSUP_MDBIterator *it)
 {
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
 
     // Only advance if the previous it->rc wasn't already at the end.
-    if(it->rc == MDB_NOTFOUND) return LSUP_END;
+    if (it->rc == MDB_NOTFOUND) return LSUP_END;
 
     if (UNLIKELY (it->rc != MDB_SUCCESS)) {
-        fprintf(stderr, mdb_strerror(it->rc));
+        fprintf (stderr, mdb_strerror (it->rc));
         return LSUP_DB_ERR;
     }
 
     LSUP_rc rc;
 
-    it->iter_op_fn(it);
+    it->iter_op_fn (it);
 
     if (it->ck) {
         rc = LSUP_NORESULT;  // Intermediary value, will never be returned.
@@ -639,14 +640,14 @@ mdbiter_next_key(LSUP_MDBIterator *it)
         MDB_val key, data;
 
         mdb_cursor_open
-            (mdb_cursor_txn(it->cur), it->store->dbi[IDX_SPO_C], &cur);
+            (mdb_cursor_txn (it->cur), it->store->dbi[IDX_SPO_C], &cur);
 
         key.mv_size = TRP_KLEN;
         data.mv_data = &it->ck;
         data.mv_size = KLEN;
 
         while (rc == LSUP_NORESULT) {
-            TRACE(STR, "begin ctx loop.");
+            TRACE (STR, "begin ctx loop.");
             // If ctx is specified, look if the matching triple is associated
             // with it. If not, move on to the next triple.
             // The loop normally exits when a triple with matching ctx is found
@@ -654,26 +655,26 @@ mdbiter_next_key(LSUP_MDBIterator *it)
             // is an error (LSUPP_DB_ERR).
             key.mv_data = it->spok;
 
-            int db_rc = mdb_cursor_get(cur, &key, &data, MDB_GET_BOTH);
+            int db_rc = mdb_cursor_get (cur, &key, &data, MDB_GET_BOTH);
 
             if (db_rc == MDB_SUCCESS) {
                 rc = LSUP_OK;
-                TRACE(STR, "Triple found for context.");
+                TRACE (STR, "Triple found for context.");
             }
 
             else if (db_rc == MDB_NOTFOUND) {
-                TRACE(STR, "No triples found for context.");
+                TRACE (STR, "No triples found for context.");
                 if (it->rc == MDB_NOTFOUND) rc = LSUP_END;
-                else it->iter_op_fn(it);
+                else it->iter_op_fn (it);
 
             } else {
-                fprintf(stderr, mdb_strerror(it->rc));
+                fprintf (stderr, mdb_strerror (it->rc));
                 rc = LSUP_DB_ERR;
             }
 
         }
 
-        mdb_cursor_close(cur);
+        mdb_cursor_close (cur);
 
     } else rc = LSUP_OK;
 
@@ -682,14 +683,14 @@ mdbiter_next_key(LSUP_MDBIterator *it)
 
 
 LSUP_rc
-LSUP_mdbiter_next(LSUP_MDBIterator *it, LSUP_SerTriple *sspo)
+LSUP_mdbiter_next (LSUP_MDBIterator *it, LSUP_SerTriple *sspo)
 {
-    LSUP_rc rc = mdbiter_next_key(it);
+    LSUP_rc rc = mdbiter_next_key (it);
 
     if (sspo && rc == LSUP_OK) {
-        key_to_sterm(it->store, it->spok[0], sspo->s, it->txn);
-        key_to_sterm(it->store, it->spok[1], sspo->p, it->txn);
-        key_to_sterm(it->store, it->spok[2], sspo->o, it->txn);
+        key_to_sterm (it->store, it->spok[0], sspo->s, it->txn);
+        key_to_sterm (it->store, it->spok[1], sspo->p, it->txn);
+        key_to_sterm (it->store, it->spok[2], sspo->o, it->txn);
 
         // TODO error handling.
     }
@@ -704,13 +705,13 @@ LSUP_mdbiter_i (LSUP_MDBIterator *it)
 
 
 void
-LSUP_mdbiter_free(MDBIterator *it)
+LSUP_mdbiter_free (MDBIterator *it)
 {
     if (it) {
-        mdb_cursor_close(it->cur);
-        if(it->store->txn != it->txn) mdb_txn_abort(it->txn);
+        mdb_cursor_close (it->cur);
+        if (it->store->txn != it->txn) mdb_txn_abort (it->txn);
 
-        free(it);
+        free (it);
         it = NULL;
     }
 }
@@ -727,56 +728,56 @@ LSUP_mdbstore_remove(
 
     if (store->default_ctx != NULL) {
         if (sc == NULL) sc = store->default_ctx;
-        ck = LSUP_sterm_to_key(sc);
+        ck = LSUP_sterm_to_key (sc);
     }
 
     MDB_txn *txn;
 
-    mdb_txn_begin(store->env, NULL, 0, &txn);
+    mdb_txn_begin (store->env, NULL, 0, &txn);
 
     MDB_cursor *dcur, *icur;
-    mdb_cursor_open(txn, store->dbi[IDX_SPO_C], &dcur);
-    mdb_cursor_open(txn, store->dbi[IDX_C_SPO], &icur);
+    mdb_cursor_open (txn, store->dbi[IDX_SPO_C], &dcur);
+    mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &icur);
 
     MDB_val spok_v, ck_v;
 
     spok_v.mv_size = TRP_KLEN;
     ck_v.mv_size = KLEN;
 
-    LSUP_MDBIterator *it = LSUP_mdbstore_lookup(store, sspo, sc, ct);
+    LSUP_MDBIterator *it = LSUP_mdbstore_lookup (store, sspo, sc, ct);
     if (UNLIKELY (!it)) return LSUP_DB_ERR;
 
-    while (mdbiter_next_key(it)) {
+    while (mdbiter_next_key (it)) {
         spok_v.mv_data = it->spok;
 
-        rc = mdb_cursor_get(dcur, &spok_v, &ck_v, MDB_GET_BOTH);
+        rc = mdb_cursor_get (dcur, &spok_v, &ck_v, MDB_GET_BOTH);
         if (rc == MDB_NOTFOUND) continue;
-        if (UNLIKELY(rc != MDB_SUCCESS)) goto _remove_abort;
+        if (UNLIKELY (rc != MDB_SUCCESS)) goto _remove_abort;
 
         // Delete spo:c entry.
-        mdb_cursor_del(dcur, 0);
+        mdb_cursor_del (dcur, 0);
 
         // Restore ck address after each delete.
         ck_v.mv_data = &ck;
 
         // Delete c::spo entry.
-        rc = mdb_cursor_get(icur, &ck_v, &spok_v, MDB_GET_BOTH);
+        rc = mdb_cursor_get (icur, &ck_v, &spok_v, MDB_GET_BOTH);
         if (rc == MDB_NOTFOUND) continue;
-        if (UNLIKELY(rc != MDB_SUCCESS)) goto _remove_abort;
+        if (UNLIKELY (rc != MDB_SUCCESS)) goto _remove_abort;
 
-        mdb_cursor_del(icur, 0);
+        mdb_cursor_del (icur, 0);
         spok_v.mv_data = it->spok;
 
         // If there are no more contexts associated with this triple,
         // remove from indices.
-        rc = mdb_cursor_get(dcur, &spok_v, NULL, MDB_SET);
+        rc = mdb_cursor_get (dcur, &spok_v, NULL, MDB_SET);
         if (rc == MDB_SUCCESS) continue;
-        if (UNLIKELY(rc != MDB_NOTFOUND)) goto _remove_abort;
+        if (UNLIKELY (rc != MDB_NOTFOUND)) goto _remove_abort;
 
-        index_triple(store, OP_REMOVE, it->spok, ck);
+        index_triple (store, OP_REMOVE, it->spok, ck);
     }
 
-    if(UNLIKELY(mdb_txn_commit(txn) != MDB_SUCCESS)) {
+    if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
         rc = LSUP_TXN_ERR;
         goto _remove_abort;
     }
@@ -784,7 +785,7 @@ LSUP_mdbstore_remove(
     return rc;
 
 _remove_abort:
-    mdb_txn_abort(txn);
+    mdb_txn_abort (txn);
 
     return rc;
 }
@@ -808,7 +809,7 @@ index_triple(
     LSUP_rc rc = LSUP_NOACTION;
     MDB_val v1, v2;
 
-    printf("Indexing triple: %lx %lx %lx\n", spok[0], spok[1], spok[2]);
+    printf ("Indexing triple: %lx %lx %lx\n", spok[0], spok[1], spok[2]);
 
     // Index c:spo.
     if (op == OP_REMOVE) {
@@ -820,15 +821,15 @@ index_triple(
             v2.mv_data = spok;
             v2.mv_size = TRP_KLEN;
 
-            mdb_cursor_open(store->txn, store->dbi[IDX_C_SPO], &cur);
-            if (mdb_cursor_get(cur, &v1, &v2, MDB_GET_BOTH) == MDB_SUCCESS) {
+            mdb_cursor_open (store->txn, store->dbi[IDX_C_SPO], &cur);
+            if (mdb_cursor_get (cur, &v1, &v2, MDB_GET_BOTH) == MDB_SUCCESS) {
                 db_rc = mdb_cursor_del (cur, 0);
                 if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
 
                 rc = LSUP_OK;
             }
 
-            mdb_cursor_close(cur);
+            mdb_cursor_close (cur);
         }
 
     } else if (op == OP_ADD) {
@@ -869,10 +870,10 @@ index_triple(
             mdb_cursor_open(
                     store->txn, store->dbi[lookup_indices[i]], &cur1);
 
-            db_rc = mdb_cursor_get(cur1, &v1, &v2, MDB_GET_BOTH);
-            if (db_rc == MDB_SUCCESS) mdb_cursor_del(cur1, 0);
+            db_rc = mdb_cursor_get (cur1, &v1, &v2, MDB_GET_BOTH);
+            if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur1, 0);
 
-            mdb_cursor_close(cur1);
+            mdb_cursor_close (cur1);
 
             // Restore pointers invalidated after delete.
             v1.mv_data = spok + i;
@@ -881,32 +882,32 @@ index_triple(
             mdb_cursor_open(
                     store->txn, store->dbi[lookup_indices[i + 3]], &cur2);
 
-            db_rc = mdb_cursor_get(cur2, &v2, &v1, MDB_GET_BOTH);
-            if (db_rc == MDB_SUCCESS) mdb_cursor_del(cur2, 0);
+            db_rc = mdb_cursor_get (cur2, &v2, &v1, MDB_GET_BOTH);
+            if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur2, 0);
             // TODO error handling.
             rc = LSUP_OK;
 
-            mdb_cursor_close(cur2);
+            mdb_cursor_close (cur2);
 
         } else { // OP_ADD is guaranteed.
             // 1-bound index.
-            TRACE("Indexing in %s: ", db_labels[lookup_indices[i]]);
+            TRACE ("Indexing in %s: ", db_labels[lookup_indices[i]]);
             TRACE(
                     "%lx: %lx %lx\n", *(size_t*)(v1.mv_data),
                     *(size_t*)(v2.mv_data), *(size_t*)(v2.mv_data) + 1);
 
-            db_rc = mdb_put(store->txn, db1, &v1, &v2, MDB_NODUPDATA);
+            db_rc = mdb_put (store->txn, db1, &v1, &v2, MDB_NODUPDATA);
 
             if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
             else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
 
             // 2-bound index.
-            TRACE("Indexing in %s: ", db_labels[lookup_indices[i + 3]]);
+            TRACE ("Indexing in %s: ", db_labels[lookup_indices[i + 3]]);
             TRACE(
                     "%lx %lx: %lx\n", *(size_t*)(v2.mv_data),
                     *(size_t*)(v2.mv_data) + 1, *(size_t*)(v1.mv_data));
 
-            db_rc = mdb_put(store->txn, db2, &v2, &v1, MDB_NODUPDATA);
+            db_rc = mdb_put (store->txn, db2, &v2, &v1, MDB_NODUPDATA);
 
             if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
             else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
@@ -924,11 +925,11 @@ index_triple(
  * Cursor: spo:c
  */
 inline static void
-it_next_0bound(MDBIterator *it)
+it_next_0bound (MDBIterator *it)
 {
-    memcpy(it->spok, it->data.mv_data, sizeof(LSUP_TripleKey));
+    memcpy (it->spok, it->data.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, NULL, MDB_NEXT);
 }
 
 
@@ -939,7 +940,7 @@ it_next_0bound(MDBIterator *it)
  * Cursor: s:po, p:so, or o:sp.
  */
 inline static void
-it_next_1bound(MDBIterator *it)
+it_next_1bound (MDBIterator *it)
 {
     LSUP_DoubleKey *lu_dset = it->data.mv_data;
 
@@ -952,18 +953,18 @@ it_next_1bound(MDBIterator *it)
             it->spok[0], it->spok[1], it->spok[2]);
 
     // Ensure next block within the same page is not beyond the last.
-    if(it->i < it->data.mv_size / DBL_KLEN - 1) {
+    if (it->i < it->data.mv_size / DBL_KLEN - 1) {
         it->i ++;
-        TRACE("Increasing page cursor to %lu.", it->i);
-        TRACE("it->rc: %d", it->rc);
+        TRACE ("Increasing page cursor to %lu.", it->i);
+        TRACE ("it->rc: %d", it->rc);
 
     } else {
         // If the last block in the page is being yielded,
         // move cursor to beginning of next page.
         it->i = 0;
-        TRACE("Reset page cursor to %lu.", it->i);
-        it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
-        TRACE("it->rc: %d", it->rc);
+        TRACE ("Reset page cursor to %lu.", it->i);
+        it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
+        TRACE ("it->rc: %d", it->rc);
     }
 }
 
@@ -975,7 +976,7 @@ it_next_1bound(MDBIterator *it)
  * Cursor: po:s, so:p, or sp:o.
  */
 inline static void
-it_next_2bound(MDBIterator *it)
+it_next_2bound (MDBIterator *it)
 {
     LSUP_Key *lu_dset = it->data.mv_data;
 
@@ -984,13 +985,13 @@ it_next_2bound(MDBIterator *it)
     it->spok[it->term_order[2]] = lu_dset[it->i];
 
     // Ensure next block within the same page is not beyond the last.
-    if(it->i < it->data.mv_size / KLEN - 1)
+    if (it->i < it->data.mv_size / KLEN - 1)
         it->i ++;
     else {
         // If the last block in the page is being yielded,
         // move cursor to beginning of next page.
         it->i = 0;
-        it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
+        it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
     }
 }
 
@@ -1002,7 +1003,7 @@ it_next_2bound(MDBIterator *it)
  * already MDB_NOTFOUND and this function will not be called.
  */
 inline static void
-it_next_3bound(MDBIterator *it)
+it_next_3bound (MDBIterator *it)
 {
     it->rc = MDB_NOTFOUND;
 }
@@ -1011,43 +1012,43 @@ it_next_3bound(MDBIterator *it)
 /* * * Term-specific lookups. * * */
 
 inline static LSUP_rc
-lookup_0bound(MDBStore *store, MDBIterator *it, size_t *ct)
+lookup_0bound (MDBStore *store, MDBIterator *it, size_t *ct)
 {
     if (store->txn) it->txn = store->txn;
     else {
-        it->rc = mdb_txn_begin(store->env, NULL, MDB_RDONLY, &it->txn);
+        it->rc = mdb_txn_begin (store->env, NULL, MDB_RDONLY, &it->txn);
         if (it->rc != MDB_SUCCESS) abort(); // TODO handle error
     }
 
-    if(ct) {
-        if(it->ck != NULL_KEY) {
+    if (ct) {
+        if (it->ck != NULL_KEY) {
             // Look up by given context.
-            it->rc = mdb_cursor_open(it->txn, store->dbi[IDX_C_SPO], &it->cur);
+            it->rc = mdb_cursor_open (it->txn, store->dbi[IDX_C_SPO], &it->cur);
 
             it->key.mv_data = &it->ck;
             it->key.mv_size = KLEN;
 
-            it->rc = mdb_cursor_get(it->cur, &it->key, NULL, MDB_SET);
-            if (it->rc == MDB_SUCCESS) mdb_cursor_count(it->cur, ct);
+            it->rc = mdb_cursor_get (it->cur, &it->key, NULL, MDB_SET);
+            if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
 
-            mdb_cursor_close(it->cur);
+            mdb_cursor_close (it->cur);
 
         } else {
             // Look up all contexts.
             MDB_stat stat;
-            mdb_stat(it->txn, store->dbi[IDX_S_PO], &stat);
+            mdb_stat (it->txn, store->dbi[IDX_S_PO], &stat);
 
             *ct = stat.ms_entries;
         }
     }
 
-    mdb_cursor_open(it->txn, store->dbi[IDX_SPO_C], &it->cur);
+    mdb_cursor_open (it->txn, store->dbi[IDX_SPO_C], &it->cur);
 
-    it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_FIRST);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_FIRST);
     it->iter_op_fn = it_next_0bound;
 
     if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
-        fprintf(stderr, "Database error: %s", mdb_strerror(it->rc));
+        fprintf (stderr, "Database error: %s", mdb_strerror (it->rc));
         return LSUP_DB_ERR;
     }
 
@@ -1056,66 +1057,66 @@ lookup_0bound(MDBStore *store, MDBIterator *it, size_t *ct)
 
 
 inline static LSUP_rc
-lookup_1bound(MDBStore *store, uint8_t idx0, MDBIterator *it, size_t *ct)
+lookup_1bound (MDBStore *store, uint8_t idx0, MDBIterator *it, size_t *ct)
 {
     it->term_order = (const uint8_t*)lookup_ordering_1bound[idx0];
 
-    TRACE("Looking up 1 bound term: %lu\n", it->luk[0]);
+    TRACE ("Looking up 1 bound term: %lu\n", it->luk[0]);
 
-    if(!it->txn) {
-        if(store->txn) it->txn = store->txn;
+    if (!it->txn) {
+        if (store->txn) it->txn = store->txn;
         else {
-            it->rc = mdb_txn_begin(store->env, NULL, MDB_RDONLY, &it->txn);
+            it->rc = mdb_txn_begin (store->env, NULL, MDB_RDONLY, &it->txn);
             if (it->rc != MDB_SUCCESS) abort();
         }
     }
 
-    mdb_cursor_open(it->txn, store->dbi[lookup_indices[idx0]], &it->cur);
+    mdb_cursor_open (it->txn, store->dbi[lookup_indices[idx0]], &it->cur);
 
     it->key.mv_data = it->luk;
     it->key.mv_size = KLEN;
 
-    if(ct) {
+    if (ct) {
         // If a context is specified, the only way to count triples matching
         // the context is to loop over them.
         if (it->ck != NULL_KEY) {
             MDBIterator *ct_it;
-            CRITICAL(ct_it = malloc(sizeof(MDBIterator)));
+            CRITICAL (ct_it = malloc (sizeof (MDBIterator)));
 
             ct_it->luk[0] = it->luk[0];
             LSUP_TripleKey ct_spok;
-            memcpy(ct_it->spok, ct_spok, sizeof(LSUP_TripleKey));
+            memcpy (ct_it->spok, ct_spok, sizeof (LSUP_TripleKey));
             ct_it->ck = it->ck;
             ct_it->store = it->store;
             ct_it->txn = it->txn;
             ct_it->key = it->key;
             ct_it->data = it->data;
             ct_it->i = 0;
-            lookup_1bound(store, idx0, ct_it, NULL);
+            lookup_1bound (store, idx0, ct_it, NULL);
 
-            while (LSUP_mdbiter_next(ct_it, NULL) != LSUP_END) {
+            while (LSUP_mdbiter_next (ct_it, NULL) != LSUP_END) {
                 ct[0] ++;
-                TRACE("Counter increased to %lu.", *ct);
+                TRACE ("Counter increased to %lu.", *ct);
             }
 
-            mdb_cursor_close(ct_it->cur);
-            free(ct_it);
+            mdb_cursor_close (ct_it->cur);
+            free (ct_it);
 
         } else {
-            it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_SET);
-            if (it->rc == MDB_SUCCESS) mdb_cursor_count(it->cur, ct);
+            it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
+            if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
         }
     }
 
     it->i = 0;
     it->iter_op_fn = it_next_1bound;
 
-    it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_SET);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
     if (it->rc == MDB_SUCCESS)
-        it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
+        it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
 
     if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
-        fprintf(stderr, "Database error: %s", mdb_strerror(it->rc));
+        fprintf (stderr, "Database error: %s", mdb_strerror (it->rc));
         return LSUP_DB_ERR;
     }
 
@@ -1132,7 +1133,7 @@ lookup_2bound(
     MDB_dbi dbi = 0;
 
     // Establish lookup ordering with some awkward offset math.
-    for(int i = 0; i < 3; i++) {
+    for (int i = 0; i < 3; i++) {
         if (
             (
                 idx0 == lookup_ordering_2bound[i][0] &&
@@ -1171,10 +1172,10 @@ lookup_2bound(
     luk[luk1_offset] = it->luk[0];
     luk[luk2_offset] = it->luk[1];
 
-    if(!it->txn) {
-        if(store->txn) it->txn = store->txn;
+    if (!it->txn) {
+        if (store->txn) it->txn = store->txn;
         else {
-            it->rc = mdb_txn_begin(store->env, NULL, MDB_RDONLY, &it->txn);
+            it->rc = mdb_txn_begin (store->env, NULL, MDB_RDONLY, &it->txn);
             if (it->rc != MDB_SUCCESS) abort();
         }
     }
@@ -1182,46 +1183,46 @@ lookup_2bound(
     it->key.mv_data = luk;
     it->key.mv_size = DBL_KLEN;
 
-    mdb_cursor_open(it->txn, dbi, &it->cur);
-    it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_SET);
+    mdb_cursor_open (it->txn, dbi, &it->cur);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
 
-    if(ct) {
+    if (ct) {
         // If a context is specified, the only way to count triples matching
         // the context is to loop over them.
         if (it->ck != NULL_KEY) {
             MDBIterator *ct_it;
-            CRITICAL(ct_it = malloc(sizeof(MDBIterator)));
+            CRITICAL (ct_it = malloc (sizeof (MDBIterator)));
 
             ct_it->luk[0] = it->luk[0];
             ct_it->luk[1] = it->luk[1];
             LSUP_TripleKey ct_spok;
-            memcpy(ct_it->spok, ct_spok, sizeof(LSUP_TripleKey));
+            memcpy (ct_it->spok, ct_spok, sizeof (LSUP_TripleKey));
             ct_it->ck = it->ck;
             ct_it->store = it->store;
             ct_it->txn = it->txn;
-            lookup_2bound(store, idx0, idx1, ct_it, NULL);
+            lookup_2bound (store, idx0, idx1, ct_it, NULL);
 
-            while (LSUP_mdbiter_next(ct_it, NULL) != LSUP_END) {
+            while (LSUP_mdbiter_next (ct_it, NULL) != LSUP_END) {
                 ct[0] ++;
             }
-            if (ct_it->cur) mdb_cursor_close(ct_it->cur);
-            free(ct_it);
+            if (ct_it->cur) mdb_cursor_close (ct_it->cur);
+            free (ct_it);
 
         } else {
-            it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_SET);
-            if (it->rc == MDB_SUCCESS) mdb_cursor_count(it->cur, ct);
+            it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
+            if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
         }
     }
 
     it->i = 0;
     it->iter_op_fn = it_next_2bound;
 
-    it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_SET);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
     if (it->rc == MDB_SUCCESS)
-        it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
+        it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
 
     if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
-        fprintf(stderr, "Database error: %s", mdb_strerror(it->rc));
+        fprintf (stderr, "Database error: %s", mdb_strerror (it->rc));
         return LSUP_DB_ERR;
     }
 
@@ -1230,43 +1231,43 @@ lookup_2bound(
 
 
 inline static LSUP_rc
-lookup_3bound(MDBStore *store, MDBIterator *it, size_t *ct)
+lookup_3bound (MDBStore *store, MDBIterator *it, size_t *ct)
 {
     TRACE(
             "Looking up 3 bound: {%lx, %lx, %lx}",
             it->luk[0], it->luk[1], it->luk[2]);
 
-    if(store->txn) it->txn = store->txn;
-    else mdb_txn_begin(store->env, NULL, MDB_RDONLY, &it->txn);
+    if (store->txn) it->txn = store->txn;
+    else mdb_txn_begin (store->env, NULL, MDB_RDONLY, &it->txn);
 
     it->key.mv_data = it->luk;
 
-    if(it->ck != NULL_KEY) {
-        it->rc = mdb_cursor_open(it->txn, store->dbi[IDX_SPO_C], &it->cur);
+    if (it->ck != NULL_KEY) {
+        it->rc = mdb_cursor_open (it->txn, store->dbi[IDX_SPO_C], &it->cur);
 
         it->key.mv_size = TRP_KLEN;
         it->data.mv_data = &it->ck;
         it->data.mv_size = KLEN;
 
     } else {
-        it->rc = mdb_cursor_open(it->txn, store->dbi[IDX_S_PO], &it->cur);
+        it->rc = mdb_cursor_open (it->txn, store->dbi[IDX_S_PO], &it->cur);
 
         it->key.mv_size = KLEN;
         it->data.mv_data = it->luk + 1;
         it->data.mv_size = DBL_KLEN;
     }
 
-    it->rc = mdb_cursor_get(it->cur, &it->key, &it->data, MDB_GET_BOTH);
+    it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_BOTH);
 
-    mdb_cursor_close(it->cur);
+    mdb_cursor_close (it->cur);
 
-    if(ct && it->rc == MDB_SUCCESS) *ct = 1;
+    if (ct && it->rc == MDB_SUCCESS) *ct = 1;
 
     it->iter_op_fn = it_next_3bound;
-    memcpy(it->spok, it->luk, sizeof(LSUP_TripleKey));
+    memcpy (it->spok, it->luk, sizeof (LSUP_TripleKey));
 
     if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
-        fprintf(stderr, "Database error: %s", mdb_strerror(it->rc));
+        fprintf (stderr, "Database error: %s", mdb_strerror (it->rc));
         return LSUP_DB_ERR;
     }
 

+ 48 - 45
src/term.c

@@ -1,6 +1,6 @@
 #include "term.h"
 
-#define NLEN(str)   (str) == NULL ? 0 : strlen((str))
+#define NLEN(str)   (str) == NULL ? 0 : strlen ((str))
 
 static regex_t ptn;
 static bool ptn_init = false;
@@ -9,23 +9,23 @@ static bool ptn_init = false;
 /* Global inline prototypes. */
 
 LSUP_Term *LSUP_uri_new (const char *data);
-LSUP_rc LSUP_uri_init(LSUP_Term *term, const char *data);
-LSUP_rc LSUP_uri_reset (LSUP_Term *term, const char *data);
+LSUP_rc LSUP_uri_init (LSUP_Term *term, const char *data);
 
 
 /**
  * Free global regex struct. Register with atexit().
  */
-void term_cleanup() { if (ptn_init) regfree(&ptn); }
+void term_cleanup() { if (ptn_init) regfree (&ptn); }
 
 
 LSUP_Term *
 LSUP_term_new (
         LSUP_term_type type, const char *data, char *datatype, char *lang)
 {
-
     LSUP_Term *term;
-    CRITICAL (term = malloc (sizeof (*term)));
+    term = malloc (sizeof (*term));
+    if (UNLIKELY (!term)) return NULL;
+
     term->data = NULL;
     term->datatype = NULL;
 
@@ -40,7 +40,7 @@ LSUP_term_new (
 
 
 LSUP_Term *
-LSUP_term_new_from_buffer(const LSUP_Buffer *sterm)
+LSUP_term_new_from_buffer (const LSUP_Buffer *sterm)
 {
     LSUP_Term *term;
     CRITICAL (term = malloc (sizeof (*term)));
@@ -54,10 +54,10 @@ LSUP_term_new_from_buffer(const LSUP_Buffer *sterm)
 
 
 LSUP_Buffer *
-LSUP_buffer_new_from_term(const LSUP_Term *term)
+LSUP_buffer_new_from_term (const LSUP_Term *term)
 {
     LSUP_Buffer *sterm;
-    CRITICAL(sterm = malloc (sizeof (*sterm)));
+    CRITICAL (sterm = malloc (sizeof (*sterm)));
     sterm->addr = NULL;
 
     if (LSUP_term_serialize (term, sterm) != LSUP_OK) {
@@ -78,35 +78,38 @@ LSUP_term_init(
     if (data == NULL) return LSUP_VALUE_ERR;
 
     if (term->type == LSUP_TERM_URI) {
-        if (UNLIKELY(!ptn_init)) {
-            assert (regcomp(&ptn, URI_REGEX_STR, REG_EXTENDED) == 0);
+        if (UNLIKELY (!ptn_init)) {
+            assert (regcomp (&ptn, URI_REGEX_STR, REG_EXTENDED) == 0);
             ptn_init = true;
-            atexit(term_cleanup);
+            atexit (term_cleanup);
         }
 
-        if (regexec(&ptn, data, 0, NULL, 0) != 0) {
-            fprintf(stderr, "Error matching URI pattern.\n");
+        if (regexec (&ptn, data, 0, NULL, 0) != 0) {
+            fprintf (stderr, "Error matching URI pattern.\n");
 
             return LSUP_VALUE_ERR;
         }
     }
 
-    CRITICAL (term->data = realloc (term->data, strlen(data) + 1));
-    strcpy(term->data, data);
+    char *data_tmp = realloc (term->data, strlen (data) + 1);
+    if (UNLIKELY (!data_tmp)) return ENOMEM;
+    term->data = data_tmp;
+    strcpy (term->data, data);
 
     if (datatype) {
-        CRITICAL (term->datatype = realloc(
-                    term->datatype, strlen(datatype) + 1));
-        strcpy(term->datatype, datatype);
+        data_tmp = realloc (term->datatype, strlen (datatype) + 1);
+        if (UNLIKELY (!data_tmp)) return ENOMEM;
+        term->datatype = data_tmp;
+        strcpy (term->datatype, datatype);
     } else {
         term->datatype = NULL;
     }
     if (lang) {
         // TODO validate language and country code
         //char lsize = 5 ? lang[2] == "-" : 2;
-        memcpy(term->lang, lang, LANG_SIZE);
+        memcpy (term->lang, lang, LANG_SIZE);
     } else {
-        memset(term->lang, 0, LANG_SIZE);
+        memset (term->lang, 0, LANG_SIZE);
     }
 
     return LSUP_OK;
@@ -116,11 +119,11 @@ LSUP_term_init(
 /*
  * This function allocates and returns the following byte sequence:
  *
- * - `sizeof(char)` bytes for the term type;
+ * - `sizeof (char)` bytes for the term type;
  * - `LANG_SIZE` bytes for the language tag;
  * - Arbitrary bytes with NUL-terminated strings for data and datatype.
  *
- * The index for `data` is consistently `LANG_SIZE + sizeof(char)`. The
+ * The index for `data` is consistently `LANG_SIZE + sizeof (char)`. The
  * index for `datatype` is found by the terminating NULL for `data`.
  *
  * Serialized representations of some RDF terms:
@@ -153,7 +156,7 @@ LSUP_term_init(
  * type   data        datatype         lang
  */
 LSUP_rc
-LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
+LSUP_term_serialize (const LSUP_Term *term, LSUP_Buffer *sterm)
 {
     size_t size, data_len, datatype_len,
            data_idx, datatype_idx, lang_idx;
@@ -176,7 +179,7 @@ LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
         }
     }
 
-    //TRACE("Serialized term size: %lu", size);
+    //TRACE ("Serialized term size: %lu", size);
     LSUP_buffer_init (sterm, size, NULL);
 
     // Copy type.
@@ -198,7 +201,7 @@ LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
 
 
 LSUP_rc
-LSUP_term_deserialize(const LSUP_Buffer *sterm, LSUP_Term *term)
+LSUP_term_deserialize (const LSUP_Buffer *sterm, LSUP_Term *term)
 {
     size_t cur;
     char *data, *datatype = NULL;
@@ -208,28 +211,28 @@ LSUP_term_deserialize(const LSUP_Buffer *sterm, LSUP_Term *term)
 
     cur = 1;
     data = (char*)sterm->addr + cur;
-    cur += strlen(data) + 1;
+    cur += strlen (data) + 1;
 
     if (type == LSUP_TERM_LITERAL && cur < sterm->size) {
         datatype = (char*)sterm->addr + cur;
-        cur += strlen(datatype) + 1;
-        if (strlen(datatype) == 0)
+        cur += strlen (datatype) + 1;
+        if (strlen (datatype) == 0)
             datatype = NULL;
 
         if (cur < sterm->size)
-            strcpy(lang, sterm->addr + cur);
+            strcpy (lang, sterm->addr + cur);
     }
 
-    return LSUP_term_init(term, type, data, datatype, lang);
+    return LSUP_term_init (term, type, data, datatype, lang);
 }
 
 
-bool LSUP_term_equals(const LSUP_Term *term1, const LSUP_Term *term2)
+bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2)
 {
     if (term1->type != term2->type)
         return false;
 
-    if (strcmp(term1->data, term2->data) != 0)
+    if (strcmp (term1->data, term2->data) != 0)
         return false;
 
     if (term1->type == LSUP_TERM_LITERAL) {
@@ -238,7 +241,7 @@ bool LSUP_term_equals(const LSUP_Term *term1, const LSUP_Term *term2)
 
         if (
                 term1->datatype != NULL &&
-                strcmp(term1->datatype, term2->datatype) != 0)
+                strcmp (term1->datatype, term2->datatype) != 0)
             return false;
 
         if ((term1->lang == NULL) != (term2->lang == NULL)) // XOR
@@ -246,7 +249,7 @@ bool LSUP_term_equals(const LSUP_Term *term1, const LSUP_Term *term2)
 
         if (
                 term1->lang != NULL &&
-                strcmp(term1->lang, term2->lang) != 0)
+                strcmp (term1->lang, term2->lang) != 0)
             return false;
     }
 
@@ -254,25 +257,25 @@ bool LSUP_term_equals(const LSUP_Term *term1, const LSUP_Term *term2)
 }
 
 
-void LSUP_term_done(LSUP_Term *term)
+void LSUP_term_done (LSUP_Term *term)
 {
-    if (LIKELY(term->data != NULL)) {
-        free(term->data);
+    if (LIKELY (term->data != NULL)) {
+        free (term->data);
         term->data = NULL;
     }
 
     if (term->datatype != NULL) {
-        free(term->datatype);
+        free (term->datatype);
         term->datatype = NULL;
     }
 }
 
 
-void LSUP_term_free(LSUP_Term *term)
+void LSUP_term_free (LSUP_Term *term)
 {
-    if (LIKELY(term != NULL)) {
-        LSUP_term_done(term);
-        free(term);
+    if (LIKELY (term != NULL)) {
+        LSUP_term_done (term);
+        free (term);
         term = NULL;
     }
 }
@@ -280,7 +283,7 @@ void LSUP_term_free(LSUP_Term *term)
 
 // Extern inline functions.
 
-LSUP_Key LSUP_sterm_to_key(const LSUP_Buffer *sterm);
+LSUP_Key LSUP_sterm_to_key (const LSUP_Buffer *sterm);
 
-LSUP_Key LSUP_term_to_key(const LSUP_Term *term);
+LSUP_Key LSUP_term_to_key (const LSUP_Term *term);
 

+ 72 - 43
src/triple.c

@@ -6,35 +6,37 @@ LSUP_Buffer *LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n);
 
 
 LSUP_Triple *
-LSUP_triple_new()
+LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
 {
     LSUP_Triple *spo = malloc (sizeof (*spo));
     if (!spo) return NULL;
 
-    spo->s = malloc (sizeof (LSUP_Term));
-    spo->p = malloc (sizeof (LSUP_Term));
-    spo->o = malloc (sizeof (LSUP_Term));
+    if (UNLIKELY (LSUP_triple_init (spo, s, p, o))) {
+        free (spo);
+        return NULL;
+    }
 
     return spo;
 }
 
 
 LSUP_SerTriple *
-LSUP_striple_new()
+LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o)
 {
     LSUP_SerTriple *sspo = malloc (sizeof (*sspo));
     if (!sspo) return NULL;
 
-    sspo->s = malloc (sizeof (LSUP_Buffer));
-    sspo->p = malloc (sizeof (LSUP_Buffer));
-    sspo->o = malloc (sizeof (LSUP_Buffer));
+    if (UNLIKELY (LSUP_striple_init (sspo, s, p, o))) {
+        free (sspo);
+        return NULL;
+    }
 
     return sspo;
 }
 
 
 LSUP_Triple *
-LSUP_triple_new_from_striple(const LSUP_SerTriple *sspo)
+LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo)
 {
     LSUP_Triple *spo = malloc (sizeof (*spo));
     if (!spo) return NULL;
@@ -48,7 +50,7 @@ LSUP_triple_new_from_striple(const LSUP_SerTriple *sspo)
 
 
 LSUP_SerTriple *
-LSUP_striple_new_from_triple(const LSUP_Triple *spo)
+LSUP_striple_new_from_triple (const LSUP_Triple *spo)
 {
     LSUP_SerTriple *sspo = malloc (sizeof (*sspo));
     if (!sspo) return NULL;
@@ -62,80 +64,107 @@ LSUP_striple_new_from_triple(const LSUP_Triple *spo)
 
 
 LSUP_rc
-LSUP_triple_serialize(const LSUP_Triple *spo, LSUP_SerTriple *sspo)
+LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
+{
+    spo->s = s;
+    spo->p = p;
+    spo->o = o;
+
+    return LSUP_OK;
+}
+
+
+LSUP_rc
+LSUP_striple_init (
+        LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o)
+{
+    sspo->s = s;
+    sspo->p = p;
+    sspo->o = o;
+
+    return LSUP_OK;
+}
+
+
+LSUP_rc
+LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo)
 {
     LSUP_rc rc;
 
-    rc = LSUP_term_serialize(spo->s, sspo->s);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
-    rc = LSUP_term_serialize(spo->p, sspo->p);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
-    rc = LSUP_term_serialize(spo->o, sspo->o);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
+    rc = LSUP_term_serialize (spo->s, sspo->s);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
+    rc = LSUP_term_serialize (spo->p, sspo->p);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
+    rc = LSUP_term_serialize (spo->o, sspo->o);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
 
     return LSUP_OK;
 }
 
 
 LSUP_rc
-LSUP_triple_deserialize(const LSUP_SerTriple *sspo, LSUP_Triple *spo)
+LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo)
 {
     LSUP_rc rc;
 
-    rc = LSUP_term_deserialize(sspo->s, spo->s);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
-    rc = LSUP_term_deserialize(sspo->p, spo->p);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
-    rc = LSUP_term_deserialize(sspo->o, spo->o);
-    if (UNLIKELY(rc != LSUP_OK)) return rc;
+    if (!spo->s) spo->s = malloc (sizeof (*spo->s));
+    if (!spo->p) spo->p = malloc (sizeof (*spo->p));
+    if (!spo->o) spo->o = malloc (sizeof (*spo->o));
+
+    rc = LSUP_term_deserialize (sspo->s, spo->s);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
+    rc = LSUP_term_deserialize (sspo->p, spo->p);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
+    rc = LSUP_term_deserialize (sspo->o, spo->o);
+    if (UNLIKELY (rc != LSUP_OK)) return rc;
 
     return LSUP_OK;
 }
 
 
 void
-LSUP_triple_done(LSUP_Triple *spo)
+LSUP_triple_done (LSUP_Triple *spo)
 {
-    if (UNLIKELY(!spo)) return;
+    if (UNLIKELY (!spo)) return;
 
-    LSUP_term_done(spo->s);
-    LSUP_term_done(spo->p);
-    LSUP_term_done(spo->o);
+    LSUP_term_done (spo->s);
+    LSUP_term_done (spo->p);
+    LSUP_term_done (spo->o);
 }
 
 
 void
-LSUP_striple_done(LSUP_SerTriple *sspo)
+LSUP_striple_done (LSUP_SerTriple *sspo)
 {
-    if (UNLIKELY(!sspo)) return;
+    if (UNLIKELY (!sspo)) return;
 
-    LSUP_buffer_done(sspo->s);
-    LSUP_buffer_done(sspo->p);
-    LSUP_buffer_done(sspo->o);
+    LSUP_buffer_done (sspo->s);
+    LSUP_buffer_done (sspo->p);
+    LSUP_buffer_done (sspo->o);
 }
 
 
 void
 LSUP_triple_free (LSUP_Triple *spo)
 {
-    if (UNLIKELY(!spo)) return;
+    if (UNLIKELY (!spo)) return;
 
-    LSUP_term_free(spo->s);
-    LSUP_term_free(spo->p);
-    LSUP_term_free(spo->o);
+    LSUP_term_free (spo->s);
+    LSUP_term_free (spo->p);
+    LSUP_term_free (spo->o);
 
     free (spo);
 }
 
 
 void
-LSUP_striple_free(LSUP_SerTriple *sspo)
+LSUP_striple_free (LSUP_SerTriple *sspo)
 {
-    if (UNLIKELY(!sspo)) return;
+    if (UNLIKELY (!sspo)) return;
 
-    LSUP_buffer_free(sspo->s);
-    LSUP_buffer_free(sspo->p);
-    LSUP_buffer_free(sspo->o);
+    LSUP_buffer_free (sspo->s);
+    LSUP_buffer_free (sspo->p);
+    LSUP_buffer_free (sspo->o);
 
     free (sspo);
 }

+ 5 - 0
test.c

@@ -4,6 +4,11 @@
 
 int main(int argc, char **argv) {
 
+    // Set env variable to test path.
+    putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_test_mdb");
+    // Clear out database from previous test.
+    rm_r (getenv ("LSUP_MDB_STORE_PATH"));
+
     int result = (
         term_tests() |
         store_mdb_test() |

+ 45 - 45
test/assets.h

@@ -8,76 +8,76 @@
 LSUP_Triple *create_triples()
 {
     LSUP_Triple *trp;
-    CRITICAL(trp = malloc(NUM_TRP * sizeof(LSUP_Triple)));
+    CRITICAL (trp = malloc (NUM_TRP * sizeof (LSUP_Triple)));
 
     // These constitute overall 10 individual triples, 8 unique.
 
     /*
-    LSUP_uri_init(&terms[0][0], "urn:s:0");
-    LSUP_uri_init(&terms[0][1], "urn:p:0");
-    LSUP_uri_init(&terms[0][2], "urn:o:0");
+    LSUP_uri_init (&terms[0][0], "urn:s:0");
+    LSUP_uri_init (&terms[0][1], "urn:p:0");
+    LSUP_uri_init (&terms[0][2], "urn:o:0");
     trp[0] = {terms[1][0], terms[1][1], terms[1][2]};
 
-    LSUP_uri_init(&terms[1][0], "urn:s:1");
-    LSUP_uri_init(&terms[1][1], "urn:p:1");
-    LSUP_uri_init(&terms[1][2], "urn:o:1");
+    LSUP_uri_init (&terms[1][0], "urn:s:1");
+    LSUP_uri_init (&terms[1][1], "urn:p:1");
+    LSUP_uri_init (&terms[1][2], "urn:o:1");
     trp[1] = {terms[1][0], terms[1][1], terms[1][2]};
 
-    LSUP_uri_init(&terms[2][0], "urn:s:2");
-    LSUP_uri_init(&terms[2][1], "urn:p:2");
-    LSUP_uri_init(&terms[2][2], "urn:o:2");
+    LSUP_uri_init (&terms[2][0], "urn:s:2");
+    LSUP_uri_init (&terms[2][1], "urn:p:2");
+    LSUP_uri_init (&terms[2][2], "urn:o:2");
     trp[2] = {terms[2][0], terms[2][1], terms[2][2]};
 
-    LSUP_uri_init(&terms[3][0], "urn:s:0");
-    LSUP_uri_init(&terms[3][1], "urn:p:1");
-    LSUP_uri_init(&terms[3][2], "urn:o:2");
+    LSUP_uri_init (&terms[3][0], "urn:s:0");
+    LSUP_uri_init (&terms[3][1], "urn:p:1");
+    LSUP_uri_init (&terms[3][2], "urn:o:2");
     trp[3] = {terms[3][0], terms[3][1], terms[3][2]};
 
-    LSUP_uri_init(&terms[4][0], "urn:s:0");
-    LSUP_uri_init(&terms[4][1], "urn:p:2");
-    LSUP_term_init(&terms[4][2], LSUP_TERM_LITERAL, "String 1", NULL, NULL);
+    LSUP_uri_init (&terms[4][0], "urn:s:0");
+    LSUP_uri_init (&terms[4][1], "urn:p:2");
+    LSUP_term_init (&terms[4][2], LSUP_TERM_LITERAL, "String 1", NULL, NULL);
     trp[4] = {terms[4][0], terms[4][1], terms[4][2]};
 
-    LSUP_uri_init(&terms[5][0], "urn:s:0");
-    LSUP_uri_init(&terms[5][1], "urn:p:5");
+    LSUP_uri_init (&terms[5][0], "urn:s:0");
+    LSUP_uri_init (&terms[5][1], "urn:p:5");
     LSUP_term_init(
             &terms[5][2], LSUP_TERM_LITERAL, "String 1", "xsd:string", NULL);
     trp[5] = {terms[5][0], terms[5][1], terms[5][2]};
 
-    LSUP_uri_init(&terms[6][0], "urn:s:1");
-    LSUP_uri_init(&terms[6][1], "urn:p:6");
+    LSUP_uri_init (&terms[6][0], "urn:s:1");
+    LSUP_uri_init (&terms[6][1], "urn:p:6");
     LSUP_term_init(
             &terms[6][2], LSUP_TERM_LITERAL, "String 1", "xsd:string", "es-ES");
     trp[6] = {terms[6][0], terms[6][1], terms[6][2]};
     */
 
-    trp[0].s = LSUP_uri_new("urn:s:0"),
-    trp[0].p = LSUP_uri_new("urn:p:0"),
-    trp[0].o = LSUP_uri_new("urn:o:0"),
+    trp[0].s = LSUP_uri_new ("urn:s:0"),
+    trp[0].p = LSUP_uri_new ("urn:p:0"),
+    trp[0].o = LSUP_uri_new ("urn:o:0"),
 
-    trp[1].s = LSUP_uri_new("urn:s:1");
-    trp[1].p = LSUP_uri_new("urn:p:1");
-    trp[1].o = LSUP_uri_new("urn:o:1");
+    trp[1].s = LSUP_uri_new ("urn:s:1");
+    trp[1].p = LSUP_uri_new ("urn:p:1");
+    trp[1].o = LSUP_uri_new ("urn:o:1");
 
-    trp[2].s = LSUP_uri_new("urn:s:2");
-    trp[2].p = LSUP_uri_new("urn:p:2");
-    trp[2].o = LSUP_uri_new("urn:o:2");
+    trp[2].s = LSUP_uri_new ("urn:s:2");
+    trp[2].p = LSUP_uri_new ("urn:p:2");
+    trp[2].o = LSUP_uri_new ("urn:o:2");
 
-    trp[3].s = LSUP_uri_new("urn:s:0");
-    trp[3].p = LSUP_uri_new("urn:p:1");
-    trp[3].o = LSUP_uri_new("urn:o:2");
+    trp[3].s = LSUP_uri_new ("urn:s:0");
+    trp[3].p = LSUP_uri_new ("urn:p:1");
+    trp[3].o = LSUP_uri_new ("urn:o:2");
 
-    trp[4].s = LSUP_uri_new("urn:s:0");
-    trp[4].p = LSUP_uri_new("urn:p:2");
-    trp[4].o = LSUP_term_new(LSUP_TERM_LITERAL, "String 1", NULL, NULL);
+    trp[4].s = LSUP_uri_new ("urn:s:0");
+    trp[4].p = LSUP_uri_new ("urn:p:2");
+    trp[4].o = LSUP_term_new (LSUP_TERM_LITERAL, "String 1", NULL, NULL);
 
-    trp[5].s = LSUP_uri_new("urn:s:0");
-    trp[5].p = LSUP_uri_new("urn:p:5");
+    trp[5].s = LSUP_uri_new ("urn:s:0");
+    trp[5].p = LSUP_uri_new ("urn:p:5");
     trp[5].o = LSUP_term_new(
             LSUP_TERM_LITERAL, "String 1", "xsd:string", NULL);
 
-    trp[6].s = LSUP_uri_new("urn:s:1");
-    trp[6].p = LSUP_uri_new("urn:p:6");
+    trp[6].s = LSUP_uri_new ("urn:s:1");
+    trp[6].p = LSUP_uri_new ("urn:p:6");
     trp[6].o = LSUP_term_new(
             LSUP_TERM_LITERAL, "String 1", "xsd:string", "es-ES");
 
@@ -101,16 +101,16 @@ LSUP_Triple *create_triples()
 }
 
 
-void free_triples(LSUP_Triple *trp)
+void free_triples (LSUP_Triple *trp)
 {
     // Last three triples are second pointers.
-    for(int i=0; i < NUM_TRP - 3; i++) {
-        LSUP_term_free(trp[i].s);
-        LSUP_term_free(trp[i].p);
-        LSUP_term_free(trp[i].o);
+    for (int i=0; i < NUM_TRP - 3; i++) {
+        LSUP_term_free (trp[i].s);
+        LSUP_term_free (trp[i].p);
+        LSUP_term_free (trp[i].o);
     }
 
-    free(trp);
+    free (trp);
 }
 #endif
 

+ 27 - 27
test/test_graph.c

@@ -4,40 +4,40 @@
 
 /* TODO Not yet implemented. */
 static int __attribute__ ((unused))
-test_graph_mem_new()
+test_graph_mem_new ()
 {
     LSUP_Graph *gr;
-    gr = LSUP_graph_new(LSUP_STORE_MEM);
+    gr = LSUP_graph_new (LSUP_STORE_MEM);
 
-    EXPECT_PASS(LSUP_graph_set_uri(gr, "urn:gr:1"));
-    EXPECT_STR_EQ(LSUP_graph_uri(gr)->data, "urn:gr:1");
+    EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1"));
+    EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
 
-    EXPECT_PASS(LSUP_graph_resize(gr, 10));
-    EXPECT_INT_EQ(LSUP_graph_capacity(gr), 10);
+    EXPECT_PASS (LSUP_graph_resize (gr, 10));
+    EXPECT_INT_EQ (LSUP_graph_capacity (gr), 10);
 
-    ASSERT(strcmp(LSUP_graph_uri(gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
-    EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
+    ASSERT (strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
 
-    LSUP_graph_free(gr);
+    LSUP_graph_free (gr);
 
     return 0;
 }
 
 
 static int
-test_graph_mdb_new()
+test_graph_mdb_new ()
 {
     LSUP_Graph *gr;
-    gr = LSUP_graph_new(LSUP_STORE_MDB);
+    gr = LSUP_graph_new (LSUP_STORE_MDB);
     ASSERT (gr != NULL, "Error creating graph!");
 
-    EXPECT_PASS(LSUP_graph_set_uri(gr, "urn:gr:1"));
-    EXPECT_STR_EQ(LSUP_graph_uri(gr)->data, "urn:gr:1");
+    EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1"));
+    EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
 
-    ASSERT(strcmp(LSUP_graph_uri(gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
-    EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
+    ASSERT (strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
 
-    LSUP_graph_free(gr);
+    LSUP_graph_free (gr);
 
     return 0;
 }
@@ -48,22 +48,22 @@ test_graph_mdb_add()
 {
     LSUP_Triple *trp = create_triples();
 
-    LSUP_Graph *gr = LSUP_graph_new(LSUP_STORE_MDB);
+    LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MDB);
 
     size_t ct;
-    LSUP_graph_add_trp(gr, trp, NUM_TRP, &ct);
+    LSUP_graph_add_trp (gr, trp, NUM_TRP, &ct);
 
-    for (int i = 0; i < sizeof(trp); i++) {
-        printf("checking triple #%d... ", i);
-        ASSERT(LSUP_graph_contains(gr, trp + i), "Triple not in graph!");
-        printf("OK.\n");
+    for (int i = 0; i < sizeof (trp); i++) {
+        printf ("checking triple #%d... ", i);
+        ASSERT (LSUP_graph_contains (gr, trp + i), "Triple not in graph!");
+        printf ("OK.\n");
     }
 
-    free_triples(trp); // gr copied data.
+    free_triples (trp); // gr copied data.
 
-    EXPECT_INT_EQ(LSUP_graph_size(gr), 8);
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
-    LSUP_graph_free(gr);
+    LSUP_graph_free (gr);
 
     return 0;
 }
@@ -71,8 +71,8 @@ test_graph_mdb_add()
 
 int graph_tests()
 {
-    RUN(test_graph_mdb_new);
-    RUN(test_graph_mdb_add);
+    RUN (test_graph_mdb_new);
+    RUN (test_graph_mdb_add);
     return 0;
 }
 

+ 60 - 96
test/test_store_mdb.c

@@ -6,50 +6,28 @@
 static char *path = "/tmp/testdb";
 
 
-static void rmdb() {
-    char data_path[32], lock_path[32];
-    sprintf(data_path, "%s/data.mdb", path);
-    sprintf(lock_path, "%s/lock.mdb", path);
-
-    printf("Removing %s\n", data_path);
-    remove(data_path);
-    printf("Removing %s\n", lock_path);
-    remove(lock_path);
-    printf("Removing %s\n", path);
-    remove(path);
-}
-
-
 /** @brief Test triple store.
  */
 static int test_triple_store()
 {
-    rmdb();
-    EXPECT_PASS(LSUP_mdbstore_setup(&path));
+    EXPECT_PASS (LSUP_mdbstore_setup (&path, true));
 
-    LSUP_MDBStore *store = LSUP_mdbstore_new(path, NULL); // triple store.
-    ASSERT(store != NULL, "Error initializing store!");
+    LSUP_MDBStore *store = LSUP_mdbstore_new (path, NULL); // triple store.
+    ASSERT (store != NULL, "Error initializing store!");
 
     LSUP_Triple *trp = create_triples();
-    LSUP_Buffer sterms[NUM_TRP][3];
     LSUP_SerTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        ser_trp[i].s = sterms[i];
-        ser_trp[i].p = sterms[i] + 1;
-        ser_trp[i].o = sterms[i] + 2;
-        for (int j = 0; j < 3; j++) {
-            LSUP_term_serialize(
-                    LSUP_triple_pos(trp + i, j),
-                    LSUP_striple_pos(ser_trp + i, j));
-        }
+        LSUP_striple_init (ser_trp + i, BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
+        LSUP_triple_serialize (trp + i, ser_trp + i);
     }
 
     // Test adding.
     size_t ins;
-    EXPECT_PASS(LSUP_mdbstore_add(store, NULL, ser_trp, NUM_TRP, &ins));
-    EXPECT_INT_EQ(ins, 8);
-    EXPECT_INT_EQ(LSUP_mdbstore_size(store), 8);
+    EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, NUM_TRP, &ins));
+    EXPECT_INT_EQ (ins, 8);
+    EXPECT_INT_EQ (LSUP_mdbstore_size (store), 8);
 
     // Test lookups.
     LSUP_SerTriple lut[12] = {
@@ -97,23 +75,23 @@ static int test_triple_store()
 
     for (int i = 0; i < 12; i++) {
         size_t ct;
-        TRACE("Testing triple lookup #%d.\n", i);
+        TRACE ("Testing triple lookup #%d.\n", i);
 
         LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
                 store, lut + i, luc[i], &ct);
-        EXPECT_INT_EQ(ct, results[i]);
+        EXPECT_INT_EQ (ct, results[i]);
 
-        LSUP_mdbiter_free(it);
+        LSUP_mdbiter_free (it);
     }
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_buffer_done(ser_trp[i].s);
-        LSUP_buffer_done(ser_trp[i].p);
-        LSUP_buffer_done(ser_trp[i].o);
+        LSUP_buffer_done (ser_trp[i].s);
+        LSUP_buffer_done (ser_trp[i].p);
+        LSUP_buffer_done (ser_trp[i].o);
     }
 
-    LSUP_mdbstore_free(store);
-    free_triples(trp);
+    LSUP_mdbstore_free (store);
+    free_triples (trp);
 
     return 0;
 }
@@ -125,55 +103,41 @@ static int test_triple_store()
  */
 static int test_quad_store()
 {
-    rmdb();
-    EXPECT_PASS(LSUP_mdbstore_setup(&path));
+    EXPECT_PASS (LSUP_mdbstore_setup (&path, true));
 
-    LSUP_Term *ctx1 = LSUP_uri_new("urn:c:1");
-    LSUP_Buffer sc1_s;
-    LSUP_Buffer *sc1 = &sc1_s;
-    LSUP_term_serialize(ctx1, sc1);
+    LSUP_Term *ctx1 = LSUP_uri_new ("urn:c:1");
+    LSUP_Buffer *sc1 = LSUP_buffer_new_from_term (ctx1);
 
-    LSUP_MDBStore *store = LSUP_mdbstore_new(path, sc1); // quad store.
-    ASSERT(store != NULL, "Error initializing store!");
+    LSUP_MDBStore *store = LSUP_mdbstore_new (path, sc1); // quad store.
+    ASSERT (store != NULL, "Error initializing store!");
 
     LSUP_Triple *trp = create_triples();
-    LSUP_Buffer sterms[NUM_TRP][3];
     LSUP_SerTriple ser_trp[NUM_TRP];
 
     for (int i = 0; i < NUM_TRP; i++) {
-        ser_trp[i].s = sterms[i];
-        ser_trp[i].p = sterms[i] + 1;
-        ser_trp[i].o = sterms[i] + 2;
-        for (int j = 0; j < 3; j++) {
-            LSUP_term_serialize(
-                    LSUP_triple_pos(trp + i, j),
-                    LSUP_striple_pos(ser_trp + i, j));
-        }
+        LSUP_striple_init (ser_trp + i, BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
+        LSUP_triple_serialize (trp + i, ser_trp + i);
     }
 
     // Only triples 0÷5 in default context.
     size_t ct;
-    EXPECT_PASS(LSUP_mdbstore_add(store, NULL, ser_trp, 6, &ct));
-    EXPECT_INT_EQ(ct, 6);
+    EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, 6, &ct));
+    EXPECT_INT_EQ (ct, 6);
 
-    LSUP_Term *ctx2 = LSUP_uri_new("urn:c:2");
-    LSUP_Buffer sc2_s;
-    LSUP_Buffer *sc2 = &sc2_s;
-    LSUP_term_serialize(ctx2, sc2);
+    LSUP_Term *ctx2 = LSUP_uri_new ("urn:c:2");
+    LSUP_Buffer *sc2 = LSUP_buffer_new_from_term (ctx2);
 
     // Only triples 4÷9 in default context.
-    EXPECT_PASS(LSUP_mdbstore_add(store, &sc2_s, ser_trp + 4, 6, &ct));
-    EXPECT_INT_EQ(ct, 4);
+    EXPECT_PASS (LSUP_mdbstore_add (store, sc2, ser_trp + 4, 6, &ct));
+    EXPECT_INT_EQ (ct, 4);
 
     // 6 triples in ctx1 + 6 in ctx2 - 2 duplicates
-    EXPECT_INT_EQ(LSUP_mdbstore_size(store), 10);
+    EXPECT_INT_EQ (LSUP_mdbstore_size (store), 10);
 
     // Test lookups.
     // This context is not with any triple.
-    LSUP_Term *ctx3 = LSUP_uri_new("urn:c:3");
-    LSUP_Buffer sc3_s;
-    LSUP_Buffer *sc3 = &sc3_s;
-    LSUP_term_serialize(ctx3, sc3);
+    LSUP_Term *ctx3 = LSUP_uri_new ("urn:c:3");
+    LSUP_Buffer *sc3 = LSUP_buffer_new_from_term (ctx3);
 
     LSUP_SerTriple lut[41] = {
         // Any context
@@ -295,46 +259,46 @@ static int test_quad_store()
     for (int i = 0; i < 41; i++) {
         size_t ct;
 
-        printf("Testing triple lookup #%d: {", i);
+        printf ("Testing triple lookup #%d: {", i);
 
-        if(lut[i].s) LSUP_buffer_print(lut[i].s);
-        else printf("NULL");
-        printf(", ");
+        if (lut[i].s) LSUP_buffer_print (lut[i].s);
+        else printf ("NULL");
+        printf (", ");
 
-        if(lut[i].p) LSUP_buffer_print(lut[i].p);
-        else printf("NULL");
-        printf(", ");
+        if (lut[i].p) LSUP_buffer_print (lut[i].p);
+        else printf ("NULL");
+        printf (", ");
 
-        if(lut[i].o) LSUP_buffer_print(lut[i].o);
-        else printf("NULL");
-        printf(", ");
+        if (lut[i].o) LSUP_buffer_print (lut[i].o);
+        else printf ("NULL");
+        printf (", ");
 
-        if(luc[i]) LSUP_buffer_print(luc[i]);
-        else printf("NULL");
-        printf("}\n");
+        if (luc[i]) LSUP_buffer_print (luc[i]);
+        else printf ("NULL");
+        printf ("}\n");
 
         LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
                 store, lut + i, luc[i], &ct);
-        ASSERT(it != NULL, "Lookup error!");
-        EXPECT_INT_EQ(ct, results[i]);
+        ASSERT (it != NULL, "Lookup error!");
+        EXPECT_INT_EQ (ct, results[i]);
 
-        LSUP_mdbiter_free(it);
+        LSUP_mdbiter_free (it);
     }
 
     for (int i = 0; i < NUM_TRP; i++) {
-        LSUP_buffer_done(ser_trp[i].s);
-        LSUP_buffer_done(ser_trp[i].p);
-        LSUP_buffer_done(ser_trp[i].o);
+        LSUP_buffer_done (ser_trp[i].s);
+        LSUP_buffer_done (ser_trp[i].p);
+        LSUP_buffer_done (ser_trp[i].o);
     }
 
-    LSUP_term_free(ctx1);
-    LSUP_term_free(ctx2);
-    LSUP_term_free(ctx3);
-    LSUP_buffer_done(&sc1_s);
-    LSUP_buffer_done(&sc2_s);
-    free_triples(trp);
+    LSUP_term_free (ctx1);
+    LSUP_term_free (ctx2);
+    LSUP_term_free (ctx3);
+    LSUP_buffer_done (sc1);
+    LSUP_buffer_done (sc2);
+    free_triples (trp);
 
-    LSUP_mdbstore_free(store);
+    LSUP_mdbstore_free (store);
 
     return 0;
 }
@@ -342,7 +306,7 @@ static int test_quad_store()
 
 int store_mdb_test()
 {
-    RUN(test_triple_store);
-    RUN(test_quad_store);
+    RUN (test_triple_store);
+    RUN (test_quad_store);
     return 0;
 }

+ 1 - 2
test/test_term.c

@@ -33,8 +33,7 @@ static int test_term_serialize_deserialize()
     LSUP_Term *tlit = LSUP_term_new(LSUP_TERM_LITERAL, "hello", "xsd:string", NULL);
     LSUP_Term *tllit = LSUP_term_new(LSUP_TERM_LITERAL, "hello", "xsd:string", "en-US");
 
-    LSUP_Buffer *sterm = LSUP_buffer_new(0, NULL);
-
+    LSUP_Buffer *sterm = BUF_DUMMY;
     LSUP_Term *dsterm = malloc (sizeof (*dsterm));
 
     LSUP_term_serialize(uri, sterm);