store_mdb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /** @file store_mdb.h
  2. *
  3. * @brief LMDB graph store backend.
  4. *
  5. * This module stores triples in a LMDB embedded store, optionally organized
  6. * into named graphs. The store is optimized and indexed for fast lookup of any
  7. * number of bound terms.
  8. *
  9. * The store must be first initialized once, to create the environment files
  10. * and folders as well as the internal databases, then it must be opened once
  11. * per session. Within that session multiple R/W operations can be performed
  12. * using transactions.
  13. *
  14. * Note that, even though the terms "graph", "context", etc. are used, no code
  15. * in this module checks for valid RDF data. In theory any term can be any
  16. * binary data. This allows using the store for non-RDF graph data.
  17. *
  18. * TODO more doc
  19. */
  20. #ifndef _LSUP_STORE_MDB_H
  21. #define _LSUP_STORE_MDB_H
  22. #include "lmdb.h"
  23. #include "triple.h"
  24. #include "namespace.h"
  25. typedef char DbLabel[8];
  26. typedef struct MDBStore LSUP_MDBStore;
  27. typedef struct MDBIterator LSUP_MDBIterator;
  28. typedef LSUP_rc (*store_match_fn_t)(const LSUP_TripleKey spok, void *data);
  29. // TODO Introduce compile-time LSUP_BIG_STORE option to define two store
  30. // options: false: 64-bit hashes, uint32 keys, max 4G entries; true:
  31. // 128-bit hashes, size_t keys, max MAX_SIZE entries, larger and slower.
  32. // Ideally this could be specified at runtime to handle different stores with
  33. // different sizes, but code could become more complex.
  34. /** @brief Create the MDB environment and databases on disk.
  35. *
  36. * This function takes care of creaating the environment path if not existing,
  37. * and checking that it's a writable directory. If the path is not specified
  38. * in the LSUP_STORE_PATH environment variable, a default directory is used.
  39. *
  40. * @param[in] clear Whether to remove a previous environment at this location.
  41. *
  42. * @param[in,out] path Path of the suggested directory to use. It may be NULL,
  43. * in which case it will be set either to the environment variable
  44. * LSUP_STORE_PATH, or if that is not set, a default local path.
  45. */
  46. LSUP_rc LSUP_mdbstore_setup (const char *path, bool clear);
  47. /** @brief Open an MDB store.
  48. *
  49. * The store must have been set up with #LSUP_mdbstore_setup.
  50. *
  51. * Some environment variables affect various store parameters:
  52. *
  53. * - LSUP_MDB_MAPSIZE Long int specifying the size of the memory map. Usually
  54. * it is not necessary to modify this, unless one is operating under memory
  55. * and disk constraints. The default map size is 1Tb.
  56. *
  57. * @param[in,out] store Uninitialized store struct pointer.
  58. *
  59. * @param[in] path MDB environment path. This must be the path given by
  60. * #LSUP_mdbstore_setup.
  61. *
  62. * @param[in] default_ctx Serialized URI to be used as a default context for
  63. * triples inserted without a context specified. If NULL, the store operates
  64. * in triple mode.
  65. */
  66. LSUP_MDBStore *
  67. LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx);
  68. /** @brief Close a store and free its handle.
  69. *
  70. * @param[in] store Store pointer.
  71. *
  72. */
  73. void LSUP_mdbstore_free (LSUP_MDBStore *store);
  74. /** @brief Print stats about a store and its databases.
  75. *
  76. * TODO
  77. *
  78. * @param store[in] The store to get stats for.
  79. */
  80. LSUP_rc LSUP_mdbstore_stat (LSUP_MDBStore *store, MDB_stat *stat);
  81. /** @brief Store size.
  82. *
  83. * @param store[in] The store to calculate size of.
  84. *
  85. * @return Number of stored SPO triples across all contexts.
  86. */
  87. size_t LSUP_mdbstore_size (LSUP_MDBStore *store);
  88. /** @brief Initialize bulk triple load.
  89. *
  90. * This is the first step of a bulk load. It is best used when the data at hand
  91. * need to be pre-processed, which can be done in the same loop as the next
  92. * step to keep memory usage low.
  93. *
  94. * @param store[in] The store to add to.
  95. *
  96. * @param sc[in] Context as a serialized term. If this is NULL, and the
  97. * default context is not NULL, triples will be added to the default context
  98. * for the store, If the default context for the store is NULL, regardless of
  99. * the value of sc, triples will be added with no context.
  100. *
  101. * @param it[out] Pointer to an iterator pointer to be passed to the following
  102. * load steps.
  103. */
  104. LSUP_MDBIterator *
  105. LSUP_mdbstore_add_init (LSUP_MDBStore *store, const LSUP_Buffer *sc);
  106. /** @brief Add one triple into the store.
  107. *
  108. * This must be called after #LSUP_mdbstore_add_init, using the iterator
  109. * yielded by that function. It may be called multiple times and must be
  110. * followed by #LSUP_mdbstore_add_done.
  111. *
  112. * NOTE: at the moment #LSUP_mdbstore_remove() or another
  113. * #LSUP_mdbstore_init() cannot be called between #LSUP_mdbstore_add_init and
  114. * #LSUP_mdbstore_add_abort or #LSUP_mdbstore_add_done. FIXME
  115. *
  116. * @param it[in] Iterator obtained by #LSUP_mdbstore_add_init.
  117. * The following members are of interest:
  118. * it->i stores the total number of records inserted.
  119. *
  120. * @param sspo[in] Serialized triple to be added.
  121. *
  122. * @return LSUP_OK if the triple was inserted; LSUP_NOACTION if the triple
  123. * already existed; LSUP_DB_ERR if an MDB error occurred.
  124. */
  125. LSUP_rc
  126. LSUP_mdbstore_add_iter (struct MDBIterator *it, const LSUP_SerTriple *sspo);
  127. /** @brief Finalize an add loop and free iterator.
  128. *
  129. * If a count of inserted records is needed, #LSUP_mdbiter_cur must be called
  130. * before this function.
  131. *
  132. * This must be called after #LSUP_mdbstore_add_iter.
  133. *
  134. * @param it[in] Iterator obtained by #LSUP_mdbstore_add_init.
  135. */
  136. LSUP_rc
  137. LSUP_mdbstore_add_done (LSUP_MDBIterator *it);
  138. /** @brief Abort an add loop and free iterator.
  139. *
  140. * Usually called on an irrecoverable error from LSUP_mdb_add_iter. None of the
  141. * successful inserts in the same loop is retained.
  142. *
  143. * @param it[in] Iterator obtained by #LSUP_mdbstore_add_init.
  144. */
  145. void
  146. LSUP_mdbstore_add_abort (LSUP_MDBIterator *it);
  147. /** @brief Add a batch of triples with optional context to the store.
  148. *
  149. * This is a shortcut for calling #LSUP_mdbstore_add_init,
  150. * #LSUP_mdbstore_add_iter and #LSUP_mdbstore_add_done in a sequence
  151. * when an array of pre-serialized triples is available.
  152. *
  153. * @param store[in] The store to add to.
  154. *
  155. * @param sc[in] Context as a serialized term. If this is NULL, and the
  156. * default context is not NULL, triples will be added to the default context
  157. * for the store. If the default context for the store is NULL, regardless of
  158. * the value of sc, triples will be added with no context.
  159. * @param data[in] Triples to be inserted as a 2D array of triples in the shape
  160. * of data[n][3], where n is the value of data_size.
  161. *
  162. * @param inserted[out] If not NULL, it will be filled with the count of
  163. * effectively inserted triples.
  164. *
  165. * @param data_size[in] Number of triples to be inserted.
  166. */
  167. LSUP_rc LSUP_mdbstore_add(
  168. struct MDBStore *store, const LSUP_Buffer *sc,
  169. const LSUP_SerTriple strp[], const size_t ct, size_t *inserted);
  170. /** @brief Delete triples by pattern matching.
  171. *
  172. * The ss, sp, so, sc terms act as a matching pattern as documented in
  173. * #LSUP_mdbstore_lookup. if not NULL, ct yields the number of triples actually
  174. * deleted.
  175. */
  176. LSUP_rc
  177. LSUP_mdbstore_remove(
  178. LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  179. const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct);
  180. /** @brief Look up matching triples and optional context.
  181. *
  182. * This function may return a count of matches and/or an iterator of results as
  183. * serialized triples.
  184. *
  185. * Any and all of the terms may be NULL, which indicates an unbound query
  186. * term. Stores with context not set will always ignore the fourth term.
  187. *
  188. * @param store[in] The store to be queried.
  189. *
  190. * @param ss Buffer representing the serialized s term.
  191. *
  192. * @param sp Buffer representing the serialized p term.
  193. *
  194. * @param so Buffer representing the serialized o term.
  195. *
  196. * @param sc Serialized context to limit search to. It may be NULL, in which
  197. * case search is done in all contexts. Note that triples inserted without
  198. * context are assigned the *default* context, indicated by the "default_ctx"
  199. * member of the store struct.
  200. *
  201. * @param it[out] Pointer to a pointer to an #LSUP_MDBIterator that will be
  202. * populated with a result iterator. This is always created even if no matches
  203. * are found and must be freed with #LSUP_mdbiter_free after use. If matches
  204. * are found, the iterator points to the first result which can be retrieved
  205. * with #LSUP_mdbiter_next.
  206. *
  207. * @param ct[out] If not NULL, this will be populated with the number of
  208. * entries found. It is very inexpensive to set for lookups without context,
  209. * much less so for 1-bound and 2-bound context lookups, in which cases it
  210. * should be set only if needed.
  211. *
  212. * @return LSUP_OK if entries were found, LSUP_NORESULT if none were found.
  213. */
  214. LSUP_MDBIterator *
  215. LSUP_mdbstore_lookup(
  216. LSUP_MDBStore *store, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  217. const LSUP_Buffer *so, const LSUP_Buffer *sc, size_t *ct);
  218. /** @brief Yield the matching triples and advance the iterator.
  219. *
  220. * This function also checks if the matching triple is associated with a
  221. * context, if one was specified. If no associated contexts are found, the next
  222. * triple is searched, until the end of the results.
  223. *
  224. * NOTE: Iterators keep LMDB cursors and (read only) transactions open. Don't
  225. * hold on to them longer than necessary.
  226. *
  227. * NOTE: The memory pointed to by the individual LSUP_Buffer pointers is
  228. * owned by the database. It must not be written to or freed. To modify
  229. * the data or use them beyond the caller's scope, this memory must be copied.
  230. *
  231. * @param it[in] Opaque iterator handle obtained with #LSUP_mdbstore_lookup.
  232. *
  233. * @param sspo[out] #LSUP_SerTriple to be populated with three serialized terms
  234. * if found, NULL if not found. Internal callers (e.g. counters) may pass NULL
  235. * if they don't need the serialized triples.
  236. *
  237. * @return LSUP_OK if results were found; LSUP_END if no (more) results were
  238. * found; LSUP_DB_ERR if a MDB_* error occurred.
  239. */
  240. LSUP_rc LSUP_mdbiter_next (LSUP_MDBIterator *it, LSUP_SerTriple *sspo);
  241. /** @brief Iterator's internal counter.
  242. *
  243. * This is only useful with #LSUP_mdbstore_add_iter to count inserted records.
  244. *
  245. * @param it[in] An iterator primed with LSUP_mdbstore_add_init.
  246. *
  247. * @return The value of the #i member. For an add iterator, this is the number
  248. * of succcessfully inserted records.
  249. */
  250. size_t
  251. LSUP_mdbiter_cur (LSUP_MDBIterator *it);
  252. /** @brief Free an iterator allocated by a lookup.
  253. *
  254. * @param it[in] Iterator pointer. It will be set to NULL after freeing.
  255. */
  256. void LSUP_mdbiter_free (struct MDBIterator *it);
  257. /** @brief Contexts that a triple key appears in.
  258. *
  259. * This function is most conveniently used by a callback to
  260. * #LSUP_mdbstore_lookup because it handles triple keys.
  261. *
  262. * @param store[in] The store to be queried.
  263. *
  264. * @param spok[in] Triple key to look up.
  265. *
  266. * @param ck[out] Pointer to an array of contexts. Memory is allocated by this
  267. * function and must be freed by the caller.
  268. *
  269. * @param ct[out] Number of contexts found.
  270. */
  271. LSUP_rc LSUP_mdbstore_triple_contexts(
  272. LSUP_MDBStore *store, LSUP_Key spok[], LSUP_Key **ck, size_t *ct);
  273. /** @brief Get all namespace prefixes in the store.
  274. *
  275. * @param[in] store MDB store to query.
  276. *
  277. * @param[out] nsm Pointer to namespace map to generate.
  278. *
  279. * @return LSUP_OK on success; LSUP_DB_ERR on MDB error.
  280. */
  281. LSUP_rc
  282. LSUP_mdbstore_nsm_get (LSUP_MDBStore *store, LSUP_NSMap **nsm);
  283. /** @brief Store an in-memory namespace map into the permanent back end.
  284. *
  285. * Existing prefixes and namespaces are not updated. Thus, if the following are
  286. * already stored:
  287. *
  288. * ns1: <urn:ns:a#>
  289. * ns2: <urn:ns:b#>
  290. *
  291. * Neither of the following will be inserted:
  292. *
  293. * ns3: <urn:ns:a#>
  294. * ns2: <urn:ns:c#>
  295. *
  296. * @param[in] store MDB store to update.
  297. *
  298. * @param[out] nsm Namespace map handle to store.
  299. *
  300. * @return LSUP_OK if all terms were updated; LSUP_CONFLICT if one or more
  301. * namespaces or terms were not updated because they already existed.
  302. */
  303. LSUP_rc
  304. LSUP_mdbstore_nsm_store (LSUP_MDBStore *store, const LSUP_NSMap *nsm);
  305. #endif