Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
lmdb_environment.test.cpp
Go to the documentation of this file.
1#include <cstddef>
2#include <cstdint>
3#include <gtest/gtest.h>
4
5#include <chrono>
6#include <cstdlib>
7#include <filesystem>
8#include <memory>
9#include <sstream>
10#include <stdexcept>
11#include <thread>
12#include <vector>
13
24
25using namespace bb::lmdblib;
26
27class LMDBEnvironmentTest : public testing::Test {
28 protected:
29 void SetUp() override
30 {
32 _mapSize = 1024 * 1024;
33 _maxReaders = 16;
34 std::filesystem::create_directories(_directory);
35 }
36
37 void TearDown() override { std::filesystem::remove_all(_directory); }
38
39 static std::string _directory;
40 static uint32_t _maxReaders;
41 static uint64_t _mapSize;
42};
43
47
53
54TEST_F(LMDBEnvironmentTest, can_create_database)
55{
58
59 {
60 environment->wait_for_writer();
61 LMDBDatabaseCreationTransaction tx(environment);
62 LMDBDatabase::SharedPtr db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
63 EXPECT_NO_THROW(tx.commit());
64 }
65}
66
67TEST_F(LMDBEnvironmentTest, can_write_to_database)
68{
71
73 {
74 environment->wait_for_writer();
75 LMDBDatabaseCreationTransaction tx(environment);
76 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
77 EXPECT_NO_THROW(tx.commit());
78 }
79
80 {
81 environment->wait_for_writer();
83 auto key = get_key(0);
84 auto data = get_value(0, 0);
85 EXPECT_NO_THROW(tx->put_value(key, data, *db));
86 EXPECT_NO_THROW(tx->commit());
87 }
88}
89
90TEST_F(LMDBEnvironmentTest, can_read_from_database)
91{
95
96 {
97 environment->wait_for_writer();
98 LMDBDatabaseCreationTransaction tx(environment);
99 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
100 EXPECT_NO_THROW(tx.commit());
101 }
102
103 {
104 environment->wait_for_writer();
106 auto key = get_key(0);
107 auto data = get_value(0, 0);
108 EXPECT_NO_THROW(tx->put_value(key, data, *db));
109 EXPECT_NO_THROW(tx->commit());
110 }
111
112 {
113 environment->wait_for_reader();
115 auto key = get_key(0);
116 auto expected = get_value(0, 0);
117 std::vector<uint8_t> data;
118 tx->get_value(key, data, *db);
119 EXPECT_EQ(data, expected);
120 }
121}
122
123TEST_F(LMDBEnvironmentTest, can_write_and_read_multiple)
124{
127
129
130 {
131 environment->wait_for_writer();
132 LMDBDatabaseCreationTransaction tx(environment);
133 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
134 EXPECT_NO_THROW(tx.commit());
135 }
136
137 int64_t numValues = 10;
138
139 {
140 for (int64_t count = 0; count < numValues; count++) {
141 environment->wait_for_writer();
143 auto key = get_key(count);
144 auto data = get_value(count, 0);
145 EXPECT_NO_THROW(tx->put_value(key, data, *db));
146 EXPECT_NO_THROW(tx->commit());
147 }
148 }
149
150 {
151 for (int64_t count = 0; count < numValues; count++) {
152 environment->wait_for_reader();
154 auto key = get_key(count);
155 auto expected = get_value(count, 0);
156 std::vector<uint8_t> data;
157 tx->get_value(key, data, *db);
158 EXPECT_EQ(data, expected);
159 }
160 }
161}
162
163TEST_F(LMDBEnvironmentTest, can_read_multiple_threads)
164{
165 LMDBEnvironment::SharedPtr environment =
167
169 {
170 environment->wait_for_writer();
171 LMDBDatabaseCreationTransaction tx(environment);
172 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
173 EXPECT_NO_THROW(tx.commit());
174 }
175
176 int64_t numValues = 10;
177 int64_t numIterationsPerThread = 1000;
178 uint32_t numThreads = 16;
179
180 {
181 for (int64_t count = 0; count < numValues; count++) {
182 environment->wait_for_writer();
184 auto key = get_key(count);
185 auto expected = get_value(count, 0);
186 EXPECT_NO_THROW(tx->put_value(key, expected, *db));
187 EXPECT_NO_THROW(tx->commit());
188 }
189 }
190
191 {
192 auto func = [&]() -> void {
193 for (int64_t iteration = 0; iteration < numIterationsPerThread; iteration++) {
194 for (int64_t count = 0; count < numValues; count++) {
195 environment->wait_for_reader();
197 auto key = get_key(count);
198 auto expected = get_value(count, 0);
199 std::vector<uint8_t> data;
200 tx->get_value(key, data, *db);
201 EXPECT_EQ(data, expected);
202 }
203 }
204 };
206 for (uint64_t count = 0; count < numThreads; count++) {
207 threads.emplace_back(std::make_unique<std::thread>(func));
208 }
209 for (uint64_t count = 0; count < numThreads; count++) {
210 threads[count]->join();
211 }
212 }
213}
static std::string _directory
std::shared_ptr< LMDBDatabase > SharedPtr
std::shared_ptr< LMDBEnvironment > SharedPtr
std::unique_ptr< LMDBReadTransaction > Ptr
std::unique_ptr< LMDBWriteTransaction > Ptr
const std::vector< FF > data
TEST_F(LMDBEnvironmentTest, can_create_environment)
Key get_key(int64_t keyCount)
Definition fixtures.hpp:30
Value get_value(int64_t keyCount, int64_t valueCount)
Definition fixtures.hpp:35
std::string random_temp_directory()
Definition fixtures.hpp:17
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13