graph.c 24 KB

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