graph.c 18 KB

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