graph.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "store_htable.h"
  4. #include "store_mdb.h"
  5. typedef enum LSUP_store_type {
  6. LSUP_STORE_MEM,
  7. LSUP_STORE_MDB
  8. } LSUP_store_type;
  9. /** @brief Graph object.
  10. */
  11. typedef struct Graph LSUP_Graph;
  12. /** @brief Graph iterator.
  13. *
  14. * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
  15. * over lookup results with #LSUP_graph_iter_next. It must be freed with
  16. * #LSUP_graph_iter_free when done.
  17. */
  18. typedef struct GraphIterator LSUP_GraphIterator;
  19. /** @brief Create an empty graph.
  20. *
  21. * The new graph has zero capacity and a random URN. To change either one, use
  22. * #LSUP_graph_resize and #LSUP_graph_set_uri, respectively.
  23. *
  24. * @param store_type[in] TYpe of store for the graph. One of the values of
  25. * #LSUP_store_type.
  26. *
  27. * @param gr[out] Pointer to a pointer to the new graph. It must be freed with
  28. * #LSUP_graph_free when done.
  29. *
  30. * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
  31. */
  32. LSUP_Graph *
  33. LSUP_graph_new(const LSUP_store_type store_type);
  34. /** @brief copy a graph into a new one.
  35. *
  36. * The new graph is compacted to the minimum required size.
  37. *
  38. * src[in] Graph to be copied.
  39. *
  40. * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
  41. *
  42. * @param gr[out] Pointer to a pointer to the destination graph. It must be
  43. * freed with #LSUP_graph_free when done.
  44. *
  45. * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
  46. */
  47. LSUP_Graph *
  48. LSUP_graph_copy(const LSUP_Graph *src);
  49. /** Perform a boolean operation between two graphs.
  50. *
  51. * This method yields a new graph as the result of the operation.
  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. It must be freed with #LSUP_graph_free when
  60. * done.
  61. */
  62. LSUP_Graph *
  63. LSUP_graph_bool_op(
  64. const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
  65. /** @brief Free a graph.
  66. */
  67. void
  68. LSUP_graph_free(LSUP_Graph *gr);
  69. /** @brief Number of triples that can be stored without resizing the graph.
  70. *
  71. * @return Dynamic capacity of an in-memory graph or maximum allowed memory for
  72. * an MDB graph.
  73. */
  74. size_t
  75. LSUP_graph_capacity(const LSUP_Graph *gr);
  76. /** @brief Number of triples in a graph.
  77. */
  78. size_t
  79. LSUP_graph_size(const LSUP_Graph *gr);
  80. /** @brief Change the capacity of an in-memory graph.
  81. *
  82. * This is useful ahead of a bulk load to save multiple reallocs. Otherwise,
  83. * the graph expands automatically on new inserts when capacity is reached.
  84. *
  85. * @param gr[in] Graph to be resized.
  86. *
  87. * @param size[in] New size. This will never be smaller than the current
  88. * occupied space. Therefore setting this value to 0 effectively compacts the
  89. * graph storage.
  90. *
  91. * @return LSUP_OK if the operation was successful; LSUP_VALUE_ERR if the store
  92. * type of the graph is not LSUP_STORE_MEM; <0 if an error occurs while
  93. * resizing.
  94. */
  95. LSUP_rc
  96. LSUP_graph_resize(LSUP_Graph *gr, size_t size);
  97. /** @brief Read-only graph URI.
  98. *
  99. * To change the graph URI, use #LSUP_graph_set_uri.
  100. */
  101. LSUP_Term *
  102. LSUP_graph_uri(const LSUP_Graph *gr);
  103. /** Set the URI of a graph.
  104. *
  105. * @param gr[in] Graph whose URI is to be changed.
  106. *
  107. * @param uri[in] New URI as a string. If NULL, a UUID4 URN is generated.
  108. */
  109. LSUP_rc
  110. LSUP_graph_set_uri(LSUP_Graph *gr, const char *uri);
  111. bool
  112. LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *spo);
  113. /** @brief Add triples and/or serialized triples to a graph.
  114. *
  115. * For API users it may be more convenient to use the more specialized
  116. * #LSUP_graph_add_trp.
  117. */
  118. LSUP_rc
  119. LSUP_graph_add(
  120. LSUP_Graph *gr,
  121. const LSUP_Triple trp[], size_t trp_ct,
  122. const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
  123. #define LSUP_graph_add_trp(gr, trp, ct, ins) \
  124. LSUP_graph_add(gr, trp, ct, NULL, 0, ins)
  125. /** @brief Delete triples by a matching pattern.
  126. *
  127. * @param gr[in] Graph to delete triples from.
  128. *
  129. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  130. *
  131. * @param ct[out] If not NULL it is populated with the number of triples
  132. * deleted.
  133. */
  134. LSUP_rc
  135. LSUP_graph_remove(LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  136. /** Look up triples by a matching pattern and yield an iterator.
  137. *
  138. * @param gr[in] Graph to look up.
  139. *
  140. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  141. * indicate unbound terms.
  142. *
  143. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  144. * freed with #LSUP_graph_iter_free after use.
  145. */
  146. LSUP_GraphIterator *
  147. LSUP_graph_lookup(const LSUP_Graph *gr, const LSUP_Triple *spo);
  148. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  149. *
  150. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  151. *
  152. * @param spo[out] Triple to be populated with the next result.
  153. *
  154. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  155. * was reached.
  156. */
  157. LSUP_rc
  158. LSUP_graph_iter_next(LSUP_GraphIterator *it, LSUP_Triple *spo);
  159. /** @brief Free a graph iterator.
  160. */
  161. void
  162. LSUP_graph_iter_free(LSUP_GraphIterator *it);
  163. #endif