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
11LMDBEnvironment::LMDBEnvironment(const std::string& directory,
12 uint64_t mapSizeKB,
13 uint32_t maxNumDBs,
14 uint32_t maxNumReaders)
15 : _id(0)
16 , _directory(directory)
17 , _readGuard(maxNumReaders)
18 , _writeGuard(1) // LMDB only permits one write transaction at a time
19{
20 call_lmdb_func("mdb_env_create", mdb_env_create, &_mdbEnv);
21 uint64_t kb = 1024;
22 uint64_t totalMapSize = kb * mapSizeKB;
23 uint32_t flags = MDB_NOTLS;
24 try {
25 call_lmdb_func("mdb_env_set_mapsize", mdb_env_set_mapsize, _mdbEnv, static_cast<size_t>(totalMapSize));
26 call_lmdb_func("mdb_env_set_maxdbs", mdb_env_set_maxdbs, _mdbEnv, static_cast<MDB_dbi>(maxNumDBs));
27 call_lmdb_func("mdb_env_set_maxreaders", mdb_env_set_maxreaders, _mdbEnv, maxNumReaders);
28 call_lmdb_func("mdb_env_open",
29 mdb_env_open,
30 _mdbEnv,
31 directory.c_str(),
32 flags,
33 static_cast<mdb_mode_t>(S_IRWXU | S_IRWXG | S_IRWXO));
34 } catch (std::runtime_error& error) {
35 call_lmdb_func(mdb_env_close, _mdbEnv);
36 throw error;
37 }
38}
39
44
49
54
59
64
66{
67 return _mdbEnv;
68}
69
71{
72 MDB_envinfo info;
73 call_lmdb_func(mdb_env_info, _mdbEnv, &info);
74 return info.me_mapsize;
75}
76
78{
79 std::string dataPath = (std::filesystem::path(_directory) / "data.mdb").string();
80 if (std::filesystem::exists(dataPath)) {
81 return std::filesystem::file_size(dataPath);
82 }
83 return 0;
84}
85} // namespace bb::lmdblib
LMDBEnvironment(const std::string &directory, uint64_t mapSizeKb, uint32_t maxNumDBs, uint32_t maxNumReaders)
Opens/creates the LMDB environment.
void info(Args... args)
Definition log.hpp:70
bool call_lmdb_func(int(*f)(TArgs...), TArgs... args)