graph.c 24 KB

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