graph.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. /** @brief Underlying graph store handle.
  97. */
  98. LSUP_Store *
  99. LSUP_graph_store (const LSUP_Graph *gr);
  100. /** Set the URI of a graph.
  101. *
  102. * Note that by changing the URI of a graph backed by a context-sensitive store
  103. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  104. * graph points to. Triples are looked up in, and added to, the context that
  105. * the graph URI represents. A non-context graph retains the same triple set
  106. * when graph URI changes.
  107. *
  108. * @param gr[in] Graph handle.
  109. *
  110. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  111. *
  112. * @return LSUP_OK on success; <0 on error.
  113. */
  114. LSUP_rc
  115. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  116. /** @brief Get the namespace map for an in-memory graph.
  117. *
  118. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  119. */
  120. LSUP_NSMap *
  121. LSUP_graph_namespace (const LSUP_Graph *gr);
  122. /** @brief Set the namespace map for an in-memory graph.
  123. *
  124. * This has no effect on graph stores with LSUP_STORE_PERM.
  125. *
  126. * @param[in] gr Graph to set the namespace map for.
  127. *
  128. * @param[in] nsm Namespace handle.
  129. */
  130. void
  131. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  132. /** @brief Number of triples in a graph.
  133. */
  134. size_t
  135. LSUP_graph_size (const LSUP_Graph *gr);
  136. /** @brief Whether a graph contains a triple.
  137. *
  138. * @param[in] gr Graph to look up into.
  139. *
  140. * @param[in] spo Triple to look up.
  141. *
  142. * @return 1 if the triple is found, 0 if not found.
  143. */
  144. bool
  145. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  146. /** @brief Begin a transaction.
  147. *
  148. * If the underlying store supports it, begin a transaction.
  149. *
  150. * Note that the transaction affects all graphs sharing the same store, and it
  151. * may be closed using a different graph as long as this is backed by the same
  152. * store. Only one transaction may be opened at a time for the store.
  153. *
  154. * The transaction must be either committed with #LSUP_graph_commit() or
  155. * rolled back with #LSUP_graph_abort().
  156. *
  157. * TODO Deprecate in favor of direct access to the store handle.
  158. *
  159. * @param[in] gr Graph handle.
  160. *
  161. * @param[in] flags Unused for now, use 0. TODO
  162. *
  163. * @return LSUP_OK on success; LSUP_VALUE_ERR if the graph store does not
  164. * support transactions; LSUP_TXN_ERR if the store has already an uncommitted
  165. * transaction; <0 on other errors from the underlying store.
  166. */
  167. LSUP_rc
  168. LSUP_graph_begin (LSUP_Graph *gr, int flags);
  169. /** @brief Commit a transaction.
  170. *
  171. * If the underlying store supports it, commit an open transaction. In case of
  172. * error, the transaction is left open and it is advisable to roll it back with
  173. * #LSUP_graph_abort().
  174. *
  175. * TODO Deprecate in favor of direct access to the store handle.
  176. *
  177. * @param[in] gr Graph handle.
  178. *
  179. * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
  180. * if NULL was passed; LSUP_TXN_ERR on error.
  181. */
  182. LSUP_rc LSUP_graph_commit (LSUP_Graph *gr);
  183. /** @brief Abort (roll back) a transaction.
  184. *
  185. * If the underlying store supports it, abort an open transaction and abandon
  186. * all changes.
  187. *
  188. * TODO Deprecate in favor of direct access to the store handle.
  189. *
  190. * @param[in] gr Graph handle.
  191. */
  192. void LSUP_graph_abort (LSUP_Graph *gr);
  193. /** @brief Initialize an iterator to add triples.
  194. *
  195. * @param[in] gr Graph to add to. It is added to the iterator state.
  196. *
  197. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  198. * must be freed with #LSUP_graph_add_done().
  199. */
  200. LSUP_GraphIterator *
  201. LSUP_graph_add_init (LSUP_Graph *gr);
  202. /** @brief Add a single triple to the store.
  203. *
  204. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  205. *
  206. * @param[in] spo Triple to add. Caller retains ownership.
  207. */
  208. LSUP_rc
  209. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  210. /** @brief Finalize an add iteration loop and free the iterator.
  211. *
  212. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  213. *
  214. * @param[in] it Iterator to finalize.
  215. */
  216. void
  217. LSUP_graph_add_done (LSUP_GraphIterator *it);
  218. /** @brief Add triples to a graph.
  219. *
  220. * @param[in] gr Graph to add triples to.
  221. *
  222. * @param[in] trp Array of triples to add. The last triple must be NULL.
  223. *
  224. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  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[], 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 ct[out] If not NULL it is populated with the number of triples
  239. * deleted.
  240. */
  241. LSUP_rc
  242. LSUP_graph_remove (
  243. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  244. const LSUP_Term *o, size_t *ct);
  245. /** Look up triples by a matching pattern and yield an iterator.
  246. *
  247. * @param gr[in] Graph to look up.
  248. *
  249. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  250. * indicate unbound terms.
  251. *
  252. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  253. * freed with #LSUP_graph_iter_free after use.
  254. */
  255. LSUP_GraphIterator *
  256. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  257. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  258. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  259. *
  260. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  261. *
  262. * @param spo[out] Triple handle pointer to be populated with the next result.
  263. * If not NULL, it will allocate a new triple and new terms, and should be
  264. * freed with LSUP_triple_free().
  265. *
  266. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  267. * was reached.
  268. */
  269. LSUP_rc
  270. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo);
  271. /** @brief Return the graph related to an iterator.
  272. */
  273. const LSUP_Graph *
  274. LSUP_graph_iter_graph (LSUP_GraphIterator *it);
  275. /** @brief Free a graph iterator.
  276. *
  277. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  278. * #LSUP_graph_add_done() with those.
  279. *
  280. * @param[in] it Iterator to finalize.
  281. */
  282. void
  283. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  284. /** @brief Get term pairs connected to a term in a graph.
  285. *
  286. * This returns a #LSUP_LinkMap extracted from a graph for a given term. The
  287. * map can generate triples using #LSUP_link_map_triples().
  288. *
  289. * Depending on the type requested (`LSUP_CONN_*), the term can be leveraged
  290. * as a subject, predicate, or object.
  291. *
  292. * @param[in] gr Graph to extract the connection list from.
  293. *
  294. * @param[in] t Term to query for connections.
  295. *
  296. * @param[in] type Type of connections to look up.
  297. *
  298. * @return Link map for the requested term. It should be freed with
  299. * #LSUP_conn_list_free().
  300. */
  301. LSUP_LinkMap *
  302. LSUP_graph_connections (
  303. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type);
  304. /** @brief Get a list of terms related to a term pair in a graph.
  305. *
  306. * @param[in] gr Graph to extract terms from.
  307. *
  308. * @param[in] t1 First term.
  309. *
  310. * @param[in] t1_pos Position of the first term in the triples to look up.
  311. *
  312. * @param[in] t2 Second term.
  313. *
  314. * @param[in] t2_pos Position of the second term in the triples to look up.
  315. *
  316. * @return Term set of results.
  317. */
  318. LSUP_TermSet *
  319. LSUP_graph_term_set (
  320. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  321. LSUP_Term *t2, LSUP_TriplePos t2_pos);
  322. /** @brief Get all unique subjcts, predicates, or objects in a graph.
  323. *
  324. * @param[in] gr Graph handle.
  325. *
  326. * @param[in] pos Position in the triples of the terms to look for.
  327. */
  328. LSUP_TermSet *
  329. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos);
  330. /** @brief Add triples for a term and related connection list to a graph.
  331. *
  332. * The connection list can be of inbound, outbound, or edge type; depending on
  333. * that, triples are added with the given term as the subject, the predicate,
  334. * or the object.
  335. *
  336. * @param[in] it Graph iterator obtained with #LSUP_graph_add_init().
  337. *
  338. * @param[in] t Term to be associated with the collection list.
  339. *
  340. * @param[in] cl Link map.
  341. *
  342. * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
  343. */
  344. size_t
  345. LSUP_graph_add_link_map (
  346. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *cl);
  347. /** @brief Add triples for an anonymous collection to a graph.
  348. *
  349. * The `rdf:first`, `rdf:rest`, etc. terms are automatically added and the term
  350. * for the first item in the list is returned.
  351. *
  352. * @param[in] it Graph iterator to use for insertion.
  353. *
  354. * @param[in] ol NUL-terminated term array.
  355. *
  356. * @return Blank node representing the first list item.
  357. */
  358. LSUP_Term *
  359. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts);
  360. #endif