graph.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.
  145. *
  146. * Note that the transaction affects all graphs sharing the same store, and it
  147. * may be closed using a different graph as long as this is backed by the same
  148. * store. Only one transaction may be opened at a time for the store.
  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. * @return LSUP_OK on success; LSUP_VALUE_ERR if the graph store does not
  158. * support transactions; LSUP_TXN_ERR if the store has already an uncommitted
  159. * transaction; <0 on other errors from the underlying store.
  160. */
  161. LSUP_rc
  162. LSUP_graph_begin (LSUP_Graph *gr, int flags);
  163. /** @brief Commit a transaction.
  164. *
  165. * If the underlying store supports it, commit an open transaction. In case of
  166. * error, the transaction is left open and it is advisable to roll it back with
  167. * #LSUP_graph_abort().
  168. *
  169. * @param[in] gr Graph handle.
  170. *
  171. * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
  172. * if NULL was passed; LSUP_TXN_ERR on error.
  173. */
  174. LSUP_rc LSUP_graph_commit (LSUP_Graph *gr);
  175. /** @brief Abort (roll back) a transaction.
  176. *
  177. * If the underlying store supports it, abort an open transaction and abandon
  178. * all changes.
  179. *
  180. * @param[in] gr Graph handle.
  181. */
  182. void LSUP_graph_abort (LSUP_Graph *gr);
  183. /** @brief Initialize an iterator to add triples.
  184. *
  185. * @param[in] gr Graph to add to. It is added to the iterator state.
  186. *
  187. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  188. * must be freed with #LSUP_graph_add_done().
  189. */
  190. LSUP_GraphIterator *
  191. LSUP_graph_add_init (LSUP_Graph *gr);
  192. /** @brief Add a single triple to the store.
  193. *
  194. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  195. *
  196. * @param[in] spo Triple to add. Caller retains ownership.
  197. */
  198. LSUP_rc
  199. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  200. /** @brief Finalize an add iteration loop and free the iterator.
  201. *
  202. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  203. *
  204. * @param[in] it Iterator to finalize.
  205. */
  206. void
  207. LSUP_graph_add_done (LSUP_GraphIterator *it);
  208. /** @brief Add triples to a graph.
  209. *
  210. * @param[in] gr Graph to add triples to.
  211. *
  212. * @param[in] trp Array of triples to add. The last triple must be NULL.
  213. *
  214. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  215. *
  216. * @param[out] ct This will be filled with the total number of triples
  217. * inserted.
  218. */
  219. LSUP_rc
  220. LSUP_graph_add (
  221. LSUP_Graph *gr, const LSUP_Triple trp[], size_t *ct);
  222. /** @brief Delete triples by a matching pattern.
  223. *
  224. * @param gr[in] Graph to delete triples from.
  225. *
  226. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  227. *
  228. * @param ct[out] If not NULL it is populated with the number of triples
  229. * deleted.
  230. */
  231. LSUP_rc
  232. LSUP_graph_remove (
  233. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  234. const LSUP_Term *o, size_t *ct);
  235. /** Look up triples by a matching pattern and yield an iterator.
  236. *
  237. * @param gr[in] Graph to look up.
  238. *
  239. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  240. * indicate unbound terms.
  241. *
  242. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  243. * freed with #LSUP_graph_iter_free after use.
  244. */
  245. LSUP_GraphIterator *
  246. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  247. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  248. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  249. *
  250. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  251. *
  252. * @param spo[out] Triple handle pointer to be populated with the next result.
  253. * If not NULL, it will allocate a new triple and new terms, and should be
  254. * freed with LSUP_triple_free().
  255. *
  256. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  257. * was reached.
  258. */
  259. LSUP_rc
  260. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo);
  261. /** @brief Return the graph related to an iterator.
  262. */
  263. const LSUP_Graph *
  264. LSUP_graph_iter_graph (LSUP_GraphIterator *it);
  265. /** @brief Free a graph iterator.
  266. *
  267. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  268. * #LSUP_graph_add_done() with those.
  269. *
  270. * @param[in] it Iterator to finalize.
  271. */
  272. void
  273. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  274. /** @brief Get term pairs connected to a term in a graph.
  275. *
  276. * This returns a #LSUP_LinkMap extracted from a graph for a given term. The
  277. * map can generate triples using #LSUP_link_map_triples().
  278. *
  279. * Depending on the type requested (`LSUP_CONN_*), the term can be leveraged
  280. * as a subject, predicate, or object.
  281. *
  282. * @param[in] gr Graph to extract the connection list from.
  283. *
  284. * @param[in] t Term to query for connections.
  285. *
  286. * @param[in] type Type of connections to look up.
  287. *
  288. * @return Link map for the requested term. It should be freed with
  289. * #LSUP_conn_list_free().
  290. */
  291. LSUP_LinkMap *
  292. LSUP_graph_connections (
  293. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type);
  294. /** @brief Get a list of terms related to a term pair in a graph.
  295. *
  296. * @param[in] gr Graph to extract terms from.
  297. *
  298. * @param[in] t1 First term.
  299. *
  300. * @param[in] t1_pos Position of the first term in the triples to look up.
  301. *
  302. * @param[in] t2 Second term.
  303. *
  304. * @param[in] t2_pos Position of the second term in the triples to look up.
  305. *
  306. * @return Term set of results.
  307. */
  308. LSUP_TermSet *
  309. LSUP_graph_term_set (
  310. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  311. LSUP_Term *t2, LSUP_TriplePos t2_pos);
  312. /** @brief Get all unique subjcts, predicates, or objects in a graph.
  313. *
  314. * @param[in] gr Graph handle.
  315. *
  316. * @param[in] pos Position in the triples of the terms to look for.
  317. */
  318. LSUP_TermSet *
  319. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos);
  320. /** @brief Add triples for a term and related connection list to a graph.
  321. *
  322. * The connection list can be of inbound, outbound, or edge type; depending on
  323. * that, triples are added with the given term as the subject, the predicate,
  324. * or the object.
  325. *
  326. * @param[in] it Graph iterator obtained with #LSUP_graph_add_init().
  327. *
  328. * @param[in] t Term to be associated with the collection list.
  329. *
  330. * @param[in] cl Link map.
  331. *
  332. * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
  333. */
  334. size_t
  335. LSUP_graph_add_link_map (
  336. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *cl);
  337. /** @brief Add triples for an anonymous collection to a graph.
  338. *
  339. * The `rdf:first`, `rdf:rest`, etc. terms are automatically added and the term
  340. * for the first item in the list is returned.
  341. *
  342. * @param[in] it Graph iterator to use for insertion.
  343. *
  344. * @param[in] ol NUL-terminated term array.
  345. *
  346. * @return Blank node representing the first list item.
  347. */
  348. LSUP_Term *
  349. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts);
  350. #endif