graph.h 15 KB

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