graph.c 12 KB

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