graph.c 18 KB

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