graph.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include "graph.h"
  2. /*
  3. * Data types.
  4. */
  5. struct graph_t {
  6. LSUP_Term *uri; ///< Graph "name" (URI).
  7. LSUP_Store * store; ///< Store handle.
  8. LSUP_NSMap * nsm; /**< Namespace map.
  9. *
  10. * NOTE: This is
  11. * NULL for permanent stores.
  12. */
  13. void * txn; ///< Store transaction.
  14. };
  15. struct graph_iter_t {
  16. const LSUP_Graph * graph; ///< Parent graph.
  17. void * data; ///< Iterator state.
  18. size_t ct; ///< Total lookup matches.
  19. LSUP_BufferTriple * sspo; ///< Buffer triple for temp values.
  20. };
  21. /*
  22. * Static prototypes.
  23. */
  24. inline static LSUP_rc
  25. graph_iter_next_buffer (LSUP_GraphIterator *it);
  26. inline static LSUP_rc
  27. graph_iter_alloc_buffers (LSUP_GraphIterator *it);
  28. #define ENTRY(a, b) (be) == (LSUP_STORE_##a) ||
  29. static inline bool
  30. check_backend (LSUP_StoreType be)
  31. { return (BACKEND_TBL false); }
  32. #undef ENTRY
  33. /*
  34. * Graph API.
  35. */
  36. LSUP_Graph *
  37. LSUP_graph_new (
  38. LSUP_Term *uri, const LSUP_StoreType store_type, const char *store_id,
  39. LSUP_NSMap *nsm, size_t size)
  40. {
  41. if (UNLIKELY (!LSUP_IS_INIT)) {
  42. log_error (
  43. "Environment is not initialized. Did you call LSUP_init()?");
  44. return NULL;
  45. }
  46. if (UNLIKELY (!check_backend (store_type))) {
  47. log_error ("Not a valid store type: %d", store_type);
  48. return NULL;
  49. }
  50. LSUP_Graph *gr;
  51. MALLOC_GUARD (gr, NULL);
  52. gr->uri = uri;
  53. MALLOC_GUARD (gr->store, NULL);
  54. gr->store->type = store_type;
  55. gr->store->id = store_id ? strdup (store_id) : NULL;
  56. // TODO implement custom default context.
  57. gr->store->data = gr->store->sif->new_fn (store_id, size);
  58. if (gr->store->sif->features & LSUP_STORE_PERM) gr->nsm = NULL;
  59. else gr->nsm = nsm ? nsm : LSUP_default_nsm;
  60. gr->txn = NULL;
  61. log_debug ("Graph created.");
  62. return gr;
  63. }
  64. LSUP_rc
  65. LSUP_graph_bool_op(
  66. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2,
  67. LSUP_Graph *res)
  68. {
  69. LSUP_rc rc = LSUP_NOACTION;
  70. if (UNLIKELY (
  71. op != LSUP_BOOL_UNION
  72. && op != LSUP_BOOL_SUBTRACTION
  73. && op != LSUP_BOOL_INTERSECTION
  74. && op != LSUP_BOOL_XOR)) {
  75. log_error ("Invalid boolean operation: %d.", op);
  76. return LSUP_VALUE_ERR;
  77. }
  78. if (op == LSUP_BOOL_UNION) {
  79. rc = LSUP_graph_copy_contents (gr1, res);
  80. PCHECK (rc, fail);
  81. rc = LSUP_graph_copy_contents (gr2, res);
  82. PCHECK (rc, fail);
  83. return LSUP_OK;
  84. }
  85. LSUP_Buffer
  86. *res_sc = LSUP_term_serialize (res->uri),
  87. *gr1_sc = LSUP_term_serialize (gr1->uri),
  88. *gr2_sc = LSUP_term_serialize (gr2->uri);
  89. void *lu1_it, *lu2_it, *add_it;
  90. LSUP_BufferTriple *sspo = BTRP_DUMMY;
  91. size_t ct;
  92. add_it = res->store->sif->add_init_fn (res->store->data, res_sc, gr1->txn);
  93. if (op == LSUP_BOOL_XOR) {
  94. // Add triples from gr2 if not found in gr1.
  95. lu2_it = gr2->store->sif->lookup_fn (
  96. gr2->store->data, NULL, NULL, NULL, gr2_sc, NULL, gr1->txn);
  97. while (gr2->store->sif->lu_next_fn (lu2_it, sspo, NULL) == LSUP_OK) {
  98. lu1_it = gr1->store->sif->lookup_fn (
  99. gr1->store->data, sspo->s, sspo->p, sspo->o, gr1_sc,
  100. gr1->txn, &ct);
  101. if (ct > 0)
  102. res->store->sif->add_iter_fn (add_it, sspo);
  103. gr1->store->sif->lu_free_fn (lu1_it);
  104. }
  105. gr2->store->sif->lu_free_fn (lu2_it);
  106. }
  107. lu1_it = gr1->store->sif->lookup_fn (
  108. gr1->store->data, NULL, NULL, NULL, gr1_sc, gr1->txn, NULL);
  109. while (gr1->store->sif->lu_next_fn (lu1_it, sspo, NULL) == LSUP_OK) {
  110. lu2_it = gr2->store->sif->lookup_fn (
  111. gr2->store->data, sspo->s, sspo->p, sspo->o, gr2_sc,
  112. gr1->txn, &ct);
  113. // For XOR and subtraction, add if not found.
  114. // For intersection, add if found.
  115. if ((ct == 0) ^ (op == LSUP_BOOL_INTERSECTION))
  116. res->store->sif->add_iter_fn (add_it, sspo);
  117. gr2->store->sif->lu_free_fn (lu2_it);
  118. }
  119. gr1->store->sif->lu_free_fn (lu1_it);
  120. res->store->sif->add_done_fn (add_it);
  121. LSUP_buffer_free (res_sc);
  122. LSUP_buffer_free (gr1_sc);
  123. LSUP_buffer_free (gr2_sc);
  124. return rc;
  125. fail:
  126. LSUP_graph_free (res);
  127. return rc;
  128. }
  129. void
  130. LSUP_graph_free (LSUP_Graph *gr)
  131. {
  132. if (UNLIKELY (!gr)) return;
  133. LSUP_term_free (gr->uri);
  134. free (gr->store->id);
  135. gr->store->sif->free_fn (gr->store->data);
  136. free (gr->store);
  137. free (gr);
  138. }
  139. LSUP_Term *
  140. LSUP_graph_uri (const LSUP_Graph *gr) { return gr->uri; }
  141. LSUP_rc
  142. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri)
  143. {
  144. if (!LSUP_IS_IRI (uri)) {
  145. log_error ("Term provided is not a IRI.");
  146. return LSUP_VALUE_ERR;
  147. }
  148. LSUP_term_free (gr->uri);
  149. gr->uri = uri;
  150. return LSUP_OK;
  151. }
  152. LSUP_NSMap *
  153. LSUP_graph_namespace (const LSUP_Graph *gr)
  154. {
  155. // If nsm_get_fn is not defined, the store has no own NS map.
  156. if (!gr->store->sif->nsm_get_fn) return gr->nsm;
  157. return gr->store->sif->nsm_get_fn (gr->store->data);
  158. }
  159. void
  160. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm)
  161. {
  162. if (!gr->store->sif->nsm_get_fn) gr->nsm = nsm;
  163. else log_warn ("Graph back end has a stored NS map.");
  164. }
  165. size_t
  166. LSUP_graph_size (const LSUP_Graph *gr)
  167. { return gr->store->sif->size_fn (gr->store->data); }
  168. bool
  169. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2)
  170. {
  171. LSUP_Graph *res = LSUP_graph_new (NULL, LSUP_STORE_HTABLE, NULL, NULL, 0);
  172. LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, res);
  173. bool ret = (LSUP_graph_size (res) == 0);
  174. LSUP_graph_free (res);
  175. return ret;
  176. }
  177. LSUP_GraphIterator *
  178. LSUP_graph_add_init (LSUP_Graph *gr)
  179. {
  180. LSUP_GraphIterator *it;
  181. CALLOC_GUARD (it, NULL);
  182. LSUP_Buffer *sc = LSUP_term_serialize (gr->uri);
  183. it->data = gr->store->sif->add_init_fn (gr->store->data, sc, gr->txn);
  184. LSUP_buffer_free (sc);
  185. it->graph = gr;
  186. return it;
  187. }
  188. LSUP_rc
  189. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo)
  190. {
  191. log_trace (
  192. "Adding triple: {%s, %s, %s}",
  193. spo->s->data, spo->p->data, spo->o->data);
  194. LSUP_BufferTriple *sspo = LSUP_triple_serialize (spo);
  195. if (UNLIKELY (!sspo)) return LSUP_MEM_ERR;
  196. const LSUP_StoreInt *sif = it->graph->store->sif;
  197. LSUP_rc rc = sif->add_iter_fn (it->data, sspo);
  198. PCHECK (rc, finally);
  199. // Store datatype term permanently if the store supports it.
  200. if (rc == LSUP_OK && sif->add_term_fn) {
  201. void *txn;
  202. for (int i = 0; i < 3; i++) {
  203. LSUP_Term *term = LSUP_triple_pos (spo, i);
  204. if (term->type == LSUP_TERM_LITERAL) {
  205. LSUP_Buffer *ser_dtype = LSUP_term_serialize (term->datatype);
  206. // Run add_term in the iterator's txn.
  207. txn = sif->iter_txn_fn ? sif->iter_txn_fn (it->data) : NULL;
  208. sif->add_term_fn ( it->graph->store->data, ser_dtype, txn);
  209. LSUP_buffer_free (ser_dtype);
  210. }
  211. }
  212. }
  213. finally:
  214. LSUP_btriple_free (sspo);
  215. return rc;
  216. }
  217. void
  218. LSUP_graph_add_done (LSUP_GraphIterator *it)
  219. {
  220. it->graph->store->sif->add_done_fn (it->data);
  221. free (it);
  222. }
  223. LSUP_rc
  224. LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct)
  225. {
  226. LSUP_rc rc = LSUP_NOACTION;
  227. // Initialize iterator.
  228. LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
  229. if (ct) *ct = 0;
  230. // Serialize and insert RDF triples.
  231. for (size_t i = 0; trp[i].s != NULL; i++) {
  232. log_trace ("Inserting triple #%lu", i);
  233. LSUP_rc db_rc = LSUP_graph_add_iter (it, trp + i);
  234. if (db_rc == LSUP_OK) {
  235. rc = LSUP_OK;
  236. if (ct) (*ct)++;
  237. // A duplicate will return LSUP_NOACTION and not increment the
  238. // counter.
  239. }
  240. if (UNLIKELY (db_rc < 0)) {
  241. rc = db_rc;
  242. goto finally;
  243. }
  244. }
  245. finally:
  246. LSUP_graph_add_done (it);
  247. return rc;
  248. }
  249. LSUP_rc
  250. LSUP_graph_remove (
  251. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  252. const LSUP_Term *o, size_t *ct)
  253. {
  254. LSUP_rc rc;
  255. LSUP_Buffer
  256. *ss = LSUP_term_serialize (s),
  257. *sp = LSUP_term_serialize (p),
  258. *so = LSUP_term_serialize (o),
  259. *sc = LSUP_term_serialize (gr->uri);
  260. rc = gr->store->sif->remove_fn (
  261. gr->store->data, ss, sp, so, sc, gr->txn, ct);
  262. LSUP_buffer_free (ss);
  263. LSUP_buffer_free (sp);
  264. LSUP_buffer_free (so);
  265. LSUP_buffer_free (sc);
  266. return rc;
  267. }
  268. /**
  269. * Copy triples from a source graph into a destination one.
  270. *
  271. * The destination graph is not initialized here, so the copy is cumulative.
  272. */
  273. LSUP_rc
  274. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
  275. {
  276. LSUP_rc rc = LSUP_NOACTION;
  277. LSUP_GraphIterator *it = LSUP_graph_lookup (src, NULL, NULL, NULL, NULL);
  278. LSUP_Triple *spo = NULL;
  279. LSUP_GraphIterator *add_it = LSUP_graph_add_init (dest);
  280. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  281. LSUP_rc add_rc = LSUP_graph_add_iter (add_it, spo);
  282. LSUP_triple_free (spo);
  283. if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
  284. else if (add_rc < 0) {
  285. rc = add_rc;
  286. break;
  287. }
  288. }
  289. LSUP_graph_add_done (add_it);
  290. LSUP_graph_iter_free (it);
  291. return rc;
  292. }
  293. LSUP_GraphIterator *
  294. LSUP_graph_lookup (
  295. const LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  296. const LSUP_Term *o, size_t *ct)
  297. {
  298. LSUP_GraphIterator *it;
  299. MALLOC_GUARD (it, NULL);
  300. LSUP_Buffer
  301. *ss = LSUP_term_serialize (s),
  302. *sp = LSUP_term_serialize (p),
  303. *so = LSUP_term_serialize (o),
  304. *sc = LSUP_term_serialize (gr->uri);
  305. it->data = gr->store->sif->lookup_fn (
  306. gr->store->data, ss, sp, so, sc, gr->txn, ct);
  307. LSUP_buffer_free (ss);
  308. LSUP_buffer_free (sp);
  309. LSUP_buffer_free (so);
  310. LSUP_buffer_free (sc);
  311. if (UNLIKELY (!it->data)) {
  312. free (it);
  313. return NULL;
  314. }
  315. it->graph = gr;
  316. RCNL (graph_iter_alloc_buffers (it));
  317. return it;
  318. }
  319. LSUP_rc
  320. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo_p)
  321. {
  322. LSUP_rc rc = graph_iter_next_buffer (it);
  323. PRCCK (rc);
  324. if (rc != LSUP_OK) return rc;
  325. LSUP_Triple *spo = LSUP_triple_new (
  326. LSUP_term_new_from_buffer (it->sspo->s),
  327. LSUP_term_new_from_buffer (it->sspo->p),
  328. LSUP_term_new_from_buffer (it->sspo->o)
  329. );
  330. if (UNLIKELY (!spo)) return LSUP_MEM_ERR;
  331. *spo_p = spo;
  332. return LSUP_OK;
  333. }
  334. const LSUP_Graph *
  335. LSUP_graph_iter_graph (LSUP_GraphIterator *it)
  336. { return it->graph; }
  337. void
  338. LSUP_graph_iter_free (LSUP_GraphIterator *it)
  339. {
  340. it->graph->store->sif->lu_free_fn (it->data);
  341. /*
  342. * This deallocates resources properly by preserving borrowed pointers from
  343. * the store in case of LSUP_STORE_COW stores.
  344. */
  345. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  346. LSUP_btriple_free_shallow (it->sspo);
  347. } else {
  348. // TODO copy-on-retrieval stores. None yet.
  349. }
  350. free (it);
  351. }
  352. bool
  353. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
  354. {
  355. LSUP_GraphIterator *it = LSUP_graph_lookup (
  356. gr, spo->s, spo->p, spo->o, NULL);
  357. LSUP_Triple *tmp_spo = NULL;
  358. bool rc = LSUP_graph_iter_next (it, &tmp_spo) != LSUP_END;
  359. LSUP_triple_free (tmp_spo);
  360. LSUP_graph_iter_free (it);
  361. return rc;
  362. }
  363. LSUP_rc
  364. LSUP_graph_begin (LSUP_Graph *gr, int flags) {
  365. if (!(gr->store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  366. return gr->store->sif->txn_begin_fn(gr->store->data, flags, &gr->txn);
  367. }
  368. LSUP_rc
  369. LSUP_graph_commit (LSUP_Graph *gr)
  370. {
  371. LSUP_rc rc = gr->store->sif->txn_commit_fn (gr->txn);
  372. if (rc == LSUP_OK) gr->txn = NULL;
  373. return rc;
  374. }
  375. void
  376. LSUP_graph_abort (LSUP_Graph *gr)
  377. {
  378. gr->store->sif->txn_abort_fn (gr->txn);
  379. gr->txn = NULL;
  380. }
  381. LSUP_LinkMap *
  382. LSUP_graph_connections (
  383. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type)
  384. {
  385. LSUP_Term
  386. *s = NULL,
  387. *p = NULL,
  388. *o = NULL;
  389. // Position of passed term and link terms, respectively.
  390. LSUP_TriplePos pos1, pos2;
  391. if (type == LSUP_LINK_INBOUND) {
  392. o = t;
  393. pos1 = TRP_POS_O;
  394. pos2 = TRP_POS_P;
  395. } else if (type == LSUP_LINK_OUTBOUND) {
  396. s = t;
  397. pos1 = TRP_POS_S;
  398. pos2 = TRP_POS_P;
  399. } else if (type == LSUP_LINK_EDGE) {
  400. p = t;
  401. pos1 = TRP_POS_P;
  402. pos2 = TRP_POS_S;
  403. } else {
  404. // Very unlikely.
  405. log_error ("Invalid connection type: %d", type);
  406. return NULL;
  407. }
  408. // Gather all linking terms in a set first.
  409. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
  410. LSUP_TermSet *lts = LSUP_term_set_new();
  411. while (graph_iter_next_buffer (it) != LSUP_END) {
  412. LSUP_Term
  413. *ex = NULL,
  414. *ins = LSUP_term_new_from_buffer (
  415. LSUP_btriple_pos (it->sspo, pos2));
  416. LSUP_term_set_add (lts, ins, &ex);
  417. if (ex) LSUP_term_free (ins);
  418. }
  419. LSUP_graph_iter_free(it);
  420. LSUP_LinkMap *ret = LSUP_link_map_new (type);
  421. size_t i = 0;
  422. LSUP_Term *lt;
  423. while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {
  424. LSUP_link_map_add (
  425. ret, LSUP_term_copy (lt),
  426. LSUP_graph_term_set (gr, t, pos1, lt, pos2));
  427. }
  428. LSUP_term_set_free (lts);
  429. return ret;
  430. }
  431. LSUP_TermSet *
  432. LSUP_graph_term_set (
  433. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  434. LSUP_Term *t2, LSUP_TriplePos t2_pos)
  435. {
  436. if (t1_pos == t2_pos) {
  437. log_error ("Term 1 and 2 positions cannot be the same!");
  438. return NULL;
  439. }
  440. LSUP_Term *spo_l[3] = {NULL};
  441. spo_l[t1_pos] = t1;
  442. spo_l[t2_pos] = t2;
  443. LSUP_TriplePos rpos = 0; // Position of term to be added to results.
  444. for (unsigned i = 0; i < 3; i++)
  445. if (t1_pos != i && t2_pos != i) rpos = i;
  446. LSUP_GraphIterator *it = LSUP_graph_lookup (
  447. gr, spo_l[0], spo_l[1], spo_l[2], NULL);
  448. LSUP_TermSet *ts = LSUP_term_set_new();
  449. while (graph_iter_next_buffer (it) != LSUP_END) {
  450. // There cannot be duplicates in a 2-bound lookup.
  451. LSUP_term_set_add (
  452. ts,
  453. LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, rpos)),
  454. NULL);
  455. }
  456. LSUP_graph_iter_free (it);
  457. return ts;
  458. }
  459. LSUP_TermSet *
  460. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos)
  461. {
  462. // TODO We should use spo indices for stores that have them...
  463. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, NULL);
  464. LSUP_TermSet *ts = LSUP_term_set_new();
  465. while (graph_iter_next_buffer (it) != LSUP_END) {
  466. LSUP_Term
  467. *ex = NULL,
  468. *ins = LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, pos));
  469. LSUP_term_set_add (ts, ins, &ex);
  470. if (ex) LSUP_term_free (ins);
  471. }
  472. LSUP_graph_iter_free(it);
  473. return ts;
  474. }
  475. size_t
  476. LSUP_graph_add_link_map (
  477. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *lmap)
  478. {
  479. LSUP_Triple *spo = TRP_DUMMY;
  480. size_t ct = 0;
  481. LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lmap, t);
  482. while (LSUP_link_map_triples (lmit, spo) != LSUP_END) {
  483. LSUP_rc rc = LSUP_graph_add_iter (it, spo);
  484. if (rc >= 0) ct++;
  485. PRCCK (rc);
  486. }
  487. LSUP_link_map_iter_free (lmit);
  488. free (spo);
  489. return ct;
  490. }
  491. LSUP_Term *
  492. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  493. {
  494. LSUP_NSMap *nsm = LSUP_graph_namespace (LSUP_graph_iter_graph (it));
  495. LSUP_Term
  496. *s = LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL),
  497. *rdf_first = LSUP_iriref_new ("rdf:first", nsm),
  498. *rdf_rest = LSUP_iriref_new ("rdf:rest", nsm),
  499. *rdf_nil = LSUP_iriref_new ("rdf:nil", nsm),
  500. *link;
  501. LSUP_Triple *spo = TRP_DUMMY;
  502. link = s;
  503. size_t i = 0;
  504. LSUP_Term *t;
  505. while (LSUP_term_set_next (ts, &i, &t) != LSUP_END) {
  506. spo->s = link;
  507. spo->p = rdf_first;
  508. spo->o = t;
  509. PRCNL (LSUP_graph_add_iter (it, spo));
  510. spo->p = rdf_rest;
  511. size_t save_i = i; // Save iterator position to restore it after peek.
  512. spo->o = (
  513. // Peek into the next result.
  514. LSUP_term_set_next (ts, &i, NULL) != LSUP_END ?
  515. LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL)
  516. : rdf_nil);
  517. i = save_i; // Restore the iterator that advanced when peeking.
  518. PRCNL (LSUP_graph_add_iter (it, spo));
  519. if (link != s) LSUP_term_free (link);
  520. // Current object becomes next subject. Irrelevant for last item.
  521. link = spo->o;
  522. }
  523. LSUP_term_free (rdf_first);
  524. LSUP_term_free (rdf_rest);
  525. LSUP_term_free (rdf_nil);
  526. free (spo);
  527. return s;
  528. }
  529. /*
  530. * Static functions.
  531. */
  532. /** @brief Advance an iterator and return a serialized triple.
  533. *
  534. * This is an internal function to pass raw buffers between higher-level
  535. * functions without serializing and deserializing triples.
  536. *
  537. * The results are stored in it->sspo.
  538. */
  539. inline static LSUP_rc
  540. graph_iter_next_buffer (LSUP_GraphIterator *it)
  541. { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
  542. /** @brief Properly allocate temporary byte buffers in advance of iteration.
  543. */
  544. inline LSUP_rc
  545. graph_iter_alloc_buffers (LSUP_GraphIterator *it)
  546. {
  547. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  548. it->sspo = BTRP_DUMMY;
  549. CALLOC_GUARD (it->sspo->s, LSUP_MEM_ERR);
  550. CALLOC_GUARD (it->sspo->p, LSUP_MEM_ERR);
  551. CALLOC_GUARD (it->sspo->o, LSUP_MEM_ERR);
  552. } else {
  553. // TODO copy-on-retrieval stores. None yet.
  554. }
  555. return LSUP_OK;
  556. }
  557. /**
  558. * Extern inline definitions.
  559. */
  560. size_t LSUP_graph_size (const LSUP_Graph *gr);