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. * graph owns the handle.
  24. *
  25. * @param[in] nsm Namespace map to use for an in-memory graph. This is ignored
  26. * by graphs backed by permanent stores, which handle their own namespace map.
  27. * If this is NULL, the graph is assigned a global namespace map that lives
  28. * until #LSUP_done() is called.
  29. *
  30. * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
  31. */
  32. LSUP_Graph *
  33. LSUP_graph_new (LSUP_Store *store, LSUP_Term *uri, LSUP_NSMap *nsm);
  34. /** @brief Copy triples from a source graph into a destination one.
  35. *
  36. * The destination graph is not initialized here, so the copy is cumulative.
  37. *
  38. * @param[in] txn Transaction handle. It may be NULL.
  39. *
  40. * @param src[in] Source graph.
  41. *
  42. * @param dest[in] Destination graph.
  43. */
  44. LSUP_rc
  45. LSUP_graph_copy_contents_txn (
  46. void *txn, const LSUP_Graph *src, LSUP_Graph *dest);
  47. /// Non-transactional version of #LSUP_graph_copy_contents_txn.
  48. #define LSUP_graph_copy_contents(...) \
  49. LSUP_graph_copy_contents_txn (NULL, __VA_ARGS__)
  50. /** Perform a boolean operation between two graphs.
  51. *
  52. * This method populates an initialized graph with the result of the operation
  53. * between two other graphs. The resulting graph may be of any store type and
  54. * may be the result of graphs of different store types.
  55. *
  56. * @param[in] txn Transaction handle. It may be NULL.
  57. *
  58. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  59. *
  60. * @param gr1[in] First operand.
  61. *
  62. * @param gr2[in] Second operand.
  63. *
  64. * @param res[out] Result graph. The handle should be initialized via
  65. * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
  66. * If an unrecoverable error occurs, this graph is freed.
  67. *
  68. * @return LSUP_OK on success; <0 on error.
  69. */
  70. LSUP_rc
  71. LSUP_graph_bool_op_txn (
  72. void *txn, const LSUP_bool_op op,
  73. const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res);
  74. /// Non-transactional version of #LSUP_graph_bool_op_txn.
  75. #define LSUP_graph_bool_op(...) LSUP_graph_bool_op_txn (NULL, __VA_ARGS__)
  76. /** @brief Free a graph.
  77. */
  78. void
  79. LSUP_graph_free (LSUP_Graph *gr);
  80. /** @brief Compare two graphs.
  81. *
  82. * Note that if any of the two graphs has an open transaction, the function
  83. * is performed in the first graph's transaction.
  84. *
  85. * @param[in] gr1 First operand.
  86. *
  87. * @param[in] gr2 Second operand.
  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);
  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. /** @brief Underlying graph store handle.
  100. */
  101. LSUP_Store *
  102. LSUP_graph_store (const LSUP_Graph *gr);
  103. /** Set the URI of a graph.
  104. *
  105. * Note that by changing the URI of a graph backed by a context-sensitive store
  106. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  107. * graph points to. Triples are looked up in, and added to, the context that
  108. * the graph URI represents. A non-context graph retains the same triple set
  109. * when graph URI changes.
  110. *
  111. * @param gr[in] Graph handle.
  112. *
  113. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  114. *
  115. * @return LSUP_OK on success; <0 on error.
  116. */
  117. LSUP_rc
  118. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  119. /** @brief Get the namespace map for an in-memory graph.
  120. *
  121. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  122. */
  123. LSUP_NSMap *
  124. LSUP_graph_namespace (const LSUP_Graph *gr);
  125. /** @brief Set the namespace map for an in-memory graph.
  126. *
  127. * This has no effect on graph stores with LSUP_STORE_PERM.
  128. *
  129. * @param[in] gr Graph to set the namespace map for.
  130. *
  131. * @param[in] nsm Namespace handle.
  132. */
  133. void
  134. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  135. /** @brief Number of triples in a graph.
  136. */
  137. size_t
  138. LSUP_graph_size (const LSUP_Graph *gr);
  139. /** @brief Whether a graph contains a triple.
  140. *
  141. * @param[in] gr Graph to look up into.
  142. *
  143. * @param[in] spo Triple to look up.
  144. *
  145. * @return 1 if the triple is found, 0 if not found.
  146. */
  147. bool
  148. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  149. /** @brief Initialize an iterator to add triples.
  150. *
  151. * @param[in] txn Transaction handle. It may be NULL. If not NULL, its handle
  152. * will be bound to the iterator handle for its whole lifa cycle.
  153. *
  154. * @param[in] gr Graph to add to. It is added to the iterator state.
  155. *
  156. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  157. * must be freed with #LSUP_graph_add_done().
  158. */
  159. LSUP_GraphIterator *
  160. LSUP_graph_add_init_txn (void *txn, LSUP_Graph *gr);
  161. /// Non-transactional version of #LSUP_graph_init_txn.
  162. #define LSUP_graph_add_init(...) LSUP_graph_add_init_txn (NULL, __VA_ARGS__)
  163. /** @brief Add a single triple to the store.
  164. *
  165. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  166. *
  167. * @param[in] spo Triple to add. Caller retains ownership.
  168. */
  169. LSUP_rc
  170. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  171. /** @brief Finalize an add iteration loop and free the iterator.
  172. *
  173. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  174. *
  175. * @param[in] it Iterator to finalize.
  176. */
  177. void
  178. LSUP_graph_add_done (LSUP_GraphIterator *it);
  179. /** @brief Add triples to a graph.
  180. *
  181. * @param[in] txn Transaction handle. It may be NULL.
  182. *
  183. * @param[in] gr Graph to add triples to.
  184. *
  185. * @param[in] trp Array of triples to add. The last triple must be NULL.
  186. *
  187. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  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, const LSUP_Triple 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. /** 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(). 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().
  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] ol NUL-terminated term array.
  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