graph.h 13 KB

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