MDBX Containers
Loading...
Searching...
No Matches
single_transaction_demo.cpp
Go to the documentation of this file.
1
5
7#include <iostream>
8#include <vector>
9#include <cstring>
10#include <limits>
11
12struct MyStruct {
13 int a;
14 float b;
15
16 std::vector<uint8_t> to_bytes() const {
17 std::vector<uint8_t> out(sizeof(MyStruct));
18 std::memcpy(out.data(), this, sizeof(MyStruct));
19 return out;
20 }
21
22 static MyStruct from_bytes(const void* data, size_t size) {
23 MyStruct out;
24 if (size >= sizeof(MyStruct))
25 std::memcpy(&out, data, sizeof(MyStruct));
26 return out;
27 }
28};
29
30int main() {
31 mdbxc::Config config;
32 config.pathname = "example_db";
33
34 auto conn = mdbxc::Connection::create(config);
36
37 auto txn = conn->transaction();
38 table.clear(txn);
39 table.insert_or_assign(1, "one", txn);
40 table.insert_or_assign(2, "two", txn);
41
42# if __cplusplus >= 201703L
43 auto result = table.find(1, txn);
44 std::cout << "Key 1: " << result.value_or("not found") << std::endl;
45# else
46 auto result = table.find_compat(1, txn);
47 if (result.first)
48 std::cout << "Key 1: " << result.second << std::endl;
49 else
50 std::cout << "Key 1: not found" << std::endl;
51# endif
52
53 txn.commit();
54
55 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
56 std::cin.get();
57 return 0;
58}
Declaration of the KeyValueTable class for managing key-value pairs in an MDBX database.
Parameters used by Connection to create the MDBX environment.
Definition Config.hpp:17
std::string pathname
Path to the database file or directory containing the database.
Definition Config.hpp:19
static std::shared_ptr< Connection > create(const Config &config)
Creates and connects a new shared Connection instance.
Template class for managing key-value pairs in an MDBX database.
void insert_or_assign(const KeyT &key, const ValueT &value, MDBX_txn *txn=nullptr)
Inserts or replaces key-value pair.
std::pair< bool, ValueT > find_compat(const KeyT &key, MDBX_txn *txn=nullptr) const
Finds value by key.
std::pair< bool, ValueT > find(const KeyT &key, MDBX_txn *txn=nullptr) const
Finds value by key.
void clear(MDBX_txn *txn=nullptr)
Clears all key-value pairs from the database.
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