graph.c 17 KB

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