123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include "graph.h"
- /**
- * Extern inline functions.
- */
- size_t LSUP_graph_size(LSUP_Graph *gr);
- size_t LSUP_graph_capacity(LSUP_Graph *gr);
- int
- LSUP_graph_init(
- LSUP_Graph *gr, size_t capacity, const LSUP_Term *uri,
- LSUP_store_type store_type)
- {
- if (uri->type != LSUP_TERM_URI)
- return -1;
- gr->uri = uri;
- gr->keys = LSUP_keyset_new(capacity, .75);
- switch (store_type ) {
- case LSUP_STORE_MEM:
- gr->idx = LSUP_index_new(gr->keys->capacity);
- break;
- case LSUP_STORE_MDB:
- // TODO
- default:
- return -1;
- }
- return 0;
- }
- LSUP_Graph *
- LSUP_graph_new(
- size_t capacity, const LSUP_Term *uri,
- LSUP_store_type store_type)
- {
- LSUP_Graph *gr;
- CRITICAL(gr = malloc(sizeof(LSUP_Graph)));
- LSUP_graph_init(gr, capacity, uri, store_type);
- return gr;
- }
- int
- LSUP_graph_add(LSUP_Graph *gr, LSUP_Triple data[], size_t data_size)
- {
- // TODO Decouple this and build interface for memory and MDB integration.
- // Resize all at once if needed.
- if (gr->keys->capacity < gr->keys->free_i + data_size)
- LSUP_keyset_resize(gr->keys, gr->keys->free_i + data_size);
- LSUP_SerTerm **sterms = malloc(
- sizeof(LSUP_SerTerm) * 3 * LSUP_keyset_size(gr->keys));
- for (size_t i = 0; i < data_size; i++) {
- LSUP_term_serialize(data[i].s, sterms[i]);
- LSUP_term_serialize(data[i].p, sterms[i] + 1);
- LSUP_term_serialize(data[i].o, sterms[i] + 2);
- LSUP_Key sk = LSUP_sterm_to_key(sterms[i]);
- LSUP_Key pk = LSUP_sterm_to_key(sterms[i] + 1);
- LSUP_Key ok = LSUP_sterm_to_key(sterms[i] + 2);
- // Add terms to index.
- LSUP_index_add_pair(gr->idx, sk, sterms[i]);
- LSUP_index_add_pair(gr->idx, pk, sterms[i] + 1);
- LSUP_index_add_pair(gr->idx, ok, sterms[i] + 2);
- // Add triple.
- LSUP_TripleKey trp = {sk, pk, ok};
- LSUP_keyset_add(gr->keys, &trp, LSUP_KS_CHECK_DUP);
- }
- return 0;
- }
- bool
- LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo)
- {
- LSUP_Key sk = LSUP_term_to_key(spo->s);
- LSUP_Key pk = LSUP_term_to_key(spo->p);
- LSUP_Key ok = LSUP_term_to_key(spo->o);
- LSUP_TripleKey spok = {sk, pk, ok};
- return LSUP_keyset_contains(gr->keys, &spok);
- }
- void
- LSUP_graph_free(LSUP_Graph *gr)
- {
- if(LIKELY(gr != NULL)) {
- LSUP_keyset_free(gr->keys);
- LSUP_index_free(gr->idx);
- free(gr);
- }
- }
|