store_htable.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /** @file store.htable.h
  2. *
  3. * @brief Simple in-memory triple store back end based on hash tables.
  4. *
  5. * This is the simplest choice to do in-memory manipulation of RDF graphs and
  6. * it has some limitations: most notably, it only supports triples without
  7. * context (one graph per store) and it is not indexed. This means that it is
  8. * optimized for fast writes and sequential lookups (iteration). Lookups on
  9. * arbitrary terms are supported but require iterating over all the triples.
  10. * This implementation is most convenient for graphs where retrieval is done
  11. * via iteration.
  12. *
  13. * Also, as it may be obvious, this store is not persistent.
  14. *
  15. * For faster random lookups and persistence, the MDB backend is preferred. If
  16. * persistence is not required (e.g. ingesting and manipulating a very large
  17. * graph and outputting some results on a file) an ad-hoc MDB store located in
  18. * RAM disk can be used, which is much faster.
  19. */
  20. #ifndef _LSUP_STORE_HTABLE_H
  21. #define _LSUP_STORE_HTABLE_H
  22. #include "buffer.h"
  23. #include "store.h"
  24. typedef struct ht_store_t LSUP_HTStore;
  25. typedef struct ht_iterator_t LSUP_HTIterator;
  26. LSUP_HTStore *
  27. LSUP_htstore_new (const size_t size);
  28. /** @brief Boolean operation on hash table triples.
  29. *
  30. * The resulting store is compacted to the minimum size necessary to hold all
  31. * results.
  32. *
  33. * @param[in] op Operation type. See #LSUP_bool_op
  34. *
  35. * @param[in] s1 First store.
  36. *
  37. * @param[in] s2 Second store.
  38. *
  39. * @return New store resulting from the operation. It must be freed with
  40. * #LSUP_htstore_free after use.
  41. */
  42. LSUP_HTStore *
  43. LSUP_htstore_bool_op (
  44. const LSUP_bool_op op, const LSUP_HTStore *s1, const LSUP_HTStore *s2);
  45. /** @brief Free a hash table store.
  46. */
  47. void
  48. LSUP_htstore_free (LSUP_HTStore *ht);
  49. /** @brief Duplicate a store.
  50. *
  51. * @param[in] src Store to duplicate.
  52. *
  53. * @return New store. It must be freed with #LSUP_htstore_free() after use.
  54. */
  55. LSUP_HTStore *
  56. LSUP_htstore_copy (const LSUP_HTStore *src);
  57. /** @brief Copy contents of a store to another store.
  58. *
  59. * The destination is not initialized, so copy is cumulative with the existing
  60. * content.
  61. *
  62. * @param[in] Store to copy to. It must be already initialized via
  63. * #LSUP_htstore_new(), #LSUP_htstore_copy(), etc.
  64. *
  65. * @param[in] src Store to copy from.
  66. */
  67. LSUP_rc
  68. LSUP_htstore_copy_contents (LSUP_HTStore *dest, const LSUP_HTStore *src);
  69. /** @brief Count triples in a store.
  70. *
  71. * @parm[in] store HTStore handle.
  72. *
  73. * @return Number of triples in the store.
  74. */
  75. size_t
  76. LSUP_htstore_size (const LSUP_HTStore *ht);
  77. /** @brief Add a term to the store.
  78. *
  79. * @parm[in] store HTStore handle.
  80. *
  81. * @param[in] sterm Serialized term to insert. The term is copied and may be
  82. * safely freed after the operation.
  83. *
  84. * @return LSUP_OK on success; LSUP_NOACTION if the term exists already; <0
  85. * on error.
  86. */
  87. LSUP_rc
  88. LSUP_htstore_add_term (LSUP_HTStore *store, const LSUP_Buffer *sterm);
  89. /** @brief Initialize a loop to add triples to a store.
  90. *
  91. * @param[in] store Store handler.
  92. *
  93. * @return Iterator to be used with #LSUP_htstore_add_iter(). It must be freed
  94. * with #LSUP_htstore_add_done().
  95. */
  96. LSUP_HTIterator *
  97. LSUP_htstore_add_init (LSUP_HTStore *store);
  98. /** @brief Add triples to the store.
  99. *
  100. * @param[in] it Iterator handle created with #LSUP_htstore_add_init().
  101. *
  102. * @param[in] sspo Serialized buffer triple to add.
  103. */
  104. LSUP_rc
  105. LSUP_htstore_add_iter (LSUP_HTIterator *it, const LSUP_BufferTriple *sspo);
  106. /** @brief Free resources related to an add loop.
  107. *
  108. * @param[in] it Iterator to free.
  109. */
  110. void
  111. LSUP_htstore_add_done (LSUP_HTIterator *it);
  112. /** @brief Find triples by pattern matching and return an iterator.
  113. *
  114. * The iterator may yield results by using #LSUP_htiter_next() and must be
  115. * freed with #LSUP_htiter_free().
  116. *
  117. * @param[in] store Store to search in.
  118. *
  119. * @param[in] ss Serialized subject term. If NULL, the term is unbound.
  120. *
  121. * @param[in] sp Serialized predicate term. If NULL, the term is unbound.
  122. *
  123. * @param[in] so Serialized object term. If NULL, the term is unbound.
  124. *
  125. * @param[out] ct If not NULL, it is populated with the number of results. Use
  126. * only if necessary: since the hash table is not indexed, retrieving the
  127. * count requires looping over the results, thus slowing down the operation.
  128. *
  129. * @return Iterator for lookup results.
  130. */
  131. LSUP_HTIterator *
  132. LSUP_htstore_lookup (
  133. LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  134. const LSUP_Buffer *so, size_t *ct);
  135. /** @brief Find the next triple in a lookup and return the result.
  136. *
  137. * @param[in] it Iterator obtained from #LSUP_htstore_lookup().
  138. *
  139. * @param[out] spo Serialized triple pointer to be populated with the result,
  140. * if found.
  141. *
  142. * @return LSUP_OK if a result was found; LSUP_END if the end of the iterator
  143. * is reached.
  144. */
  145. LSUP_rc
  146. LSUP_htiter_next (LSUP_HTIterator *it, LSUP_BufferTriple *sspo);
  147. /** @brief Count of lookup results or triples added in an iteration.
  148. *
  149. * @param[in] it Iterator handle.
  150. *
  151. * @return Number of results yielded, or triples added, at a certain point of
  152. * an iterator.
  153. */
  154. /*
  155. size_t
  156. LSUP_htiter_count (const LSUP_HTIterator *it);
  157. */
  158. /** @brief Free an iterator.
  159. *
  160. * @param[in] it Iterator handle obtained from #LSUP_htstore_lookup().
  161. */
  162. void
  163. LSUP_htiter_free (LSUP_HTIterator *it);
  164. /** @brief Remove triples by pattern matching.
  165. *
  166. * The search criteria are the same used for #LSUP_htstore_lookup().
  167. *
  168. * @param[in] store Store to remove triples from.
  169. *
  170. * @param[in] ss Serialized subject term. If NULL, the term is unbound.
  171. *
  172. * @param[in] sp Serialized predicate term. If NULL, the term is unbound.
  173. *
  174. * @param[in] so Serialized object term. If NULL, the term is unbound.
  175. *
  176. * @param[out] Optional pointer to a counter. If not NULL, it is populated with
  177. * the number of triples removed. It is undefined if LSUP_DB_ERR is
  178. * returned.
  179. *
  180. * @return LSUP_OK if any triples were deleted; LSUP_NOACTION if no triples
  181. * were found for deletion; <0 on error.
  182. */
  183. LSUP_rc
  184. LSUP_htstore_remove (
  185. LSUP_HTStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  186. const LSUP_Buffer *so, size_t *ct);
  187. #endif // _LSUP_STORE_HTABLE_H