graph.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "store_mdb.h"
  4. #include "store_htable.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. * @param store_type[in] TYpe of store for the graph. One of the values of
  33. * #LSUP_store_type.
  34. *
  35. * @param gr[out] Pointer to a pointer to the new graph. It must be freed with
  36. * #LSUP_graph_free when done.
  37. *
  38. * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  39. */
  40. LSUP_Graph *
  41. LSUP_graph_new (const LSUP_store_type store_type);
  42. /** @brief copy a graph into a new one.
  43. *
  44. * The new graph is compacted to the minimum required size.
  45. *
  46. * src[in] Graph to be copied.
  47. *
  48. * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
  49. *
  50. * @param gr[out] Pointer to a pointer to the destination graph. It must be
  51. * freed with #LSUP_graph_free when done.
  52. *
  53. * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  54. */
  55. LSUP_Graph *
  56. LSUP_graph_copy (const LSUP_Graph *src);
  57. /** Perform a boolean operation between two graphs.
  58. *
  59. * This method yields a new graph as the result of the operation.
  60. *
  61. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  62. *
  63. * @param gr1[in] First operand.
  64. *
  65. * @param gr2[in] Second operand.
  66. *
  67. * @param res[out] Result graph. It must be freed with #LSUP_graph_free when
  68. * done.
  69. */
  70. LSUP_Graph *
  71. LSUP_graph_bool_op(
  72. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  73. /** @brief Free a graph.
  74. */
  75. void
  76. LSUP_graph_free (LSUP_Graph *gr);
  77. /** @brief Number of triples in a graph.
  78. */
  79. size_t
  80. LSUP_graph_size (const LSUP_Graph *gr);
  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. * @param gr[in] Graph whose URI is to be changed.
  92. *
  93. * @param uri[in] New URI as a string. If NULL, a UUID4 URN is generated.
  94. */
  95. LSUP_rc
  96. LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri);
  97. bool
  98. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  99. /** @brief Initialize an iterator to add triples.
  100. *
  101. * @param[in] gr Graph to add to. It is added to the iterator state.
  102. *
  103. * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
  104. * must be freed with #LSUP_graph_add_done().
  105. */
  106. LSUP_GraphIterator *
  107. LSUP_graph_add_init (LSUP_Graph *gr);
  108. /** @brief Add a single triple to the store.
  109. *
  110. * @param[in] it Iterator obtained with #LSUP_graph_add_init().
  111. *
  112. * @param[in] sspo Serialized triple to add.
  113. */
  114. LSUP_rc
  115. LSUP_graph_add_iter (
  116. LSUP_GraphIterator *it, const LSUP_SerTriple *sspo);
  117. /** @brief Finalize an add iteration loop and free the iterator.
  118. *
  119. * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
  120. *
  121. * @param[in] it Iterator to finalize.
  122. */
  123. void
  124. LSUP_graph_add_done (LSUP_GraphIterator *it);
  125. /** @brief Add triples and/or serialized triples to a graph.
  126. *
  127. * For API users it may be more convenient to use the more specialized
  128. * #LSUP_graph_add_trp.
  129. */
  130. LSUP_rc
  131. LSUP_graph_add(
  132. LSUP_Graph *gr,
  133. const LSUP_Triple trp[], size_t trp_ct,
  134. const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
  135. #define LSUP_graph_add_trp(gr, trp, ct, ins) \
  136. LSUP_graph_add (gr, trp, ct, NULL, 0, ins)
  137. /** @brief Delete triples by a matching pattern.
  138. *
  139. * @param gr[in] Graph to delete triples from.
  140. *
  141. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  142. *
  143. * @param ct[out] If not NULL it is populated with the number of triples
  144. * deleted.
  145. */
  146. LSUP_rc
  147. LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  148. /** Look up triples by a matching pattern and yield an iterator.
  149. *
  150. * @param gr[in] Graph to look up.
  151. *
  152. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  153. * indicate unbound terms.
  154. *
  155. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  156. * freed with #LSUP_graph_iter_free after use.
  157. */
  158. LSUP_GraphIterator *
  159. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  160. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  161. *
  162. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  163. *
  164. * @param spo[out] Triple to be populated with the next result. May be NULL
  165. * (e.g. for counters).
  166. *
  167. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  168. * was reached.
  169. */
  170. LSUP_rc
  171. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  172. /** @brief Free a graph iterator.
  173. *
  174. * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
  175. * #LSUP_graph_add_done() with those.
  176. *
  177. * @param[in] it Iterator to finalize.
  178. */
  179. void
  180. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  181. /** @brief Get the iterator counter.
  182. */
  183. size_t
  184. LSUP_graph_iter_cur (LSUP_GraphIterator *it);
  185. #endif