MDBX Containers
Loading...
Searching...
No Matches
table_methods_showcase.cpp
Go to the documentation of this file.
1
5
7#include <iostream>
8#include <string>
9
10int main() {
11 mdbxc::Config config;
12 config.pathname = "full_methods_db";
13 config.max_dbs = 1;
14
15 auto conn = mdbxc::Connection::create(config);
16 mdbxc::KeyValueTable<int, std::string> table(conn, "full_demo");
17
18 // insert_or_assign
19 table.insert_or_assign(1, "one");
20 table.insert_or_assign(2, "two");
21
22 // insert (no overwrite)
23 bool inserted = table.insert(3, "three");
24 std::cout << "Inserted key 3: " << inserted << std::endl;
25
26 inserted = table.insert(2, "TWO");
27 std::cout << "Inserted key 2 again: " << inserted << " (should be false)" << std::endl;
28
29 // contains
30 std::cout << "Contains key 1: " << table.contains(1) << std::endl;
31 std::cout << "Contains key 4: " << table.contains(4) << std::endl;
32
33 // operator[]
34 table[4] = "four";
35 std::cout << "table[4]: " << static_cast<std::string>(table[4]) << std::endl;
36
37 // operator()
38 std::map<int, std::string> snapshot = table();
39 std::cout << "Snapshot:\n";
40 for (const auto& [k, v] : snapshot)
41 std::cout << k << ": " << v << std::endl;
42
43 // operator=
44 std::unordered_map<int, std::string> temp_data = {
45 {100, "hundred"},
46 {200, "two hundred"}
47 };
48 table = temp_data;
49
50 // retrieve_all()
51 std::map<int, std::string> snapshot_2 = table.retrieve_all();
52
53# if __cplusplus >= 201703L
54 // find
55 auto result = table.find(100);
56 if (result)
57 std::cout << "Found key 100: " << *result << std::endl;
58 else
59 std::cout << "Key 100 not found\n";
60# else
61 auto result = table.find_compat(100);
62 if (result.first)
63 std::cout << "Found key 100: " << result.second << std::endl;
64 else
65 std::cout << "Key 100 not found\n";
66# endif
67
68 // erase
69 bool erased = table.erase(200);
70 std::cout << "Erased key 200: " << erased << std::endl;
71
72 // load
73 std::cout << "All key-value pairs:\n";
74 std::vector<std::pair<int, std::string>> all;
75 table.load(all);
76 for (size_t i = 0; i < all.size(); ++i)
77 std::cout << all[i].first << ": " << all[i].second << std::endl;
78
79 // clear
80 table.clear();
81 std::cout << "After clear, size = " << table.count() << std::endl;
82
83 return 0;
84}
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
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.
Template class for managing key-value pairs in an MDBX database.
int main()
Basic example using Config to initialize a table.