graph.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.
  19. *
  20. * @param store_type[in] Type of store backing the graph. One of the values of
  21. * #LSUP_StoreType.
  22. *
  23. * @param[in] store_id Identifier for the back end store. This may be
  24. * interpreted differently by each store implementation. For the MDB store,
  25. * this is the file path where the store is located. It is ignored by volatile
  26. * stores (with LSUP_STORE_PERM feature flag set to false). If a store
  27. * does not exist for the given identifier, a new one is initialized. If this
  28. * parameter is NULL, the default store for the selected type is used.
  29. *
  30. * @param[in] nsm Namespace map to use for an in-memory graph. This is ignored
  31. * by graphs backed by permanent stores, which handle their own namespace map.
  32. * If this is NULL, the graph is assigned a global namespace map that lives
  33. * until #LSUP_done() is called.
  34. *
  35. * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
  36. */
  37. LSUP_Graph *
  38. LSUP_graph_new (
  39. LSUP_Term *uri, const LSUP_StoreType store_type, const char *store_id,
  40. LSUP_NSMap *nsm, size_t size);
  41. /** @brief Copy triples from a source graph into a destination one.
  42. *
  43. * The destination graph is not initialized here, so the copy is cumulative.
  44. *
  45. * @param src[in] Source graph.
  46. *
  47. * @param dest[in] Destination graph.
  48. *
  49. * @param[in] txn Transaction handle.
  50. */
  51. LSUP_rc
  52. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest, void *txn);
  53. /** Perform a boolean operation between two graphs.
  54. *
  55. * This method populates an initialized graph with the result of the operation
  56. * between two other graphs. The resulting graph may be of any store type and
  57. * may be the result of graphs of different store types.
  58. *
  59. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  60. *
  61. * @param gr1[in] First operand.
  62. *
  63. * @param gr2[in] Second operand.
  64. *
  65. * @param[in] txn Transaction handle.
  66. *
  67. * @param res[out] Result graph. The handle should be initialized via
  68. * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
  69. * If an unrecoverable error occurs, this graph is freed.
  70. *
  71. * @return LSUP_OK on success; <0 on error.
  72. */
  73. LSUP_rc
  74. LSUP_graph_bool_op(
  75. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2,
  76. void *txn, LSUP_Graph *res);
  77. /** @brief Free a graph.
  78. */
  79. void
  80. LSUP_graph_free (LSUP_Graph *gr);
  81. /** @brief Compare two graphs.
  82. *
  83. * @param[in] gr1 First operand.
  84. *
  85. * @param[in] gr2 Second operand.
  86. *
  87. * @param[in] txn Transaction handle.
  88. *
  89. * @return True if the graphs are topologically equal, false otherwise.
  90. */
  91. bool
  92. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2, void *txn);
  93. /** @brief Read-only graph URI.
  94. *
  95. * To change the graph URI, use #LSUP_graph_set_uri.
  96. */
  97. LSUP_Term *
  98. LSUP_graph_uri (const LSUP_Graph *gr);
  99. /** Set the URI of a graph.
  100. *
  101. * Note that by changing the URI of a graph backed by a context-sensitive store
  102. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  103. * graph points to. Triples are looked up in, and added to, the context that
  104. * the graph URI represents. A non-context graph retains the same triple set
  105. * when graph URI changes.
  106. *
  107. * @param gr[in] Graph handle.
  108. *
  109. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  110. *
  111. * @return LSUP_OK on success; <0 on error.
  112. */
  113. LSUP_rc
  114. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  115. /** @brief Get the namespace map for an in-memory graph.
  116. *
  117. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  118. */
  119. LSUP_NSMap *
  120. LSUP_graph_namespace (const LSUP_Graph *gr);
  121. /** @brief Set the namespace map for an in-memory graph.
  122. *
  123. * This has no effect on graph stores with LSUP_STORE_PERM.
  124. *
  125. * @param[in] gr Graph to set the namespace map for.
  126. *
  127. * @param[in] nsm Namespace handle.
  128. */
  129. void
  130. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  131. /** @brief Number of triples in a graph.
  132. */
  133. size_t
  134. LSUP_graph_size (const LSUP_Graph *gr);
  135. /** @brief Whether a graph contains a triple.
  136. *
  137. * @param[in] gr Graph to look up into.
  138. *
  139. * @param[in] spo Triple to look up.
  140. *
  141. * @return 1 if the triple is found, 0 if not found.
  142. */
  143. bool
  144. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo, void *txn);
  145. /** @brief Begin a transaction.
  146. *
  147. * If the underlying store supports it, begin a transaction and return
  148. * an opaque handle.
  149. *
  150. * The transaction must be either committed with #LSUP_graph_commit() or
  151. * rolled back with #LSUP_graph_abort().
  152. *
  153. * @param[in] gr Graph handle.
  154. *
  155. * @param[in] flags Unused for now, use 0. TODO
  156. *
  157. * @param[out] txn Transaction handle. This may be passed to all subsequent
  158. * operations that one wants to perform within that transaction.
  159. *
  160. * @return LSUP_OK on success; LSUP_VALUE_ERR if the graph store does not
  161. * support transactions; <0 on other errors from the underlying store.
  162. */
  163. LSUP_rc
  164. LSUP_graph_begin (const LSUP_Graph *gr, int flags, void **txn);
  165. /** @brief Commit a transaction.
  166. *
  167. * If the underlying store supports it, commit an open transaction. In case of
  168. * error, the transaction is left open and it is advisable to roll it back with
  169. * #LSUP_graph_abort().
  170. *
  171. * @param[in] gr Graph handle.
  172. *
  173. * @param[in] txn Transaction handle.
  174. *
  175. * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
  176. * if NULL was passed; LSUP_TXN_ERR on error.
  177. */
  178. LSUP_rc LSUP_graph_commit (const LSUP_Graph *gr, void *txn);
  179. /** @brief Abort (roll back) a transaction.
  180. *
  181. * If the underlying store supports it, abort an open transaction and abandon
  182. * all changes.
  183. *
  184. * @param[in] gr Graph handle.
  185. *
  186. * @param[in] txn Transaction handle.
  187. */
  188. void LSUP_graph_abort (const LSUP_Graph *gr, void *txn);
  189. /** @brief Initialize an iterator to add triples.
  190. *
  191. * @param[in] gr Graph to add to. It is added to the iterator state.
  192. *
  193. * @param[in] txn Transaction handle.
  194. *
  195. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  196. * must be freed with #LSUP_graph_add_done().
  197. */
  198. LSUP_GraphIterator *
  199. LSUP_graph_add_init (LSUP_Graph *gr, void *txn);
  200. /** @brief Add a single triple to the store.
  201. *
  202. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  203. *
  204. * @param[in] spo Triple to add. Caller retains ownership.
  205. */
  206. LSUP_rc
  207. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  208. /** @brief Finalize an add iteration loop and free the iterator.
  209. *
  210. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  211. *
  212. * @param[in] it Iterator to finalize.
  213. */
  214. void
  215. LSUP_graph_add_done (LSUP_GraphIterator *it);
  216. /** @brief Add triples to a graph.
  217. *
  218. * @param[in] gr Graph to add triples to.
  219. *
  220. * @param[in] trp Array of triples to add. The last triple must be NULL.
  221. *
  222. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  223. *
  224. * @param[in] txn Transaction handle.
  225. *
  226. * @param[out] ct This will be filled with the total number of triples
  227. * inserted.
  228. */
  229. LSUP_rc
  230. LSUP_graph_add (
  231. LSUP_Graph *gr, const LSUP_Triple trp[], void *txn, size_t *ct);
  232. /** @brief Delete triples by a matching pattern.
  233. *
  234. * @param gr[in] Graph to delete triples from.
  235. *
  236. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  237. *
  238. * @param[in] txn Transaction handle.
  239. *
  240. * @param ct[out] If not NULL it is populated with the number of triples
  241. * deleted.
  242. */
  243. LSUP_rc
  244. LSUP_graph_remove (
  245. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  246. const LSUP_Term *o, void *txn, size_t *ct);
  247. /** Look up triples by a matching pattern and yield an iterator.
  248. *
  249. * @param gr[in] Graph to look up.
  250. *
  251. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  252. * indicate unbound terms.
  253. *
  254. * @param[in] txn Transaction handle.
  255. *
  256. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  257. * freed with #LSUP_graph_iter_free after use.
  258. */
  259. LSUP_GraphIterator *
  260. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  261. const LSUP_Term *p, const LSUP_Term *o, void *txn, size_t *ct);
  262. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  263. *
  264. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  265. *
  266. * @param spo[out] Triple to be populated with the next result. May be NULL
  267. * (e.g. for counters).
  268. *
  269. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  270. * was reached.
  271. */
  272. LSUP_rc
  273. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  274. /** @brief Free a graph iterator.
  275. *
  276. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  277. * #LSUP_graph_add_done() with those.
  278. *
  279. * @param[in] it Iterator to finalize.
  280. */
  281. void
  282. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  283. #endif