graph.c 18 KB

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