graph.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. #include "lsup/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. };
  14. struct graph_iter_t {
  15. const LSUP_Graph * graph; ///< Parent graph.
  16. void * data; ///< Iterator state.
  17. size_t ct; ///< Total lookup matches.
  18. LSUP_BufferTriple * sspo; ///< Buffer triple for temp values.
  19. };
  20. /*
  21. * Static prototypes.
  22. */
  23. inline static LSUP_rc
  24. graph_iter_next_buffer (LSUP_GraphIterator *it);
  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 (LSUP_Store *store, const char *uri_str, LSUP_NSMap *nsm)
  35. {
  36. // Create a HTable graph by default.
  37. LSUP_Graph *gr;
  38. MALLOC_GUARD (gr, NULL);
  39. gr->store = (store) ? store : LSUP_store_new (LSUP_STORE_HTABLE, NULL, 0);
  40. if (gr->store->sif->nsm_get_fn)
  41. gr->nsm = gr->store->sif->nsm_get_fn (gr->store->data);
  42. else gr->nsm = nsm ? nsm : LSUP_default_nsm;
  43. gr->uri =
  44. uri_str? LSUP_iriref_new (uri_str, gr->nsm) :
  45. LSUP_iriref_new (NULL, NULL);
  46. LOG_DEBUG("Graph created.");
  47. return gr;
  48. }
  49. LSUP_Graph *
  50. LSUP_graph_get_txn (
  51. void *txn, LSUP_Store *store, const LSUP_Term *uri, size_t *ct)
  52. {
  53. LSUP_Buffer *sc = LSUP_term_serialize (uri);
  54. void *it = store->sif->lookup_fn (
  55. store->data, NULL, NULL, NULL, sc, NULL, NULL);
  56. LSUP_Graph *gr = LSUP_graph_new (NULL, uri->data, NULL);
  57. LSUP_BufferTriple *sspo = BTRP_DUMMY;
  58. void *add_it = LSUP_graph_add_init_txn (txn, gr);
  59. size_t _ct = 0;
  60. while (store->sif->lu_next_fn (it, sspo, NULL) == LSUP_OK) {
  61. // This is deserializing a buffer triple that will be re-serialized by
  62. // LSUP_graph_add_iter. But it's necessary to relativize URIs.
  63. LSUP_Triple *spo = LSUP_triple_new_from_btriple (sspo);
  64. LSUP_graph_add_iter (add_it, spo);
  65. LSUP_triple_free (spo);
  66. _ct++;
  67. }
  68. LSUP_graph_add_done (add_it);
  69. store->sif->lu_free_fn(it);
  70. LSUP_buffer_free (sc);
  71. LSUP_btriple_free (sspo);
  72. // Do not create a new graph if no results were found.
  73. if (_ct == 0) {
  74. LSUP_graph_free (gr);
  75. gr = NULL;
  76. }
  77. if (ct) *ct = _ct;
  78. return gr;
  79. }
  80. LSUP_rc
  81. LSUP_graph_bool_op_txn (
  82. void *txn, const LSUP_bool_op op,
  83. const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res)
  84. {
  85. LSUP_rc rc = LSUP_NOACTION;
  86. if (UNLIKELY (
  87. op != LSUP_BOOL_UNION
  88. && op != LSUP_BOOL_SUBTRACTION
  89. && op != LSUP_BOOL_INTERSECTION
  90. && op != LSUP_BOOL_XOR)) {
  91. log_error ("Invalid boolean operation: %d.", op);
  92. return LSUP_VALUE_ERR;
  93. }
  94. /* BEGIN union block. */
  95. if (op == LSUP_BOOL_UNION) {
  96. // No need to use a transaction here: the graph is freed on failure.
  97. rc = LSUP_graph_copy_contents (gr1, res, NULL, NULL, NULL);
  98. PCHECK (rc, fail);
  99. rc = LSUP_graph_copy_contents (gr2, res, NULL, NULL, NULL);
  100. PCHECK (rc, fail);
  101. return LSUP_OK;
  102. }
  103. /* END union block. */
  104. LSUP_Buffer
  105. *res_sc = LSUP_term_serialize (res->uri),
  106. *gr1_sc = LSUP_term_serialize (gr1->uri),
  107. *gr2_sc = LSUP_term_serialize (gr2->uri);
  108. void *lu1_it, *lu2_it, *add_it;
  109. LSUP_BufferTriple *sspo = BTRP_DUMMY;
  110. size_t ct;
  111. // Handle transactions for graphs possibly in different storage back ends.
  112. void
  113. *lu1_txn = NULL,
  114. *lu2_txn = NULL,
  115. *res_txn = NULL;
  116. // Whether gr1 or gr2 txn will be open independently from res txn.
  117. bool
  118. open_txn1 = false,
  119. open_txn2 = false;
  120. add_it = res->store->sif->add_init_fn (res->store->data, res_sc, txn);
  121. if (res->store->sif->features & LSUP_STORE_TXN)
  122. res_txn = res->store->sif->iter_txn_fn (add_it);
  123. /* If either source graph is in the same store as the destination and has
  124. * an open transaction, reuse that transaction. A new reader cannot be
  125. * opened in LMDB while a writer is open already.
  126. */
  127. // Handle gr1 transaction.
  128. if (gr1->store->sif->features & LSUP_STORE_TXN) {
  129. if (gr1->store == res->store) lu1_txn = res_txn;
  130. // FIXME: MDB_RDONLY is implementation-specific and doesn't belong here.
  131. else {
  132. CHECK (gr1->store->sif->txn_begin_fn (
  133. gr1->store->data, MDB_RDONLY, &lu1_txn), fail);
  134. open_txn1 = true;
  135. }
  136. }
  137. // Handle gr2 transaction.
  138. if (gr2->store->sif->features & LSUP_STORE_TXN) {
  139. if (gr2->store == res->store) lu2_txn = res_txn;
  140. else if (gr2->store == gr1->store) lu2_txn = lu1_txn;
  141. // FIXME: see above.
  142. else {
  143. CHECK (gr2->store->sif->txn_begin_fn (
  144. gr2->store->data, MDB_RDONLY, &lu2_txn), fail);
  145. open_txn2 = true;
  146. }
  147. }
  148. LOG_TRACE (
  149. "lu1_txn: %p ; lu2_txn: %p ; res_txn: %p",
  150. lu1_txn, lu2_txn, res_txn);
  151. /* BEGIN XOR block. */
  152. if (op == LSUP_BOOL_XOR) {
  153. // Add triples from gr2 if not found in gr1.
  154. lu2_it = gr2->store->sif->lookup_fn (
  155. gr2->store->data, NULL, NULL, NULL, gr2_sc, lu2_txn, NULL);
  156. while (gr2->store->sif->lu_next_fn (lu2_it, sspo, NULL) == LSUP_OK) {
  157. lu1_it = gr1->store->sif->lookup_fn (
  158. gr1->store->data, sspo->s, sspo->p, sspo->o, gr1_sc,
  159. lu1_txn, &ct);
  160. if (ct == 0) {
  161. res->store->sif->add_iter_fn (add_it, sspo);
  162. rc = LSUP_OK;
  163. }
  164. gr1->store->sif->lu_free_fn (lu1_it);
  165. }
  166. gr2->store->sif->lu_free_fn (lu2_it);
  167. }
  168. /* BEGIN subtraction and intersection block. */
  169. lu1_it = gr1->store->sif->lookup_fn (
  170. gr1->store->data, NULL, NULL, NULL, gr1_sc, lu1_txn, NULL);
  171. while (gr1->store->sif->lu_next_fn (lu1_it, sspo, NULL) == LSUP_OK) {
  172. lu2_it = gr2->store->sif->lookup_fn (
  173. gr2->store->data, sspo->s, sspo->p, sspo->o, gr2_sc,
  174. lu2_txn, &ct);
  175. if (UNLIKELY (!lu2_it)) {
  176. rc = LSUP_DB_ERR;
  177. gr1->store->sif->lu_free_fn (lu1_it);
  178. goto fail;
  179. }
  180. // For XOR and subtraction, add if not found.
  181. // For intersection, add if found.
  182. if ((ct == 0) ^ (op == LSUP_BOOL_INTERSECTION)) {
  183. res->store->sif->add_iter_fn (add_it, sspo);
  184. rc = LSUP_OK;
  185. }
  186. gr2->store->sif->lu_free_fn (lu2_it);
  187. }
  188. gr1->store->sif->lu_free_fn (lu1_it);
  189. if (open_txn1) gr1->store->sif->txn_commit_fn (lu1_txn);
  190. if (open_txn2) gr2->store->sif->txn_commit_fn (lu2_txn);
  191. res->store->sif->add_done_fn (add_it);
  192. LSUP_btriple_free (sspo);
  193. LSUP_buffer_free (res_sc);
  194. LSUP_buffer_free (gr1_sc);
  195. LSUP_buffer_free (gr2_sc);
  196. /* END subtraction, intersection, XOR block. */
  197. return rc;
  198. fail:
  199. if (lu1_txn) gr1->store->sif->txn_abort_fn (lu1_txn);
  200. if (lu2_txn) gr2->store->sif->txn_abort_fn (lu2_txn);
  201. LSUP_graph_free (res);
  202. return rc;
  203. }
  204. void
  205. LSUP_graph_free (LSUP_Graph *gr)
  206. {
  207. if (UNLIKELY (!gr)) return;
  208. LSUP_term_free (gr->uri);
  209. free (gr->store->id);
  210. // If the store has the nsm_get_fn, it's been uniquely created for this
  211. // graph and it's safe to free.
  212. if (gr->store->sif->nsm_get_fn) LSUP_nsmap_free (gr->nsm);
  213. // If the store is a HTable, it means it has been created with the graph
  214. // and must go with it.
  215. if (gr->store->type == LSUP_STORE_HTABLE) {
  216. gr->store->sif->free_fn (gr->store->data);
  217. free (gr->store);
  218. }
  219. free (gr);
  220. }
  221. const LSUP_Term *
  222. LSUP_graph_uri (const LSUP_Graph *gr) { return gr->uri; }
  223. LSUP_Store *
  224. LSUP_graph_store (const LSUP_Graph *gr)
  225. { return gr->store; }
  226. LSUP_rc
  227. LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri_str)
  228. {
  229. LSUP_rc rc = LSUP_OK;
  230. LSUP_Buffer *old_sc = NULL, *new_sc = NULL;
  231. LSUP_Term *uri = LSUP_iriref_new (uri_str, gr->nsm);
  232. if (UNLIKELY (!uri)) {
  233. rc = LSUP_MEM_ERR;
  234. goto finally;
  235. }
  236. // Update context for triples in the graph.
  237. if (gr->store->sif->features & LSUP_STORE_CTX) {
  238. old_sc = LSUP_term_serialize (gr->uri);
  239. new_sc = LSUP_term_serialize (uri);
  240. if (UNLIKELY (!old_sc || !new_sc)) {
  241. rc = LSUP_MEM_ERR;
  242. goto finally;
  243. }
  244. PCHECK (rc = gr->store->sif->update_ctx_fn (
  245. gr->store->data, old_sc, new_sc, NULL), finally);
  246. // Overall success even if rc of underlying fn was LSUP_NOACTION.
  247. if (rc == LSUP_NOACTION) rc = LSUP_OK;
  248. }
  249. LSUP_term_free (gr->uri);
  250. gr->uri = uri;
  251. finally:
  252. if (old_sc) LSUP_buffer_free (old_sc);
  253. if (new_sc) LSUP_buffer_free (new_sc);
  254. return rc;
  255. }
  256. LSUP_NSMap *
  257. LSUP_graph_namespace (const LSUP_Graph *gr)
  258. {
  259. // If nsm_get_fn is not defined, the store has no own NS map.
  260. if (!gr->store->sif->nsm_get_fn) return gr->nsm;
  261. return gr->store->sif->nsm_get_fn (gr->store->data);
  262. }
  263. void
  264. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm)
  265. {
  266. if (!gr->store->sif->nsm_get_fn) gr->nsm = nsm;
  267. else log_warn ("Graph back end has a stored NS map.");
  268. }
  269. size_t
  270. LSUP_graph_size (const LSUP_Graph *gr)
  271. {
  272. size_t ct = 0;
  273. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  274. void *it = gr->store->sif->lookup_fn (
  275. gr->store->data, NULL, NULL, NULL, sc, NULL, &ct);
  276. gr->store->sif->lu_free_fn (it);
  277. LSUP_buffer_free (sc);
  278. return ct;
  279. }
  280. bool
  281. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2)
  282. {
  283. LSUP_Graph *res = LSUP_graph_new (NULL, NULL, NULL);
  284. LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, res);
  285. bool ret = (LSUP_graph_size (res) == 0);
  286. LSUP_graph_free (res);
  287. return ret;
  288. }
  289. LSUP_GraphIterator *
  290. LSUP_graph_add_init_txn (void *txn, LSUP_Graph *gr)
  291. {
  292. LSUP_GraphIterator *it;
  293. CALLOC_GUARD (it, NULL);
  294. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  295. it->data = gr->store->sif->add_init_fn (gr->store->data, sc, txn);
  296. LSUP_buffer_free (sc);
  297. it->graph = gr;
  298. return it;
  299. }
  300. LSUP_rc
  301. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo)
  302. {
  303. LOG_TRACE(
  304. "Adding triple {%s, %s, %s} to %s",
  305. spo->s->data, spo->p->data, spo->o->data,
  306. LSUP_graph_uri(it->graph)->data);
  307. // Make relative s and o.
  308. LSUP_Term *rel_s, *rel_o;
  309. if (LSUP_IS_IRI (spo->s))
  310. rel_s = LSUP_iriref_relative (it->graph->uri, spo->s);
  311. else rel_s = spo->s;
  312. if (LSUP_IS_IRI (spo->o))
  313. rel_o = LSUP_iriref_relative (it->graph->uri, spo->o);
  314. else rel_o = spo->o;
  315. LSUP_Triple *rel_spo = LSUP_triple_new (rel_s, spo->p, rel_o);
  316. LOG_TRACE (
  317. "Adding relative triple: {%s, %s, %s}",
  318. rel_s->data, spo->p->data, rel_o->data);
  319. // Serialize relative triple.
  320. LSUP_BufferTriple *sspo = LSUP_triple_serialize (rel_spo);
  321. if (UNLIKELY (!sspo)) return LSUP_MEM_ERR;
  322. // Selectively free triple members and structure.
  323. if (rel_s != spo->s) LSUP_term_free (rel_s);
  324. if (rel_o != spo->o) LSUP_term_free (rel_o);
  325. free (rel_spo);
  326. const LSUP_StoreInt *sif = it->graph->store->sif;
  327. LSUP_rc rc;
  328. PCHECK (rc = sif->add_iter_fn (it->data, sspo), finally);
  329. // Store datatype term permanently.
  330. if (rc == LSUP_OK && sif->add_term_fn) {
  331. for (int i = 0; i < 3; i++) {
  332. LSUP_Term *term = LSUP_triple_pos (spo, i);
  333. if (term->type == LSUP_TERM_LITERAL) {
  334. LSUP_Buffer *ser_dtype = LSUP_term_serialize (term->datatype);
  335. void *txn =
  336. sif->features & LSUP_STORE_TXN ?
  337. sif->iter_txn_fn (it->data) : NULL;
  338. LSUP_rc term_rc = sif->add_term_fn (
  339. it->graph->store->data, ser_dtype, txn);
  340. PCHECK (term_rc, finally);
  341. LSUP_buffer_free (ser_dtype);
  342. }
  343. }
  344. }
  345. finally:
  346. LSUP_btriple_free (sspo);
  347. return rc;
  348. }
  349. void
  350. LSUP_graph_add_done (LSUP_GraphIterator *it)
  351. {
  352. it->graph->store->sif->add_done_fn (it->data);
  353. free (it);
  354. }
  355. LSUP_rc
  356. LSUP_graph_add_txn (
  357. void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct)
  358. {
  359. LSUP_rc rc = LSUP_NOACTION;
  360. // Initialize iterator.
  361. LSUP_GraphIterator *it = LSUP_graph_add_init_txn (txn, gr);
  362. if (ct) *ct = 0;
  363. // Serialize and insert RDF triples.
  364. for (size_t i = 0; trp[i] != NULL; i++) {
  365. LOG_TRACE("Inserting triple #%lu", i);
  366. LSUP_rc db_rc = LSUP_graph_add_iter (it, trp[i]);
  367. if (db_rc == LSUP_OK) {
  368. rc = LSUP_OK;
  369. if (ct) (*ct)++;
  370. // A duplicate will return LSUP_NOACTION and not increment ct.
  371. }
  372. if (UNLIKELY (db_rc < 0)) {
  373. rc = db_rc;
  374. goto finally;
  375. }
  376. }
  377. finally:
  378. LSUP_graph_add_done (it);
  379. return rc;
  380. }
  381. LSUP_rc
  382. LSUP_graph_remove_txn (
  383. void *txn, LSUP_Graph *gr,
  384. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  385. size_t *ct)
  386. {
  387. LSUP_Buffer
  388. *ss = LSUP_term_serialize (s),
  389. *sp = LSUP_term_serialize (p),
  390. *so = LSUP_term_serialize (o),
  391. *sc = LSUP_term_serialize (gr->uri);
  392. LSUP_rc rc = gr->store->sif->remove_fn (
  393. gr->store->data, ss, sp, so, sc, txn, ct);
  394. LSUP_buffer_free (ss);
  395. LSUP_buffer_free (sp);
  396. LSUP_buffer_free (so);
  397. LSUP_buffer_free (sc);
  398. return rc;
  399. }
  400. LSUP_rc
  401. LSUP_graph_copy_contents_txn (
  402. void *txn, const LSUP_Graph *src, LSUP_Graph *dest,
  403. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o)
  404. {
  405. LSUP_rc rc = LSUP_NOACTION;
  406. LSUP_GraphIterator *it = LSUP_graph_lookup_txn (txn, src, s, p, o, NULL);
  407. LSUP_Triple *spo = NULL;
  408. LSUP_GraphIterator *add_it = LSUP_graph_add_init_txn (txn, dest);
  409. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  410. LSUP_rc add_rc = LSUP_graph_add_iter (add_it, spo);
  411. LSUP_triple_free (spo);
  412. if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
  413. else if (add_rc < 0) {
  414. rc = add_rc;
  415. break;
  416. }
  417. }
  418. LSUP_graph_add_done (add_it);
  419. LSUP_graph_iter_free (it);
  420. return rc;
  421. }
  422. LSUP_GraphIterator *
  423. LSUP_graph_lookup_txn (
  424. void *txn, const LSUP_Graph *gr,
  425. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  426. size_t *ct)
  427. {
  428. LSUP_GraphIterator *it;
  429. MALLOC_GUARD (it, NULL);
  430. // Make relative s and o.
  431. LSUP_Term *rel_s, *rel_o;
  432. if (s && LSUP_IS_IRI (s)) {
  433. rel_s = LSUP_iriref_relative (gr->uri, s);
  434. LOG_DEBUG ("Relative S lookup: %s", rel_s->data);
  435. } else rel_s = (LSUP_Term *)s;
  436. if (o && LSUP_IS_IRI (o)) {
  437. rel_o = LSUP_iriref_relative (gr->uri, o);
  438. LOG_DEBUG ("Relative O lookup: %s", rel_o->data);
  439. } else rel_o = (LSUP_Term *)o;
  440. LSUP_Buffer
  441. *ss = LSUP_term_serialize (rel_s),
  442. *sp = LSUP_term_serialize (p),
  443. *so = LSUP_term_serialize (rel_o),
  444. *sc = LSUP_term_serialize (gr->uri);
  445. // Selectively free triple members and structure.
  446. if (rel_s != s) LSUP_term_free (rel_s);
  447. if (rel_o != o) LSUP_term_free (rel_o);
  448. it->data = gr->store->sif->lookup_fn (
  449. gr->store->data, ss, sp, so, sc, txn, ct);
  450. LSUP_buffer_free (ss);
  451. LSUP_buffer_free (sp);
  452. LSUP_buffer_free (so);
  453. LSUP_buffer_free (sc);
  454. if (UNLIKELY (!it->data)) {
  455. free (it);
  456. return NULL;
  457. }
  458. it->graph = gr;
  459. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  460. // Copy-on-write store.
  461. it->sspo = BTRP_DUMMY;
  462. if (UNLIKELY (it->sspo == NULL)) return NULL;
  463. it->sspo->s->flags |= LSUP_BUF_BORROWED;
  464. it->sspo->p->flags |= LSUP_BUF_BORROWED;
  465. it->sspo->o->flags |= LSUP_BUF_BORROWED;
  466. } else {
  467. // TODO copy-on-retrieval store. No implementations yet.
  468. }
  469. return it;
  470. }
  471. LSUP_rc
  472. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo_p)
  473. {
  474. LSUP_rc rc = graph_iter_next_buffer (it);
  475. PRCCK (rc);
  476. if (rc != LSUP_OK) return rc;
  477. LSUP_Triple *spo = LSUP_triple_new (
  478. LSUP_term_new_from_buffer (it->sspo->s),
  479. LSUP_term_new_from_buffer (it->sspo->p),
  480. LSUP_term_new_from_buffer (it->sspo->o)
  481. );
  482. if (UNLIKELY (!spo)) return LSUP_MEM_ERR;
  483. *spo_p = spo;
  484. return LSUP_OK;
  485. }
  486. const LSUP_Graph *
  487. LSUP_graph_iter_graph (LSUP_GraphIterator *it)
  488. { return it->graph; }
  489. void
  490. LSUP_graph_iter_free (LSUP_GraphIterator *it)
  491. {
  492. if (UNLIKELY (!it)) return;
  493. it->graph->store->sif->lu_free_fn (it->data);
  494. /*
  495. * This deallocates resources properly by preserving borrowed pointers from
  496. * the store in case of LSUP_STORE_COW stores.
  497. */
  498. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  499. LSUP_btriple_free (it->sspo);
  500. LOG_DEBUG("Freeing dummy triple @ %p", it->sspo);
  501. } else {
  502. // TODO copy-on-retrieval stores. None yet.
  503. }
  504. free (it);
  505. }
  506. LSUP_TermSet *
  507. LSUP_graph_list_txn (void *txn, LSUP_Store *store)
  508. {
  509. LSUP_Buffer **tdata = store->sif->ctx_list_fn (store->data, txn);
  510. if (UNLIKELY (!tdata)) return NULL;
  511. LSUP_TermSet *ts = LSUP_term_set_new();
  512. size_t i = 0;
  513. while (tdata[i]) {
  514. LSUP_Term *t = LSUP_term_new_from_buffer (tdata[i]);
  515. LSUP_rc rc = LSUP_term_set_add (ts, t, NULL);
  516. LSUP_buffer_free (tdata[i]);
  517. if (UNLIKELY (rc != LSUP_OK)) {
  518. LSUP_term_free (t);
  519. LSUP_term_set_free (ts);
  520. return NULL;
  521. }
  522. i++;
  523. }
  524. free (tdata);
  525. return ts;
  526. }
  527. bool
  528. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
  529. {
  530. LSUP_GraphIterator *it = LSUP_graph_lookup (
  531. gr, spo->s, spo->p, spo->o, NULL);
  532. LSUP_Triple *tmp_spo = NULL;
  533. bool rc = LSUP_graph_iter_next (it, &tmp_spo) != LSUP_END;
  534. LSUP_triple_free (tmp_spo);
  535. LSUP_graph_iter_free (it);
  536. return rc;
  537. }
  538. void
  539. LSUP_graph_print (const LSUP_Graph *gr)
  540. {
  541. size_t ct;
  542. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, &ct);
  543. if (UNLIKELY (!it)) {
  544. log_error ("Could not inspect graph for printing.");
  545. return;
  546. }
  547. printf ("*** Graph %s (%zu triples):\n\n", gr->uri->data, ct);
  548. LSUP_Triple *spo = NULL;
  549. ct = 0;
  550. LSUP_rc rc = LSUP_NORESULT;
  551. while ((rc = LSUP_graph_iter_next (it, &spo)) == LSUP_OK) {
  552. printf (
  553. "#%-6zu {%s %s %s}\n",
  554. ct, spo->s->data, spo->p->data, spo->o->data);
  555. ct++;
  556. }
  557. if (rc != LSUP_END)
  558. log_error (
  559. "Output truncated due to abnormal return: %s",
  560. LSUP_strerror (rc));
  561. }
  562. LSUP_LinkMap *
  563. LSUP_graph_connections (
  564. const LSUP_Graph *gr, const LSUP_Term *t, const LSUP_LinkType type)
  565. {
  566. const LSUP_Term
  567. *s = NULL,
  568. *p = NULL,
  569. *o = NULL;
  570. // Position of passed term and link terms, respectively.
  571. LSUP_TriplePos pos1, pos2;
  572. if (type == LSUP_LINK_INBOUND) {
  573. o = t;
  574. pos1 = TRP_POS_O;
  575. pos2 = TRP_POS_P;
  576. } else if (type == LSUP_LINK_OUTBOUND) {
  577. s = t;
  578. pos1 = TRP_POS_S;
  579. pos2 = TRP_POS_P;
  580. } else if (type == LSUP_LINK_EDGE) {
  581. p = t;
  582. pos1 = TRP_POS_P;
  583. pos2 = TRP_POS_S;
  584. } else {
  585. log_error ("Invalid connection type: %d", type);
  586. return NULL;
  587. }
  588. // Gather all linking terms in a set first.
  589. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
  590. if (!it) return NULL;
  591. LSUP_TermSet *lts = LSUP_term_set_new();
  592. while (graph_iter_next_buffer (it) != LSUP_END) {
  593. LSUP_Term
  594. *ex = NULL,
  595. *ins = LSUP_term_new_from_buffer (
  596. LSUP_btriple_pos (it->sspo, pos2));
  597. LSUP_term_set_add (lts, ins, &ex);
  598. if (ex) LSUP_term_free (ins);
  599. }
  600. LSUP_graph_iter_free(it);
  601. LSUP_LinkMap *ret = LSUP_link_map_new (t, type);
  602. if (!ret) return NULL;
  603. size_t i = 0;
  604. LSUP_Term *lt;
  605. while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {
  606. LSUP_link_map_add (
  607. ret, LSUP_term_copy (lt),
  608. LSUP_graph_term_set (gr, t, pos1, lt, pos2));
  609. }
  610. LSUP_term_set_free (lts);
  611. return ret;
  612. }
  613. LSUP_TermSet *
  614. LSUP_graph_term_set (
  615. const LSUP_Graph *gr, const LSUP_Term *t1, const LSUP_TriplePos t1_pos,
  616. const LSUP_Term *t2, const LSUP_TriplePos t2_pos)
  617. {
  618. if (t1_pos == t2_pos) {
  619. log_error ("Term 1 and 2 positions cannot be the same!");
  620. return NULL;
  621. }
  622. const LSUP_Term *spo_l[3] = {NULL};
  623. spo_l[t1_pos] = t1;
  624. spo_l[t2_pos] = t2;
  625. LSUP_TriplePos rpos = 0; // Position of term to be added to results.
  626. for (unsigned i = 0; i < 3; i++)
  627. if (t1_pos != i && t2_pos != i) rpos = i;
  628. LSUP_GraphIterator *it = LSUP_graph_lookup (
  629. gr, spo_l[0], spo_l[1], spo_l[2], NULL);
  630. LSUP_TermSet *ts = LSUP_term_set_new();
  631. while (graph_iter_next_buffer (it) != LSUP_END) {
  632. // There cannot be duplicates in a 2-bound lookup.
  633. LSUP_term_set_add (
  634. ts,
  635. LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, rpos)),
  636. NULL);
  637. }
  638. LSUP_graph_iter_free (it);
  639. return ts;
  640. }
  641. LSUP_TermSet *
  642. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos)
  643. {
  644. // TODO We should use spo indices for stores that have them...
  645. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, NULL);
  646. LSUP_TermSet *ts = LSUP_term_set_new();
  647. while (graph_iter_next_buffer (it) != LSUP_END) {
  648. LSUP_Term
  649. *ex = NULL,
  650. *ins = LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, pos));
  651. LSUP_term_set_add (ts, ins, &ex);
  652. if (ex) LSUP_term_free (ins);
  653. }
  654. LSUP_graph_iter_free(it);
  655. return ts;
  656. }
  657. size_t
  658. LSUP_graph_add_link_map (LSUP_GraphIterator *it, LSUP_LinkMap *lm)
  659. {
  660. LSUP_Triple *spo = TRP_DUMMY;
  661. size_t ct = 0;
  662. LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lm);
  663. while (LSUP_link_map_triples (lmit, spo) != LSUP_END) {
  664. LSUP_rc rc = LSUP_graph_add_iter (it, spo);
  665. if (rc >= 0) ct++;
  666. PRCCK (rc);
  667. }
  668. LSUP_link_map_iter_free (lmit);
  669. free (spo);
  670. return ct;
  671. }
  672. LSUP_Term *
  673. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  674. {
  675. LSUP_NSMap *nsm = LSUP_graph_namespace (LSUP_graph_iter_graph (it));
  676. LSUP_Term
  677. *s = LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL),
  678. *rdf_first = LSUP_iriref_new ("rdf:first", nsm),
  679. *rdf_rest = LSUP_iriref_new ("rdf:rest", nsm),
  680. *rdf_nil = LSUP_iriref_new ("rdf:nil", nsm),
  681. *link;
  682. LSUP_Triple *spo = TRP_DUMMY;
  683. link = s;
  684. size_t i = 0;
  685. LSUP_Term *t;
  686. while (LSUP_term_set_next (ts, &i, &t) != LSUP_END) {
  687. spo->s = link;
  688. spo->p = rdf_first;
  689. spo->o = t;
  690. PRCNL (LSUP_graph_add_iter (it, spo));
  691. spo->p = rdf_rest;
  692. size_t save_i = i; // Save iterator position to restore it after peek.
  693. spo->o = (
  694. // Peek into the next result.
  695. LSUP_term_set_next (ts, &i, NULL) != LSUP_END ?
  696. LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL)
  697. : rdf_nil);
  698. i = save_i; // Restore the iterator that advanced when peeking.
  699. PRCNL (LSUP_graph_add_iter (it, spo));
  700. if (link != s) LSUP_term_free (link);
  701. // Current object becomes next subject. Irrelevant for last item.
  702. link = spo->o;
  703. }
  704. LSUP_term_free (rdf_first);
  705. LSUP_term_free (rdf_rest);
  706. LSUP_term_free (rdf_nil);
  707. free (spo);
  708. return s;
  709. }
  710. /*
  711. * Static functions.
  712. */
  713. /** @brief Advance an iterator and return a serialized triple.
  714. *
  715. * This is an internal function to pass raw buffers between higher-level
  716. * functions without serializing and deserializing triples.
  717. *
  718. * The results are stored in it->sspo.
  719. */
  720. inline static LSUP_rc
  721. graph_iter_next_buffer (LSUP_GraphIterator *it)
  722. { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
  723. /**
  724. * Extern inline definitions.
  725. */
  726. size_t LSUP_graph_size (const LSUP_Graph *gr);