MDBX Containers
Loading...
Searching...
No Matches
any_value_table_test.cpp
Go to the documentation of this file.
1#include <cassert>
3#include <vector>
4#include <string>
5#include <iostream>
6#include <cstring>
7
9struct MyStruct {
10 int a;
11 double b;
12
13 std::vector<uint8_t> to_bytes() const {
14 std::vector<uint8_t> bytes(sizeof(MyStruct));
15 std::memcpy(bytes.data(), this, sizeof(MyStruct));
16 return bytes;
17 }
18
19 static MyStruct from_bytes(const void* data, size_t size) {
20 if (size != sizeof(MyStruct)) {
21 throw std::runtime_error("Invalid data size for MyStruct");
22 }
23 MyStruct out{};
24 std::memcpy(&out, data, sizeof(MyStruct));
25 return out;
26 }
27
28 bool operator==(const MyStruct& other) const {
29 return a == other.a && b == other.b;
30 }
31};
32
33int main() {
34 mdbxc::Config cfg;
35 cfg.pathname = "data/any_value_table.mdbx";
36 cfg.max_dbs = 8;
37 cfg.no_subdir = true;
38 cfg.relative_to_exe = true;
39 auto conn = mdbxc::Connection::create(cfg);
40
41 mdbxc::AnyValueTable<std::string> table(conn, "test_any");
42 table.set<int>("answer", 42);
43 table.set<std::string>("greeting", "hello");
44 MyStruct expected{7, 3.5};
45 table.set<MyStruct>("object", expected);
46
47 assert(table.get<int>("answer") == 42);
48#if __cplusplus >= 201703L
49 assert(table.find<std::string>("greeting").value() == "hello");
50#else
51 {
52 auto res = table.find_compat<std::string>("greeting");
53 assert(res.first && res.second == "hello");
54 }
55#endif
56 assert(table.get<MyStruct>("object") == expected);
57
58 auto ks = table.keys();
59 assert(ks.size() == 3);
60
61 std::cout << "AnyValueTable test passed.\n";
62 return 0;
63}
Table storing values of arbitrary type indexed by key.
int main()
Table storing values of arbitrary type associated with a key.
std::vector< KeyT > keys(MDBX_txn *txn=nullptr) const
List all keys stored in table.
void set(const KeyT &key, const T &value, MDBX_txn *txn=nullptr)
Set value for key, replacing existing value.
std::pair< bool, T > find_compat(const KeyT &key, MDBX_txn *txn=nullptr) const
Find value by key.
T get(const KeyT &key, MDBX_txn *txn=nullptr) const
Retrieve stored value or throw if missing.
Parameters used by Connection to create the MDBX environment.
Definition Config.hpp:17
bool no_subdir
Whether to store the database in a single file instead of a directory.
Definition Config.hpp:30
std::string pathname
Path to the database file or directory containing the database.
Definition Config.hpp:19
bool relative_to_exe
Whether to resolve a relative path relative to the executable directory.
Definition Config.hpp:33
int64_t max_dbs
Maximum number of named databases (DBI) in the environment.
Definition Config.hpp:27
static std::shared_ptr< Connection > create(const Config &config)
Creates and connects a new shared Connection instance.
Demonstrates storing values of arbitrary types using AnyValueTable.
static MyStruct from_bytes(const void *data, size_t size)
std::vector< uint8_t > to_bytes() const
bool operator==(const MyStruct &other) const