graph.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "environment.h"
  4. #include "term.h"
  5. /*
  6. * Define backend types.
  7. *
  8. * Add new store implementations to this table.
  9. */
  10. #define BACKEND_TBL \
  11. /* #enum pfx #store #iterator */\
  12. /*ENTRY( HTABLE, htstore_int, htiter_int )*/ \
  13. ENTRY( MDB, mdbstore_int, mdbiter_int ) \
  14. ENTRY( MDB_TMP, mdbstore_int, mdbiter_int ) \
  15. /** @brief Store types. All prefixed with `LSUP_STORE_`.
  16. */
  17. typedef enum {
  18. #define ENTRY(a, b, c) LSUP_STORE_##a,
  19. BACKEND_TBL
  20. #undef ENTRY
  21. } LSUP_store_type;
  22. /** @brief Graph object.
  23. */
  24. typedef struct Graph LSUP_Graph;
  25. /** @brief Graph iterator.
  26. *
  27. * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
  28. * over lookup results with #LSUP_graph_iter_next. It must be freed with
  29. * #LSUP_graph_iter_free when done.
  30. */
  31. typedef struct GraphIterator LSUP_GraphIterator;
  32. /** @brief Create an empty graph.
  33. *
  34. * The new graph has zero capacity and a random URN. To change either one, use
  35. * #LSUP_graph_resize and #LSUP_graph_set_uri, respectively.
  36. *
  37. * The graph is assigned a default (volatile) namespace map if it's in memory,
  38. * hence all graphs share the same namespace map by default. To change this,
  39. * use #LSUP_graph_set_namespace(). MDB graphs use a persistent namespace map
  40. * that is common to all the graphs in the same store. This cannot be changed.
  41. *
  42. * @param store_type[in] Type of store for the graph. One of the values of
  43. * #LSUP_store_type.
  44. *
  45. * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  46. */
  47. LSUP_Graph *
  48. LSUP_graph_new_env (
  49. const LSUP_Env *env, LSUP_Term *uri, const LSUP_store_type store_type);
  50. /** @brief Create an empty graph with the default environment.
  51. *
  52. * This is likely to be used more often than #LSUP_graph_new_env().
  53. */
  54. #define LSUP_graph_new(uri, type) \
  55. LSUP_graph_new_env (LSUP_default_env, uri, type)
  56. /** @brief Shortcut for #LSUP_graph_new_lookup_env() on default MDB store.
  57. */
  58. #define LSUP_graph_new_lookup(s, p, o) \
  59. LSUP_graph_new_lookup_env (LSUP_default_env, (s), (p), (o))
  60. /** @brief copy a graph into a new one.
  61. *
  62. * The new graph is compacted to the minimum required size.
  63. *
  64. * src[in] Graph to be copied.
  65. *
  66. * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
  67. *
  68. * @param gr[out] Pointer to a pointer to the destination graph. It must be
  69. * freed with #LSUP_graph_free when done.
  70. *
  71. * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  72. */
  73. LSUP_Graph *
  74. LSUP_graph_copy (const LSUP_Graph *src);
  75. /** @brief Copy the contents of a graph into a permanent store.
  76. *
  77. * It is possible to store a memory graph, a RAMdisk MDB graph, or a
  78. * permanently stored graph into another environment.
  79. *
  80. * The namespace map associated with the graph is stored into the destination
  81. * as well, except for existing namespaces and prefixes.
  82. *
  83. * @param[in] src Graph to store.
  84. *
  85. * @param[out] dest Pointer to graph handle for the new stored graph. The new
  86. * graph will have the same URI as the source. It may be NULL.
  87. *
  88. * @param[in] env Environment to copy to. If NULL, it is set to the deafult
  89. * LSUP store. This makes it possible to copy MDB graphs across different
  90. * environments. If the source is a MDB graph and the environment is the same
  91. * as the source, no change occurs.
  92. *
  93. * @return LSUP_OK on success; LSUP_NOACTION if the graph is already stored in
  94. * the same enviroment; <0 on error.
  95. */
  96. LSUP_rc
  97. LSUP_graph_store (
  98. const LSUP_Graph *src, LSUP_Graph **dest_p, const LSUP_Env *env);
  99. /** Perform a boolean operation between two graphs.
  100. *
  101. * This method yields a new graph as the result of the operation.
  102. *
  103. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  104. *
  105. * @param gr1[in] First operand.
  106. *
  107. * @param gr2[in] Second operand.
  108. *
  109. * @param res[out] Result graph. It must be freed with #LSUP_graph_free when
  110. * done.
  111. */
  112. LSUP_Graph *
  113. LSUP_graph_bool_op(
  114. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  115. /** @brief Free a graph.
  116. */
  117. void
  118. LSUP_graph_free (LSUP_Graph *gr);
  119. /** @brief Compare two graphs.
  120. *
  121. * @param[in] gr1 First operand.
  122. *
  123. * @param[in] gr2 Second operand.
  124. *
  125. * @return True if the graphs are topologically equal, false otherwise.
  126. */
  127. bool
  128. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  129. /** @brief Read-only graph URI.
  130. *
  131. * To change the graph URI, use #LSUP_graph_set_uri.
  132. */
  133. LSUP_Term *
  134. LSUP_graph_uri (const LSUP_Graph *gr);
  135. /** Set the URI of a graph.
  136. *
  137. * Note that by changing the URI of a graph backed by a context-sensitive store
  138. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  139. * graph points to. Triples are looked up in, and added to, the context that
  140. * the graph URI represents. A non-context graph retains the same triple set
  141. * when graph URI changes.
  142. *
  143. * @param gr[in] Graph handle.
  144. *
  145. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  146. *
  147. * @return LSUP_OK on success; <0 on error.
  148. */
  149. LSUP_rc
  150. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  151. /** @brief Get the namespace map for an in-memory graph.
  152. *
  153. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  154. */
  155. LSUP_NSMap *
  156. LSUP_graph_namespace (const LSUP_Graph *gr);
  157. /** @brief Set the namespace map for an in-memory graph.
  158. *
  159. * This has no effect on MDB graphs.
  160. *
  161. * @param[in] gr Graph to set the namespace map for.
  162. *
  163. * @param[in] nsm Namespace handle.
  164. */
  165. void
  166. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  167. /** @brief Number of triples in a graph.
  168. */
  169. size_t
  170. LSUP_graph_size (const LSUP_Graph *gr);
  171. /** @brief Whether a graph contains a triple.
  172. *
  173. * @param[in] gr Graph to look up into.
  174. *
  175. * @param[in] spo Triple to look up.
  176. *
  177. * @return 1 if the triple is found, 0 if not found.
  178. */
  179. bool
  180. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  181. /** @brief Initialize an iterator to add triples.
  182. *
  183. * @param[in] gr Graph to add to. It is added to the iterator state.
  184. *
  185. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  186. * must be freed with #LSUP_graph_add_done().
  187. */
  188. LSUP_GraphIterator *
  189. LSUP_graph_add_init (LSUP_Graph *gr);
  190. /** @brief Add a single triple to the store.
  191. *
  192. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  193. *
  194. * @param[in] spo Triple to add. Caller retains ownership.
  195. */
  196. LSUP_rc
  197. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  198. /** @brief Finalize an add iteration loop and free the iterator.
  199. *
  200. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  201. *
  202. * @param[in] it Iterator to finalize.
  203. */
  204. void
  205. LSUP_graph_add_done (LSUP_GraphIterator *it);
  206. /** @brief Add triples to a graph.
  207. *
  208. * @param[in] gr Graph to add triples to.
  209. *
  210. * @param[in] trp Array of triples to add. The last triple must be NULL.
  211. *
  212. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  213. *
  214. * @param[out] inserted This will be filled with the total number of triples
  215. * inserted.
  216. */
  217. LSUP_rc
  218. LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *inserted);
  219. /** @brief Delete triples by a matching pattern.
  220. *
  221. * @param gr[in] Graph to delete triples from.
  222. *
  223. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  224. *
  225. * @param ct[out] If not NULL it is populated with the number of triples
  226. * deleted.
  227. */
  228. LSUP_rc
  229. LSUP_graph_remove (
  230. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  231. const LSUP_Term *o, size_t *ct);
  232. /** Look up triples by a matching pattern and yield an iterator.
  233. *
  234. * @param gr[in] Graph to look up.
  235. *
  236. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  237. * indicate unbound terms.
  238. *
  239. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  240. * freed with #LSUP_graph_iter_free after use.
  241. */
  242. LSUP_GraphIterator *
  243. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  244. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  245. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  246. *
  247. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  248. *
  249. * @param spo[out] Triple to be populated with the next result. May be NULL
  250. * (e.g. for counters).
  251. *
  252. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  253. * was reached.
  254. */
  255. LSUP_rc
  256. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  257. /** @brief Free a graph iterator.
  258. *
  259. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  260. * #LSUP_graph_add_done() with those.
  261. *
  262. * @param[in] it Iterator to finalize.
  263. */
  264. void
  265. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  266. #endif