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
11struct MyStruct {
12 int a;
13 float b;
14
15 std::vector<uint8_t> to_bytes() const {
16 std::vector<uint8_t> out(sizeof(MyStruct));
17 std::memcpy(out.data(), this, sizeof(MyStruct));
18 return out;
19 }
20
21 static MyStruct from_bytes(const void* data, size_t size) {
22 MyStruct out;
23 if (size >= sizeof(MyStruct))
24 std::memcpy(&out, data, sizeof(MyStruct));
25 return out;
26 }
27};
28
29int main() {
30 mdbxc::Config config;
31 config.pathname = "example_db";
32
33 auto conn = mdbxc::Connection::create(config);
35
36 auto txn = conn->transaction();
37 table.clear(txn);
38 table.insert_or_assign(1, "one", txn);
39 table.insert_or_assign(2, "two", txn);
40
41# if __cplusplus >= 201703L
42 auto result = table.find(1, txn);
43 std::cout << "Key 1: " << result.value_or("not found") << std::endl;
44# else
45 auto result = table.find_compat(1, txn);
46 if (result.first)
47 std::cout << "Key 1: " << result.second << std::endl;
48 else
49 std::cout << "Key 1: not found" << std::endl;
50# endif
51
52 txn.commit();
53 return 0;
54}
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.
void clear(MDBX_txn *txn=nullptr)
Clears all key-value pairs from the database.
Shows scoped transaction usage.
static MyStruct from_bytes(const void *data, size_t size)
std::vector< uint8_t > to_bytes() const