graph.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. if (!store) store = LSUP_store_new (LSUP_STORE_HTABLE, NULL, 0);
  38. LSUP_Graph *gr;
  39. MALLOC_GUARD (gr, NULL);
  40. gr->uri =
  41. uri_str? LSUP_iriref_new (uri_str, nsm) :
  42. LSUP_iriref_new (NULL, NULL);
  43. gr->store = store;
  44. if (gr->store->sif->features & LSUP_STORE_PERM) gr->nsm = NULL;
  45. else gr->nsm = nsm ? nsm : LSUP_default_nsm;
  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 is a HTable, it means it has been created with the graph
  211. // and must go with it.
  212. if (gr->store->type == LSUP_STORE_HTABLE) {
  213. gr->store->sif->free_fn (gr->store->data);
  214. free (gr->store);
  215. }
  216. free (gr);
  217. }
  218. const LSUP_Term *
  219. LSUP_graph_uri (const LSUP_Graph *gr) { return gr->uri; }
  220. LSUP_Store *
  221. LSUP_graph_store (const LSUP_Graph *gr)
  222. { return gr->store; }
  223. LSUP_rc
  224. LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri_str)
  225. {
  226. LSUP_rc rc = LSUP_OK;
  227. LSUP_Buffer *old_sc = NULL, *new_sc = NULL;
  228. LSUP_Term *uri = LSUP_iriref_new (uri_str, gr->nsm);
  229. if (UNLIKELY (!uri)) {
  230. rc = LSUP_MEM_ERR;
  231. goto finally;
  232. }
  233. // Update context for triples in the graph.
  234. if (gr->store->sif->features & LSUP_STORE_CTX) {
  235. old_sc = LSUP_term_serialize (gr->uri);
  236. new_sc = LSUP_term_serialize (uri);
  237. if (UNLIKELY (!old_sc || !new_sc)) {
  238. rc = LSUP_MEM_ERR;
  239. goto finally;
  240. }
  241. PCHECK (rc = gr->store->sif->update_ctx_fn (
  242. gr->store->data, old_sc, new_sc, NULL), finally);
  243. // Overall success even if rc of underlying fn was LSUP_NOACTION.
  244. if (rc == LSUP_NOACTION) rc = LSUP_OK;
  245. }
  246. LSUP_term_free (gr->uri);
  247. gr->uri = uri;
  248. finally:
  249. if (old_sc) LSUP_buffer_free (old_sc);
  250. if (new_sc) LSUP_buffer_free (new_sc);
  251. return rc;
  252. }
  253. LSUP_NSMap *
  254. LSUP_graph_namespace (const LSUP_Graph *gr)
  255. {
  256. // If nsm_get_fn is not defined, the store has no own NS map.
  257. if (!gr->store->sif->nsm_get_fn) return gr->nsm;
  258. return gr->store->sif->nsm_get_fn (gr->store->data);
  259. }
  260. void
  261. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm)
  262. {
  263. if (!gr->store->sif->nsm_get_fn) gr->nsm = nsm;
  264. else log_warn ("Graph back end has a stored NS map.");
  265. }
  266. size_t
  267. LSUP_graph_size (const LSUP_Graph *gr)
  268. {
  269. size_t ct = 0;
  270. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  271. void *it = gr->store->sif->lookup_fn (
  272. gr->store->data, NULL, NULL, NULL, sc, NULL, &ct);
  273. gr->store->sif->lu_free_fn (it);
  274. LSUP_buffer_free (sc);
  275. return ct;
  276. }
  277. bool
  278. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2)
  279. {
  280. LSUP_Graph *res = LSUP_graph_new (NULL, NULL, NULL);
  281. LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, res);
  282. bool ret = (LSUP_graph_size (res) == 0);
  283. LSUP_graph_free (res);
  284. return ret;
  285. }
  286. LSUP_GraphIterator *
  287. LSUP_graph_add_init_txn (void *txn, LSUP_Graph *gr)
  288. {
  289. LSUP_GraphIterator *it;
  290. CALLOC_GUARD (it, NULL);
  291. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  292. it->data = gr->store->sif->add_init_fn (gr->store->data, sc, txn);
  293. LSUP_buffer_free (sc);
  294. it->graph = gr;
  295. return it;
  296. }
  297. LSUP_rc
  298. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo)
  299. {
  300. LOG_TRACE(
  301. "Adding triple {%s, %s, %s} to %s",
  302. spo->s->data, spo->p->data, spo->o->data,
  303. LSUP_graph_uri(it->graph)->data);
  304. // Make relative s and o.
  305. LSUP_Term *rel_s, *rel_o;
  306. if (LSUP_IS_IRI (spo->s))
  307. rel_s = LSUP_iriref_relative (it->graph->uri, spo->s);
  308. else rel_s = spo->s;
  309. if (LSUP_IS_IRI (spo->o))
  310. rel_o = LSUP_iriref_relative (it->graph->uri, spo->o);
  311. else rel_o = spo->o;
  312. LSUP_Triple *rel_spo = LSUP_triple_new (rel_s, spo->p, rel_o);
  313. LOG_TRACE (
  314. "Adding relative triple: {%s, %s, %s}",
  315. rel_s->data, spo->p->data, rel_o->data);
  316. // Serialize relative triple.
  317. LSUP_BufferTriple *sspo = LSUP_triple_serialize (rel_spo);
  318. if (UNLIKELY (!sspo)) return LSUP_MEM_ERR;
  319. // Selectively free triple members and structure.
  320. if (rel_s != spo->s) LSUP_term_free (rel_s);
  321. if (rel_o != spo->o) LSUP_term_free (rel_o);
  322. free (rel_spo);
  323. const LSUP_StoreInt *sif = it->graph->store->sif;
  324. LSUP_rc rc;
  325. PCHECK (rc = sif->add_iter_fn (it->data, sspo), finally);
  326. // Store datatype term permanently.
  327. if (rc == LSUP_OK && sif->add_term_fn) {
  328. for (int i = 0; i < 3; i++) {
  329. LSUP_Term *term = LSUP_triple_pos (spo, i);
  330. if (term->type == LSUP_TERM_LITERAL) {
  331. LSUP_Buffer *ser_dtype = LSUP_term_serialize (term->datatype);
  332. void *txn =
  333. sif->features & LSUP_STORE_TXN ?
  334. sif->iter_txn_fn (it->data) : NULL;
  335. LSUP_rc term_rc = sif->add_term_fn (
  336. it->graph->store->data, ser_dtype, txn);
  337. PCHECK (term_rc, finally);
  338. LSUP_buffer_free (ser_dtype);
  339. }
  340. }
  341. }
  342. finally:
  343. LSUP_btriple_free (sspo);
  344. return rc;
  345. }
  346. void
  347. LSUP_graph_add_done (LSUP_GraphIterator *it)
  348. {
  349. it->graph->store->sif->add_done_fn (it->data);
  350. free (it);
  351. }
  352. LSUP_rc
  353. LSUP_graph_add_txn (
  354. void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct)
  355. {
  356. LSUP_rc rc = LSUP_NOACTION;
  357. // Initialize iterator.
  358. LSUP_GraphIterator *it = LSUP_graph_add_init_txn (txn, gr);
  359. if (ct) *ct = 0;
  360. // Serialize and insert RDF triples.
  361. for (size_t i = 0; trp[i] != NULL; i++) {
  362. LOG_TRACE("Inserting triple #%lu", i);
  363. LSUP_rc db_rc = LSUP_graph_add_iter (it, trp[i]);
  364. if (db_rc == LSUP_OK) {
  365. rc = LSUP_OK;
  366. if (ct) (*ct)++;
  367. // A duplicate will return LSUP_NOACTION and not increment ct.
  368. }
  369. if (UNLIKELY (db_rc < 0)) {
  370. rc = db_rc;
  371. goto finally;
  372. }
  373. }
  374. finally:
  375. LSUP_graph_add_done (it);
  376. return rc;
  377. }
  378. LSUP_rc
  379. LSUP_graph_remove_txn (
  380. void *txn, LSUP_Graph *gr,
  381. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  382. size_t *ct)
  383. {
  384. LSUP_Buffer
  385. *ss = LSUP_term_serialize (s),
  386. *sp = LSUP_term_serialize (p),
  387. *so = LSUP_term_serialize (o),
  388. *sc = LSUP_term_serialize (gr->uri);
  389. LSUP_rc rc = gr->store->sif->remove_fn (
  390. gr->store->data, ss, sp, so, sc, txn, ct);
  391. LSUP_buffer_free (ss);
  392. LSUP_buffer_free (sp);
  393. LSUP_buffer_free (so);
  394. LSUP_buffer_free (sc);
  395. return rc;
  396. }
  397. LSUP_rc
  398. LSUP_graph_copy_contents_txn (
  399. void *txn, const LSUP_Graph *src, LSUP_Graph *dest,
  400. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o)
  401. {
  402. LSUP_rc rc = LSUP_NOACTION;
  403. LSUP_GraphIterator *it = LSUP_graph_lookup_txn (txn, src, s, p, o, NULL);
  404. LSUP_Triple *spo = NULL;
  405. LSUP_GraphIterator *add_it = LSUP_graph_add_init_txn (txn, dest);
  406. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  407. LSUP_rc add_rc = LSUP_graph_add_iter (add_it, spo);
  408. LSUP_triple_free (spo);
  409. if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
  410. else if (add_rc < 0) {
  411. rc = add_rc;
  412. break;
  413. }
  414. }
  415. LSUP_graph_add_done (add_it);
  416. LSUP_graph_iter_free (it);
  417. return rc;
  418. }
  419. LSUP_GraphIterator *
  420. LSUP_graph_lookup_txn (
  421. void *txn, const LSUP_Graph *gr,
  422. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  423. size_t *ct)
  424. {
  425. LSUP_GraphIterator *it;
  426. MALLOC_GUARD (it, NULL);
  427. // Make relative s and o.
  428. LSUP_Term *rel_s, *rel_o;
  429. if (s && LSUP_IS_IRI (s)) {
  430. rel_s = LSUP_iriref_relative (gr->uri, s);
  431. LOG_DEBUG ("Relative S lookup: %s", rel_s->data);
  432. } else rel_s = (LSUP_Term *)s;
  433. if (o && LSUP_IS_IRI (o)) {
  434. rel_o = LSUP_iriref_relative (gr->uri, o);
  435. LOG_DEBUG ("Relative O lookup: %s", rel_o->data);
  436. } else rel_o = (LSUP_Term *)o;
  437. LSUP_Buffer
  438. *ss = LSUP_term_serialize (rel_s),
  439. *sp = LSUP_term_serialize (p),
  440. *so = LSUP_term_serialize (rel_o),
  441. *sc = LSUP_term_serialize (gr->uri);
  442. // Selectively free triple members and structure.
  443. if (rel_s != s) LSUP_term_free (rel_s);
  444. if (rel_o != o) LSUP_term_free (rel_o);
  445. it->data = gr->store->sif->lookup_fn (
  446. gr->store->data, ss, sp, so, sc, txn, ct);
  447. LSUP_buffer_free (ss);
  448. LSUP_buffer_free (sp);
  449. LSUP_buffer_free (so);
  450. LSUP_buffer_free (sc);
  451. if (UNLIKELY (!it->data)) {
  452. free (it);
  453. return NULL;
  454. }
  455. it->graph = gr;
  456. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  457. // Copy-on-write store.
  458. it->sspo = BTRP_DUMMY;
  459. if (UNLIKELY (it->sspo == NULL)) return NULL;
  460. it->sspo->s->flags |= LSUP_BUF_BORROWED;
  461. it->sspo->p->flags |= LSUP_BUF_BORROWED;
  462. it->sspo->o->flags |= LSUP_BUF_BORROWED;
  463. } else {
  464. // TODO copy-on-retrieval store. No implementations yet.
  465. }
  466. return it;
  467. }
  468. LSUP_rc
  469. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo_p)
  470. {
  471. LSUP_rc rc = graph_iter_next_buffer (it);
  472. PRCCK (rc);
  473. if (rc != LSUP_OK) return rc;
  474. LSUP_Triple *spo = LSUP_triple_new (
  475. LSUP_term_new_from_buffer (it->sspo->s),
  476. LSUP_term_new_from_buffer (it->sspo->p),
  477. LSUP_term_new_from_buffer (it->sspo->o)
  478. );
  479. if (UNLIKELY (!spo)) return LSUP_MEM_ERR;
  480. *spo_p = spo;
  481. return LSUP_OK;
  482. }
  483. const LSUP_Graph *
  484. LSUP_graph_iter_graph (LSUP_GraphIterator *it)
  485. { return it->graph; }
  486. void
  487. LSUP_graph_iter_free (LSUP_GraphIterator *it)
  488. {
  489. it->graph->store->sif->lu_free_fn (it->data);
  490. /*
  491. * This deallocates resources properly by preserving borrowed pointers from
  492. * the store in case of LSUP_STORE_COW stores.
  493. */
  494. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  495. LSUP_btriple_free (it->sspo);
  496. LOG_DEBUG("Freeing dummy triple @ %p", it->sspo);
  497. } else {
  498. // TODO copy-on-retrieval stores. None yet.
  499. }
  500. free (it);
  501. }
  502. bool
  503. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
  504. {
  505. LSUP_GraphIterator *it = LSUP_graph_lookup (
  506. gr, spo->s, spo->p, spo->o, NULL);
  507. LSUP_Triple *tmp_spo = NULL;
  508. bool rc = LSUP_graph_iter_next (it, &tmp_spo) != LSUP_END;
  509. LSUP_triple_free (tmp_spo);
  510. LSUP_graph_iter_free (it);
  511. return rc;
  512. }
  513. void
  514. LSUP_graph_print (const LSUP_Graph *gr)
  515. {
  516. size_t ct;
  517. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, &ct);
  518. if (UNLIKELY (!it)) {
  519. log_error ("Could not inspect graph for printing.");
  520. return;
  521. }
  522. printf ("*** Graph %s (%zu triples):\n\n", gr->uri->data, ct);
  523. LSUP_Triple *spo = NULL;
  524. ct = 0;
  525. LSUP_rc rc = LSUP_NORESULT;
  526. while ((rc = LSUP_graph_iter_next (it, &spo)) == LSUP_OK) {
  527. printf (
  528. "#%-6zu {%s %s %s}\n",
  529. ct, spo->s->data, spo->p->data, spo->o->data);
  530. ct++;
  531. }
  532. if (rc != LSUP_END)
  533. log_error (
  534. "Output truncated due to abnormal return: %s",
  535. LSUP_strerror (rc));
  536. }
  537. LSUP_LinkMap *
  538. LSUP_graph_connections (
  539. const LSUP_Graph *gr, const LSUP_Term *t, const LSUP_LinkType type)
  540. {
  541. const LSUP_Term
  542. *s = NULL,
  543. *p = NULL,
  544. *o = NULL;
  545. // Position of passed term and link terms, respectively.
  546. LSUP_TriplePos pos1, pos2;
  547. if (type == LSUP_LINK_INBOUND) {
  548. o = t;
  549. pos1 = TRP_POS_O;
  550. pos2 = TRP_POS_P;
  551. } else if (type == LSUP_LINK_OUTBOUND) {
  552. s = t;
  553. pos1 = TRP_POS_S;
  554. pos2 = TRP_POS_P;
  555. } else if (type == LSUP_LINK_EDGE) {
  556. p = t;
  557. pos1 = TRP_POS_P;
  558. pos2 = TRP_POS_S;
  559. } else {
  560. log_error ("Invalid connection type: %d", type);
  561. return NULL;
  562. }
  563. // Gather all linking terms in a set first.
  564. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
  565. LSUP_TermSet *lts = LSUP_term_set_new();
  566. while (graph_iter_next_buffer (it) != LSUP_END) {
  567. LSUP_Term
  568. *ex = NULL,
  569. *ins = LSUP_term_new_from_buffer (
  570. LSUP_btriple_pos (it->sspo, pos2));
  571. LSUP_term_set_add (lts, ins, &ex);
  572. if (ex) LSUP_term_free (ins);
  573. }
  574. LSUP_graph_iter_free(it);
  575. LSUP_LinkMap *ret = LSUP_link_map_new (t, type);
  576. size_t i = 0;
  577. LSUP_Term *lt;
  578. while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {
  579. LSUP_link_map_add (
  580. ret, LSUP_term_copy (lt),
  581. LSUP_graph_term_set (gr, t, pos1, lt, pos2));
  582. }
  583. LSUP_term_set_free (lts);
  584. return ret;
  585. }
  586. LSUP_TermSet *
  587. LSUP_graph_term_set (
  588. const LSUP_Graph *gr, const LSUP_Term *t1, const LSUP_TriplePos t1_pos,
  589. const LSUP_Term *t2, const LSUP_TriplePos t2_pos)
  590. {
  591. if (t1_pos == t2_pos) {
  592. log_error ("Term 1 and 2 positions cannot be the same!");
  593. return NULL;
  594. }
  595. const LSUP_Term *spo_l[3] = {NULL};
  596. spo_l[t1_pos] = t1;
  597. spo_l[t2_pos] = t2;
  598. LSUP_TriplePos rpos = 0; // Position of term to be added to results.
  599. for (unsigned i = 0; i < 3; i++)
  600. if (t1_pos != i && t2_pos != i) rpos = i;
  601. LSUP_GraphIterator *it = LSUP_graph_lookup (
  602. gr, spo_l[0], spo_l[1], spo_l[2], NULL);
  603. LSUP_TermSet *ts = LSUP_term_set_new();
  604. while (graph_iter_next_buffer (it) != LSUP_END) {
  605. // There cannot be duplicates in a 2-bound lookup.
  606. LSUP_term_set_add (
  607. ts,
  608. LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, rpos)),
  609. NULL);
  610. }
  611. LSUP_graph_iter_free (it);
  612. return ts;
  613. }
  614. LSUP_TermSet *
  615. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos)
  616. {
  617. // TODO We should use spo indices for stores that have them...
  618. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, NULL);
  619. LSUP_TermSet *ts = LSUP_term_set_new();
  620. while (graph_iter_next_buffer (it) != LSUP_END) {
  621. LSUP_Term
  622. *ex = NULL,
  623. *ins = LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, pos));
  624. LSUP_term_set_add (ts, ins, &ex);
  625. if (ex) LSUP_term_free (ins);
  626. }
  627. LSUP_graph_iter_free(it);
  628. return ts;
  629. }
  630. size_t
  631. LSUP_graph_add_link_map (LSUP_GraphIterator *it, LSUP_LinkMap *lm)
  632. {
  633. LSUP_Triple *spo = TRP_DUMMY;
  634. size_t ct = 0;
  635. LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lm);
  636. while (LSUP_link_map_triples (lmit, spo) != LSUP_END) {
  637. LSUP_rc rc = LSUP_graph_add_iter (it, spo);
  638. if (rc >= 0) ct++;
  639. PRCCK (rc);
  640. }
  641. LSUP_link_map_iter_free (lmit);
  642. free (spo);
  643. return ct;
  644. }
  645. LSUP_Term *
  646. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  647. {
  648. LSUP_NSMap *nsm = LSUP_graph_namespace (LSUP_graph_iter_graph (it));
  649. LSUP_Term
  650. *s = LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL),
  651. *rdf_first = LSUP_iriref_new ("rdf:first", nsm),
  652. *rdf_rest = LSUP_iriref_new ("rdf:rest", nsm),
  653. *rdf_nil = LSUP_iriref_new ("rdf:nil", nsm),
  654. *link;
  655. LSUP_Triple *spo = TRP_DUMMY;
  656. link = s;
  657. size_t i = 0;
  658. LSUP_Term *t;
  659. while (LSUP_term_set_next (ts, &i, &t) != LSUP_END) {
  660. spo->s = link;
  661. spo->p = rdf_first;
  662. spo->o = t;
  663. PRCNL (LSUP_graph_add_iter (it, spo));
  664. spo->p = rdf_rest;
  665. size_t save_i = i; // Save iterator position to restore it after peek.
  666. spo->o = (
  667. // Peek into the next result.
  668. LSUP_term_set_next (ts, &i, NULL) != LSUP_END ?
  669. LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL)
  670. : rdf_nil);
  671. i = save_i; // Restore the iterator that advanced when peeking.
  672. PRCNL (LSUP_graph_add_iter (it, spo));
  673. if (link != s) LSUP_term_free (link);
  674. // Current object becomes next subject. Irrelevant for last item.
  675. link = spo->o;
  676. }
  677. LSUP_term_free (rdf_first);
  678. LSUP_term_free (rdf_rest);
  679. LSUP_term_free (rdf_nil);
  680. free (spo);
  681. return s;
  682. }
  683. /*
  684. * Static functions.
  685. */
  686. /** @brief Advance an iterator and return a serialized triple.
  687. *
  688. * This is an internal function to pass raw buffers between higher-level
  689. * functions without serializing and deserializing triples.
  690. *
  691. * The results are stored in it->sspo.
  692. */
  693. inline static LSUP_rc
  694. graph_iter_next_buffer (LSUP_GraphIterator *it)
  695. { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
  696. /**
  697. * Extern inline definitions.
  698. */
  699. size_t LSUP_graph_size (const LSUP_Graph *gr);