SQLite Containers
Loading...
Searching...
No Matches
sqlite_containers::KeyDB< KeyT > Class Template Referencefinal

Template class for managing keys in a SQLite database. More...

#include <KeyDB.hpp>

Inheritance diagram for sqlite_containers::KeyDB< KeyT >:
sqlite_containers::BaseDB

Public Member Functions

 KeyDB ()
 Default constructor.
 
 KeyDB (const Config &config)
 Constructor with configuration.
 
 ~KeyDB () override final=default
 Destructor.
 
template<template< class... > class ContainerT>
KeyDBoperator= (const ContainerT< KeyT > &container)
 Assigns a container (e.g., std::set, std::unordered_set, std::vector, or std::list) to the database.
 
template<template< class... > class ContainerT = std::set>
ContainerT< KeyT > operator() ()
 Loads all keys from the database into a container (e.g., std::set, std::unordered_set, std::vector, or std::list).
 
template<template< class... > class ContainerT>
void load (ContainerT< KeyT > &container)
 Loads data from the database into the container.
 
template<template< class... > class ContainerT>
void load (ContainerT< KeyT > &container, const TransactionMode &mode)
 Loads data from the database into the container with a transaction.
 
template<template< class... > class ContainerT>
ContainerT< KeyT > retrieve_all ()
 Retrieves all keys from the database.
 
template<template< class... > class ContainerT>
ContainerT< KeyT > retrieve_all (const TransactionMode &mode)
 Retrieves all keys from the database with a transaction.
 
template<template< class... > class ContainerT>
void append (const ContainerT< KeyT > &container)
 Appends the content of the container to the database.
 
template<template< class... > class ContainerT>
void append (const ContainerT< KeyT > &container, const TransactionMode &mode)
 Appends the content of the container to the database with a transaction.
 
template<template< class... > class ContainerT>
void reconcile (const ContainerT< KeyT > &container)
 Reconciles the database with the container.
 
template<template< class... > class ContainerT>
void reconcile (const ContainerT< KeyT > &container, const TransactionMode &mode)
 Reconciles the database with the container using a transaction.
 
void insert (const KeyT &key)
 Inserts a key into the database.
 
bool find (const KeyT &key)
 Finds if a key exists in the database.
 
std::size_t count () const
 Returns the number of keys in the database.
 
bool empty () const
 Checks if the database is empty (no keys present).
 
void remove (const KeyT &key)
 Removes a key from the database.
 
void clear ()
 Clears all keys 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 table in the database.
 
template<template< class... > class ContainerT>
void db_load (ContainerT< KeyT > &container)
 Loads data from the database into the container.
 
bool db_find (const KeyT &key)
 Finds if a key exists in the database.
 
std::size_t db_count () const
 Returns the total number of keys stored in the database.
 
