graph.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "store.h"
  4. #include "environment.h"
  5. #include "term.h"
  6. /** @brief Graph object.
  7. */
  8. typedef struct graph_t LSUP_Graph;
  9. /** @brief Graph iterator.
  10. *
  11. * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
  12. * over lookup results with #LSUP_graph_iter_next. It must be freed with
  13. * #LSUP_graph_iter_free when done.
  14. */
  15. typedef struct graph_iter_t LSUP_GraphIterator;
  16. /** @brief Create an empty graph.
  17. *
  18. * @param[in] uri URI of the new graph. If NULL, a UUID4 URN is generated. The
  19. * graph owns the handle.
  20. *
  21. * @param store_type[in] Type of store backing the graph. One of the values of
  22. * #LSUP_StoreType.
  23. *
  24. * @param[in] store_id Identifier for the back end store. This may be
  25. * interpreted differently by each store implementation. For the MDB store,
  26. * this is the file path where the store is located. It is ignored by volatile
  27. * stores (with LSUP_STORE_PERM feature flag set to false). If a store
  28. * does not exist for the given identifier, a new one is initialized. If this
  29. * parameter is NULL, the default store for the selected type is used.
  30. *
  31. * @param[in] nsm Namespace map to use for an in-memory graph. This is ignored
  32. * by graphs backed by permanent stores, which handle their own namespace map.
  33. * If this is NULL, the graph is assigned a global namespace map that lives
  34. * until #LSUP_done() is called.
  35. *
  36. * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
  37. */
  38. LSUP_Graph *
  39. LSUP_graph_new (
  40. LSUP_Term *uri, const LSUP_StoreType store_type, const char *store_id,
  41. LSUP_NSMap *nsm, size_t size);
  42. /** @brief Copy triples from a source graph into a destination one.
  43. *
  44. * The destination graph is not initialized here, so the copy is cumulative.
  45. *
  46. * @param[in] txn Transaction handle. It may be NULL.
  47. *
  48. * @param src[in] Source graph.
  49. *
  50. * @param dest[in] Destination graph.
  51. */
  52. LSUP_rc
  53. LSUP_graph_copy_contents_txn (
  54. void *txn, const LSUP_Graph *src, LSUP_Graph *dest);
  55. /// Non-transactional version of #LSUP_graph_copy_contents_txn.
  56. inline LSUP_rc
  57. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
  58. { return LSUP_graph_copy_contents_txn (NULL, src, dest); }
  59. /** Perform a boolean operation between two graphs.
  60. *
  61. * This method populates an initialized graph with the result of the operation
  62. * between two other graphs. The resulting graph may be of any store type and
  63. * may be the result of graphs of different store types.
  64. *
  65. * @param[in] txn Transaction handle. It may be NULL.
  66. *
  67. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  68. *
  69. * @param gr1[in] First operand.
  70. *
  71. * @param gr2[in] Second operand.
  72. *
  73. * @param res[out] Result graph. The handle should be initialized via
  74. * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
  75. * If an unrecoverable error occurs, this graph is freed.
  76. *
  77. * @return LSUP_OK on success; <0 on error.
  78. */
  79. LSUP_rc
  80. LSUP_graph_bool_op_txn (
  81. void *txn, const LSUP_bool_op op,
  82. const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res);
  83. /// Non-transactional version of #LSUP_graph_bool_op_txn.
  84. inline LSUP_rc
  85. LSUP_graph_bool_op (
  86. const LSUP_bool_op op,
  87. const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res)
  88. { return LSUP_graph_bool_op_txn (NULL, op, gr1, gr2, res); }
  89. /** @brief Free a graph.
  90. */
  91. void
  92. LSUP_graph_free (LSUP_Graph *gr);
  93. /** @brief Compare two graphs.
  94. *
  95. * Note that if any of the two graphs has an open transaction, the function
  96. * is performed in the first graph's transaction.
  97. *
  98. * @param[in] gr1 First operand.
  99. *
  100. * @param[in] gr2 Second operand.
  101. *
  102. * @return True if the graphs are topologically equal, false otherwise.
  103. */
  104. bool
  105. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  106. /** @brief Read-only graph URI.
  107. *
  108. * To change the graph URI, use #LSUP_graph_set_uri.
  109. */
  110. LSUP_Term *
  111. LSUP_graph_uri (const LSUP_Graph *gr);
  112. /** @brief Underlying graph store handle.
  113. */
  114. LSUP_Store *
  115. LSUP_graph_store (const LSUP_Graph *gr);
  116. /** Set the URI of a graph.
  117. *
  118. * Note that by changing the URI of a graph backed by a context-sensitive store
  119. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  120. * graph points to. Triples are looked up in, and added to, the context that
  121. * the graph URI represents. A non-context graph retains the same triple set
  122. * when graph URI changes.
  123. *
  124. * @param gr[in] Graph handle.
  125. *
  126. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  127. *
  128. * @return LSUP_OK on success; <0 on error.
  129. */
  130. LSUP_rc
  131. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  132. /** @brief Get the namespace map for an in-memory graph.
  133. *
  134. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  135. */
  136. LSUP_NSMap *
  137. LSUP_graph_namespace (const LSUP_Graph *gr);
  138. /** @brief Set the namespace map for an in-memory graph.
  139. *
  140. * This has no effect on graph stores with LSUP_STORE_PERM.
  141. *
  142. * @param[in] gr Graph to set the namespace map for.
  143. *
  144. * @param[in] nsm Namespace handle.
  145. */
  146. void
  147. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  148. /** @brief Number of triples in a graph.
  149. */
  150. size_t
  151. LSUP_graph_size (const LSUP_Graph *gr);
  152. /** @brief Whether a graph contains a triple.
  153. *
  154. * @param[in] gr Graph to look up into.
  155. *
  156. * @param[in] spo Triple to look up.
  157. *
  158. * @return 1 if the triple is found, 0 if not found.
  159. */
  160. bool
  161. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  162. /** @brief Begin a transaction.
  163. *
  164. * If the underlying store supports it, begin a transaction.
  165. *
  166. * Note that the transaction affects all graphs sharing the same store, and it
  167. * may be closed using a different graph as long as this is backed by the same
  168. * store. Only one transaction may be opened at a time for the store.
  169. *
  170. * The transaction must be either committed with #LSUP_graph_commit() or
  171. * rolled back with #LSUP_graph_abort().
  172. *
  173. * TODO Deprecate in favor of direct access to the store handle.
  174. *
  175. * @param[in] gr Graph handle.
  176. *
  177. * @param[in] flags Unused for now, use 0. TODO
  178. *
  179. * @param[out] txn Address to be populated with the new transaction handle.
  180. *
  181. * @return LSUP_OK on success; LSUP_VALUE_ERR if the graph store does not
  182. * support transactions; LSUP_TXN_ERR if the store has already an uncommitted
  183. * transaction; <0 on other errors from the underlying store.
  184. */
  185. LSUP_rc
  186. LSUP_graph_begin (LSUP_Graph *gr, int flags, void **txn);
  187. /** @brief Commit a transaction.
  188. *
  189. * If the underlying store supports it, commit an open transaction. In case of
  190. * error, the transaction is left open and it is advisable to roll it back with
  191. * #LSUP_graph_abort().
  192. *
  193. * TODO Deprecate in favor of direct access to the store handle.
  194. *
  195. * @param[in] gr Graph handle.
  196. *
  197. * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
  198. * if NULL was passed; LSUP_TXN_ERR on error.
  199. */
  200. LSUP_rc LSUP_graph_commit (LSUP_Graph *gr, void *txn);
  201. /** @brief Abort (roll back) a transaction.
  202. *
  203. * If the underlying store supports it, abort an open transaction and abandon
  204. * all changes.
  205. *
  206. * TODO Deprecate in favor of direct access to the store handle.
  207. *
  208. * @param[in] gr Graph handle.
  209. */
  210. void LSUP_graph_abort (LSUP_Graph *gr, void *txn);
  211. /** @brief Initialize an iterator to add triples.
  212. *
  213. * @param[in] txn Transaction handle. It may be NULL.
  214. *
  215. * @param[in] gr Graph to add to. It is added to the iterator state.
  216. *
  217. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  218. * must be freed with #LSUP_graph_add_done().
  219. */
  220. LSUP_GraphIterator *
  221. LSUP_graph_add_init_txn (void *txn, LSUP_Graph *gr);
  222. /// Non-transactional version of #LSUP_graph_init_txn.
  223. inline LSUP_GraphIterator *
  224. LSUP_graph_add_init (LSUP_Graph *gr)
  225. { return LSUP_graph_add_init_txn (NULL, gr); }
  226. /** @brief Add a single triple to the store.
  227. *
  228. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  229. *
  230. * @param[in] spo Triple to add. Caller retains ownership.
  231. */
  232. LSUP_rc
  233. LSUP_graph_add_iter_txn (
  234. void *txn, LSUP_GraphIterator *it, const LSUP_Triple *spo);
  235. /// Non-transactional version of #LSUP_graph_add_iter_txn.
  236. inline LSUP_rc
  237. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo)
  238. { return LSUP_graph_add_iter_txn (NULL, it, spo); }
  239. /** @brief Finalize an add iteration loop and free the iterator.
  240. *
  241. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  242. *
  243. * @param[in] it Iterator to finalize.
  244. */
  245. void
  246. LSUP_graph_add_done (LSUP_GraphIterator *it);
  247. /** @brief Add triples to a graph.
  248. *
  249. * @param[in] txn Transaction handle. It may be NULL.
  250. *
  251. * @param[in] gr Graph to add triples to.
  252. *
  253. * @param[in] trp Array of triples to add. The last triple must be NULL.
  254. *
  255. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  256. *
  257. * @param[out] ct This will be filled with the total number of triples
  258. * inserted.
  259. */
  260. LSUP_rc
  261. LSUP_graph_add_txn (
  262. void *txn, LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct);
  263. /// Non-transactional version of #LSUP_graph_add_txn.
  264. inline LSUP_rc
  265. LSUP_graph_add (
  266. LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct)
  267. { return LSUP_graph_add_txn (NULL, gr, trp, ct); }
  268. /** @brief Delete triples by a matching pattern.
  269. *
  270. * @param[in] txn Transaction handle. It may be NULL.
  271. *
  272. * @param gr[in] Graph to delete triples from.
  273. *
  274. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  275. *
  276. * @param ct[out] If not NULL it is populated with the number of triples
  277. * deleted.
  278. */
  279. LSUP_rc
  280. LSUP_graph_remove_txn (
  281. void *txn, LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  282. const LSUP_Term *o, size_t *ct);
  283. /// Non-transactional version of #LSUP_graph_remove_txn.
  284. inline LSUP_rc
  285. LSUP_graph_remove (
  286. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  287. const LSUP_Term *o, size_t *ct)
  288. { return LSUP_graph_remove_txn (NULL, gr, s, p, o, ct); }
  289. /** Look up triples by a matching pattern and yield an iterator.
  290. *
  291. * @param[in] txn Transaction handle. It may be NULL.
  292. *
  293. * @param gr[in] Graph to look up.
  294. *
  295. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  296. * indicate unbound terms.
  297. *
  298. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  299. * freed with #LSUP_graph_iter_free after use.
  300. */
  301. LSUP_GraphIterator *
  302. LSUP_graph_lookup_txn (void *txn, const LSUP_Graph *gr, const LSUP_Term *s,
  303. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  304. /// Non-transactional version of #LSUP_graph_lookup_txn.
  305. inline LSUP_GraphIterator *
  306. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  307. const LSUP_Term *p, const LSUP_Term *o, size_t *ct)
  308. { return LSUP_graph_lookup_txn (NULL, gr, s, p, o, ct); }
  309. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  310. *
  311. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  312. *
  313. * @param spo[out] Triple handle pointer to be populated with the next result.
  314. * If not NULL, it will allocate a new triple and new terms, and should be
  315. * freed with LSUP_triple_free().
  316. *
  317. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  318. * was reached.
  319. */
  320. LSUP_rc
  321. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo);
  322. /** @brief Return the graph related to an iterator.
  323. */
  324. const LSUP_Graph *
  325. LSUP_graph_iter_graph (LSUP_GraphIterator *it);
  326. /** @brief Free a graph iterator.
  327. *
  328. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  329. * #LSUP_graph_add_done() with those.
  330. *
  331. * @param[in] it Iterator to finalize.
  332. */
  333. void
  334. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  335. /** @brief Get term pairs connected to a term in a graph.
  336. *
  337. * This returns a #LSUP_LinkMap extracted from a graph for a given term. The
  338. * map can generate triples using #LSUP_link_map_triples().
  339. *
  340. * Depending on the type requested (`LSUP_CONN_*), the term can be leveraged
  341. * as a subject, predicate, or object.
  342. *
  343. * @param[in] gr Graph to extract the connection list from.
  344. *
  345. * @param[in] t Term to query for connections.
  346. *
  347. * @param[in] type Type of connections to look up.
  348. *
  349. * @return Link map for the requested term. It should be freed with
  350. * #LSUP_conn_list_free().
  351. */
  352. LSUP_LinkMap *
  353. LSUP_graph_connections (
  354. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type);
  355. /** @brief Get a list of terms related to a term pair in a graph.
  356. *
  357. * @param[in] gr Graph to extract terms from.
  358. *
  359. * @param[in] t1 First term.
  360. *
  361. * @param[in] t1_pos Position of the first term in the triples to look up.
  362. *
  363. * @param[in] t2 Second term.
  364. *
  365. * @param[in] t2_pos Position of the second term in the triples to look up.
  366. *
  367. * @return Term set of results.
  368. */
  369. LSUP_TermSet *
  370. LSUP_graph_term_set (
  371. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  372. LSUP_Term *t2, LSUP_TriplePos t2_pos);
  373. /** @brief Get all unique subjcts, predicates, or objects in a graph.
  374. *
  375. * @param[in] gr Graph handle.
  376. *
  377. * @param[in] pos Position in the triples of the terms to look for.
  378. */
  379. LSUP_TermSet *
  380. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos);
  381. /** @brief Add triples for a term and related connection list to a graph.
  382. *
  383. * The connection list can be of inbound, outbound, or edge type; depending on
  384. * that, triples are added with the given term as the subject, the predicate,
  385. * or the object.
  386. *
  387. * @param[in] it Graph iterator obtained with #LSUP_graph_add_init().
  388. *
  389. * @param[in] t Term to be associated with the collection list.
  390. *
  391. * @param[in] cl Link map.
  392. *
  393. * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
  394. */
  395. size_t
  396. LSUP_graph_add_link_map (
  397. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *cl);
  398. /** @brief Add triples for an anonymous collection to a graph.
  399. *
  400. * The `rdf:first`, `rdf:rest`, etc. terms are automatically added and the term
  401. * for the first item in the list is returned.
  402. *
  403. * @param[in] it Graph iterator to use for insertion.
  404. *
  405. * @param[in] ol NUL-terminated term array.
  406. *
  407. * @return Blank node representing the first list item.
  408. */
  409. LSUP_Term *
  410. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts);
  411. #endif