graph.h 15 KB

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