graph.h 13 KB

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