graph.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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, const LSUP_Triple 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].s != 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 the
  230. // counter.
  231. }
  232. if (UNLIKELY (db_rc < 0)) {
  233. rc = db_rc;
  234. goto finally;
  235. }
  236. log_trace ("Graph size at end of add iter: %lu", LSUP_graph_size (gr));
  237. }
  238. finally:
  239. LSUP_graph_add_done (it);
  240. return rc;
  241. }
  242. LSUP_rc
  243. LSUP_graph_remove_txn (
  244. void *txn, LSUP_Graph *gr,
  245. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  246. size_t *ct)
  247. {
  248. LSUP_rc rc;
  249. LSUP_Buffer
  250. *ss = LSUP_term_serialize (s),
  251. *sp = LSUP_term_serialize (p),
  252. *so = LSUP_term_serialize (o),
  253. *sc = LSUP_term_serialize (gr->uri);
  254. rc = gr->store->sif->remove_fn (
  255. gr->store->data, ss, sp, so, sc, txn, ct);
  256. LSUP_buffer_free (ss);
  257. LSUP_buffer_free (sp);
  258. LSUP_buffer_free (so);
  259. LSUP_buffer_free (sc);
  260. return rc;
  261. }
  262. /**
  263. * Copy triples from a source graph into a destination one.
  264. *
  265. * The destination graph is not initialized here, so the copy is cumulative.
  266. */
  267. LSUP_rc
  268. LSUP_graph_copy_contents_txn (
  269. void *txn, const LSUP_Graph *src, LSUP_Graph *dest)
  270. {
  271. LSUP_rc rc = LSUP_NOACTION;
  272. LSUP_GraphIterator *it = LSUP_graph_lookup_txn (
  273. txn, src, NULL, NULL, NULL, NULL);
  274. LSUP_Triple *spo = NULL;
  275. LSUP_GraphIterator *add_it = LSUP_graph_add_init_txn (txn, dest);
  276. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  277. LSUP_rc add_rc = LSUP_graph_add_iter (add_it, spo);
  278. LSUP_triple_free (spo);
  279. if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
  280. else if (add_rc < 0) {
  281. rc = add_rc;
  282. break;
  283. }
  284. }
  285. LSUP_graph_add_done (add_it);
  286. LSUP_graph_iter_free (it);
  287. return rc;
  288. }
  289. LSUP_GraphIterator *
  290. LSUP_graph_lookup_txn (
  291. void *txn, const LSUP_Graph *gr,
  292. const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o,
  293. size_t *ct)
  294. {
  295. LSUP_GraphIterator *it;
  296. MALLOC_GUARD (it, NULL);
  297. LSUP_Buffer
  298. *ss = LSUP_term_serialize (s),
  299. *sp = LSUP_term_serialize (p),
  300. *so = LSUP_term_serialize (o),
  301. *sc = LSUP_term_serialize (gr->uri);
  302. it->data = gr->store->sif->lookup_fn (
  303. gr->store->data, ss, sp, so, sc, txn, ct);
  304. LSUP_buffer_free (ss);
  305. LSUP_buffer_free (sp);
  306. LSUP_buffer_free (so);
  307. LSUP_buffer_free (sc);
  308. if (UNLIKELY (!it->data)) {
  309. free (it);
  310. return NULL;
  311. }
  312. it->graph = gr;
  313. RCNL (graph_iter_alloc_buffers (it));
  314. return it;
  315. }
  316. LSUP_rc
  317. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo_p)
  318. {
  319. LSUP_rc rc = graph_iter_next_buffer (it);
  320. PRCCK (rc);
  321. if (rc != LSUP_OK) return rc;
  322. LSUP_Triple *spo = LSUP_triple_new (
  323. LSUP_term_new_from_buffer (it->sspo->s),
  324. LSUP_term_new_from_buffer (it->sspo->p),
  325. LSUP_term_new_from_buffer (it->sspo->o)
  326. );
  327. if (UNLIKELY (!spo)) return LSUP_MEM_ERR;
  328. *spo_p = spo;
  329. return LSUP_OK;
  330. }
  331. const LSUP_Graph *
  332. LSUP_graph_iter_graph (LSUP_GraphIterator *it)
  333. { return it->graph; }
  334. void
  335. LSUP_graph_iter_free (LSUP_GraphIterator *it)
  336. {
  337. it->graph->store->sif->lu_free_fn (it->data);
  338. /*
  339. * This deallocates resources properly by preserving borrowed pointers from
  340. * the store in case of LSUP_STORE_COW stores.
  341. */
  342. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  343. LSUP_btriple_free_shallow (it->sspo);
  344. } else {
  345. // TODO copy-on-retrieval stores. None yet.
  346. }
  347. free (it);
  348. }
  349. bool
  350. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
  351. {
  352. LSUP_GraphIterator *it = LSUP_graph_lookup (
  353. gr, spo->s, spo->p, spo->o, NULL);
  354. LSUP_Triple *tmp_spo = NULL;
  355. bool rc = LSUP_graph_iter_next (it, &tmp_spo) != LSUP_END;
  356. LSUP_triple_free (tmp_spo);
  357. LSUP_graph_iter_free (it);
  358. return rc;
  359. }
  360. LSUP_LinkMap *
  361. LSUP_graph_connections (
  362. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type)
  363. {
  364. LSUP_Term
  365. *s = NULL,
  366. *p = NULL,
  367. *o = NULL;
  368. // Position of passed term and link terms, respectively.
  369. LSUP_TriplePos pos1, pos2;
  370. if (type == LSUP_LINK_INBOUND) {
  371. o = t;
  372. pos1 = TRP_POS_O;
  373. pos2 = TRP_POS_P;
  374. } else if (type == LSUP_LINK_OUTBOUND) {
  375. s = t;
  376. pos1 = TRP_POS_S;
  377. pos2 = TRP_POS_P;
  378. } else if (type == LSUP_LINK_EDGE) {
  379. p = t;
  380. pos1 = TRP_POS_P;
  381. pos2 = TRP_POS_S;
  382. } else {
  383. // Very unlikely.
  384. log_error ("Invalid connection type: %d", type);
  385. return NULL;
  386. }
  387. // Gather all linking terms in a set first.
  388. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
  389. LSUP_TermSet *lts = LSUP_term_set_new();
  390. while (graph_iter_next_buffer (it) != LSUP_END) {
  391. LSUP_Term
  392. *ex = NULL,
  393. *ins = LSUP_term_new_from_buffer (
  394. LSUP_btriple_pos (it->sspo, pos2));
  395. LSUP_term_set_add (lts, ins, &ex);
  396. if (ex) LSUP_term_free (ins);
  397. }
  398. LSUP_graph_iter_free(it);
  399. LSUP_LinkMap *ret = LSUP_link_map_new (type);
  400. size_t i = 0;
  401. LSUP_Term *lt;
  402. while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {
  403. LSUP_link_map_add (
  404. ret, LSUP_term_copy (lt),
  405. LSUP_graph_term_set (gr, t, pos1, lt, pos2));
  406. }
  407. LSUP_term_set_free (lts);
  408. return ret;
  409. }
  410. LSUP_TermSet *
  411. LSUP_graph_term_set (
  412. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  413. LSUP_Term *t2, LSUP_TriplePos t2_pos)
  414. {
  415. if (t1_pos == t2_pos) {
  416. log_error ("Term 1 and 2 positions cannot be the same!");
  417. return NULL;
  418. }
  419. LSUP_Term *spo_l[3] = {NULL};
  420. spo_l[t1_pos] = t1;
  421. spo_l[t2_pos] = t2;
  422. LSUP_TriplePos rpos = 0; // Position of term to be added to results.
  423. for (unsigned i = 0; i < 3; i++)
  424. if (t1_pos != i && t2_pos != i) rpos = i;
  425. LSUP_GraphIterator *it = LSUP_graph_lookup (
  426. gr, spo_l[0], spo_l[1], spo_l[2], NULL);
  427. LSUP_TermSet *ts = LSUP_term_set_new();
  428. while (graph_iter_next_buffer (it) != LSUP_END) {
  429. // There cannot be duplicates in a 2-bound lookup.
  430. LSUP_term_set_add (
  431. ts,
  432. LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, rpos)),
  433. NULL);
  434. }
  435. LSUP_graph_iter_free (it);
  436. return ts;
  437. }
  438. LSUP_TermSet *
  439. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos)
  440. {
  441. // TODO We should use spo indices for stores that have them...
  442. LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, NULL);
  443. LSUP_TermSet *ts = LSUP_term_set_new();
  444. while (graph_iter_next_buffer (it) != LSUP_END) {
  445. LSUP_Term
  446. *ex = NULL,
  447. *ins = LSUP_term_new_from_buffer (LSUP_btriple_pos (it->sspo, pos));
  448. LSUP_term_set_add (ts, ins, &ex);
  449. if (ex) LSUP_term_free (ins);
  450. }
  451. LSUP_graph_iter_free(it);
  452. return ts;
  453. }
  454. size_t
  455. LSUP_graph_add_link_map (
  456. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *lmap)
  457. {
  458. LSUP_Triple *spo = TRP_DUMMY;
  459. size_t ct = 0;
  460. LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lmap, t);
  461. while (LSUP_link_map_triples (lmit, spo) != LSUP_END) {
  462. LSUP_rc rc = LSUP_graph_add_iter (it, spo);
  463. if (rc >= 0) ct++;
  464. PRCCK (rc);
  465. }
  466. LSUP_link_map_iter_free (lmit);
  467. free (spo);
  468. return ct;
  469. }
  470. LSUP_Term *
  471. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  472. {
  473. LSUP_NSMap *nsm = LSUP_graph_namespace (LSUP_graph_iter_graph (it));
  474. LSUP_Term
  475. *s = LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL),
  476. *rdf_first = LSUP_iriref_new ("rdf:first", nsm),
  477. *rdf_rest = LSUP_iriref_new ("rdf:rest", nsm),
  478. *rdf_nil = LSUP_iriref_new ("rdf:nil", nsm),
  479. *link;
  480. LSUP_Triple *spo = TRP_DUMMY;
  481. link = s;
  482. size_t i = 0;
  483. LSUP_Term *t;
  484. while (LSUP_term_set_next (ts, &i, &t) != LSUP_END) {
  485. spo->s = link;
  486. spo->p = rdf_first;
  487. spo->o = t;
  488. PRCNL (LSUP_graph_add_iter (it, spo));
  489. spo->p = rdf_rest;
  490. size_t save_i = i; // Save iterator position to restore it after peek.
  491. spo->o = (
  492. // Peek into the next result.
  493. LSUP_term_set_next (ts, &i, NULL) != LSUP_END ?
  494. LSUP_term_new (LSUP_TERM_BNODE, NULL, NULL)
  495. : rdf_nil);
  496. i = save_i; // Restore the iterator that advanced when peeking.
  497. PRCNL (LSUP_graph_add_iter (it, spo));
  498. if (link != s) LSUP_term_free (link);
  499. // Current object becomes next subject. Irrelevant for last item.
  500. link = spo->o;
  501. }
  502. LSUP_term_free (rdf_first);
  503. LSUP_term_free (rdf_rest);
  504. LSUP_term_free (rdf_nil);
  505. free (spo);
  506. return s;
  507. }
  508. /*
  509. * Static functions.
  510. */
  511. /** @brief Advance an iterator and return a serialized triple.
  512. *
  513. * This is an internal function to pass raw buffers between higher-level
  514. * functions without serializing and deserializing triples.
  515. *
  516. * The results are stored in it->sspo.
  517. */
  518. inline static LSUP_rc
  519. graph_iter_next_buffer (LSUP_GraphIterator *it)
  520. { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
  521. /** @brief Properly allocate temporary byte buffers in advance of iteration.
  522. */
  523. inline LSUP_rc
  524. graph_iter_alloc_buffers (LSUP_GraphIterator *it)
  525. {
  526. if (it->graph->store->sif->features & LSUP_STORE_COW) {
  527. it->sspo = BTRP_DUMMY;
  528. CALLOC_GUARD (it->sspo->s, LSUP_MEM_ERR);
  529. CALLOC_GUARD (it->sspo->p, LSUP_MEM_ERR);
  530. CALLOC_GUARD (it->sspo->o, LSUP_MEM_ERR);
  531. } else {
  532. // TODO copy-on-retrieval stores. None yet.
  533. }
  534. return LSUP_OK;
  535. }
  536. /**
  537. * Extern inline definitions.
  538. */
  539. size_t LSUP_graph_size (const LSUP_Graph *gr);