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 <vector>
12
23
24using namespace bb::lmdblib;
25
26class LMDBEnvironmentTest : public testing::Test {
27 protected:
28 void SetUp() override
29 {
31 _mapSize = 1024 * 1024;
32 _maxReaders = 16;
33 std::filesystem::create_directories(_directory);
34 }
35
36 void TearDown() override { std::filesystem::remove_all(_directory); }
37
38 static std::string _directory;
39 static uint32_t _maxReaders;
40 static uint64_t _mapSize;
41};
42
46
52
53TEST_F(LMDBEnvironmentTest, can_create_database)
54{
57
58 {
59 environment->wait_for_writer();
60 LMDBDatabaseCreationTransaction tx(environment);
61 LMDBDatabase::SharedPtr db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
62 EXPECT_NO_THROW(tx.commit());
63 }
64}
65
66TEST_F(LMDBEnvironmentTest, can_write_to_database)
67{
70
72 {
73 environment->wait_for_writer();
74 LMDBDatabaseCreationTransaction tx(environment);
75 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
76 EXPECT_NO_THROW(tx.commit());
77 }
78
79 {
80 environment->wait_for_writer();
82 auto key = get_key(0);
83 auto data = get_value(0, 0);
84 EXPECT_NO_THROW(tx->put_value(key, data, *db));
85 EXPECT_NO_THROW(tx->commit());
86 }
87}
88
89TEST_F(LMDBEnvironmentTest, can_read_from_database)
90{
94
95 {
96 environment->wait_for_writer();
97 LMDBDatabaseCreationTransaction tx(environment);
98 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
99 EXPECT_NO_THROW(tx.commit());
100 }
101
102 {
103 environment->wait_for_writer();
105 auto key = get_key(0);
106 auto data = get_value(0, 0);
107 EXPECT_NO_THROW(tx->put_value(key, data, *db));
108 EXPECT_NO_THROW(tx->commit());
109 }
110
111 {
112 environment->wait_for_reader();
114 auto key = get_key(0);
115 auto expected = get_value(0, 0);
116 std::vector<uint8_t> data;
117 tx->get_value(key, data, *db);
118 EXPECT_EQ(data, expected);
119 }
120}
121
122TEST_F(LMDBEnvironmentTest, can_write_and_read_multiple)
123{
126
128
129 {
130 environment->wait_for_writer();
131 LMDBDatabaseCreationTransaction tx(environment);
132 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
133 EXPECT_NO_THROW(tx.commit());
134 }
135
136 int64_t numValues = 10;
137
138 {
139 for (int64_t count = 0; count < numValues; count++) {
140 environment->wait_for_writer();
142 auto key = get_key(count);
143 auto data = get_value(count, 0);
144 EXPECT_NO_THROW(tx->put_value(key, data, *db));
145 EXPECT_NO_THROW(tx->commit());
146 }
147 }
148
149 {
150 for (int64_t count = 0; count < numValues; count++) {
151 environment->wait_for_reader();
153 auto key = get_key(count);
154 auto expected = get_value(count, 0);
155 std::vector<uint8_t> data;
156 tx->get_value(key, data, *db);
157 EXPECT_EQ(data, expected);
158 }
159 }
160}
161
162TEST_F(LMDBEnvironmentTest, can_read_multiple_threads)
163{
164 LMDBEnvironment::SharedPtr environment =
166
168 {
169 environment->wait_for_writer();
170 LMDBDatabaseCreationTransaction tx(environment);
171 db = std::make_unique<LMDBDatabase>(environment, tx, "DB", false, false);
172 EXPECT_NO_THROW(tx.commit());
173 }
174
175 int64_t numValues = 10;
176 int64_t numIterationsPerThread = 1000;
177 uint32_t numThreads = 16;
178
179 {
180 for (int64_t count = 0; count < numValues; count++) {
181 environment->wait_for_writer();
183 auto key = get_key(count);
184 auto expected = get_value(count, 0);
185 EXPECT_NO_THROW(tx->put_value(key, expected, *db));
186 EXPECT_NO_THROW(tx->commit());
187 }
188 }
189
190 {
191 auto func = [&]() -> void {
192 for (int64_t iteration = 0; iteration < numIterationsPerThread; iteration++) {
193 for (int64_t count = 0; count < numValues; count++) {
194 environment->wait_for_reader();
196 auto key = get_key(count);
197 auto expected = get_value(count, 0);
198 std::vector<uint8_t> data;
199 tx->get_value(key, data, *db);
200 EXPECT_EQ(data, expected);
201 }
202 }
203 };
205 for (uint64_t count = 0; count < numThreads; count++) {
206 threads.emplace_back(std::make_unique<std::thread>(func));
207 }
208 for (uint64_t count = 0; count < numThreads; count++) {
209 threads[count]->join();
210 }
211 }
212}
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