MDBX Containers
Loading...
Searching...
No Matches
manual_transaction_example.cpp
Go to the documentation of this file.
1
5
7#include <iostream>
8#include <limits>
9
10int main() {
11 mdbxc::Config config;
12 config.pathname = "manual_txn_db";
13
14 auto conn = mdbxc::Connection::create(config);
16 table.clear();
17
18 // Start writable transaction manually
20
21 // Insert key-value pairs
22 table.insert_or_assign(10, "ten");
23 table.insert_or_assign(20, "twenty");
24
25 // Use operator[] to assign a value (modifies in-place)
26 table[30] = "thirty";
27
28 // Use operator[] to read the value
29 std::cout << "Key 20 (operator[]): " << static_cast<std::string>(table[20]) << std::endl;
30
31 // Use at() for bounds-checked access
32 try {
33 std::cout << "Key 30 (at): " << table.at(30) << std::endl;
34 } catch (const std::out_of_range&) {
35 std::cout << "Key 30 not found" << std::endl;
36 }
37
38# if __cplusplus >= 201703L
39 auto result = table.find(10);
40 std::cout << "Key 10 (find): " << result.value_or("not found") << std::endl;
41# else
42 auto result = table.find_compat(10);
43 if (result.first)
44 std::cout << "Key 10 (find): " << result.second << std::endl;
45 else
46 std::cout << "Key 10 (find): not found" << std::endl;
47# endif
48
49 // Commit transaction
50 conn->commit();
51
52 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
53 std::cin.get();
54 return 0;
55}
Declaration of the KeyValueTable class for managing key-value pairs in an MDBX database.
int main()
Entry point demonstrating AnyValueTable.
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.
@ WRITABLE
Writable transaction (allows inserts, updates, deletes).