graph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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] store Back end store handle. It may be the result of
  19. * #LSUP_store_new(), or NULL; in the latter case, it defaults to a temporary
  20. * HTable store that is freed together with the graph.
  21. *
  22. * @param[in] uri URI of the new graph. If NULL, a UUID4 URN is generated. The
  23. * term is copied into the graph and may be freed after this function is
  24. * called.
  25. *
  26. * @param[in] nsm Namespace map to use for an in-memory graph. This is ignored
  27. * by graphs backed by permanent stores, which handle their own namespace map.
  28. * If this is NULL, the graph is assigned a global namespace map that lives
  29. * until #LSUP_done() is called.
  30. *
  31. * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
  32. */
  33. LSUP_Graph *
  34. LSUP_graph_new (LSUP_Store *store, LSUP_Term *uri, LSUP_NSMap *nsm);
  35. /** @brief Copy triples from a source graph into a destination one.
  36. *
  37. * The destination graph is not initialized here, so the copy is cumulative.
  38. *
  39. * @param[in] txn Transaction handle. It may be NULL.
  40. *
  41. * @param src[in] Source graph.
  42. *
  43. * @param dest[in] Destination graph.
  44. */
  45. LSUP_rc
  46. LSUP_graph_copy_contents_txn (
  47. void *txn, const LSUP_Graph *src, LSUP_Graph *dest);
  48. /// Non-transactional version of #LSUP_graph_copy_contents_txn.
  49. #define LSUP_graph_copy_contents(...) \
  50. LSUP_graph_copy_contents_txn (NULL, __VA_ARGS__)
  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[in] txn Transaction handle. It may be NULL.
  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 res[out] Result graph. The handle should be initialized via
  66. * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
  67. * If an unrecoverable error occurs, this graph is freed.
  68. *
  69. * @return LSUP_OK on success; <0 on error.
  70. */
  71. LSUP_rc
  72. LSUP_graph_bool_op_txn (
  73. void *txn, const LSUP_bool_op op,
  74. const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res);
  75. /// Non-transactional version of #LSUP_graph_bool_op_txn.
  76. #define LSUP_graph_bool_op(...) LSUP_graph_bool_op_txn (NULL, __VA_ARGS__)
  77. /** @brief Free a graph.
  78. */
  79. void
  80. LSUP_graph_free (LSUP_Graph *gr);
  81. /** @brief Compare two graphs.
  82. *
  83. * Note that if any of the two graphs has an open transaction, the function
  84. * is performed in the first graph's transaction.
  85. *
  86. * @param[in] gr1 First operand.
  87. *
  88. * @param[in] gr2 Second operand.
  89. *
  90. * @return True if the graphs are topologically equal, false otherwise.
  91. */
  92. bool
  93. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  94. /** @brief Read-only graph URI.
  95. *
  96. * To change the graph URI, use #LSUP_graph_set_uri.
  97. */
  98. LSUP_Term *
  99. LSUP_graph_uri (const LSUP_Graph *gr);
  100. /** @brief Underlying graph store handle.
  101. */
  102. LSUP_Store *
  103. LSUP_graph_store (const LSUP_Graph *gr);
  104. /** Set the URI of a graph.
  105. *
  106. * Note that by changing the URI of a graph backed by a context-sensitive store
  107. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  108. * graph points to. Triples are looked up in, and added to, the context that
  109. * the graph URI represents. A non-context graph retains the same triple set
  110. * when graph URI changes.
  111. *
  112. * @param gr[in] Graph handle.
  113. *
  114. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  115. *
  116. * @return LSUP_OK on success; <0 on error.
  117. */
  118. LSUP_rc
  119. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  120. /** @brief Get the namespace map for an in-memory graph.
  121. *
  122. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  123. */
  124. LSUP_NSMap *
  125. LSUP_graph_namespace (const LSUP_Graph *gr);
  126. /** @brief Set the namespace map for an in-memory graph.
  127. *
  128. * This has no effect on graph stores with LSUP_STORE_PERM.
  129. *
  130. * @param[in] gr Graph to set the namespace map for.
  131. *
  132. * @param[in] nsm Namespace handle.
  133. */
  134. void
  135. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  136. /** @brief Number of triples in a graph.
  137. */
  138. size_t
  139. LSUP_graph_size (const LSUP_Graph *gr);
  140. /** @brief Whether a graph contains a triple.
  141. *
  142. * @param[in] gr Graph to look up into.
  143. *
  144. * @param[in] spo Triple to look up.
  145. *
  146. * @return 1 if the triple is found, 0 if not found.
  147. */
  148. bool
  149. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  150. /** @brief Initialize an iterator to add triples.
  151. *
  152. * @param[in] txn Transaction handle. It may be NULL. If not NULL, its handle
  153. * will be bound to the iterator handle for its whole lifa cycle.
  154. *
  155. * @param[in] gr Graph to add to. It is added to the iterator state.
  156. *
  157. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  158. * must be freed with #LSUP_graph_add_done().
  159. */
  160. LSUP_GraphIterator *
  161. LSUP_graph_add_init_txn (void *txn, LSUP_Graph *gr);
  162. /// Non-transactional version of #LSUP_graph_init_txn().
  163. #define LSUP_graph_add_init(...) LSUP_graph_add_init_txn (NULL, __VA_ARGS__)
  164. /** @brief Add a single triple to the store.
  165. *
  166. * @param[in] it Iterator obtained with #LSUP_graph_add_init_txn().
  167. *
  168. * @param[in] spo Triple to add. Caller retains ownership.
  169. */
  170. LSUP_rc
  171. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  172. /** @brief Finalize an add iteration loop and free the iterator.
  173. *
  174. * DO NOT USE with iterators obtained with other than
  175. * #LSUP_graph_add_init_txn().
  176. *
  177. * @param[in] it Iterator to finalize.
  178. */
  179. void
  180. LSUP_graph_add_done (LSUP_GraphIterator *it);
  181. /** @brief Add triples to a graph.
  182. *
  183. * @param[in] txn Transaction handle. It may be NULL.
  184. *
  185. * @param[in] gr Graph to add triples to.
  186. *
  187. * @param[in] trp NULL-terminated array of triple handles to add.
  188. *
  189. * @param[out] ct This will be filled with the total number of triples
  190. * inserted.
  191. */
  192. LSUP_rc
  193. LSUP_graph_add_txn (
  194. void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct);
  195. /// Non-transactional version of #LSUP_graph_add_txn.
  196. #define LSUP_graph_add(...) LSUP_graph_add_txn (NULL, __VA_ARGS__)
  197. /** @brief Delete triples by a matching pattern.
  198. *
  199. * @param[in] txn Transaction handle. It may be NULL.
  200. *
  201. * @param gr[in] Graph to delete triples from.
  202. *
  203. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  204. *
  205. * @param ct[out] If not NULL it is populated with the number of triples
  206. * deleted.
  207. */
  208. LSUP_rc
  209. LSUP_graph_remove_txn (
  210. void *txn, LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  211. const LSUP_Term *o, size_t *ct);
  212. /// Non-transactional version of #LSUP_graph_remove_txn.
  213. #define LSUP_graph_remove(...) LSUP_graph_remove_txn (NULL, __VA_ARGS__)
  214. /** @brief Look up triples by a matching pattern and yield an iterator.
  215. *
  216. * @param[in] txn Transaction handle. It may be NULL.
  217. *
  218. * @param gr[in] Graph to look up.
  219. *
  220. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  221. * indicate unbound terms.
  222. *
  223. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  224. * freed with #LSUP_graph_iter_free after use.
  225. */
  226. LSUP_GraphIterator *
  227. LSUP_graph_lookup_txn (void *txn, const LSUP_Graph *gr, const LSUP_Term *s,
  228. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  229. /// Non-transactional version of #LSUP_graph_lookup_txn.
  230. #define LSUP_graph_lookup(...) LSUP_graph_lookup_txn (NULL, __VA_ARGS__)
  231. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  232. *
  233. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup_txn.
  234. *
  235. * @param spo[out] Triple handle pointer to be populated with the next result.
  236. * If not NULL, it will allocate a new triple and new terms, and should be
  237. * freed with LSUP_triple_free().
  238. *
  239. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  240. * was reached.
  241. */
  242. LSUP_rc
  243. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple **spo);
  244. /** @brief Return the graph related to an iterator.
  245. */
  246. const LSUP_Graph *
  247. LSUP_graph_iter_graph (LSUP_GraphIterator *it);
  248. /** @brief Free a graph iterator.
  249. *
  250. * DO NOT USE with iterators obtained with #LSUP_graph_add_init_txn(). Use
  251. * #LSUP_graph_add_done() with those.
  252. *
  253. * @param[in] it Iterator to finalize.
  254. */
  255. void
  256. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  257. /** @brief Get term pairs connected to a term in a graph.
  258. *
  259. * This returns a #LSUP_LinkMap extracted from a graph for a given term. The
  260. * map can generate triples using #LSUP_link_map_triples().
  261. *
  262. * Depending on the type requested (`LSUP_CONN_*), the term can be leveraged
  263. * as a subject, predicate, or object.
  264. *
  265. * @param[in] gr Graph to extract the connection list from.
  266. *
  267. * @param[in] t Term to query for connections.
  268. *
  269. * @param[in] type Type of connections to look up.
  270. *
  271. * @return Link map for the requested term. It should be freed with
  272. * #LSUP_conn_list_free().
  273. */
  274. LSUP_LinkMap *
  275. LSUP_graph_connections (
  276. const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type);
  277. /** @brief Get a list of terms related to a term pair in a graph.
  278. *
  279. * @param[in] gr Graph to extract terms from.
  280. *
  281. * @param[in] t1 First term.
  282. *
  283. * @param[in] t1_pos Position of the first term in the triples to look up.
  284. *
  285. * @param[in] t2 Second term.
  286. *
  287. * @param[in] t2_pos Position of the second term in the triples to look up.
  288. *
  289. * @return Term set of results.
  290. */
  291. LSUP_TermSet *
  292. LSUP_graph_term_set (
  293. const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
  294. LSUP_Term *t2, LSUP_TriplePos t2_pos);
  295. /** @brief Get all unique subjcts, predicates, or objects in a graph.
  296. *
  297. * @param[in] gr Graph handle.
  298. *
  299. * @param[in] pos Position in the triples of the terms to look for.
  300. */
  301. LSUP_TermSet *
  302. LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos);
  303. /** @brief Add triples for a term and related connection list to a graph.
  304. *
  305. * The connection list can be of inbound, outbound, or edge type; depending on
  306. * that, triples are added with the given term as the subject, the predicate,
  307. * or the object.
  308. *
  309. * @param[in] it Graph iterator obtained with #LSUP_graph_add_init_txn().
  310. *
  311. * @param[in] t Term to be associated with the collection list.
  312. *
  313. * @param[in] cl Link map.
  314. *
  315. * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
  316. */
  317. size_t
  318. LSUP_graph_add_link_map (
  319. LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *cl);
  320. /** @brief Add triples for an anonymous collection to a graph.
  321. *
  322. * The `rdf:first`, `rdf:rest`, etc. terms are automatically added and the term
  323. * for the first item in the list is returned.
  324. *
  325. * @param[in] it Graph iterator to use for insertion.
  326. *
  327. * @param[in] ts Source term set.
  328. *
  329. * @return Blank node representing the first list item.
  330. */
  331. LSUP_Term *
  332. LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts);
  333. #endif