|
@@ -52,12 +52,12 @@ typedef struct GraphIterator {
|
|
|
|
|
|
* Extern inline functions.
|
|
|
*/
|
|
|
-size_t LSUP_graph_size(const LSUP_Graph *gr);
|
|
|
+size_t LSUP_graph_size (const LSUP_Graph *gr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-static inline void mdbstore_init();
|
|
|
+static inline LSUP_rc mdbstore_init();
|
|
|
|
|
|
|
|
|
|
|
@@ -72,14 +72,14 @@ int match_rm_fn(
|
|
|
void *ctx);
|
|
|
|
|
|
static LSUP_rc
|
|
|
-graph_iter_next_buffer(GraphIterator *it, LSUP_SerTriple *sspo);
|
|
|
+graph_iter_next_buffer (GraphIterator *it, LSUP_SerTriple *sspo);
|
|
|
|
|
|
|
|
|
|
|
|
-void ctx_cleanup() { LSUP_buffer_done(default_ctx); }
|
|
|
+void ctx_cleanup() { LSUP_buffer_done (default_ctx); }
|
|
|
|
|
|
|
|
|
-static inline bool is_null_trp(const LSUP_TripleKey *trp)
|
|
|
+static inline bool is_null_trp (const LSUP_TripleKey *trp)
|
|
|
{
|
|
|
return (
|
|
|
*trp[0] == NULL_KEY &&
|
|
@@ -97,23 +97,23 @@ check_backend (LSUP_store_type be)
|
|
|
|
|
|
|
|
|
Graph *
|
|
|
-LSUP_graph_new(const LSUP_store_type store_type)
|
|
|
+LSUP_graph_new (const LSUP_store_type store_type)
|
|
|
{
|
|
|
- if (!check_backend(store_type)) return NULL;
|
|
|
+ if (!check_backend (store_type)) return NULL;
|
|
|
|
|
|
LSUP_Graph *gr;
|
|
|
- CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
|
|
|
- gr->uri = LSUP_uri_new(NULL);
|
|
|
+ CRITICAL (gr = malloc (sizeof (LSUP_Graph)));
|
|
|
+ gr->uri = LSUP_uri_new (NULL);
|
|
|
|
|
|
- mdbstore_init();
|
|
|
+ if (mdbstore_init() != LSUP_OK) return NULL;
|
|
|
|
|
|
if (store_type == LSUP_STORE_MEM) {
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
} else if (store_type == LSUP_STORE_MDB) {
|
|
|
gr->mdb_store = LSUP_mdbstore_new(
|
|
|
- getenv("LSUP_MDB_STORE_PATH"), default_ctx);
|
|
|
+ getenv ("LSUP_MDB_STORE_PATH"), default_ctx);
|
|
|
if (!gr->mdb_store) return NULL;
|
|
|
|
|
|
} else {
|
|
@@ -132,18 +132,18 @@ LSUP_graph_new(const LSUP_store_type store_type)
|
|
|
* The destination graph is not initialized here, so the copy is cumulative.
|
|
|
*/
|
|
|
static LSUP_rc
|
|
|
-graph_copy_contents(const LSUP_Graph *src, LSUP_Graph *dest)
|
|
|
+graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
|
|
|
{
|
|
|
LSUP_rc rc = LSUP_NOACTION;
|
|
|
const LSUP_Triple trp = {NULL, NULL, NULL};
|
|
|
|
|
|
- GraphIterator *it = LSUP_graph_lookup(src, &trp);
|
|
|
+ GraphIterator *it = LSUP_graph_lookup (src, &trp);
|
|
|
|
|
|
LSUP_SerTriple sspo;
|
|
|
|
|
|
- while (graph_iter_next_buffer(it, &sspo) != LSUP_END) {
|
|
|
- TRACE("Inserting triple #%lu\n", it->i);
|
|
|
- LSUP_rc add_rc = LSUP_graph_add(dest, NULL, 0, &sspo, 1, NULL);
|
|
|
+ while (graph_iter_next_buffer (it, &sspo) != LSUP_END) {
|
|
|
+ TRACE ("Inserting triple #%lu\n", it->i);
|
|
|
+ LSUP_rc add_rc = LSUP_graph_add (dest, NULL, 0, &sspo, 1, NULL);
|
|
|
if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
|
|
|
else if (add_rc < 0) return add_rc;
|
|
|
}
|
|
@@ -153,12 +153,12 @@ graph_copy_contents(const LSUP_Graph *src, LSUP_Graph *dest)
|
|
|
|
|
|
|
|
|
LSUP_Graph *
|
|
|
-LSUP_graph_copy(const Graph *src)
|
|
|
+LSUP_graph_copy (const Graph *src)
|
|
|
{
|
|
|
- LSUP_Graph *dest = LSUP_graph_new(src->store_type);
|
|
|
+ LSUP_Graph *dest = LSUP_graph_new (src->store_type);
|
|
|
if (UNLIKELY (!dest)) return NULL;
|
|
|
|
|
|
- LSUP_rc rc = graph_copy_contents(src, dest);
|
|
|
+ LSUP_rc rc = graph_copy_contents (src, dest);
|
|
|
if (UNLIKELY (rc != LSUP_OK)) return NULL;
|
|
|
|
|
|
return dest;
|
|
@@ -187,8 +187,8 @@ LSUP_graph_bool_op(
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- LSUP_Graph *res = LSUP_graph_new(LSUP_STORE_MEM);
|
|
|
- res->ht_store = LSUP_htstore_bool_op(op, gr1->ht_store, gr2->ht_store);
|
|
|
+ LSUP_Graph *res = LSUP_graph_new (LSUP_STORE_MEM);
|
|
|
+ res->ht_store = LSUP_htstore_bool_op (op, gr1->ht_store, gr2->ht_store);
|
|
|
|
|
|
return res;
|
|
|
}
|
|
@@ -196,58 +196,58 @@ LSUP_graph_bool_op(
|
|
|
|
|
|
|
|
|
void
|
|
|
-LSUP_graph_free(LSUP_Graph *gr)
|
|
|
+LSUP_graph_free (LSUP_Graph *gr)
|
|
|
{
|
|
|
- if (LIKELY(gr != NULL)) {
|
|
|
- LSUP_term_free(gr->uri);
|
|
|
+ if (LIKELY (gr != NULL)) {
|
|
|
+ LSUP_term_free (gr->uri);
|
|
|
|
|
|
if (gr->store_type == LSUP_STORE_MEM)
|
|
|
- NULL;
|
|
|
+ NULL;
|
|
|
else
|
|
|
- LSUP_mdbstore_free(gr->mdb_store);
|
|
|
+ LSUP_mdbstore_free (gr->mdb_store);
|
|
|
|
|
|
- free(gr);
|
|
|
+ free (gr);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
LSUP_Term *
|
|
|
-LSUP_graph_uri(const LSUP_Graph *gr) { return gr->uri; }
|
|
|
+LSUP_graph_uri (const LSUP_Graph *gr) { return gr->uri; }
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri)
|
|
|
-{ return LSUP_uri_init(gr->uri, uri); }
|
|
|
+LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri)
|
|
|
+{ return LSUP_uri_init (gr->uri, uri); }
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_graph_resize(LSUP_Graph *gr, size_t size)
|
|
|
+LSUP_graph_resize (LSUP_Graph *gr, size_t size)
|
|
|
{
|
|
|
return LSUP_OK;
|
|
|
if (gr->store_type == LSUP_STORE_MEM)
|
|
|
- return 0;
|
|
|
+ return 0;
|
|
|
|
|
|
return LSUP_VALUE_ERR;
|
|
|
}
|
|
|
|
|
|
|
|
|
size_t
|
|
|
-LSUP_graph_capacity(const Graph *gr)
|
|
|
+LSUP_graph_capacity (const Graph *gr)
|
|
|
{
|
|
|
- if(gr->store_type == LSUP_STORE_MEM)
|
|
|
- return 0;
|
|
|
+ if (gr->store_type == LSUP_STORE_MEM)
|
|
|
+ return 0;
|
|
|
|
|
|
return LSUP_NOT_IMPL_ERR;
|
|
|
}
|
|
|
|
|
|
|
|
|
size_t
|
|
|
-LSUP_graph_size(const Graph *gr)
|
|
|
+LSUP_graph_size (const Graph *gr)
|
|
|
{
|
|
|
- if(gr->store_type == LSUP_STORE_MEM)
|
|
|
- return 0;
|
|
|
+ if (gr->store_type == LSUP_STORE_MEM)
|
|
|
+ return 0;
|
|
|
|
|
|
- return LSUP_mdbstore_size(gr->mdb_store);
|
|
|
+ return LSUP_mdbstore_size (gr->mdb_store);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -269,11 +269,11 @@ LSUP_graph_add(
|
|
|
return LSUP_NOT_IMPL_ERR;
|
|
|
|
|
|
|
|
|
- htsize_t prealloc = LSUP_htstore_size(gr->ht_store) + trp_ct + strp_ct;
|
|
|
- if (LSUP_htstore_capacity(gr->ht_store) < prealloc) {
|
|
|
- rc = LSUP_htstore_resize(gr->ht_store, prealloc * PREALLOC_FACTOR);
|
|
|
+ htsize_t prealloc = LSUP_htstore_size (gr->ht_store) + trp_ct + strp_ct;
|
|
|
+ if (LSUP_htstore_capacity (gr->ht_store) < prealloc) {
|
|
|
+ rc = LSUP_htstore_resize (gr->ht_store, prealloc * PREALLOC_FACTOR);
|
|
|
|
|
|
- if (UNLIKELY(rc != LSUP_OK)) return rc;
|
|
|
+ if (UNLIKELY (rc != LSUP_OK)) return rc;
|
|
|
}
|
|
|
|
|
|
rc = LSUP_NOACTION;
|
|
@@ -282,16 +282,16 @@ LSUP_graph_add(
|
|
|
if (trp_ct > 0) {
|
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
|
LSUP_SerTriple sspo;
|
|
|
- LSUP_triple_serialize(trp + i, &sspo);
|
|
|
+ LSUP_triple_serialize (trp + i, &sspo);
|
|
|
|
|
|
- TRACE("Inserting triple #%lu\n", i);
|
|
|
- LSUP_rc db_rc = LSUP_htstore_add(gr->ht_store, &sspo);
|
|
|
- if (LIKELY(db_rc == LSUP_OK)) {
|
|
|
+ TRACE ("Inserting triple #%lu\n", i);
|
|
|
+ LSUP_rc db_rc = LSUP_htstore_add (gr->ht_store, &sspo);
|
|
|
+ if (LIKELY (db_rc == LSUP_OK)) {
|
|
|
rc = LSUP_OK;
|
|
|
(*inserted) ++;
|
|
|
}
|
|
|
|
|
|
- LSUP_striple_done(&sspo);
|
|
|
+ LSUP_striple_done (&sspo);
|
|
|
|
|
|
if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
|
}
|
|
@@ -299,8 +299,8 @@ LSUP_graph_add(
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < strp_ct; i++) {
|
|
|
- TRACE("Inserting triple #%lu\n", i);
|
|
|
- LSUP_rc db_rc = LSUP_htstore_add(gr->ht_store, strp + i);
|
|
|
+ TRACE ("Inserting triple #%lu\n", i);
|
|
|
+ LSUP_rc db_rc = LSUP_htstore_add (gr->ht_store, strp + i);
|
|
|
|
|
|
if (LIKELY (db_rc == LSUP_OK)) {
|
|
|
rc = LSUP_OK;
|
|
@@ -315,26 +315,25 @@ LSUP_graph_add(
|
|
|
rc = LSUP_NOACTION;
|
|
|
|
|
|
LSUP_Buffer sc;
|
|
|
- LSUP_term_serialize(gr->uri, &sc);
|
|
|
+ LSUP_term_serialize (gr->uri, &sc);
|
|
|
|
|
|
- LSUP_MDBIterator *it = LSUP_mdbstore_add_init(gr->mdb_store, &sc);
|
|
|
- LSUP_buffer_done(&sc);
|
|
|
+ LSUP_MDBIterator *it = LSUP_mdbstore_add_init (gr->mdb_store, &sc);
|
|
|
+ LSUP_buffer_done (&sc);
|
|
|
|
|
|
|
|
|
if (trp_ct > 0) {
|
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
|
- LSUP_SerTriple sspo;
|
|
|
- LSUP_triple_serialize(trp + i, &sspo);
|
|
|
+ LSUP_SerTriple *sspo = LSUP_striple_new_from_triple (trp + i);
|
|
|
|
|
|
- TRACE("Inserting triple #%lu\n", i);
|
|
|
- LSUP_rc db_rc = LSUP_mdbstore_add_iter(it, &sspo);
|
|
|
+ TRACE ("Inserting triple #%lu\n", i);
|
|
|
+ LSUP_rc db_rc = LSUP_mdbstore_add_iter (it, sspo);
|
|
|
|
|
|
if (LIKELY (db_rc == LSUP_OK)) {
|
|
|
rc = LSUP_OK;
|
|
|
(*inserted) ++;
|
|
|
}
|
|
|
|
|
|
- LSUP_striple_done(&sspo);
|
|
|
+ LSUP_striple_free (sspo);
|
|
|
|
|
|
if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
|
}
|
|
@@ -342,8 +341,8 @@ LSUP_graph_add(
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < strp_ct; i++) {
|
|
|
- TRACE("Inserting triple #%lu\n", i);
|
|
|
- LSUP_rc db_rc = LSUP_mdbstore_add_iter(it, strp + i);
|
|
|
+ TRACE ("Inserting triple #%lu\n", i);
|
|
|
+ LSUP_rc db_rc = LSUP_mdbstore_add_iter (it, strp + i);
|
|
|
|
|
|
if (LIKELY (db_rc == LSUP_OK)) {
|
|
|
rc = LSUP_OK;
|
|
@@ -352,7 +351,7 @@ LSUP_graph_add(
|
|
|
if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
|
}
|
|
|
|
|
|
- LSUP_mdbstore_add_done(it);
|
|
|
+ LSUP_mdbstore_add_done (it);
|
|
|
|
|
|
return rc;
|
|
|
}
|
|
@@ -362,47 +361,46 @@ LSUP_graph_add(
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_graph_remove(Graph *gr, const LSUP_Triple *spo, size_t *ct)
|
|
|
+LSUP_graph_remove (Graph *gr, const LSUP_Triple *spo, size_t *ct)
|
|
|
{
|
|
|
LSUP_rc rc;
|
|
|
|
|
|
LSUP_SerTriple sspo;
|
|
|
- LSUP_triple_serialize(spo, &sspo);
|
|
|
- LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
|
|
|
+ LSUP_triple_serialize (spo, &sspo);
|
|
|
+ LSUP_Buffer *sc = LSUP_buffer_new_from_term (gr->uri);
|
|
|
|
|
|
if (gr->store_type == LSUP_STORE_MEM)
|
|
|
- rc = 0;
|
|
|
+ rc = 0;
|
|
|
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_buffer_free(sc);
|
|
|
+ LSUP_striple_done (&sspo);
|
|
|
+ LSUP_buffer_free (sc);
|
|
|
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
|
|
|
|
GraphIterator *
|
|
|
-LSUP_graph_lookup(const Graph *gr, const LSUP_Triple *spo)
|
|
|
+LSUP_graph_lookup (const Graph *gr, const LSUP_Triple *spo)
|
|
|
{
|
|
|
GraphIterator *it;
|
|
|
- CRITICAL(it = malloc(sizeof(GraphIterator)));
|
|
|
+ CRITICAL (it = malloc (sizeof (GraphIterator)));
|
|
|
|
|
|
it->graph = gr;
|
|
|
|
|
|
- LSUP_SerTriple sspo;
|
|
|
- LSUP_triple_serialize(spo, &sspo);
|
|
|
- LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
|
|
|
+ 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) {
|
|
|
-
|
|
|
+
|
|
|
|
|
|
} else {
|
|
|
- it->mdb_iter = LSUP_mdbstore_lookup(gr->mdb_store, &sspo, sc, &it->ct);
|
|
|
+ it->mdb_iter = LSUP_mdbstore_lookup (gr->mdb_store, sspo, sc, &it->ct);
|
|
|
}
|
|
|
|
|
|
- LSUP_striple_done(&sspo);
|
|
|
- LSUP_buffer_free(sc);
|
|
|
+ LSUP_striple_free (sspo);
|
|
|
+ LSUP_buffer_free (sc);
|
|
|
|
|
|
return it;
|
|
|
}
|
|
@@ -414,55 +412,63 @@ LSUP_graph_lookup(const Graph *gr, const LSUP_Triple *spo)
|
|
|
* functions without serializing and deserializing triples.
|
|
|
*/
|
|
|
static LSUP_rc
|
|
|
-graph_iter_next_buffer(GraphIterator *it, LSUP_SerTriple *sspo)
|
|
|
+graph_iter_next_buffer (GraphIterator *it, LSUP_SerTriple *sspo)
|
|
|
{
|
|
|
LSUP_rc rc;
|
|
|
|
|
|
if (it->graph->store_type == LSUP_STORE_MEM)
|
|
|
- rc = 0;
|
|
|
- else rc = LSUP_mdbiter_next(it->mdb_iter, sspo);
|
|
|
+ rc = 0;
|
|
|
+ else rc = LSUP_mdbiter_next (it->mdb_iter, sspo);
|
|
|
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_graph_iter_next(GraphIterator *it, LSUP_Triple *spo)
|
|
|
+LSUP_graph_iter_next (GraphIterator *it, LSUP_Triple *spo)
|
|
|
{
|
|
|
- LSUP_SerTriple sspo;
|
|
|
- LSUP_rc rc = graph_iter_next_buffer(it, &sspo);
|
|
|
+ LSUP_SerTriple *sspo = LSUP_striple_new();
|
|
|
+ LSUP_rc rc = graph_iter_next_buffer (it, sspo);
|
|
|
|
|
|
if (rc == LSUP_OK) {
|
|
|
- LSUP_term_deserialize(sspo.s, spo->s);
|
|
|
- LSUP_term_deserialize(sspo.p, spo->p);
|
|
|
- LSUP_term_deserialize(sspo.o, spo->o);
|
|
|
+ if (spo) {
|
|
|
+ LSUP_term_deserialize (sspo->s, spo->s);
|
|
|
+ LSUP_term_deserialize (sspo->p, spo->p);
|
|
|
+ LSUP_term_deserialize (sspo->o, spo->o);
|
|
|
+ }
|
|
|
|
|
|
it->i++;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ free (sspo->s);
|
|
|
+ free (sspo->p);
|
|
|
+ free (sspo->o);
|
|
|
+ free (sspo);
|
|
|
+
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
|
|
|
|
void
|
|
|
-LSUP_graph_iter_free(GraphIterator *it)
|
|
|
+LSUP_graph_iter_free (GraphIterator *it)
|
|
|
{
|
|
|
if (it->graph->store_type == LSUP_STORE_MEM)
|
|
|
- NULL;
|
|
|
+ NULL;
|
|
|
else
|
|
|
- LSUP_mdbiter_free(it->mdb_iter);
|
|
|
+ LSUP_mdbiter_free (it->mdb_iter);
|
|
|
|
|
|
- free(it);
|
|
|
+ free (it);
|
|
|
}
|
|
|
|
|
|
|
|
|
bool
|
|
|
-LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo)
|
|
|
+LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
|
|
|
{
|
|
|
- GraphIterator *it = LSUP_graph_lookup(gr, spo);
|
|
|
- bool rc = LSUP_graph_iter_next(it, NULL) != LSUP_NORESULT;
|
|
|
+ GraphIterator *it = LSUP_graph_lookup (gr, spo);
|
|
|
+ bool rc = LSUP_graph_iter_next (it, NULL) != LSUP_NORESULT;
|
|
|
|
|
|
- LSUP_graph_iter_free(it);
|
|
|
+ LSUP_graph_iter_free (it);
|
|
|
|
|
|
return rc;
|
|
|
}
|
|
@@ -470,25 +476,30 @@ LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo)
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ *
|
|
|
+ * This is done only once per process.
|
|
|
*
|
|
|
* The ramdisk store persists after the application is closed, but will be
|
|
|
* wiped clean the next time this function is called.
|
|
|
*/
|
|
|
-static inline void mdbstore_init()
|
|
|
+static inline LSUP_rc
|
|
|
+mdbstore_init()
|
|
|
{
|
|
|
- if(UNLIKELY(!default_ctx)) {
|
|
|
+ if (UNLIKELY (!default_ctx)) {
|
|
|
|
|
|
char *path = MDB_RAMDISK_PATH;
|
|
|
- LSUP_mdbstore_setup (&path, true);
|
|
|
+ if (LSUP_mdbstore_setup (&path, true) != LSUP_OK) return LSUP_DB_ERR;
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ path = getenv ("LSUP_MDB_STORE_PATH");
|
|
|
+ if (LSUP_mdbstore_setup (&path, false) != LSUP_OK) return LSUP_DB_ERR;
|
|
|
|
|
|
- LSUP_Term *default_ctx_uri = LSUP_uri_new(default_ctx_label);
|
|
|
- default_ctx = LSUP_buffer_new_from_term(default_ctx_uri);
|
|
|
- LSUP_term_free(default_ctx_uri);
|
|
|
- atexit(ctx_cleanup);
|
|
|
+ LSUP_Term *default_ctx_uri = LSUP_uri_new (default_ctx_label);
|
|
|
+ default_ctx = LSUP_buffer_new_from_term (default_ctx_uri);
|
|
|
+ LSUP_term_free (default_ctx_uri);
|
|
|
+ atexit (ctx_cleanup);
|
|
|
}
|
|
|
+
|
|
|
+ return LSUP_OK;
|
|
|
}
|