![]() |
MDBX Containers
|
This page describes how to work with persistent container classes such as mdbxc::KeyValueTable, mdbxc::KeyTable, mdbxc::KeyMultiValueTable, and mdbxc::AnyValueTable. Containers expose STL-like interfaces while storing data durably in an MDBX database.
All tables operate inside an mdbxc::Connection. A minimal setup creates the environment and names a logical table:
KeyValueTable mirrors std::map. Insert or update elements with insert_or_assign(), query them with find(), erase with erase(), or use operator[] for convenient assignment. Each modifying call runs inside a transaction managed automatically by the table.
Tables can be synchronized with existing containers. Assigning from std::map or std::unordered_map replaces database content, while calling the table object loads all pairs into a container:
mdbxc::KeyTable acts like std::set, storing only keys. Use insert() to add a key, contains() to test for existence, and erase() to remove it.
mdbxc::KeyMultiValueTable allows duplicate keys similar to std::multimap. insert() adds a new value for a key, find() iterates over the range of values, and erase(key, value) removes individual duplicates.
mdbxc::AnyValueTable stores values of arbitrary types by tagging each record with its C++ type. set<T>(), insert<T>(), and get<T>() operate on typed values, and update<T>() modifies them in place with a functor.
Every modifying operation uses an mdbxc::MDBX transaction. Tables create transactions automatically, but a caller may supply an external transaction to group multiple operations:
The library supports both C++11 and C++17. When built with C++17, functions such as find() return std::optional. With C++11 use the *_compat variants returning std::pair<bool, T>.
Trivially copyable types are stored as raw bytes. Custom types participate by providing to_bytes() and from_bytes() functions, allowing complex structures to be persisted without manual serialization logic.