graph.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 and is used to iterate
  12. * over lookup results with #LSUP_graph_iter_next. It must be freed with
  13. * #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] uri URI of the new graph. If NULL, a UUID4 URN is generated.
  19. *
  20. * @param store_type[in] Type of store backing the graph. One of the values of
  21. * #LSUP_StoreType.
  22. *
  23. * @param[in] store_id Identifier for the back end store. This may be
  24. * interpreted differently by each store implementation. For the MDB store,
  25. * this is the file path where the store is located. It is ignored by volatile
  26. * stores (with LSUP_STORE_PERM feature flag set to false). If a store
  27. * does not exist for the given identifier, a new one is initialized. If this
  28. * parameter is NULL, the default store for the selected type is used.
  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 (
  39. LSUP_Term *uri, const LSUP_StoreType store_type, const char *store_id,
  40. LSUP_NSMap *nsm, size_t size);
  41. /** @brief copy a graph into a new one.
  42. *
  43. * The new graph is compacted to the minimum required size.
  44. *
  45. * @param[in] src Graph to be copied.
  46. *
  47. * @param[in] uri URI of the destination graph. If NULL, a UUID4 URN is
  48. * generated.
  49. *
  50. * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
  51. */
  52. LSUP_Graph *
  53. LSUP_graph_copy (const LSUP_Graph *src, LSUP_Term *uri);
  54. #if 0
  55. /** @brief Copy the contents of a graph into a permanent store.
  56. *
  57. * It is possible to store a memory graph, a RAMdisk MDB graph, or a
  58. * permanently stored graph into another environment.
  59. *
  60. * The namespace map associated with the graph is stored into the destination
  61. * as well, except for existing namespaces and prefixes.
  62. *
  63. * @param[in] src Graph to store.
  64. *
  65. * @param[out] dest Pointer to graph handle for the new stored graph. The new
  66. * graph will have the same URI as the source. It may be NULL.
  67. *
  68. * @param[in] env Environment to copy to. If NULL, it is set to the deafult
  69. * LSUP store. This makes it possible to copy MDB graphs across different
  70. * environments. If the source is a MDB graph and the environment is the same
  71. * as the source, no change occurs.
  72. *
  73. * @return LSUP_OK on success; LSUP_NOACTION if the graph is already stored in
  74. * the same enviroment; <0 on error.
  75. */
  76. LSUP_rc
  77. LSUP_graph_store (
  78. const LSUP_Graph *src, LSUP_Graph **dest_p, const LSUP_Env *env);
  79. #endif
  80. /** @brief Copy triples from a source graph into a destination one.
  81. *
  82. * The destination graph is not initialized here, so the copy is cumulative.
  83. */
  84. LSUP_rc
  85. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest);
  86. /** Perform a boolean operation between two graphs.
  87. *
  88. * This method populates an initialized graph with the result of the operation
  89. * between two other graphs. The resulting graph may be of any store type and
  90. * may be the result of graphs of different store types.
  91. *
  92. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  93. *
  94. * @param gr1[in] First operand.
  95. *
  96. * @param gr2[in] Second operand.
  97. *
  98. * @param res[out] Result graph. The handle should be initialized via
  99. * #LSUP_graph_new(). Any preexisting contents are not removed. If an
  100. * unrecoverable error occurs, this graph is freed.
  101. *
  102. * @return LSUP_OK on success; <0 on error.
  103. */
  104. LSUP_rc
  105. LSUP_graph_bool_op(
  106. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2,
  107. LSUP_Graph *res);
  108. /** @brief Free a graph.
  109. */
  110. void
  111. LSUP_graph_free (LSUP_Graph *gr);
  112. /** @brief Compare two graphs.
  113. *
  114. * @param[in] gr1 First operand.
  115. *
  116. * @param[in] gr2 Second operand.
  117. *
  118. * @return True if the graphs are topologically equal, false otherwise.
  119. */
  120. bool
  121. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  122. /** @brief Read-only graph URI.
  123. *
  124. * To change the graph URI, use #LSUP_graph_set_uri.
  125. */
  126. LSUP_Term *
  127. LSUP_graph_uri (const LSUP_Graph *gr);
  128. /** Set the URI of a graph.
  129. *
  130. * Note that by changing the URI of a graph backed by a context-sensitive store
  131. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  132. * graph points to. Triples are looked up in, and added to, the context that
  133. * the graph URI represents. A non-context graph retains the same triple set
  134. * when graph URI changes.
  135. *
  136. * @param gr[in] Graph handle.
  137. *
  138. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  139. *
  140. * @return LSUP_OK on success; <0 on error.
  141. */
  142. LSUP_rc
  143. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  144. /** @brief Get the namespace map for an in-memory graph.
  145. *
  146. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  147. */
  148. LSUP_NSMap *
  149. LSUP_graph_namespace (const LSUP_Graph *gr);
  150. /** @brief Set the namespace map for an in-memory graph.
  151. *
  152. * This has no effect on MDB graphs.
  153. *
  154. * @param[in] gr Graph to set the namespace map for.
  155. *
  156. * @param[in] nsm Namespace handle.
  157. */
  158. void
  159. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  160. /** @brief Number of triples in a graph.
  161. */
  162. size_t
  163. LSUP_graph_size (const LSUP_Graph *gr);
  164. /** @brief Whether a graph contains a triple.
  165. *
  166. * @param[in] gr Graph to look up into.
  167. *
  168. * @param[in] spo Triple to look up.
  169. *
  170. * @return 1 if the triple is found, 0 if not found.
  171. */
  172. bool
  173. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  174. /** @brief Initialize an iterator to add triples.
  175. *
  176. * @param[in] gr Graph to add to. It is added to the iterator state.
  177. *
  178. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  179. * must be freed with #LSUP_graph_add_done().
  180. */
  181. LSUP_GraphIterator *
  182. LSUP_graph_add_init (LSUP_Graph *gr);
  183. /** @brief Add a single triple to the store.
  184. *
  185. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  186. *
  187. * @param[in] spo Triple to add. Caller retains ownership.
  188. */
  189. LSUP_rc
  190. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  191. /** @brief Finalize an add iteration loop and free the iterator.
  192. *
  193. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  194. *
  195. * @param[in] it Iterator to finalize.
  196. */
  197. void
  198. LSUP_graph_add_done (LSUP_GraphIterator *it);
  199. /** @brief Add triples to a graph.
  200. *
  201. * @param[in] gr Graph to add triples to.
  202. *
  203. * @param[in] trp Array of triples to add. The last triple must be NULL.
  204. *
  205. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  206. *
  207. * @param[out] inserted This will be filled with the total number of triples
  208. * inserted.
  209. */
  210. LSUP_rc
  211. LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *inserted);
  212. /** @brief Delete triples by a matching pattern.
  213. *
  214. * @param gr[in] Graph to delete triples from.
  215. *
  216. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  217. *
  218. * @param ct[out] If not NULL it is populated with the number of triples
  219. * deleted.
  220. */
  221. LSUP_rc
  222. LSUP_graph_remove (
  223. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  224. const LSUP_Term *o, size_t *ct);
  225. /** Look up triples by a matching pattern and yield an iterator.
  226. *
  227. * @param gr[in] Graph to look up.
  228. *
  229. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  230. * indicate unbound terms.
  231. *
  232. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  233. * freed with #LSUP_graph_iter_free after use.
  234. */
  235. LSUP_GraphIterator *
  236. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  237. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  238. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  239. *
  240. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  241. *
  242. * @param spo[out] Triple to be populated with the next result. May be NULL
  243. * (e.g. for counters).
  244. *
  245. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  246. * was reached.
  247. */
  248. LSUP_rc
  249. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  250. /** @brief Free a graph iterator.
  251. *
  252. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  253. * #LSUP_graph_add_done() with those.
  254. *
  255. * @param[in] it Iterator to finalize.
  256. */
  257. void
  258. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  259. #endif