graph.h 9.6 KB

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