graph.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "triple.h"
  4. #include "namespace.h"
  5. #include "environment.h"
  6. /*
  7. * Define backend types and checks.
  8. */
  9. #define BACKEND_TBL \
  10. ENTRY( MEM, 0)/* Memory backend, hash map. */ \
  11. ENTRY( MDB, 1)/* LMDB back end on persistent disk. */ \
  12. ENTRY( MDB_TMP, 2)/* LMDB back end on RAM disk. */ \
  13. typedef enum LSUP_store_type {
  14. #define ENTRY(a, b) LSUP_STORE_##a = b,
  15. BACKEND_TBL
  16. #undef ENTRY
  17. } LSUP_store_type;
  18. /** @brief Graph object.
  19. */
  20. typedef struct Graph LSUP_Graph;
  21. /** @brief Graph iterator.
  22. *
  23. * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
  24. * over lookup results with #LSUP_graph_iter_next. It must be freed with
  25. * #LSUP_graph_iter_free when done.
  26. */
  27. typedef struct GraphIterator LSUP_GraphIterator;
  28. /** @brief Create an empty graph.
  29. *
  30. * The new graph has zero capacity and a random URN. To change either one, use
  31. * #LSUP_graph_resize and #LSUP_graph_set_uri, respectively.
  32. *
  33. * The graph is assigned a default (volatile) namespace map if it's in memory,
  34. * hence all graphs share the same namespace map by default. To change this,
  35. * use #LSUP_graph_set_namespace(). MDB graphs use a persistent namespace map
  36. * that is common to all the graphs in the same store. This cannot be changed.
  37. *
  38. * @param store_type[in] TYpe of store for the graph. One of the values of
  39. * #LSUP_store_type.
  40. *
  41. * @param gr[out] Pointer to a pointer to the new graph. It must be freed with
  42. * #LSUP_graph_free when done.
  43. *
  44. * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  45. */
  46. LSUP_Graph *
  47. LSUP_graph_new_env (const LSUP_Env *env, const LSUP_store_type store_type);
  48. /** @brief Create an empty graph with the default environment.
  49. *
  50. * This is likely to be used more often than #LSUP_graph_new_env().
  51. */
  52. #define LSUP_graph_new(type) LSUP_graph_new_env (LSUP_default_env, type)
  53. /** @brief copy a graph into a new one.
  54. *
  55. * The new graph is compacted to the minimum required size.
  56. *
  57. * src[in] Graph to be copied.
  58. *
  59. * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
  60. *
  61. * @param gr[out] Pointer to a pointer to the destination graph. It must be
  62. * freed with #LSUP_graph_free when done.
  63. *
  64. * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  65. */
  66. LSUP_Graph *
  67. LSUP_graph_copy (const LSUP_Graph *src);
  68. /** @brief Copy the contents of a graph into a permanent store.
  69. *
  70. * It is possible to store a memory graph, a RAMdisk MDB graph, or a
  71. * permanently stored graph into another environment.
  72. *
  73. * The namespace map associated with the graph is stored into the destination
  74. * as well, except for existing namespaces and prefixes.
  75. *
  76. * @param[in] src Graph to store.
  77. *
  78. * @param[in] env Environment to copy to. If NULL, it is set to the deafult
  79. * LSUP store. This makes it possible to copy MDB graphs across different
  80. * envirnments. If the source is a MDB graph and the environment is the same
  81. * as the source, no change occurs.
  82. *
  83. * @return LSUP_OK on success; LSUP_NOACTION if the graph is already stored in
  84. * the same enviroment; <0 on error.
  85. */
  86. LSUP_rc
  87. LSUP_graph_store (const LSUP_Graph *src, const LSUP_Env *env);
  88. /** Perform a boolean operation between two graphs.
  89. *
  90. * This method yields a new graph as the result of the operation.
  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. It must be freed with #LSUP_graph_free when
  99. * done.
  100. */
  101. LSUP_Graph *
  102. LSUP_graph_bool_op(
  103. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  104. /** @brief Free a graph.
  105. */
  106. void
  107. LSUP_graph_free (LSUP_Graph *gr);
  108. /** @brief Compare two graphs.
  109. *
  110. * @param[in] gr1 First operand.
  111. *
  112. * @param[in] gr2 Second operand.
  113. *
  114. * @return True if the graphs are topologically equal, false otherwise.
  115. */
  116. bool
  117. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  118. /** @brief Read-only graph URI.
  119. *
  120. * To change the graph URI, use #LSUP_graph_set_uri.
  121. */
  122. LSUP_Term *
  123. LSUP_graph_uri (const LSUP_Graph *gr);
  124. /** Set the URI of a graph.
  125. *
  126. * @param gr[in] Graph whose URI is to be changed.
  127. *
  128. * @param uri[in] New URI as a string. If NULL, a UUID4 URN is generated.
  129. */
  130. LSUP_rc
  131. LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri);
  132. /** @brief Get the namespace map for an in-memory graph.
  133. *
  134. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  135. */
  136. LSUP_NSMap *
  137. LSUP_graph_namespace (const LSUP_Graph *gr);
  138. /** @brief Set the namespace map for an in-memory graph.
  139. *
  140. * This has no effect on MDB graphs.
  141. *
  142. * @param[in] gr Graph to set the namespace map for.
  143. *
  144. * @param[in] nsm Namespace handle.
  145. */
  146. void
  147. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  148. /** @brief Number of triples in a graph.
  149. */
  150. size_t
  151. LSUP_graph_size (const LSUP_Graph *gr);
  152. /** @brief Whether a graph contains a triple.
  153. *
  154. * @param[in] gr Graph to look up into.
  155. *
  156. * @param[in] spo Triple to look up.
  157. *
  158. * @return 1 if the triple is found, 0 if not found.
  159. */
  160. bool
  161. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  162. /** @brief Initialize an iterator to add triples.
  163. *
  164. * @param[in] gr Graph to add to. It is added to the iterator state.
  165. *
  166. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  167. * must be freed with #LSUP_graph_add_done().
  168. */
  169. LSUP_GraphIterator *
  170. LSUP_graph_add_init (LSUP_Graph *gr);
  171. /** @brief Add a single triple to the store.
  172. *
  173. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  174. *
  175. * @param[in] sspo Serialized triple to add.
  176. */
  177. LSUP_rc
  178. LSUP_graph_add_iter (
  179. LSUP_GraphIterator *it, const LSUP_SerTriple *sspo);
  180. /** @brief Finalize an add iteration loop and free the iterator.
  181. *
  182. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  183. *
  184. * @param[in] it Iterator to finalize.
  185. */
  186. void
  187. LSUP_graph_add_done (LSUP_GraphIterator *it);
  188. /** @brief Add triples and/or serialized triples to a graph.
  189. *
  190. * For API users it may be more convenient to use the more specialized
  191. * #LSUP_graph_add_trp.
  192. *
  193. * @param[in] gr Graph to add triples to.
  194. *
  195. * @param[in] trp Array of triples to add. The last triple must be NULL.
  196. *
  197. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  198. *
  199. * @param[out] inserted This will be filled with the total number of triples
  200. * inserted.
  201. */
  202. LSUP_rc
  203. LSUP_graph_add (
  204. LSUP_Graph *gr, const LSUP_Triple trp[],
  205. const LSUP_SerTriple strp[], size_t *inserted);
  206. /** @brief Insert RDF triples into a graph.
  207. *
  208. * This is a convenience method for external callers which most likely have
  209. * non-serialized triples at hand.
  210. */
  211. #define LSUP_graph_add_trp(gr, trp, ins) LSUP_graph_add (gr, trp, NULL, ins)
  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. /** @brief Get the iterator counter.
  260. */
  261. size_t
  262. LSUP_graph_iter_cur (LSUP_GraphIterator *it);
  263. #endif