graph.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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( LSUP_STORE_MEM )/* Memory backend, hash map. */ \
  9. ENTRY( LSUP_STORE_MDB )/* LMDB back end on persistent disk. */ \
  10. ENTRY( LSUP_STORE_MDB_TMP )/* LMDB back end on RAM disk. */ \
  11. typedef enum LSUP_store_type {
  12. #define ENTRY(x) x,
  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. /** @brief Add triples and/or serialized triples to a graph.
  121. *
  122. * For API users it may be more convenient to use the more specialized
  123. * #LSUP_graph_add_trp.
  124. */
  125. LSUP_rc
  126. LSUP_graph_add(
  127. LSUP_Graph *gr,
  128. const LSUP_Triple trp[], size_t trp_ct,
  129. const LSUP_SerTriple strp[], size_t strp_ct, size_t *inserted);
  130. #define LSUP_graph_add_trp(gr, trp, ct, ins) \
  131. LSUP_graph_add (gr, trp, ct, NULL, 0, ins)
  132. /** @brief Delete triples by a matching pattern.
  133. *
  134. * @param gr[in] Graph to delete triples from.
  135. *
  136. * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
  137. *
  138. * @param ct[out] If not NULL it is populated with the number of triples
  139. * deleted.
  140. */
  141. LSUP_rc
  142. LSUP_graph_remove (LSUP_Graph *gr, const LSUP_Triple *spo, size_t *ct);
  143. /** Look up triples by a matching pattern and yield an iterator.
  144. *
  145. * @param gr[in] Graph to look up.
  146. *
  147. * @param spo[in] Triple to look for. Any and all terms can be NULL, which
  148. * indicate unbound terms.
  149. *
  150. * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
  151. * freed with #LSUP_graph_iter_free after use.
  152. */
  153. LSUP_GraphIterator *
  154. LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Triple *spo);
  155. /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  156. *
  157. * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
  158. *
  159. * @param spo[out] Triple to be populated with the next result. May be NULL
  160. * (e.g. for counters).
  161. *
  162. * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
  163. * was reached.
  164. */
  165. LSUP_rc
  166. LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
  167. /** @brief Free a graph iterator.
  168. */
  169. void
  170. LSUP_graph_iter_free (LSUP_GraphIterator *it);
  171. #endif