graph.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "store_mdb.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. * @param store_type[in] TYpe of store for the graph. One of the values of
  32. * #LSUP_store_type.
  33. *
  34. * @param gr[out] Pointer to a pointer to the new graph. It must be freed with
  35. * #LSUP_graph_free when done.
  36. *
  37. * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  38. */
  39. LSUP_Graph *
  40. LSUP_graph_new (const LSUP_store_type store_type);
  41. /** @brief copy a graph into a new one.
  42. *
  43. * The new graph is compacted to the minimum required size.
  44. *
  45. * src[in] Graph to be copied.
  46. *
  47. * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
  48. *
  49. * @param gr[out] Pointer to a pointer to the destination graph. It must be
  50. * freed with #LSUP_graph_free when done.
  51. *
  52. * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  53. */
  54. LSUP_Graph *
  55. LSUP_graph_copy (const LSUP_Graph *src);
  56. /** Perform a boolean operation between two graphs.
  57. *
  58. * This method yields a new graph as the result of the operation.
  59. *
  60. * @param op[in] Operation to perform. One of #LSUP_bool_op.
  61. *
  62. * @param gr1[in] First operand.
  63. *
  64. * @param gr2[in] Second operand.
  65. *
  66. * @param res[out] Result graph. It must be freed with #LSUP_graph_free when
  67. * done.
  68. */
  69. LSUP_Graph *
  70. LSUP_graph_bool_op(
  71. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  72. /** @brief Free a graph.
  73. */
  74. void
  75. LSUP_graph_free (LSUP_Graph *gr);
  76. /** @brief Number of triples that can be stored without resizing the graph.
  77. *
  78. * @return Dynamic capacity of an in-memory graph or maximum allowed memory for
  79. * an MDB graph.
  80. */
  81. size_t
  82. LSUP_graph_capacity (const LSUP_Graph *gr);
  83. /** @brief Number of triples in a graph.
  84. */
  85. size_t
  86. LSUP_graph_size (const LSUP_Graph *gr);
  87. /** @brief Change the capacity of an in-memory graph.
  88. *
  89. * This is useful ahead of a bulk load to save multiple reallocs. Otherwise,
  90. * the graph expands automatically on new inserts when capacity is reached.
  91. *
  92. * @param gr[in] Graph to be resized.
  93. *
  94. * @param size[in] New size. This will never be smaller than the current
  95. * occupied space. Therefore setting this value to 0 effectively compacts the
  96. * graph storage.
  97. *
  98. * @return LSUP_OK if the operation was successful; LSUP_VALUE_ERR if the store
  99. * type of the graph is not LSUP_STORE_MEM; <0 if an error occurs while
  100. * resizing.
  101. */
  102. LSUP_rc
  103. LSUP_graph_resize (LSUP_Graph *gr, size_t size);
  104. /** @brief Read-only graph URI.
  105. *
  106. * To change the graph URI, use #LSUP_graph_set_uri.
  107. */
  108. LSUP_Term *
  109. LSUP_graph_uri (const LSUP_Graph *gr);
  110. /** Set the URI of a graph.
  111. *
  112. * @param gr[in] Graph whose URI is to be changed.
  113. *
  114. * @param uri[in] New URI as a string. If NULL, a UUID4 URN is generated.
  115. */
  116. LSUP_rc
  117. LSUP_graph_set_uri (LSUP_Graph *gr, const char *uri);
  118. bool
  119. LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
  120. LSUP_GraphIterator *
  121. LSUP_graph_add_stream_init (LSUP_Graph *gr);
  122. LSUP_rc
  123. LSUP_graph_add_stream_iter (
  124. LSUP_GraphIterator *it, const LSUP_SerTriple *sspo);
  125. void
  126. LSUP_graph_add_stream_done (LSUP_GraphIterator *it);
  127. /** @brief Add triples and/or serialized triples to a graph.
  128. *
  129. * For API users it may be more convenient to use the more specialized
  130. * #LSUP_graph_add_trp.
  131. */
  132. LSUP_rc
  133. LSUP_graph_add(
  134. LSUP_Graph *gr,
  135. const LSUP_Triple trp[], size_t trp_ct,
  136. const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
  137. #define LSUP_graph_add_trp(gr, trp, ct, ins) \
  138. LSUP_graph_add (gr, trp, ct, NULL, 0, ins)
  139. /** @brief Delete triples by a matching pattern.
  140. *
  141. * @param gr[in] Graph to delete triples from.
  142. *
  143. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  144. *
  145. * @param ct[out] If not NULL it is populated with the number of triples
  146. * deleted.
  147. */
  148. LSUP_rc
  149. LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  150. /** Look up triples by a matching pattern and yield an iterator.
  151. *
  152. * @param gr[in] Graph to look up.
  153. *
  154. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  155. * indicate unbound terms.
  156. *
  157. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  158. * freed with #LSUP_graph_iter_free after use.
  159. */
  160. LSUP_GraphIterator *
  161. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  162. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  163. *
  164. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  165. *
  166. * @param spo[out] Triple to be populated with the next result. May be NULL
  167. * (e.g. for counters).
  168. *
  169. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  170. * was reached.
  171. */
  172. LSUP_rc
  173. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  174. /** @brief Free a graph iterator.
  175. */
  176. void
  177. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  178. /** @brief Get the iterator counter.
  179. */
  180. size_t
  181. LSUP_graph_iter_cur (LSUP_GraphIterator *it);
  182. #endif