template<template< class... > class ContainerT>
void db_reconcile (const ContainerT< KeyT > &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.
 
template<template< class... > class ContainerT>
void db_append (const ContainerT< KeyT > &container)
 Appends the content of the container to the database.
 
void db_insert (const KeyT &key)
 Inserts a key into the database.
 
void db_remove (const KeyT &key)
 Removes a key from the database.
 
void db_clear ()
 Clears all keys 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_find
 Statement for finding a key.
 
SqliteStmt m_stmt_count
 Statement for counting the number of keys in the database.
 
SqliteStmt m_stmt_remove
 Statement for removing a key.
 
SqliteStmt m_stmt_clear
 Statement for clearing the 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 sqlite_containers::KeyDB< KeyT >

Template class for managing keys in a SQLite database.

Template Parameters
KeyTType of the keys.

This class supports various container types, including std::set, std::unordered_set, std::vector, and std::list. The class allows insertion, retrieval, and removal of keys from a SQLite database while maintaining transactional integrity.

Definition at line 17 of file KeyDB.hpp.

Constructor & Destructor Documentation

◆ KeyDB() [1/2]

template<class KeyT >
sqlite_containers::KeyDB< KeyT >::KeyDB ( )
inline

Default constructor.

Definition at line 21 of file KeyDB.hpp.

◆ KeyDB() [2/2]

template<class KeyT >
sqlite_containers::KeyDB< KeyT >::KeyDB ( const Config & config)
inline

Constructor with configuration.

Parameters
configConfiguration settings for the database.

Definition at line 25 of file KeyDB.hpp.

◆ ~KeyDB()

template<class KeyT >
sqlite_containers::KeyDB< KeyT >::~KeyDB ( )
finaloverridedefault

Destructor.

Member Function Documentation

◆ append() [1/2]

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

Appends the content of the container to the database.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
containerContainer with content to be synchronized to the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 152 of file KeyDB.hpp.

◆ append() [2/2]

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

Appends the content of the container to the database with a transaction.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
containerContainer with content to be synchronized to the database.
modeTransaction mode.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 163 of file KeyDB.hpp.

◆ clear()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::clear ( )
inline

Clears all keys from the database.

Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 247 of file KeyDB.hpp.

◆ count()

template<class KeyT >
std::size_t sqlite_containers::KeyDB< KeyT >::count ( ) const
inline

Returns the number of keys in the database.

Returns
The number of keys in the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 225 of file KeyDB.hpp.

◆ db_append()

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

Appends the content of the container to the database.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
containerContainer with content to be synchronized to the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 461 of file KeyDB.hpp.

◆ db_clear()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::db_clear ( )
inlineprivate

Clears all keys from the database.

Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 513 of file KeyDB.hpp.

◆ db_count()

template<class KeyT >
std::size_t sqlite_containers::KeyDB< KeyT >::db_count ( ) const
inlineprivate

Returns the total number of keys stored in the database.

Returns
The number of keys stored in the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 380 of file KeyDB.hpp.

◆ db_create_table()

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

Creates the table in the database.

Parameters
configConfiguration settings.

Implements sqlite_containers::BaseDB.

Definition at line 267 of file KeyDB.hpp.

◆ db_find()

template<class KeyT >
bool sqlite_containers::KeyDB< KeyT >::db_find ( const KeyT & key)
inlineprivate

Finds if a key exists in the database.

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

Definition at line 340 of file KeyDB.hpp.

◆ db_insert()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::db_insert ( const KeyT & key)
inlineprivate

Inserts a key into the database.

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

Definition at line 480 of file KeyDB.hpp.

◆ db_load()

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

Loads data from the database into the container.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
setContainer to be synchronized with database content.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 303 of file KeyDB.hpp.

◆ db_reconcile()

template<class KeyT >
template<template< class... > class ContainerT>
void sqlite_containers::KeyDB< KeyT >::db_reconcile ( const ContainerT< KeyT > & 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 (e.g., std::set, std::unordered_set, std::vector, or std::list).
Parameters
containerContainer with keys to be reconciled with the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 421 of file KeyDB.hpp.

◆ db_remove()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::db_remove ( const KeyT & key)
inlineprivate

Removes a key from the database.

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

Definition at line 497 of file KeyDB.hpp.

◆ empty()

template<class KeyT >
bool sqlite_containers::KeyDB< KeyT >::empty ( ) const
inline

Checks if the database is empty (no keys present).

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

Definition at line 233 of file KeyDB.hpp.

◆ find()

template<class KeyT >
bool sqlite_containers::KeyDB< KeyT >::find ( const KeyT & key)
inline

Finds if a key exists in the database.

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

Definition at line 217 of file KeyDB.hpp.

◆ insert()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::insert ( const KeyT & key)
inline

Inserts a key into the database.

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

Definition at line 208 of file KeyDB.hpp.

◆ load() [1/2]

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

Loads data from the database into the container.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
containerContainer to be synchronized with database content.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 75 of file KeyDB.hpp.

◆ load() [2/2]

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

Loads data from the database into the container with a transaction.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
containerContainer to be synchronized with database content.
modeTransaction mode.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 86 of file KeyDB.hpp.

◆ operator()()

template<class KeyT >
template<template< class... > class ContainerT = std::set>
ContainerT< KeyT > sqlite_containers::KeyDB< KeyT >::operator() ( )
inline

Loads all keys from the database into a container (e.g., std::set, std::unordered_set, std::vector, or std::list).

Template Parameters
ContainerTThe type of the container (e.g., std::set, std::unordered_set, std::vector, or std::list).
Returns
A container populated with all keys from the database.
Exceptions
sqlite_exceptionif an SQLite error occurs.
Note
The transaction mode is taken from the database configuration.

Definition at line 57 of file KeyDB.hpp.

◆ operator=()

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

Assigns a container (e.g., std::set, std::unordered_set, std::vector, or std::list) to the database.

Template Parameters
ContainerTThe type of the container (e.g., std::set, std::unordered_set).
Parameters
containerThe container with keys to be inserted into the database.
Returns
Reference to this KeyDB.
Exceptions
sqlite_exceptionif an SQLite error occurs.
Note
The transaction mode is taken from the database configuration.

Definition at line 41 of file KeyDB.hpp.

◆ reconcile() [1/2]

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

Reconciles the database with the container.

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

Definition at line 186 of file KeyDB.hpp.

◆ reconcile() [2/2]

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

Reconciles the database with the container using a transaction.

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

Definition at line 197 of file KeyDB.hpp.

◆ remove()

template<class KeyT >
void sqlite_containers::KeyDB< KeyT >::remove ( const KeyT & key)
inline

Removes a key from the database.

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

Definition at line 240 of file KeyDB.hpp.

◆ retrieve_all() [1/2]

template<class KeyT >
template<template< class... > class ContainerT>
ContainerT< KeyT > sqlite_containers::KeyDB< KeyT >::retrieve_all ( )
inline

Retrieves all keys from the database.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Returns
A container with all keys.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 111 of file KeyDB.hpp.

◆ retrieve_all() [2/2]

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

Retrieves all keys from the database with a transaction.

Template Parameters
ContainerTTemplate for the container type (vector, deque, list, set or unordered_set).
Parameters
modeTransaction mode.
Returns
A container with all keys.
Exceptions
sqlite_exceptionif an SQLite error occurs.

Definition at line 125 of file KeyDB.hpp.

Member Data Documentation

◆ m_stmt_clear

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_clear
private

Statement for clearing the table.

Definition at line 258 of file KeyDB.hpp.

◆ m_stmt_clear_temp

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_clear_temp
private

Statement for clearing the temporary table.

Definition at line 263 of file KeyDB.hpp.

◆ m_stmt_count

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_count
mutableprivate

Statement for counting the number of keys in the database.

Definition at line 256 of file KeyDB.hpp.

◆ m_stmt_find

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_find
private

Statement for finding a key.

Definition at line 255 of file KeyDB.hpp.

◆ m_stmt_insert_temp

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_insert_temp
private

Statement for inserting data into the temporary table.

Definition at line 260 of file KeyDB.hpp.

◆ m_stmt_load

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_load
private

Statement for loading data from the database.

Definition at line 253 of file KeyDB.hpp.

◆ m_stmt_merge_temp

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_merge_temp
private

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

Definition at line 262 of file KeyDB.hpp.

◆ m_stmt_purge_main

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_purge_main
private

Statement for purging stale data from the main table.

Definition at line 261 of file KeyDB.hpp.

◆ m_stmt_remove

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_remove
private

Statement for removing a key.

Definition at line 257 of file KeyDB.hpp.

◆ m_stmt_replace

template<class KeyT >
SqliteStmt sqlite_containers::KeyDB< KeyT >::m_stmt_replace
private

Statement for replacing key-value pairs in the database.

Definition at line 254 of file KeyDB.hpp.


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