store_mdb.h 4.8 KB

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