graph.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 triples from a source graph into a destination one.
  42. *
  43. * The destination graph is not initialized here, so the copy is cumulative.
  44. */
  45. LSUP_rc
  46. LSUP_graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest);
  47. /** Perform a boolean operation between two graphs.
  48. *
  49. * This method populates an initialized graph with the result of the operation
  50. * between two other graphs. The resulting graph may be of any store type and
  51. * may be the result of graphs of different store types.
  52. *
  53. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  54. *
  55. * @param gr1[in] First operand.
  56. *
  57. * @param gr2[in] Second operand.
  58. *
  59. * @param res[out] Result graph. The handle should be initialized via
  60. * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
  61. * If an unrecoverable error occurs, this graph is freed.
  62. *
  63. * @return LSUP_OK on success; <0 on error.
  64. */
  65. LSUP_rc
  66. LSUP_graph_bool_op(
  67. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2,
  68. LSUP_Graph *res);
  69. /** @brief Free a graph.
  70. */
  71. void
  72. LSUP_graph_free (LSUP_Graph *gr);
  73. /** @brief Compare two graphs.
  74. *
  75. * @param[in] gr1 First operand.
  76. *
  77. * @param[in] gr2 Second operand.
  78. *
  79. * @return True if the graphs are topologically equal, false otherwise.
  80. */
  81. bool
  82. LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  83. /** @brief Read-only graph URI.
  84. *
  85. * To change the graph URI, use #LSUP_graph_set_uri.
  86. */
  87. LSUP_Term *
  88. LSUP_graph_uri (const LSUP_Graph *gr);
  89. /** Set the URI of a graph.
  90. *
  91. * Note that by changing the URI of a graph backed by a context-sensitive store
  92. * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
  93. * graph points to. Triples are looked up in, and added to, the context that
  94. * the graph URI represents. A non-context graph retains the same triple set
  95. * when graph URI changes.
  96. *
  97. * @param gr[in] Graph handle.
  98. *
  99. * @param uri[in] IRI handle. The graph takes ownership of the handle.
  100. *
  101. * @return LSUP_OK on success; <0 on error.
  102. */
  103. LSUP_rc
  104. LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
  105. /** @brief Get the namespace map for an in-memory graph.
  106. *
  107. * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
  108. */
  109. LSUP_NSMap *
  110. LSUP_graph_namespace (const LSUP_Graph *gr);
  111. /** @brief Set the namespace map for an in-memory graph.
  112. *
  113. * This has no effect on MDB graphs.
  114. *
  115. * @param[in] gr Graph to set the namespace map for.
  116. *
  117. * @param[in] nsm Namespace handle.
  118. */
  119. void
  120. LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
  121. /** @brief Number of triples in a graph.
  122. */
  123. size_t
  124. LSUP_graph_size (const LSUP_Graph *gr);
  125. /** @brief Whether a graph contains a triple.
  126. *
  127. * @param[in] gr Graph to look up into.
  128. *
  129. * @param[in] spo Triple to look up.
  130. *
  131. * @return 1 if the triple is found, 0 if not found.
  132. */
  133. bool
  134. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  135. /** @brief Initialize an iterator to add triples.
  136. *
  137. * @param[in] gr Graph to add to. It is added to the iterator state.
  138. *
  139. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  140. * must be freed with #LSUP_graph_add_done().
  141. */
  142. LSUP_GraphIterator *
  143. LSUP_graph_add_init (LSUP_Graph *gr);
  144. /** @brief Add a single triple to the store.
  145. *
  146. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  147. *
  148. * @param[in] spo Triple to add. Caller retains ownership.
  149. */
  150. LSUP_rc
  151. LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
  152. /** @brief Finalize an add iteration loop and free the iterator.
  153. *
  154. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  155. *
  156. * @param[in] it Iterator to finalize.
  157. */
  158. void
  159. LSUP_graph_add_done (LSUP_GraphIterator *it);
  160. /** @brief Add triples to a graph.
  161. *
  162. * @param[in] gr Graph to add triples to.
  163. *
  164. * @param[in] trp Array of triples to add. The last triple must be NULL.
  165. *
  166. * @param[in] strp Array of buffer triples to add. The last one must be NULL.
  167. *
  168. * @param[out] inserted This will be filled with the total number of triples
  169. * inserted.
  170. */
  171. LSUP_rc
  172. LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *inserted);
  173. /** @brief Delete triples by a matching pattern.
  174. *
  175. * @param gr[in] Graph to delete triples from.
  176. *
  177. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  178. *
  179. * @param ct[out] If not NULL it is populated with the number of triples
  180. * deleted.
  181. */
  182. LSUP_rc
  183. LSUP_graph_remove (
  184. LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
  185. const LSUP_Term *o, size_t *ct);
  186. /** Look up triples by a matching pattern and yield an iterator.
  187. *
  188. * @param gr[in] Graph to look up.
  189. *
  190. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  191. * indicate unbound terms.
  192. *
  193. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  194. * freed with #LSUP_graph_iter_free after use.
  195. */
  196. LSUP_GraphIterator *
  197. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
  198. const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
  199. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  200. *
  201. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  202. *
  203. * @param spo[out] Triple to be populated with the next result. May be NULL
  204. * (e.g. for counters).
  205. *
  206. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  207. * was reached.
  208. */
  209. LSUP_rc
  210. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  211. /** @brief Free a graph iterator.
  212. *
  213. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  214. * #LSUP_graph_add_done() with those.
  215. *
  216. * @param[in] it Iterator to finalize.
  217. */
  218. void
  219. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  220. #endif