![]() |
MDBX Containers
|
Template class for managing key-value pairs in an MDBX database. More...
#include <KeyValueTable.hpp>
Classes | |
class | AssignmentProxy |
Helper proxy for convenient assignment via operator[]. More... | |
Public Member Functions | |
KeyValueTable (std::shared_ptr< Connection > connection, std::string name="kv_store", MDBX_db_flags_t flags=MDBX_DB_DEFAULTS|MDBX_CREATE) | |
Default constructor. | |
KeyValueTable (const Config &config, std::string name="kv_store", MDBX_db_flags_t flags=MDBX_DB_DEFAULTS|MDBX_CREATE) | |
Constructor with configuration. | |
~KeyValueTable () override final=default | |
Destructor. | |
template<template< class... > class ContainerT> | |
KeyValueTable & | operator= (const ContainerT< KeyT, ValueT > &container) |
Assigns a container (e.g., std::map or std::unordered_map) to the database. | |
KeyValueTable & | operator= (const std::vector< std::pair< KeyT, ValueT > > &container) |
Assigns a vector of key-value pairs to the database. | |
template<template< class... > class ContainerT = std::map> | |
auto | operator() () |
Loads all key-value pairs from the database into a container. | |
AssignmentProxy | operator[] (const KeyT &key) |
Provides convenient access to insert or read a value by key. | |
template<template< class... > class ContainerT> | |
void | load (ContainerT< KeyT, ValueT > &container, MDBX_txn *txn=nullptr) |
Loads data from the database into the container. | |
template<template< class... > class ContainerT> | |
void | load (ContainerT< KeyT, ValueT > &container, const Transaction &txn) |
Loads data from the database into the container using provided transaction. | |
void | load (std::vector< std::pair< KeyT, ValueT > > &container, MDBX_txn *txn=nullptr) |
Loads all key-value pairs into a std::vector of pairs. | |
void | load (std::vector< std::pair< KeyT, ValueT > > &container, const Transaction &txn) |
Loads all key-value pairs into a std::vector of pairs. | |
template<template< class... > class ContainerT = std::map> | |
auto | retrieve_all (MDBX_txn *txn=nullptr) |
Retrieves all key-value pairs into the specified container type. | |
template<template< class... > class ContainerT = std::map> | |
auto | retrieve_all (const Transaction &txn) |
Retrieves all key-value pairs into the specified container type. | |
template<template< class... > class ContainerT> | |
void | append (const ContainerT< KeyT, ValueT > &container, MDBX_txn *txn=nullptr) |
Appends data to the database. | |
template<template< class... > class ContainerT> | |
void | append (const ContainerT< KeyT, ValueT > &container, const Transaction &txn) |
Appends data to the database. | |
void | append (const std::vector< std::pair< KeyT, ValueT > > &container, MDBX_txn *txn=nullptr) |
Appends data from a vector to the database. | |
void | append (const std::vector< std::pair< KeyT, ValueT > > &container, const Transaction &txn) |
Appends data from a vector to the database using a provided transaction. | |
template<template< class... > class ContainerT> | |
void | reconcile (const ContainerT< KeyT, ValueT > &container, MDBX_txn *txn=nullptr) |
Reconciles the database with the container. | |
template<template< class... > class ContainerT> | |
void | reconcile (const ContainerT< KeyT, ValueT > &container, const Transaction &txn) |
Reconciles the database with the container. | |
void | reconcile (const std::vector< std::pair< KeyT, ValueT > > &container, MDBX_txn *txn=nullptr) |
Reconciles the database with the vector of key-value pairs. | |
void | reconcile (const std::vector< std::pair< KeyT, ValueT > > &container, const Transaction &txn) |
Reconciles the database with the vector of key-value pairs using provided transaction. | |
bool | insert (const KeyT &key, const ValueT &value, MDBX_txn *txn=nullptr) |
Inserts key-value only if key is absent. | |
bool | insert (const KeyT &key, const ValueT &value, const Transaction &txn) |
Inserts key-value only if key is absent. | |
bool | insert (const std::pair< KeyT, ValueT > &pair, MDBX_txn *txn=nullptr) |
Inserts key-value only if key is absent. | |
bool | insert (const std::pair< KeyT, ValueT > &pair, const Transaction &txn) |
Inserts key-value only if key is absent. | |
void | insert_or_assign (const KeyT &key, const ValueT &value, MDBX_txn *txn=nullptr) |
Inserts or replaces key-value pair. | |
void | insert_or_assign (const KeyT &key, const ValueT &value, const Transaction &txn) |
Inserts or replaces key-value pair. | |
void | insert_or_assign (const std::pair< KeyT, ValueT > &pair, MDBX_txn *txn=nullptr) |
Inserts or replaces key-value pair. | |
void | insert_or_assign (const std::pair< KeyT, ValueT > &pair, const Transaction &txn) |
Inserts or replaces key-value pair. | |
ValueT | at (const KeyT &key, MDBX_txn *txn=nullptr) const |
Retrieves value by key or throws. | |
ValueT | at (const KeyT &key, const Transaction &txn) const |
Retrieves value by key or throws. | |
bool | try_get (const KeyT &key, ValueT &out, MDBX_txn *txn) const |
Tries to find value by key. | |
bool | try_get (const KeyT &key, ValueT &out, const Transaction &txn) const |
Tries to find value by key. | |
std::pair< bool, ValueT > | find_compat (const KeyT &key, MDBX_txn *txn=nullptr) const |
Finds value by key. | |
std::pair< bool, ValueT > | find_compat (const KeyT &key, const Transaction &txn) const |
Finds value by key. | |
bool | contains (const KeyT &key, MDBX_txn *txn=nullptr) const |
Checks whether a key exists in the database. | |
bool | contains (const KeyT &key, const Transaction &txn) const |
Checks whether a key exists in the database. | |
std::size_t | count (MDBX_txn *txn=nullptr) const |
Returns the number of elements in the database. | |
std::size_t | count (const Transaction &txn) const |
Returns the number of elements in the database. | |
bool | empty (MDBX_txn *txn=nullptr) const |
Checks if the database is empty. | |
bool | empty (const Transaction &txn) const |
Checks if the database is empty. | |
bool | erase (const KeyT &key, MDBX_txn *txn=nullptr) |
Removes key from DB. | |
bool | erase (const KeyT &key, const Transaction &txn) |
Removes key from DB. | |
void | clear (MDBX_txn *txn=nullptr) |
Clears all key-value pairs from the database. | |
void | clear (const Transaction &txn) |
Clears all key-value pairs from the database. | |
![]() | |
BaseTable (std::shared_ptr< Connection > connection, std::string name, MDBX_db_flags_t flags) | |
Construct the database table accessor. | |
virtual | ~BaseTable ()=default |
bool | is_connected () const |
Checks if the connection is currently active. | |
void | connect () |
Connects to the MDBX environment if not already connected. | |
void | disconnect () |
Disconnects the MDBX environment. | |
void | begin (TransactionMode mode=TransactionMode::WRITABLE) |
Begins a manual transaction (must be committed or rolled back later). | |
void | commit () |
Commits the current manual transaction. | |
void | rollback () |
Rolls back the current manual transaction. | |
template<typename Func> | |
void | execute_in_transaction (Func operation, TransactionMode mode=TransactionMode::WRITABLE) |
Executes an operation inside an automatic transaction. | |
Private Member Functions | |
template<typename F> | |
void | with_transaction (F &&action, TransactionMode mode=TransactionMode::WRITABLE, MDBX_txn *txn=nullptr) const |
Executes a functor within a transaction context. | |
template<template< class... > class ContainerT> | |
void | db_load (ContainerT< KeyT, ValueT > &container, MDBX_txn *txn_handle) |
Loads data from the database into the container. | |
void | db_load (std::vector< std::pair< KeyT, ValueT > > &out_vector, MDBX_txn *txn) |
Loads all key-value pairs into a std::vector of pairs. | |
bool | db_get (const KeyT &key, ValueT &value, MDBX_txn *txn_handle) const |
Gets a value by key from the database. | |
bool | db_contains (const KeyT &key, MDBX_txn *txn_handle) const |
Checks whether a key exists in the database. | |
std::size_t | db_count (MDBX_txn *txn_handle) const |
Returns the number of elements in the database. | |
template<template< class... > class ContainerT> | |
void | db_append (const ContainerT< KeyT, ValueT > &container, MDBX_txn *txn_handle) |
Appends the content of the container to the database. | |
void | db_append (const std::vector< std::pair< KeyT, ValueT > > &container, MDBX_txn *txn_handle) |
Appends the content of the vector to the database. | |
template<template< class... > class ContainerT> | |
void | db_reconcile (const ContainerT< KeyT, ValueT > &container, MDBX_txn *txn_handle) |
Reconciles the content of the database with the container. | |
void | db_reconcile (const std::vector< std::pair< KeyT, ValueT > > &container, MDBX_txn *txn_handle) |
Reconciles the content of the database with the given vector of key-value pairs. | |
bool | db_insert_if_absent (const KeyT &key, const ValueT &value, MDBX_txn *txn_handle) |
Inserts a key-value pair only if the key does not already exist. | |
void | db_insert_or_assign (const KeyT &key, const ValueT &value, MDBX_txn *txn_handle) |
Inserts or replaces the key-value pair. | |
bool | db_erase (const KeyT &key, MDBX_txn *txn_handle) |
Removes a key from the database. | |
void | db_clear (MDBX_txn *txn_handle) |
Clears all key-value pairs from the database. | |
Additional Inherited Members | |
![]() | |
MDBX_txn * | thread_txn () const |
Returns the transaction bound to the current thread, if any. | |
MDBX_dbi | handle () const |
Gets the raw DBI handle. | |
![]() | |
std::shared_ptr< Connection > | m_connection |
Shared connection to MDBX environment. | |
MDBX_dbi | m_dbi {} |
DBI handle for the opened table. | |
Template class for managing key-value pairs in an MDBX database.
KeyT | Type of the keys. |
ValueT | Type of the values. |
This class provides functionality to store, retrieve, and manipulate key-value pairs in an MDBX database. It supports various container types, such as std::map
, std::unordered_map
, std::vector
, and std::list
. Key-value pairs can be inserted, reconciled, retrieved, and removed with transaction support. The class includes methods for bulk loading and appending data with transactional integrity, ensuring that operations are safely executed in a database environment. Additionally, temporary tables are used during reconciliation to ensure consistent data synchronization. This class also provides methods for checking the count and emptiness of the database, and efficiently handles database errors with detailed exception handling.
Definition at line 27 of file KeyValueTable.hpp.
|
inline |
Default constructor.
connection | Existing Connection instance. |
name | Name of the table within the MDBX environment. |
flags | Additional MDBX database flags. |
Definition at line 34 of file KeyValueTable.hpp.
|
inlineexplicit |
Constructor with configuration.
config | Configuration settings for the database. |
name | Name of the table within the MDBX environment. |
flags | Additional MDBX database flags. |
Definition at line 45 of file KeyValueTable.hpp.
|
finaloverridedefault |
Destructor.
|
inline |
Appends data to the database.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container with content to be synchronized. |
txn | Optional transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 230 of file KeyValueTable.hpp.
|
inline |
Appends data to the database.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container with content to be synchronized. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 218 of file KeyValueTable.hpp.
|
inline |
Appends data from a vector to the database using a provided transaction.
container | Vector with content to be synchronized. |
txn | Transaction wrapper. |
MdbxException | if a database error occurs. |
Definition at line 249 of file KeyValueTable.hpp.
|
inline |
Appends data from a vector to the database.
container | Vector with content to be synchronized. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 239 of file KeyValueTable.hpp.
|
inline |
Retrieves value by key or throws.
key | The key to look up. |
txn | Transaction wrapper used for the lookup. |
std::out_of_range | if key not found. |
MdbxException | if DB error occurs. |
Definition at line 399 of file KeyValueTable.hpp.
|
inline |
Retrieves value by key or throws.
key | The key to look up. |
txn | Active MDBX transaction. |
std::out_of_range | if key not found. |
MdbxException | if DB error occurs. |
Definition at line 383 of file KeyValueTable.hpp.
|
inline |
Clears all key-value pairs from the database.
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 574 of file KeyValueTable.hpp.
|
inline |
Clears all key-value pairs from the database.
txn | Active transaction. |
MdbxException | if a database error occurs. |
Definition at line 565 of file KeyValueTable.hpp.
|
inline |
Checks whether a key exists in the database.
key | The key to look up. |
txn | Transaction wrapper used for the lookup. |
MdbxException | if DB error occurs. |
Definition at line 496 of file KeyValueTable.hpp.
|
inline |
Checks whether a key exists in the database.
key | The key to look up. |
txn | Active transaction. |
MdbxException | if DB error occurs. |
Definition at line 483 of file KeyValueTable.hpp.
|
inline |
Returns the number of elements in the database.
txn | Transaction wrapper used for the count operation. |
MdbxException | if DB error occurs. |
Definition at line 516 of file KeyValueTable.hpp.
|
inline |
Returns the number of elements in the database.
txn | Active transaction. |
MdbxException | if DB error occurs. |
Definition at line 504 of file KeyValueTable.hpp.
|
inlineprivate |
Appends the content of the container to the database.
ContainerT | Template for the container type (map or unordered_map). |
container | Container with content to append. |
txn_handle | Active transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 696 of file KeyValueTable.hpp.
|
inlineprivate |
Appends the content of the vector to the database.
container | Vector with key-value pairs to append. |
txn_handle | Active transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 711 of file KeyValueTable.hpp.
|
inlineprivate |
Clears all key-value pairs from the database.
MdbxException | if an MDBX error occurs. |
Definition at line 845 of file KeyValueTable.hpp.
|
inlineprivate |
Checks whether a key exists in the database.
key | The key to check. |
txn_handle | The active transaction. |
MdbxException | if a database error occurs. |
Definition at line 671 of file KeyValueTable.hpp.
|
inlineprivate |
Returns the number of elements in the database.
txn_handle | Active transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 684 of file KeyValueTable.hpp.
|
inlineprivate |
Removes a key from the database.
key | The key of the pair to be removed. |
txn_handle | The active MDBX transaction. |
MdbxException | if deletion fails for other reasons. |
Definition at line 834 of file KeyValueTable.hpp.
|
inlineprivate |
Gets a value by key from the database.
key | The key to search for. |
value | Reference to store the retrieved value. |
txn_handle | The active transaction. |
MdbxException | if a database error occurs. |
Definition at line 656 of file KeyValueTable.hpp.
|
inlineprivate |
Inserts a key-value pair only if the key does not already exist.
key | The key to insert. |
value | The value to insert. |
txn_handle | The active MDBX transaction. |
MdbxException | if the insert fails for reasons other than key existence. |
Definition at line 801 of file KeyValueTable.hpp.
|
inlineprivate |
Inserts or replaces the key-value pair.
key | The key to insert or replace. |
value | The value to set. |
txn_handle | The active MDBX transaction. |
MdbxException | if the operation fails. |
Definition at line 820 of file KeyValueTable.hpp.
|
inlineprivate |
Loads data from the database into the container.
ContainerT | Template for the container type. |
container | Container to be synchronized with database content. |
txn_handle | Active transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 618 of file KeyValueTable.hpp.
|
inlineprivate |
Loads all key-value pairs into a std::vector of pairs.
out_vector | The output vector to fill. |
txn | Optional transaction. |
MdbxException | if a database error occurs. |
Definition at line 636 of file KeyValueTable.hpp.
|
inlineprivate |
Reconciles the content of the database with the container.
Synchronizes the main table with the content of the container by using a temporary table. Clears old data, inserts new data, and updates existing records in the main table.
ContainerT | Template for the container type (map or unordered_map). |
container | Container with key-value pairs to be reconciled with the database. |
txn_handle | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 730 of file KeyValueTable.hpp.
|
inlineprivate |
Reconciles the content of the database with the given vector of key-value pairs.
Clears existing records not present in the vector, and upserts all from the vector.
container | Vector of key-value pairs to reconcile with the database. |
txn_handle | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 763 of file KeyValueTable.hpp.
|
inline |
Checks if the database is empty.
txn | Transaction wrapper used for the check. |
MdbxException | if a database error occurs. |
Definition at line 536 of file KeyValueTable.hpp.
|
inline |
Checks if the database is empty.
txn | Optional transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 524 of file KeyValueTable.hpp.
|
inline |
Removes key from DB.
key | The key of the pair to be removed. |
txn | Transaction wrapper used for the operation. |
MdbxException | if deletion fails. |
Definition at line 558 of file KeyValueTable.hpp.
|
inline |
Removes key from DB.
key | The key of the pair to be removed. |
txn | Active transaction. |
MdbxException | if deletion fails. |
Definition at line 545 of file KeyValueTable.hpp.
|
inline |
Finds value by key.
key | Key to search for. |
txn | Transaction wrapper used for the lookup. |
MdbxException | on DB error. |
Definition at line 474 of file KeyValueTable.hpp.
|
inline |
Finds value by key.
key | Key to search for. |
txn | Optional active MDBX transaction. |
MdbxException | on DB error. |
Definition at line 459 of file KeyValueTable.hpp.
|
inline |
Inserts key-value only if key is absent.
key | The key to be inserted. |
value | The value to be inserted. |
txn | Transaction wrapper used for the insertion. |
MdbxException | if a database error occurs. |
Definition at line 313 of file KeyValueTable.hpp.
|
inline |
Inserts key-value only if key is absent.
key | The key to be inserted. |
value | The value to be inserted. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 299 of file KeyValueTable.hpp.
|
inline |
Inserts key-value only if key is absent.
pair | The key-value pair to be inserted. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 335 of file KeyValueTable.hpp.
|
inline |
Inserts key-value only if key is absent.
pair | The key-value pair to be inserted. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 322 of file KeyValueTable.hpp.
|
inline |
Inserts or replaces key-value pair.
key | The key to be inserted. |
value | The value to be inserted. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 355 of file KeyValueTable.hpp.
|
inline |
Inserts or replaces key-value pair.
key | The key to be inserted. |
value | The value to be inserted. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 344 of file KeyValueTable.hpp.
|
inline |
Inserts or replaces key-value pair.
pair | The key-value pair to be inserted. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 373 of file KeyValueTable.hpp.
|
inline |
Inserts or replaces key-value pair.
pair | The key-value pair to be inserted. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 363 of file KeyValueTable.hpp.
|
inline |
Loads data from the database into the container using provided transaction.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be synchronized with database content. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 160 of file KeyValueTable.hpp.
|
inline |
Loads data from the database into the container.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be synchronized with database content. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 148 of file KeyValueTable.hpp.
|
inline |
Loads all key-value pairs into a std::vector of pairs.
container | Container to be synchronized with database content. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 178 of file KeyValueTable.hpp.
|
inline |
Loads all key-value pairs into a std::vector of pairs.
container | Container to be synchronized with database content. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 168 of file KeyValueTable.hpp.
|
inline |
Loads all key-value pairs from the database into a container.
ContainerT | Container type (e.g., std::map, std::unordered_map, std::vector). |
MdbxException | if a database error occurs. |
Definition at line 87 of file KeyValueTable.hpp.
|
inline |
Assigns a container (e.g., std::map or std::unordered_map) to the database.
container | The container with key-value pairs. |
MdbxException | if a database error occurs. |
Definition at line 63 of file KeyValueTable.hpp.
|
inline |
Assigns a vector of key-value pairs to the database.
container | The vector with key-value pairs. |
MdbxException | if a database error occurs. |
Definition at line 75 of file KeyValueTable.hpp.
|
inline |
Provides convenient access to insert or read a value by key.
key | Key to access. |
Definition at line 136 of file KeyValueTable.hpp.
|
inline |
Reconciles the database with the container.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be reconciled with the database. |
txn | Transaction wrapper used for the operation. |
MdbxException | if a database error occurs. |
Definition at line 271 of file KeyValueTable.hpp.
|
inline |
Reconciles the database with the container.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be reconciled with the database. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 259 of file KeyValueTable.hpp.
|
inline |
Reconciles the database with the vector of key-value pairs using provided transaction.
container | Vector to be reconciled with the database. |
txn | Transaction wrapper. |
MdbxException | if a database error occurs. |
Definition at line 289 of file KeyValueTable.hpp.
|
inline |
Reconciles the database with the vector of key-value pairs.
container | Vector to be reconciled with the database. |
txn | Active MDBX transaction. |
MdbxException | if a database error occurs. |
Definition at line 279 of file KeyValueTable.hpp.
|
inline |
Retrieves all key-value pairs into the specified container type.
ContainerT | Container type (e.g., std::map, std::unordered_map, std::vector). |
txn | Transaction wrapper used for the retrieval. |
MdbxException | if a database error occurs. |
Definition at line 208 of file KeyValueTable.hpp.
|
inline |
Retrieves all key-value pairs into the specified container type.
ContainerT | Container type (e.g., std::map, std::unordered_map, std::vector). |
txn | Optional transaction handle. |
MdbxException | if a database error occurs. |
Definition at line 188 of file KeyValueTable.hpp.
|
inline |
Tries to find value by key.
key | The key to look up. |
out | Reference to output value. |
txn | Transaction wrapper used for the lookup. |
MdbxException | if DB error occurs. |
Definition at line 423 of file KeyValueTable.hpp.
|
inline |
Tries to find value by key.
key | The key to look up. |
out | Reference to output value. |
txn | Active MDBX transaction. |
MdbxException | if DB error occurs. |
Definition at line 409 of file KeyValueTable.hpp.
|
inlineprivate |
Executes a functor within a transaction context.
F | Callable type accepting MDBX_txn* . |
action | Functor to execute. |
mode | Transaction mode used when a new transaction is created. |
txn | Optional existing transaction handle. |
Definition at line 586 of file KeyValueTable.hpp.