|
@@ -70,7 +70,7 @@ graph_iter_next_buffer(GraphIterator *it, LSUP_SerTriple *sspo);
|
|
|
|
|
|
|
|
|
|
/* Atexit functions. */
|
|
/* Atexit functions. */
|
|
-void ctx_cleanup() { free(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)
|
|
@@ -84,33 +84,30 @@ static inline bool is_null_trp(const LSUP_TripleKey *trp)
|
|
|
|
|
|
/* * * GRAPH * * */
|
|
/* * * GRAPH * * */
|
|
|
|
|
|
-LSUP_rc
|
|
|
|
-LSUP_graph_new(const LSUP_store_type store_type, Graph **gr_p)
|
|
|
|
|
|
+Graph *
|
|
|
|
+LSUP_graph_new(const LSUP_store_type store_type)
|
|
{
|
|
{
|
|
LSUP_Graph *gr;
|
|
LSUP_Graph *gr;
|
|
CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
|
|
CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
|
|
|
|
|
|
- *gr_p = gr;
|
|
|
|
-
|
|
|
|
// Initialize default context only once per process.
|
|
// Initialize default context only once per process.
|
|
if(UNLIKELY(!default_ctx)) {
|
|
if(UNLIKELY(!default_ctx)) {
|
|
- LSUP_Term default_ctx_uri;
|
|
|
|
- LSUP_uri_new(default_ctx_label, &default_ctx_uri);
|
|
|
|
- LSUP_term_serialize(&default_ctx_uri, default_ctx);
|
|
|
|
- LSUP_term_free(&default_ctx_uri);
|
|
|
|
|
|
+ LSUP_Term *default_ctx_uri = LSUP_uri_new(default_ctx_label);
|
|
|
|
+ LSUP_term_serialize(default_ctx_uri, default_ctx);
|
|
|
|
+ LSUP_term_free(default_ctx_uri);
|
|
atexit(ctx_cleanup);
|
|
atexit(ctx_cleanup);
|
|
}
|
|
}
|
|
|
|
|
|
if (store_type == LSUP_STORE_MEM) {
|
|
if (store_type == LSUP_STORE_MEM) {
|
|
- LSUP_htstore_new(0, &gr->ht_store);
|
|
|
|
|
|
+ gr->ht_store = LSUP_htstore_new(0);
|
|
|
|
|
|
} else if (store_type == LSUP_STORE_MDB) {
|
|
} else if (store_type == LSUP_STORE_MDB) {
|
|
- LSUP_mdbstore_new(
|
|
|
|
- getenv("LSUP_MDB_STORE_PATH"), default_ctx, &gr->mdb_store);
|
|
|
|
|
|
+ gr->mdb_store = LSUP_mdbstore_new(
|
|
|
|
+ getenv("LSUP_MDB_STORE_PATH"), default_ctx);
|
|
|
|
|
|
- } else return LSUP_VALUE_ERR;
|
|
|
|
|
|
+ } else return NULL;
|
|
|
|
|
|
- return LSUP_OK;
|
|
|
|
|
|
+ return gr;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -124,9 +121,8 @@ graph_copy_contents(const LSUP_Graph *src, LSUP_Graph *dest)
|
|
{
|
|
{
|
|
LSUP_rc rc = LSUP_NOACTION;
|
|
LSUP_rc rc = LSUP_NOACTION;
|
|
const LSUP_Triple trp = {NULL, NULL, NULL};
|
|
const LSUP_Triple trp = {NULL, NULL, NULL};
|
|
- GraphIterator *it;
|
|
|
|
|
|
|
|
- LSUP_graph_lookup(src, &trp, &it);
|
|
|
|
|
|
+ GraphIterator *it = LSUP_graph_lookup(src, &trp);
|
|
|
|
|
|
LSUP_SerTriple sspo;
|
|
LSUP_SerTriple sspo;
|
|
|
|
|
|
@@ -141,27 +137,22 @@ graph_copy_contents(const LSUP_Graph *src, LSUP_Graph *dest)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-LSUP_rc
|
|
|
|
-LSUP_graph_copy(const Graph *src, Graph **dest_p)
|
|
|
|
|
|
+LSUP_Graph *
|
|
|
|
+LSUP_graph_copy(const Graph *src)
|
|
{
|
|
{
|
|
- LSUP_rc rc;
|
|
|
|
- LSUP_Graph *dest;
|
|
|
|
-
|
|
|
|
- rc = LSUP_graph_new(src->store_type, &dest);
|
|
|
|
- if (UNLIKELY (rc != LSUP_OK)) return rc;
|
|
|
|
-
|
|
|
|
- rc = graph_copy_contents(src, dest);
|
|
|
|
|
|
+ LSUP_Graph *dest = LSUP_graph_new(src->store_type);
|
|
|
|
+ if (UNLIKELY (!dest)) return NULL;
|
|
|
|
|
|
- if (LIKELY (rc == LSUP_OK)) *dest_p = dest;
|
|
|
|
|
|
+ LSUP_rc rc = graph_copy_contents(src, dest);
|
|
|
|
+ if (UNLIKELY (rc != LSUP_OK)) return NULL;
|
|
|
|
|
|
- return rc;
|
|
|
|
|
|
+ return dest;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-LSUP_rc
|
|
|
|
|
|
+Graph *
|
|
LSUP_graph_bool_op(
|
|
LSUP_graph_bool_op(
|
|
- const LSUP_bool_op op, const Graph *gr1, const Graph *gr2,
|
|
|
|
- Graph **res_p)
|
|
|
|
|
|
+ const LSUP_bool_op op, const Graph *gr1, const Graph *gr2)
|
|
{
|
|
{
|
|
if (UNLIKELY (gr1->store_type != LSUP_STORE_MEM)) {
|
|
if (UNLIKELY (gr1->store_type != LSUP_STORE_MEM)) {
|
|
fprintf(
|
|
fprintf(
|
|
@@ -169,7 +160,7 @@ LSUP_graph_bool_op(
|
|
"First operand %s is not an in-memory graph. "
|
|
"First operand %s is not an in-memory graph. "
|
|
"Cannot perform boolean operation.",
|
|
"Cannot perform boolean operation.",
|
|
gr1->uri->data);
|
|
gr1->uri->data);
|
|
- return LSUP_VALUE_ERR;
|
|
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
if (UNLIKELY (gr2->store_type != LSUP_STORE_MEM)) {
|
|
if (UNLIKELY (gr2->store_type != LSUP_STORE_MEM)) {
|
|
fprintf(
|
|
fprintf(
|
|
@@ -177,14 +168,13 @@ LSUP_graph_bool_op(
|
|
"Second operand %s is not an in-memory graph. "
|
|
"Second operand %s is not an in-memory graph. "
|
|
"Cannot perform boolean operation.",
|
|
"Cannot perform boolean operation.",
|
|
gr2->uri->data);
|
|
gr2->uri->data);
|
|
- return LSUP_VALUE_ERR;
|
|
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
- LSUP_Graph *res;
|
|
|
|
- LSUP_graph_new(LSUP_STORE_MEM, &res);
|
|
|
|
|
|
+ LSUP_Graph *res = LSUP_graph_new(LSUP_STORE_MEM);
|
|
|
|
+ res->ht_store = LSUP_htstore_bool_op(op, gr1->ht_store, gr2->ht_store);
|
|
|
|
|
|
- return LSUP_htstore_bool_op(
|
|
|
|
- op, gr1->ht_store, gr2->ht_store, &res->ht_store);
|
|
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -210,7 +200,7 @@ LSUP_graph_uri(const LSUP_Graph *gr) { return gr->uri; }
|
|
|
|
|
|
LSUP_rc
|
|
LSUP_rc
|
|
LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri)
|
|
LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri)
|
|
-{ return LSUP_uri_new(uri, gr->uri); }
|
|
|
|
|
|
+{ return LSUP_uri_init(gr->uri, uri); }
|
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
LSUP_rc
|
|
@@ -270,13 +260,9 @@ LSUP_graph_add(
|
|
|
|
|
|
// Serialize and insert RDF triples.
|
|
// Serialize and insert RDF triples.
|
|
if (trp_ct > 0) {
|
|
if (trp_ct > 0) {
|
|
- LSUP_SerTriple sspo;
|
|
|
|
-
|
|
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
-
|
|
|
|
- LSUP_term_serialize(trp[i].s, sspo.s);
|
|
|
|
- LSUP_term_serialize(trp[i].p, sspo.p);
|
|
|
|
- LSUP_term_serialize(trp[i].o, sspo.o);
|
|
|
|
|
|
+ LSUP_SerTriple sspo;
|
|
|
|
+ LSUP_triple_serialize(trp + i, &sspo);
|
|
|
|
|
|
TRACE("Inserting triple #%lu\n", i);
|
|
TRACE("Inserting triple #%lu\n", i);
|
|
LSUP_rc db_rc = LSUP_htstore_add(gr->ht_store, &sspo);
|
|
LSUP_rc db_rc = LSUP_htstore_add(gr->ht_store, &sspo);
|
|
@@ -284,12 +270,11 @@ LSUP_graph_add(
|
|
rc = LSUP_OK;
|
|
rc = LSUP_OK;
|
|
(*inserted) ++;
|
|
(*inserted) ++;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ LSUP_striple_done(&sspo);
|
|
|
|
+
|
|
if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
}
|
|
}
|
|
-
|
|
|
|
- LSUP_buffer_done(sspo.s);
|
|
|
|
- LSUP_buffer_done(sspo.p);
|
|
|
|
- LSUP_buffer_done(sspo.o);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// Insert serialized triples.
|
|
// Insert serialized triples.
|
|
@@ -313,18 +298,14 @@ LSUP_graph_add(
|
|
LSUP_Buffer sc;
|
|
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, &it);
|
|
|
|
|
|
+ LSUP_MDBIterator *it = LSUP_mdbstore_add_init(gr->mdb_store, &sc);
|
|
|
|
+ LSUP_buffer_done(&sc);
|
|
|
|
|
|
// Serialize and insert RDF triples.
|
|
// Serialize and insert RDF triples.
|
|
if (trp_ct > 0) {
|
|
if (trp_ct > 0) {
|
|
- LSUP_SerTriple sspo;
|
|
|
|
-
|
|
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
for (size_t i = 0; i < trp_ct; i++) {
|
|
-
|
|
|
|
- LSUP_term_serialize(trp[i].s, sspo.s);
|
|
|
|
- LSUP_term_serialize(trp[i].p, sspo.p);
|
|
|
|
- LSUP_term_serialize(trp[i].o, sspo.o);
|
|
|
|
|
|
+ LSUP_SerTriple sspo;
|
|
|
|
+ LSUP_triple_serialize(trp + i, &sspo);
|
|
|
|
|
|
TRACE("Inserting triple #%lu\n", i);
|
|
TRACE("Inserting triple #%lu\n", i);
|
|
LSUP_rc db_rc = LSUP_mdbstore_add_iter(it, &sspo);
|
|
LSUP_rc db_rc = LSUP_mdbstore_add_iter(it, &sspo);
|
|
@@ -333,12 +314,11 @@ LSUP_graph_add(
|
|
rc = LSUP_OK;
|
|
rc = LSUP_OK;
|
|
(*inserted) ++;
|
|
(*inserted) ++;
|
|
}
|
|
}
|
|
- if (UNLIKELY(db_rc < 0)) return db_rc;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- LSUP_buffer_done(sspo.s);
|
|
|
|
- LSUP_buffer_done(sspo.p);
|
|
|
|
- LSUP_buffer_done(sspo.o);
|
|
|
|
|
|
+ LSUP_striple_done(&sspo);
|
|
|
|
+
|
|
|
|
+ if (UNLIKELY (db_rc < 0)) return db_rc;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// Insert serialized triples.
|
|
// Insert serialized triples.
|
|
@@ -364,61 +344,46 @@ 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_rc rc;
|
|
- LSUP_SerTriple sspo_s;
|
|
|
|
- LSUP_SerTriple *sspo = &sspo_s;
|
|
|
|
- LSUP_Buffer sc_s;
|
|
|
|
- LSUP_Buffer *sc = &sc_s;
|
|
|
|
|
|
|
|
- LSUP_term_serialize(spo->s, sspo->s);
|
|
|
|
- LSUP_term_serialize(spo->p, sspo->s);
|
|
|
|
- LSUP_term_serialize(spo->o, sspo->s);
|
|
|
|
- LSUP_term_serialize(gr->uri, sc);
|
|
|
|
|
|
+ LSUP_SerTriple sspo;
|
|
|
|
+ LSUP_triple_serialize(spo, &sspo);
|
|
|
|
+ LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
|
|
|
|
|
|
if (gr->store_type == LSUP_STORE_MEM)
|
|
if (gr->store_type == LSUP_STORE_MEM)
|
|
- rc = LSUP_htstore_remove(gr->ht_store, sspo, ct);
|
|
|
|
|
|
+ rc = LSUP_htstore_remove(gr->ht_store, &sspo, ct);
|
|
else
|
|
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_done(sc);
|
|
|
|
|
|
+ LSUP_striple_done(&sspo);
|
|
|
|
+ LSUP_buffer_free(sc);
|
|
|
|
|
|
return rc;
|
|
return rc;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-LSUP_rc
|
|
|
|
-LSUP_graph_lookup(
|
|
|
|
- const Graph *gr, const LSUP_Triple *spo, GraphIterator **it_p)
|
|
|
|
|
|
+GraphIterator *
|
|
|
|
+LSUP_graph_lookup(const Graph *gr, const LSUP_Triple *spo)
|
|
{
|
|
{
|
|
- LSUP_rc rc;
|
|
|
|
GraphIterator *it;
|
|
GraphIterator *it;
|
|
CRITICAL(it = malloc(sizeof(GraphIterator)));
|
|
CRITICAL(it = malloc(sizeof(GraphIterator)));
|
|
- *it_p = it;
|
|
|
|
|
|
|
|
it->graph = gr;
|
|
it->graph = gr;
|
|
|
|
|
|
- LSUP_SerTriple sspo_s;
|
|
|
|
- LSUP_SerTriple *sspo = &sspo_s;
|
|
|
|
- LSUP_Buffer sc_s;
|
|
|
|
- LSUP_Buffer *sc = &sc_s;
|
|
|
|
-
|
|
|
|
- LSUP_term_serialize(spo->s, sspo->s);
|
|
|
|
- LSUP_term_serialize(spo->p, sspo->s);
|
|
|
|
- LSUP_term_serialize(spo->o, sspo->s);
|
|
|
|
- LSUP_term_serialize(gr->uri, sc);
|
|
|
|
|
|
+ LSUP_SerTriple sspo;
|
|
|
|
+ LSUP_triple_serialize(spo, &sspo);
|
|
|
|
+ LSUP_Buffer *sc = LSUP_buffer_new_from_term(gr->uri);
|
|
|
|
|
|
if (gr->store_type == LSUP_STORE_MEM) {
|
|
if (gr->store_type == LSUP_STORE_MEM) {
|
|
- rc = LSUP_htstore_lookup(gr->ht_store, sspo, &it->ht_iter, &it->ct);
|
|
|
|
|
|
+ it->ht_iter = LSUP_htstore_lookup(gr->ht_store, &sspo, &it->ct);
|
|
|
|
|
|
} else {
|
|
} else {
|
|
- rc = LSUP_mdbstore_lookup(
|
|
|
|
- gr->mdb_store, sspo, sc, &it->mdb_iter, &it->ct);
|
|
|
|
|
|
+ it->mdb_iter = LSUP_mdbstore_lookup(gr->mdb_store, &sspo, sc, &it->ct);
|
|
}
|
|
}
|
|
|
|
|
|
- LSUP_striple_done(sspo);
|
|
|
|
- LSUP_buffer_done(sc);
|
|
|
|
|
|
+ LSUP_striple_done(&sspo);
|
|
|
|
+ LSUP_buffer_free(sc);
|
|
|
|
|
|
- return rc;
|
|
|
|
|
|
+ return it;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -473,9 +438,7 @@ LSUP_graph_iter_free(GraphIterator *it)
|
|
bool
|
|
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, &it);
|
|
|
|
|
|
+ GraphIterator *it = LSUP_graph_lookup(gr, spo);
|
|
bool rc = LSUP_graph_iter_next(it, NULL) != LSUP_NORESULT;
|
|
bool rc = LSUP_graph_iter_next(it, NULL) != LSUP_NORESULT;
|
|
|
|
|
|
LSUP_graph_iter_free(it);
|
|
LSUP_graph_iter_free(it);
|