SQLite Containers
Loading...
Searching...
No Matches
sqlite_containers::KeyValueDB< KeyT, ValueT > Class Template Referencefinal

Template class for managing key-value pairs in a SQLite database. More...

#include <KeyValueDB.hpp>

Inheritance diagram for sqlite_containers::KeyValueDB< KeyT, ValueT >:
sqlite_containers::BaseDB

Public Member Functions

 KeyValueDB ()
 Default constructor.
 
 KeyValueDB (const Config &config)
 Constructor with configuration.
 
 ~KeyValueDB () override final=default
 Destructor.
 
template<template< class... > class ContainerT>
KeyValueDBoperator= (const ContainerT< KeyT, ValueT > &container)
 Assigns a container (e.g., std::map or std::unordered_map) to the database.
 
template<template< class... > class ContainerT = std::map>
ContainerT< KeyT, ValueT > operator() ()
 Loads all key-value pairs from the database into a container (e.g., std::map or std::unordered_map).
 
template<template< class... > class ContainerT>
void load (ContainerT< KeyT, ValueT > &container)
 Loads data from the database into the container.
 
template<template< class... > class ContainerT>
void load (ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
 Loads data with a transaction.
 
template<template< class... > class ContainerT>
ContainerT< KeyT, ValueT > retrieve_all ()
 Retrieves all key-value pairs.
 
template<template< class... > class ContainerT>
ContainerT< KeyT, ValueT > retrieve_all (const TransactionMode &mode)
 Retrieves all key-value pairs with a transaction.
 
template<template< class... > class ContainerT>
void append (const ContainerT< KeyT, ValueT > &container)
 Appends data to the database.
 
template<template< class... > class ContainerT>
void append (const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
 Appends data with a transaction.
 
template<template< class... > class ContainerT>
void reconcile (const ContainerT< KeyT, ValueT > &container)
 Reconciles the database with the container.
 
template<template< class... > class ContainerT>
void reconcile (const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
 Reconciles the database with the container using a transaction.
 
void insert (const KeyT &key, const ValueT &value)
 Inserts a key-value pair into the database.
 
void insert (const std::pair< KeyT, ValueT > &pair)
 Inserts a key-value pair into the database.
 
bool find (const KeyT &key, ValueT &value)
 Finds a value by key.
 
std::size_t count () const
 Returns the number of elements in the database.
 
bool empty () const
 Checks if the database is empty.
 
void remove (const KeyT &key)
 Removes a key-value pair from the database.
 
void clear ()
 Clears all key-value pairs from the database.
 
- Public Member Functions inherited from sqlite_containers::BaseDB
 BaseDB ()=default
 Default constructor.
 
virtual ~BaseDB ()
 Destructor. Disconnects from the database if connected.
 
void set_config (const Config &config)
 Sets the configuration for the database.
 
Config get_config () const
 Gets the current configuration of the database.
 
void connect ()
 Connects to the database using the current configuration. Initializes a connection to the database by creating necessary directories, opening the database, creating tables, and setting up database parameters.
 
void connect (const Config &config)
 Connects to the database with the given configuration.
 
void disconnect ()
 Disconnects from the database.
 
void begin (const TransactionMode &mode=TransactionMode::DEFERRED)
 Begins a database transaction.
 
void commit ()
 Commits the current transaction.
 
void rollback ()
 Rolls back the current transaction.
 
template<typename Func >
void execute_in_transaction (Func operation, const TransactionMode &mode)
 Executes an operation inside a transaction.
 
virtual void process ()
 Processes asynchronous database requests (can be overridden).
 

Private Member Functions

void db_create_table (const Config &config) override final
 Creates the main and temporary tables in the database. This method creates both the main key-value table and a temporary table for handling synchronization.
 
template<template< class... > class ContainerT>
void db_load (ContainerT< KeyT, ValueT > &container)
 Loads data from the database into the container.
 
bool db_find (const KeyT &key, ValueT &value)
 Finds a value by key in the database.
 
std::size_t db_count () const
 Returns the number of elements in the database.
 
template<template< class... > class ContainerT>
void db_append (const ContainerT< KeyT, ValueT > &container)
 Appends the content of the container to the database.
 
template<template< class... > class ContainerT>
void db_reconcile (const ContainerT< KeyT, ValueT > &container)
 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.
 
void db_insert (const KeyT &key, const ValueT &value)
 Inserts a key-value pair into the database.
 
void db_remove (const KeyT &key)
 Removes a key-value pair from the database.
 
void db_clear ()
 Clears all key-value pairs from the database.
 

Private Attributes

SqliteStmt m_stmt_load
 Statement for loading data from the database.
 
SqliteStmt m_stmt_replace
 Statement for replacing key-value pairs in the database.
 
SqliteStmt m_stmt_get_value
 Statement for retrieving value by key from the database.
 
SqliteStmt m_stmt_count
 
SqliteStmt m_stmt_remove
 Statement for removing key-value pair from the database.
 
SqliteStmt m_stmt_clear_main
 Statement for clearing the main table.
 
SqliteStmt m_stmt_insert_temp
 Statement for inserting data into the temporary table.
 
SqliteStmt m_stmt_purge_main
 Statement for purging stale data from the main table.
 
SqliteStmt m_stmt_merge_temp
 Statement for merging data from the temporary table into the main table.
 
SqliteStmt m_stmt_clear_temp
 Statement for clearing the temporary table.
 

Additional Inherited Members

- Protected Member Functions inherited from sqlite_containers::BaseDB
void db_begin (const TransactionMode &mode=TransactionMode::DEFERRED)
 Begins a transaction with the given mode.
 
void db_commit ()
 Commits the current transaction.
 
void db_rollback ()
 Rolls back the current transaction.
 
void db_handle_exception (std::exception_ptr ex, const std::vector< SqliteStmt * > &stmts, const std::string &message="Unknown error occurred.") const
 Handles an exception by resetting and clearing bindings of prepared SQL statements.
 
virtual void on_db_open ()
 Called after the database is opened. Can be overridden in derived classes.
 
virtual void on_db_close ()
 Called before the database is closed. Can be overridden in derived classes.
 
- Protected Attributes inherited from sqlite_containers::BaseDB
sqlite3 * m_sqlite_db = nullptr
 
std::mutex m_sqlite_mutex
 

Detailed Description

template<class KeyT, class ValueT>
class sqlite_containers::KeyValueDB< KeyT, ValueT >

Template class for managing key-value pairs in a SQLite database.

Template Parameters
KeyTType of the keys.
ValueTType of the values.

This class provides functionality to store, retrieve, and manipulate key-value pairs in a SQLite 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 22 of file KeyValueDB.hpp.

Constructor & Destructor Documentation

◆ KeyValueDB() [1/2]

template<class KeyT , class ValueT >
sqlite_containers::KeyValueDB< KeyT, ValueT >::KeyValueDB ( )
inline

Default constructor.

Definition at line 26 of file KeyValueDB.hpp.

◆ KeyValueDB() [2/2]

template<class KeyT , class ValueT >
sqlite_containers::KeyValueDB< KeyT, ValueT >::KeyValueDB ( const Config & config)
inlineexplicit

Constructor with configuration.

Parameters
configConfiguration settings for the database.

Definition at line 30 of file KeyValueDB.hpp.

◆ ~KeyValueDB()

template<class KeyT , class ValueT >
sqlite_containers::KeyValueDB< KeyT, ValueT >::~KeyValueDB ( )
finaloverridedefault

Destructor.

Member Function Documentation

◆ append() [1/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::append ( const ContainerT< KeyT, ValueT > & container)
inline

Appends data to the database.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer with content to be synchronized.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 129 of file KeyValueDB.hpp.

◆ append() [2/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::append ( const ContainerT< KeyT, ValueT > & container,
const TransactionMode & mode )
inline

Appends data with a transaction.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer with content to be synchronized.
modeTransaction mode.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 140 of file KeyValueDB.hpp.

◆ clear()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::clear ( )
inline

Clears all key-value pairs from the database.

Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 224 of file KeyValueDB.hpp.

◆ count()

template<class KeyT , class ValueT >
std::size_t sqlite_containers::KeyValueDB< KeyT, ValueT >::count ( ) const
inline

Returns the number of elements in the database.

Returns
The number of key-value pairs.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 202 of file KeyValueDB.hpp.

◆ db_append()

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_append ( const ContainerT< KeyT, ValueT > & container)
inlineprivate

Appends the content of the container to the database.

Template Parameters
ContainerTTemplate for the container type (map or unordered_map).
Parameters
containerContainer with content to
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 402 of file KeyValueDB.hpp.

◆ db_clear()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_clear ( )
inlineprivate

Clears all key-value pairs from the database.

Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 501 of file KeyValueDB.hpp.

◆ db_count()

template<class KeyT , class ValueT >
std::size_t sqlite_containers::KeyValueDB< KeyT, ValueT >::db_count ( ) const
inlineprivate

Returns the number of elements in the database.

Returns
The number of key-value pairs in the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 363 of file KeyValueDB.hpp.

◆ db_create_table()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_create_table ( const Config & config)
inlinefinaloverrideprivatevirtual

Creates the main and temporary tables in the database. This method creates both the main key-value table and a temporary table for handling synchronization.

Parameters
configConfiguration settings for the database, such as table names.

Implements sqlite_containers::BaseDB.

Definition at line 245 of file KeyValueDB.hpp.

◆ db_find()

template<class KeyT , class ValueT >
bool sqlite_containers::KeyValueDB< KeyT, ValueT >::db_find ( const KeyT & key,
ValueT & value )
inlineprivate

Finds a value by key in the database.

Parameters
keyThe key to search for.
valueThe value associated with the key.
Returns
True if the key was found, false otherwise.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 322 of file KeyValueDB.hpp.

◆ db_insert()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_insert ( const KeyT & key,
const ValueT & value )
inlineprivate

Inserts a key-value pair into the database.

Parameters
keyThe key to be inserted.
valueThe value to be inserted.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 467 of file KeyValueDB.hpp.

◆ db_load()

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_load ( ContainerT< KeyT, ValueT > & container)
inlineprivate

Loads data from the database into the container.

Template Parameters
ContainerTTemplate for the container type.
Parameters
containerContainer to be synchronized with database content.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 283 of file KeyValueDB.hpp.

◆ db_reconcile()

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_reconcile ( const ContainerT< KeyT, ValueT > & container)
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.

Template Parameters
ContainerTTemplate for the container type (map or unordered_map).
Parameters
containerContainer with key-value pairs to be reconciled with the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 426 of file KeyValueDB.hpp.

◆ db_remove()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::db_remove ( const KeyT & key)
inlineprivate

Removes a key-value pair from the database.

Parameters
keyThe key of the pair to be removed.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 485 of file KeyValueDB.hpp.

◆ empty()

template<class KeyT , class ValueT >
bool sqlite_containers::KeyValueDB< KeyT, ValueT >::empty ( ) const
inline

Checks if the database is empty.

Returns
True if the database is empty, false otherwise.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 210 of file KeyValueDB.hpp.

◆ find()

template<class KeyT , class ValueT >
bool sqlite_containers::KeyValueDB< KeyT, ValueT >::find ( const KeyT & key,
ValueT & value )
inline

Finds a value by key.

Parameters
keyThe key to search for.
valueThe value associated with the key.
Returns
True if the key was found, false otherwise.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 194 of file KeyValueDB.hpp.

◆ insert() [1/2]

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::insert ( const KeyT & key,
const ValueT & value )
inline

Inserts a key-value pair into the database.

Parameters
keyThe key to be inserted.
valueThe value to be inserted.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 176 of file KeyValueDB.hpp.

◆ insert() [2/2]

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::insert ( const std::pair< KeyT, ValueT > & pair)
inline

Inserts a key-value pair into the database.

Parameters
pairThe key-value pair to be inserted.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 184 of file KeyValueDB.hpp.

◆ load() [1/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::load ( ContainerT< KeyT, ValueT > & container)
inline

Loads data from the database into the container.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer to be synchronized with database content.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 79 of file KeyValueDB.hpp.

◆ load() [2/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::load ( ContainerT< KeyT, ValueT > & container,
const TransactionMode & mode )
inline

Loads data with a transaction.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer to be synchronized with database content.
modeTransaction mode.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 90 of file KeyValueDB.hpp.

◆ operator()()

template<class KeyT , class ValueT >
template<template< class... > class ContainerT = std::map>
ContainerT< KeyT, ValueT > sqlite_containers::KeyValueDB< KeyT, ValueT >::operator() ( )
inline

Loads all key-value pairs from the database into a container (e.g., std::map or std::unordered_map).

Template Parameters
ContainerTThe type of the container (e.g., std::map or std::unordered_map).
Returns
A container populated with all key-value pairs from the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.
Note
The transaction mode is taken from the database configuration.

Definition at line 61 of file KeyValueDB.hpp.

◆ operator=()

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
KeyValueDB & sqlite_containers::KeyValueDB< KeyT, ValueT >::operator= ( const ContainerT< KeyT, ValueT > & container)
inline

Assigns a container (e.g., std::map or std::unordered_map) to the database.

Parameters
containerThe container with key-value pairs.
Returns
Reference to this KeyValueDB.
Exceptions
sqlite_exceptionif an SQLite error occurs.
Note
The transaction mode is taken from the database configuration.

Definition at line 45 of file KeyValueDB.hpp.

◆ reconcile() [1/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::reconcile ( const ContainerT< KeyT, ValueT > & container)
inline

Reconciles the database with the container.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer to be reconciled with the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 153 of file KeyValueDB.hpp.

◆ reconcile() [2/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyValueDB< KeyT, ValueT >::reconcile ( const ContainerT< KeyT, ValueT > & container,
const TransactionMode & mode )
inline

Reconciles the database with the container using a transaction.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
containerContainer to be reconciled with the database.
modeTransaction mode.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 164 of file KeyValueDB.hpp.

◆ remove()

template<class KeyT , class ValueT >
void sqlite_containers::KeyValueDB< KeyT, ValueT >::remove ( const KeyT & key)
inline

Removes a key-value pair from the database.

Parameters
keyThe key of the pair to be removed.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 217 of file KeyValueDB.hpp.

◆ retrieve_all() [1/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
ContainerT< KeyT, ValueT > sqlite_containers::KeyValueDB< KeyT, ValueT >::retrieve_all ( )
inline

Retrieves all key-value pairs.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Returns
A container with all key-value pairs.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 103 of file KeyValueDB.hpp.

◆ retrieve_all() [2/2]

template<class KeyT , class ValueT >
template<template< class... > class ContainerT>
ContainerT< KeyT, ValueT > sqlite_containers::KeyValueDB< KeyT, ValueT >::retrieve_all ( const TransactionMode & mode)
inline

Retrieves all key-value pairs with a transaction.

Template Parameters
ContainerTContainer type (e.g., std::map or std::unordered_map).
Parameters
modeTransaction mode.
Returns
A container with all key-value pairs.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 116 of file KeyValueDB.hpp.

Member Data Documentation

◆ m_stmt_clear_main

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_clear_main
private

Statement for clearing the main table.

Definition at line 235 of file KeyValueDB.hpp.

◆ m_stmt_clear_temp

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_clear_temp
private

Statement for clearing the temporary table.

Definition at line 240 of file KeyValueDB.hpp.

◆ m_stmt_count

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_count
mutableprivate

Definition at line 233 of file KeyValueDB.hpp.

◆ m_stmt_get_value

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_get_value
private

Statement for retrieving value by key from the database.

Definition at line 232 of file KeyValueDB.hpp.

◆ m_stmt_insert_temp

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_insert_temp
private

Statement for inserting data into the temporary table.

Definition at line 237 of file KeyValueDB.hpp.

◆ m_stmt_load

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_load
private

Statement for loading data from the database.

Definition at line 230 of file KeyValueDB.hpp.

◆ m_stmt_merge_temp

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_merge_temp
private

Statement for merging data from the temporary table into the main table.

Definition at line 239 of file KeyValueDB.hpp.

◆ m_stmt_purge_main

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_purge_main
private

Statement for purging stale data from the main table.

Definition at line 238 of file KeyValueDB.hpp.

◆ m_stmt_remove

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_remove
private

Statement for removing key-value pair from the database.

Definition at line 234 of file KeyValueDB.hpp.

◆ m_stmt_replace

template<class KeyT , class ValueT >
SqliteStmt sqlite_containers::KeyValueDB< KeyT, ValueT >::m_stmt_replace
private

Statement for replacing key-value pairs in the database.

Definition at line 231 of file KeyValueDB.hpp.


The documentation for this class was generated from the following file: