graph.h 9.5 KB

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