Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
lmdb_environment.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <condition_variable>
5#include <cstdint>
6#include <lmdb.h>
7#include <memory>
8#include <mutex>
9#include <string>
10namespace bb::lmdblib {
11
12/*
13 * RAII wrapper around an LMDB environment.
14 * Opens/creates the environemnt and manages read access to the enviroment.
15 * The environment has an upper limit on the number of concurrent read transactions
16 * and this is managed through the use of mutex/condition variables
17 */
19 public:
21 using SharedPtr = std::shared_ptr<LMDBEnvironment>;
36 LMDBEnvironment(const std::string& directory,
37 uint64_t mapSizeKb,
38 uint32_t maxNumDBs,
39 uint32_t maxNumReaders,
40 bool ephemeral = false);
41 LMDBEnvironment(const LMDBEnvironment& other) = delete;
43 LMDBEnvironment& operator=(const LMDBEnvironment& other) = delete;
45
47
48 MDB_env* underlying() const;
49
50 void wait_for_reader();
51
52 void release_reader();
53
54 void wait_for_writer();
55
56 void release_writer();
57
58 uint64_t getNextId() { return _id++; }
59
60 uint64_t get_map_size() const;
61
62 uint64_t get_data_file_size() const;
63
64 private:
66 std::string _directory;
67 MDB_env* _mdbEnv;
68
70 uint32_t _maxAllowed;
71 uint32_t _current;
72 std::mutex _lock;
74
75 ResourceGuard(uint32_t maxAllowed)
76 : _maxAllowed(maxAllowed)
77 , _current(0)
78 {}
79
80 void wait()
81 {
82 std::unique_lock lock(_lock);
83 if (_current >= _maxAllowed) {
84 _condition.wait(lock, [&] { return _current < _maxAllowed; });
85 }
86 ++_current;
87 }
88
89 void release()
90 {
91 std::unique_lock lock(_lock);
92 --_current;
93 _condition.notify_one();
94 }
95 };
98};
99} // namespace bb::lmdblib
LMDBEnvironment(const LMDBEnvironment &other)=delete
LMDBEnvironment & operator=(const LMDBEnvironment &other)=delete
std::unique_ptr< LMDBEnvironment > Ptr
LMDBEnvironment(LMDBEnvironment &&other)=delete
std::shared_ptr< LMDBEnvironment > SharedPtr
LMDBEnvironment & operator=(LMDBEnvironment &&other)=delete
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13