graph.c 18 KB

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