Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
lmdb_environment.cpp
Go to the documentation of this file.
3#include "lmdb.h"
4#include <cstdint>
5#include <filesystem>
6#include <stdexcept>
7#include <sys/stat.h>
8
9namespace bb::lmdblib {
10
12 const std::string& directory, uint64_t mapSizeKB, uint32_t maxNumDBs, uint32_t maxNumReaders, bool ephemeral)
13 : _id(0)
14 , _directory(directory)
15 , _readGuard(maxNumReaders)
16 , _writeGuard(1) // LMDB only permits one write transaction at a time
17{
18 call_lmdb_func("mdb_env_create", mdb_env_create, &_mdbEnv);
19 uint64_t kb = 1024;
20 uint64_t totalMapSize = kb * mapSizeKB;
21 uint32_t flags = MDB_NOTLS;
22 if (ephemeral) {
23 flags |= MDB_NOSYNC | MDB_NOMETASYNC;
24 }
25 try {
26 call_lmdb_func("mdb_env_set_mapsize", mdb_env_set_mapsize, _mdbEnv, static_cast<size_t>(totalMapSize));
27 call_lmdb_func("mdb_env_set_maxdbs", mdb_env_set_maxdbs, _mdbEnv, static_cast<MDB_dbi>(maxNumDBs));
28 call_lmdb_func("mdb_env_set_maxreaders", mdb_env_set_maxreaders, _mdbEnv, maxNumReaders);
29 call_lmdb_func("mdb_env_open",
30 mdb_env_open,
31 _mdbEnv,
32 directory.c_str(),
33 flags,
34 static_cast<mdb_mode_t>(S_IRWXU | S_IRWXG | S_IRWXO));
35 } catch (std::runtime_error& error) {
36 call_lmdb_func(mdb_env_close, _mdbEnv);
37 throw error;
38 }
39}
40
45
50
55
60
65
67{
68 return _mdbEnv;
69}
70
72{
73 MDB_envinfo info;
74 call_lmdb_func(mdb_env_info, _mdbEnv, &info);
75 return info.me_mapsize;
76}
77
79{
80 std::string dataPath = (std::filesystem::path(_directory) / "data.mdb").string();
81 if (std::filesystem::exists(dataPath)) {
82 return std::filesystem::file_size(dataPath);
83 }
84 return 0;
85}
86} // namespace bb::lmdblib
LMDBEnvironment(const std::string &directory, uint64_t mapSizeKb, uint32_t maxNumDBs, uint32_t maxNumReaders, bool ephemeral=false)
Opens/creates the LMDB environment.
#define info(...)
Definition log.hpp:93
bool call_lmdb_func(int(*f)(TArgs...), TArgs... args)