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>;
29 LMDBEnvironment(const std::string& directory, uint64_t mapSizeKb, uint32_t maxNumDBs, uint32_t maxNumReaders);
30 LMDBEnvironment(const LMDBEnvironment& other) = delete;
32 LMDBEnvironment& operator=(const LMDBEnvironment& other) = delete;
34
36
37 MDB_env* underlying() const;
38
39 void wait_for_reader();
40
41 void release_reader();
42
43 void wait_for_writer();
44
45 void release_writer();
46
47 uint64_t getNextId() { return _id++; }
48
49 uint64_t get_map_size() const;
50
51 uint64_t get_data_file_size() const;
52
53 private:
55 std::string _directory;
56 MDB_env* _mdbEnv;
57
59 uint32_t _maxAllowed;
60 uint32_t _current;
61 std::mutex _lock;
63
64 ResourceGuard(uint32_t maxAllowed)
65 : _maxAllowed(maxAllowed)
66 , _current(0)
67 {}
68
69 void wait()
70 {
71 std::unique_lock lock(_lock);
72 if (_current >= _maxAllowed) {
73 _condition.wait(lock, [&] { return _current < _maxAllowed; });
74 }
75 ++_current;
76 }
77
78 void release()
79 {
80 std::unique_lock lock(_lock);
81 --_current;
82 _condition.notify_one();
83 }
84 };
87};
88} // 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