graph.h 11 KB

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