14 std::vector<uint8_t> bytes(
sizeof(
MyStruct));
15 std::memcpy(bytes.data(),
this,
sizeof(
MyStruct));
21 throw std::runtime_error(
"Invalid data size for MyStruct");
24 std::memcpy(&out, data,
sizeof(
MyStruct));
29 return a == other.
a &&
b == other.
b;
35 cfg.
pathname =
"data/any_value_table.mdbx";
42 table.
set<
int>(
"answer", 42);
43 table.
set<std::string>(
"greeting",
"hello");
47 assert(table.
get<
int>(
"answer") == 42);
48#if __cplusplus >= 201703L
49 assert(table.find<std::string>(
"greeting").value() ==
"hello");
52 auto res = table.
find_compat<std::string>(
"greeting");
53 assert(res.first && res.second ==
"hello");
58 auto ks = table.
keys();
59 assert(ks.size() == 3);
61 std::cout <<
"AnyValueTable test passed.\n";
Table storing values of arbitrary type indexed by key.
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.
bool no_subdir
Whether to store the database in a single file instead of a directory.
std::string pathname
Path to the database file or directory containing the database.
bool relative_to_exe
Whether to resolve a relative path relative to the executable directory.
int64_t max_dbs
Maximum number of named databases (DBI) in the environment.
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