graph.h 12 KB

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