graph.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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_LinkMap *
  362. LSUP_graph_connections (
  363. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type)
  364. {
  365. LSUP_Term
  366. *s = NULL,
  367. *p = NULL,
  368. *o = NULL;
  369. // Position of passed term and link terms, respectively.
  370. LSUP_TriplePos pos1, pos2;
  371. if (type == LSUP_LINK_INBOUND) {
  372. o = t;
  373. pos1 = TRP_POS_O;
  374. pos2 = TRP_POS_P;
  375. } else if (type == LSUP_LINK_OUTBOUND) {
  376. s = t;
  377. pos1 = TRP_POS_S;
  378. pos2 = TRP_POS_P;
  379. } else if (type == LSUP_LINK_EDGE) {
  380. p = t;
  381. pos1 = TRP_POS_P;
  382. pos2 = TRP_POS_S;
  383. } else {
  384. // Very unlikely.
  385. log_error ("Invalid connection type: %d", type);
  386. return NULL;
  387. }
  388. // Gather all linking terms in a set first.
  389. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
  390. LSUP_TermSet *lts = LSUP_term_set_new();
  391. while (graph_iter_next_buffer (it) != LSUP_END) {
  392. LSUP_Term
  393. *ex = NULL,
  394. *ins = LSUP_term_new_from_buffer (
  395. LSUP_btriple_pos (it->sspo, pos2));
  396. LSUP_term_set_add (lts, ins, &ex);
  397. if (ex) LSUP_term_free (ins);
  398. }
  399. LSUP_graph_iter_free(it);
  400. LSUP_LinkMap *ret = LSUP_link_map_new (type);
  401. size_t i = 0;
  402. LSUP_Term *lt;
  403. while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {
  404. LSUP_link_map_add (
  405. ret, LSUP_term_copy (lt),
  406. LSUP_graph_term_set (gr, t, pos1, lt, pos2));
  407. }
  408. LSUP_term_set_free (lts);
  409. return ret;
  410. }
  411. LSUP_TermSet *
  412. LSUP_graph_term_set (
  413. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  414. LSUP_Term *t2, LSUP_TriplePos t2_pos)
  415. {
  416. if (t1_pos == t2_pos) {
  417. log_error ("Term 1 and 2 positions cannot be the same!");
  418. return NULL;
  419. }
  420. LSUP_Term *spo_l[3] = {NULL};
  421. spo_l[t1_pos] = t1;
  422. spo_l[t2_pos] = t2;
  423. LSUP_TriplePos rpos = 0; // Position of term to be added to results.
  424. for (unsigned i = 0; i < 3; i++)
  425. if (t1_pos != i && t2_pos != i) rpos = i;
  426. LSUP_GraphIterator *it = LSUP_graph_lookup (
  427. gr, spo_l[0], spo_l[1], spo_l[2], NULL);
  428. LSUP_TermSet *ts = LSUP_term_set_new();
  429. while (graph_iter_next_buffer (it) != LSUP_END) {
  430. // There cannot be duplicates in a 2-bound lookup.
  431. LSUP_term_set_add (
  432. ts,
  433. LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, rpos)),
  434. NULL);
  435. }
  436. LSUP_graph_iter_free (it);
  437. return ts;
  438. }
  439. LSUP_TermSet *
  440. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos)
  441. {
  442. // TODO We should use spo indices for stores that have them...
  443. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, NULL);
  444. LSUP_TermSet *ts = LSUP_term_set_new();
  445. while (graph_iter_next_buffer (it) != LSUP_END) {
  446. LSUP_Term
  447. *ex = NULL,
  448. *ins = LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, pos));
  449. LSUP_term_set_add (ts, ins, &ex);
  450. if (ex) LSUP_term_free (ins);
  451. }
  452. LSUP_graph_iter_free(it);
  453. return ts;
  454. }
  455. size_t
  456. LSUP_graph_add_link_map (
  457. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *lmap)
  458. {
  459. LSUP_Triple *spo = TRP_DUMMY;
  460. size_t ct = 0;
  461. LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lmap, t);
  462. while (LSUP_link_map_triples (lmit, spo) != LSUP_END) {
  463. LSUP_rc rc = LSUP_graph_add_iter (it, spo);
  464. if (rc >= 0) ct++;
  465. PRCCK (rc);
  466. }
  467. LSUP_link_map_iter_free (lmit);
  468. free (spo);
  469. return ct;
  470. }
  471. LSUP_Term *
  472. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  473. {
  474. LSUP_NSMap *nsm = LSUP_graph_namespace (LSUP_graph_iter_graph (it));
  475. LSUP_Term
  476. *s = LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL),
  477. *rdf_first = LSUP_iriref_new ("rdf:first", nsm),
  478. *rdf_rest = LSUP_iriref_new ("rdf:rest", nsm),
  479. *rdf_nil = LSUP_iriref_new ("rdf:nil", nsm),
  480. *link;
  481. LSUP_Triple *spo = TRP_DUMMY;
  482. link = s;
  483. size_t i = 0;
  484. LSUP_Term *t;
  485. while (LSUP_term_set_next (ts, &i, &t) != LSUP_END) {
  486. spo->s = link;
  487. spo->p = rdf_first;
  488. spo->o = t;
  489. PRCNL (LSUP_graph_add_iter (it, spo));
  490. spo->p = rdf_rest;
  491. size_t save_i = i; // Save iterator position to restore it after peek.
  492. spo->o = (
  493. // Peek into the next result.
  494. LSUP_term_set_next (ts, &i, NULL) != LSUP_END ?
  495. LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL)
  496. : rdf_nil);
  497. i = save_i; // Restore the iterator that advanced when peeking.
  498. PRCNL (LSUP_graph_add_iter (it, spo));
  499. if (link != s) LSUP_term_free (link);
  500. // Current object becomes next subject. Irrelevant for last item.
  501. link = spo->o;
  502. }
  503. LSUP_term_free (rdf_first);
  504. LSUP_term_free (rdf_rest);
  505. LSUP_term_free (rdf_nil);
  506. free (spo);
  507. return s;
  508. }
  509. /*
  510. * Static functions.
  511. */
  512. /** @brief Advance an iterator and return a serialized triple.
  513. *
  514. * This is an internal function to pass raw buffers between higher-level
  515. * functions without serializing and deserializing triples.
  516. *
  517. * The results are stored in it->sspo.
  518. */
  519. inline static LSUP_rc
  520. graph_iter_next_buffer (LSUP_GraphIterator *it)
  521. { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
  522. /** @brief Properly allocate temporary byte buffers in advance of iteration.
  523. */
  524. inline LSUP_rc
  525. graph_iter_alloc_buffers (LSUP_GraphIterator *it)
  526. {
  527. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  528. it->sspo = BTRP_DUMMY;
  529. CALLOC_GUARD (it->sspo->s, LSUP_MEM_ERR);
  530. CALLOC_GUARD (it->sspo->p, LSUP_MEM_ERR);
  531. CALLOC_GUARD (it->sspo->o, LSUP_MEM_ERR);
  532. } else {
  533. // TODO copy-on-retrieval stores. None yet.
  534. }
  535. return LSUP_OK;
  536. }
  537. /**
  538. * Extern inline definitions.
  539. */
  540. size_t LSUP_graph_size (const LSUP_Graph *gr);