graph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "graph.h"
  2. /*
  3. * Data types.
  4. */
  5. struct graph_t {
  6. LSUP_Term *uri; ///< Graph "name" (URI).
  7. LSUP_Store * store; ///< Store handle.
  8. LSUP_NSMap * nsm; /**< Namespace map. NOTE: This is
  9. * NULL for permanent stores. */
  10. };
  11. struct graph_iter_t {
  12. LSUP_Store * store; ///< Store tied to the iterator.
  13. void * data; ///< Iterator state.
  14. size_t ct; ///< Total lookup matches.
  15. };
  16. /*
  17. * Static prototypes.
  18. */
  19. inline static LSUP_rc
  20. graph_iter_next_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo);
  21. #define ENTRY(a, b) (be) == (LSUP_STORE_##a) ||
  22. static inline bool
  23. check_backend (LSUP_StoreType be)
  24. { return (BACKEND_TBL false); }
  25. #undef ENTRY
  26. /*
  27. * Graph API.
  28. */
  29. LSUP_Graph *
  30. LSUP_graph_new (
  31. LSUP_Term *uri, const LSUP_StoreType store_type, const char *store_id,
  32. LSUP_NSMap *nsm, size_t size)
  33. {
  34. if (UNLIKELY (!LSUP_IS_INIT)) {
  35. log_error (
  36. "Environment is not initialized. Did you call LSUP_init()?");
  37. return NULL;
  38. }
  39. if (UNLIKELY (!check_backend (store_type))) return NULL;
  40. LSUP_Graph *gr;
  41. MALLOC_GUARD (gr, NULL);
  42. gr->uri = uri;
  43. MALLOC_GUARD (gr->store, NULL);
  44. gr->store->type = store_type;
  45. gr->store->id = store_id ? strdup (store_id) : NULL;
  46. switch (gr->store->type) {
  47. #define ENTRY(a, b) \
  48. case LSUP_STORE_##a: gr->store->sif = &b; break;
  49. BACKEND_TBL
  50. #undef ENTRY
  51. default:
  52. log_error ("Not a valid store type: %d", store_type); return NULL;
  53. };
  54. // TODO implement custom default context.
  55. gr->store->data = gr->store->sif->new_fn (store_id, size);
  56. if (gr->store->sif->features & LSUP_STORE_PERM) gr->nsm = NULL;
  57. else gr->nsm = nsm ? nsm : LSUP_default_nsm;
  58. log_debug ("Graph created.");
  59. return gr;
  60. }
  61. LSUP_rc
  62. LSUP_graph_bool_op(
  63. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2,
  64. LSUP_Graph *res)
  65. {
  66. LSUP_rc rc = LSUP_NOACTION;
  67. if (UNLIKELY (
  68. op != LSUP_BOOL_UNION
  69. && op != LSUP_BOOL_SUBTRACTION
  70. && op != LSUP_BOOL_INTERSECTION
  71. && op != LSUP_BOOL_XOR)) {
  72. log_error ("Invalid boolean operation: %d.", op);
  73. return LSUP_VALUE_ERR;
  74. }
  75. if (op == LSUP_BOOL_UNION) {
  76. rc = LSUP_graph_copy_contents (gr1, res);
  77. PCHECK (rc, fail);
  78. rc = LSUP_graph_copy_contents (gr2, res);
  79. PCHECK (rc, fail);
  80. return LSUP_OK;
  81. }
  82. LSUP_Buffer
  83. *res_sc = LSUP_term_serialize (res->uri),
  84. *gr1_sc = LSUP_term_serialize (gr1->uri),
  85. *gr2_sc = LSUP_term_serialize (gr2->uri);
  86. void *lu1_it, *lu2_it, *add_it;
  87. LSUP_BufferTriple *sspo = BTRP_DUMMY;
  88. size_t ct;
  89. add_it = res->store->sif->add_init_fn (res->store->data, res_sc);
  90. if (op == LSUP_BOOL_XOR) {
  91. // Add triples from gr2 if not found in gr1.
  92. lu2_it = gr2->store->sif->lookup_fn (
  93. gr2->store->data, NULL, NULL, NULL, gr2_sc, NULL);
  94. while (gr2->store->sif->lu_next_fn (lu2_it, sspo, NULL) == LSUP_OK) {
  95. lu1_it = gr1->store->sif->lookup_fn (
  96. gr1->store->data, sspo->s, sspo->p, sspo->o, gr1_sc, &ct);
  97. if (ct > 0)
  98. res->store->sif->add_iter_fn (add_it, sspo);
  99. gr1->store->sif->lu_free_fn (lu1_it);
  100. }
  101. gr2->store->sif->lu_free_fn (lu2_it);
  102. }
  103. lu1_it = gr1->store->sif->lookup_fn (
  104. gr1->store->data, NULL, NULL, NULL, gr1_sc, NULL);
  105. while (gr1->store->sif->lu_next_fn (lu1_it, sspo, NULL) == LSUP_OK) {
  106. lu2_it = gr2->store->sif->lookup_fn (
  107. gr2->store->data, sspo->s, sspo->p, sspo->o, gr2_sc, &ct);
  108. // For XOR and subtraction, add if not found.
  109. // For intersection, add if found.
  110. if ((ct == 0) ^ (op == LSUP_BOOL_INTERSECTION))
  111. res->store->sif->add_iter_fn (add_it, sspo);
  112. gr2->store->sif->lu_free_fn (lu2_it);
  113. }
  114. gr1->store->sif->lu_free_fn (lu1_it);
  115. res->store->sif->add_done_fn (add_it);
  116. LSUP_buffer_free (res_sc);
  117. LSUP_buffer_free (gr1_sc);
  118. LSUP_buffer_free (gr2_sc);
  119. return rc;
  120. fail:
  121. LSUP_graph_free (res);
  122. return rc;
  123. }
  124. void
  125. LSUP_graph_free (LSUP_Graph *gr)
  126. {
  127. if (UNLIKELY (!gr)) return;
  128. LSUP_term_free (gr->uri);
  129. free (gr->store->id);
  130. gr->store->sif->free_fn (gr->store->data);
  131. free (gr->store);
  132. free (gr);
  133. }
  134. LSUP_Term *
  135. LSUP_graph_uri (const LSUP_Graph *gr) { return gr->uri; }
  136. LSUP_rc
  137. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri)
  138. {
  139. if (!LSUP_IS_IRI (uri)) {
  140. log_error ("Term provided is not a IRI.");
  141. return LSUP_VALUE_ERR;
  142. }
  143. LSUP_term_free (gr->uri);
  144. gr->uri = uri;
  145. return LSUP_OK;
  146. }
  147. LSUP_NSMap *
  148. LSUP_graph_namespace (const LSUP_Graph *gr)
  149. {
  150. // If nsm_get_fn is not defined, the store has no own NS map.
  151. if (!gr->store->sif->nsm_get_fn) return gr->nsm;
  152. return gr->store->sif->nsm_get_fn (gr->store->data);
  153. }
  154. void
  155. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm)
  156. {
  157. if (!gr->store->sif->nsm_get_fn) gr->nsm = nsm;
  158. else log_warn ("Graph back end has a stored NS map.");
  159. }
  160. size_t
  161. LSUP_graph_size (const LSUP_Graph *gr)
  162. { return gr->store->sif->size_fn (gr->store->data); }
  163. bool
  164. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2)
  165. {
  166. LSUP_Graph *res = LSUP_graph_new (NULL, LSUP_STORE_HTABLE, NULL, NULL, 0);
  167. LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, res);
  168. bool ret = (LSUP_graph_size (res) == 0);
  169. LSUP_graph_free (res);
  170. return ret;
  171. }
  172. LSUP_GraphIterator *
  173. LSUP_graph_add_init (LSUP_Graph *gr)
  174. {
  175. LSUP_GraphIterator *it;
  176. CALLOC_GUARD (it, NULL);
  177. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  178. it->data = gr->store->sif->add_init_fn (gr->store->data, sc);
  179. LSUP_buffer_free (sc);
  180. it->store = gr->store;
  181. return it;
  182. }
  183. LSUP_rc
  184. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo)
  185. {
  186. LSUP_BufferTriple *sspo = LSUP_triple_serialize (spo);
  187. if (UNLIKELY (!sspo)) return LSUP_MEM_ERR;
  188. LSUP_rc rc = it->store->sif->add_iter_fn (it->data, sspo);
  189. PCHECK (rc, finally);
  190. // Store datatype term permanently if the store supports it.
  191. if (rc == LSUP_OK && it->store->sif->add_term_fn) {
  192. for (int i = 0; i < 3; i++) {
  193. LSUP_Term *term = LSUP_triple_pos (spo, i);
  194. if (term->type == LSUP_TERM_LITERAL) {
  195. LSUP_Buffer *ser_dtype = LSUP_term_serialize (term->datatype);
  196. it->store->sif->add_term_fn (it->store->data, ser_dtype);
  197. LSUP_buffer_free (ser_dtype);
  198. }
  199. }
  200. }
  201. finally:
  202. LSUP_btriple_free (sspo);
  203. return rc;
  204. }
  205. void
  206. LSUP_graph_add_done (LSUP_GraphIterator *it)
  207. {
  208. it->store->sif->add_done_fn (it->data);
  209. free (it);
  210. }
  211. LSUP_rc
  212. LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct)
  213. {
  214. LSUP_rc rc = LSUP_NOACTION;
  215. // Initialize iterator.
  216. LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
  217. if (ct) *ct = 0;
  218. // Serialize and insert RDF triples.
  219. for (size_t i = 0; trp[i].s != NULL; i++) {
  220. log_trace ("Inserting triple #%lu", i);
  221. LSUP_rc db_rc = LSUP_graph_add_iter (it, trp + i);
  222. if (db_rc == LSUP_OK) {
  223. rc = LSUP_OK;
  224. if (ct) (*ct)++;
  225. // A duplicate will return LSUP_NOACTION and not increment the
  226. // counter.
  227. }
  228. if (UNLIKELY (db_rc < 0)) {
  229. rc = db_rc;
  230. goto finally;
  231. }
  232. }
  233. finally:
  234. LSUP_graph_add_done (it);
  235. return rc;
  236. }
  237. LSUP_rc
  238. LSUP_graph_remove (
  239. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  240. const LSUP_Term *o, size_t *ct)
  241. {
  242. LSUP_rc rc;
  243. LSUP_Buffer
  244. *ss = LSUP_term_serialize (s),
  245. *sp = LSUP_term_serialize (p),
  246. *so = LSUP_term_serialize (o),
  247. *sc = LSUP_term_serialize (gr->uri);
  248. rc = gr->store->sif->remove_fn (gr->store->data, ss, sp, so, sc, ct);
  249. LSUP_buffer_free (ss);
  250. LSUP_buffer_free (sp);
  251. LSUP_buffer_free (so);
  252. LSUP_buffer_free (sc);
  253. return rc;
  254. }
  255. /**
  256. * Copy triples from a source graph into a destination one.
  257. *
  258. * The destination graph is not initialized here, so the copy is cumulative.
  259. */
  260. LSUP_rc
  261. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
  262. {
  263. LSUP_rc rc = LSUP_NOACTION;
  264. LSUP_GraphIterator *it = LSUP_graph_lookup (src, NULL, NULL, NULL, NULL);
  265. LSUP_Triple spo;
  266. LSUP_GraphIterator *add_it = LSUP_graph_add_init (dest);
  267. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  268. LSUP_rc add_rc = LSUP_graph_add_iter (add_it, &spo);
  269. LSUP_triple_done (&spo);
  270. if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
  271. else if (add_rc < 0) {
  272. rc = add_rc;
  273. break;
  274. }
  275. }
  276. LSUP_graph_add_done (add_it);
  277. LSUP_graph_iter_free (it);
  278. return rc;
  279. }
  280. LSUP_GraphIterator *
  281. LSUP_graph_lookup (
  282. const LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  283. const LSUP_Term *o, size_t *ct)
  284. {
  285. LSUP_GraphIterator *it;
  286. MALLOC_GUARD (it, NULL);
  287. it->store = gr->store;
  288. LSUP_Buffer
  289. *ss = LSUP_term_serialize (s),
  290. *sp = LSUP_term_serialize (p),
  291. *so = LSUP_term_serialize (o),
  292. *sc = LSUP_term_serialize (gr->uri);
  293. it->data = it->store->sif->lookup_fn (it->store->data, ss, sp, so, sc, ct);
  294. if (UNLIKELY (!it->data)) {
  295. free (it);
  296. it = NULL;
  297. }
  298. LSUP_buffer_free (ss);
  299. LSUP_buffer_free (sp);
  300. LSUP_buffer_free (so);
  301. LSUP_buffer_free (sc);
  302. return it;
  303. }
  304. LSUP_rc
  305. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo)
  306. {
  307. LSUP_Buffer *ss, *sp, *so;
  308. LSUP_BufferTriple *sspo;
  309. if (it->store->sif->features & LSUP_STORE_COW) {
  310. CALLOC_GUARD (ss, LSUP_MEM_ERR);
  311. CALLOC_GUARD (sp, LSUP_MEM_ERR);
  312. CALLOC_GUARD (so, LSUP_MEM_ERR);
  313. sspo = LSUP_btriple_new (ss, sp, so);
  314. } else {
  315. // TODO copy-on-retrieval stores. None yet.
  316. }
  317. LSUP_rc rc = graph_iter_next_buffer (it, sspo);
  318. if (rc == LSUP_OK) {
  319. spo->s = LSUP_term_new_from_buffer (sspo->s);
  320. if (!spo->s) return LSUP_ERROR;
  321. spo->p = LSUP_term_new_from_buffer (sspo->p);
  322. if (!spo->p) return LSUP_ERROR;
  323. spo->o = LSUP_term_new_from_buffer (sspo->o);
  324. if (!spo->o) return LSUP_ERROR;
  325. }
  326. if (it->store->sif->features & LSUP_STORE_COW) {
  327. LSUP_btriple_free_shallow (sspo);
  328. } else {
  329. // TODO copy-on-retrieval stores. None yet.
  330. }
  331. return rc;
  332. }
  333. void
  334. LSUP_graph_iter_free (LSUP_GraphIterator *it)
  335. {
  336. it->store->sif->lu_free_fn (it->data);
  337. free (it);
  338. }
  339. bool
  340. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
  341. {
  342. LSUP_GraphIterator *it = LSUP_graph_lookup (
  343. gr, spo->s, spo->p, spo->o, NULL);
  344. LSUP_Triple *tmp_spo = TRP_DUMMY;
  345. bool rc = LSUP_graph_iter_next (it, tmp_spo) != LSUP_END;
  346. LSUP_triple_free (tmp_spo);
  347. LSUP_graph_iter_free (it);
  348. return rc;
  349. }
  350. /*
  351. * Static functions.
  352. */
  353. /** @brief Advance an iterator and return a serialized triple.
  354. *
  355. * This is an internal function to pass raw buffers between higher-level
  356. * functions without serializing and deserializing triples.
  357. */
  358. inline static LSUP_rc
  359. graph_iter_next_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo)
  360. { return it->store->sif->lu_next_fn (it->data, sspo, NULL); }
  361. /**
  362. * Extern inline definitions.
  363. */
  364. size_t LSUP_graph_size (const LSUP_Graph *gr);