store_mdb.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. typedef char DbLabel[8];
  25. typedef struct MDBStore LSUP_MDBStore;
  26. typedef LSUP_rc (*mdb_store_match_fn_t)(const LSUP_TripleKey spok, void *data);
  27. // TODO Introduce compile-time LSUP_BIG_STORE option to define two store
  28. // options: false: 64-bit hashes, uint32 keys, max 4G entries; true:
  29. // 128-bit hashes, size_t keys, max MAX_SIZE entries, larger and slower.
  30. // Ideally this could be specified at runtime to handle different stores with
  31. // different sizes, but code could become more complex.
  32. /** @brief Create the MDB environment and databases on disk.
  33. *
  34. * This function takes care of creaating the environment path if not existing,
  35. * and checking that it's a writable directory. If the path is not specified
  36. * in the LSUP_STORE_PATH environment variable, a default directory is used.
  37. *
  38. * @param[in,out] path Path of the suggested directory to use. It may be NULL,
  39. * in which case it will be set either to the environment variable
  40. * LSUP_STORE_PATH, or if that is not set, a default local path.
  41. */
  42. LSUP_rc LSUP_store_setup(char **path/*, bool clear*/);
  43. /** @brief Open an MDB store.
  44. *
  45. * The store must have been set up with #LSUP_store_setup.
  46. *
  47. * Some environment variables affect various store parameters:
  48. *
  49. * - LSUP_MDB_MAPSIZE Long int specifying the size of the memory map. Usually
  50. * it is not necessary to modify this, unless one is operating under memory
  51. * and disk constraints. The default map size is 1Tb.
  52. *
  53. * @param[in,out] store Uninitialized store struct pointer.
  54. *
  55. * @param[in] path MDB environment path. This must be the path given by
  56. * #LSUP_store_setup.
  57. *
  58. * @param[in] default_ctx Serialized URI to be used as a default context for
  59. * triples inserted without a context specified. If NULL, the store operates
  60. * in triple mode.
  61. */
  62. LSUP_MDBStore *
  63. LSUP_store_new(const char *path, const LSUP_Buffer *default_ctx);
  64. /** @brief Close a store and free its handle.
  65. *
  66. * @param[in] store Store pointer.
  67. *
  68. */
  69. void LSUP_store_free(LSUP_MDBStore *store);
  70. /** @brief Print stats about a store and its databases.
  71. *
  72. * TODO
  73. *
  74. * @param store[in] The store to get stats for.
  75. */
  76. LSUP_rc LSUP_store_stats(LSUP_MDBStore *store);
  77. /** @brief Store size.
  78. *
  79. * @param store[in] The store to calculate size of.
  80. *
  81. * @return Number of stored SPO triples across all contexts.
  82. */
  83. size_t LSUP_store_size(LSUP_MDBStore *store);
  84. /** @brief Add a batch of triples with optional context to the store.
  85. *
  86. * @param store[in] The store to add to.
  87. *
  88. * @param sc[in] Context as a serialized term. If this is NULL, and the
  89. * default context is not NULL, triples will be added to the default context
  90. * for the store, If the default context for the store is NULL, regardless of
  91. * the value of sc, triples will be added with no context.
  92. * @param data[in] Triples to be inserted as a 2D array of triples in the shape
  93. * of data[n][3], where n is the value of data_size.
  94. *
  95. * @param data_size[in] Number of triples to be inserted.
  96. */
  97. LSUP_rc LSUP_store_add(
  98. struct MDBStore *store, const LSUP_Buffer *sc,
  99. const LSUP_SerTriple *data, const size_t data_size);
  100. /** @brief Look up matching triples and optional context.
  101. *
  102. * This function may return a count of matches and/or run a callback function
  103. * on each of the matching triples.
  104. *
  105. * @param store[in] The store to be queried.
  106. *
  107. * @param sspoc Array of 4 serialized term pointers representing the s, p, o, c
  108. * terms. Any and all of these may be NULL, which indicates an unbound query
  109. * term. Stores with context not set will always ignore the fourth term.
  110. *
  111. * @param ct[out] If not NULL, this will be populated with the number of
  112. * entries found. This can be used when calling this function once to
  113. * pre-allocate some memory based on the results obtained, then again to
  114. * perform an operation on the allocated memory.
  115. *
  116. * @param callback[in] This function is applied to the matching
  117. * triple keys. It may be NULL if the function is called only to count the
  118. * matches; which in most cases will be much faster.
  119. *
  120. * @param data[in,out] Arbitrary data to be used in the callback function.
  121. *
  122. * @return LSUP_OK if entries were found, LSUP_NORESULT if none were found; if
  123. * a callback was applied, and an error (<0) code was returned in any of the
  124. * interactions, that code is returned and the callback execution is stopped.
  125. */
  126. LSUP_rc LSUP_store_lookup(
  127. LSUP_MDBStore *store, LSUP_SerTerm *sspoc[], size_t *ct,
  128. mdb_store_match_fn_t callback_fn, void *data);
  129. /** @brief Contexts that a triple key appears in.
  130. *
  131. * This function is most conveniently used by a callback to #LSUP_store_lookup
  132. * because it handles triple keys.
  133. *
  134. * @param store[in] The store to be queried.
  135. *
  136. * @param spok[in] Triple key to look up.
  137. *
  138. * @param ck[out] Pointer to an array of contexts. Memory is allocated by this
  139. * function and must be freed by the caller.
  140. *
  141. * @param ct[out] Number of contexts found.
  142. */
  143. LSUP_rc LSUP_store_triple_contexts(
  144. LSUP_MDBStore *store, LSUP_Key spok[], LSUP_Key **ck, size_t *ct);
  145. #endif