40 template<template <class...> class ContainerT>
41 KeyDB& operator=(const ContainerT<KeyT>& container) {
56 template<
template <
class...>
class ContainerT = std::set>
58 ContainerT<KeyT> container;
74 template<
template <
class...>
class ContainerT>
75 void load(ContainerT<KeyT>& container) {
85 template<
template <
class...>
class ContainerT>
87 ContainerT<KeyT>& container,
97 }
catch(
const std::exception &e) {
110 template<
template <
class...>
class ContainerT>
112 ContainerT<KeyT> container;
124 template<
template <
class...>
class ContainerT>
126 ContainerT<KeyT> container;
136 }
catch(
const std::exception &e) {
151 template<
template <
class...>
class ContainerT>
152 void append(
const ContainerT<KeyT>& container) {
162 template<
template <
class...>
class ContainerT>
172 }
catch(
const std::exception &e) {
185 template<
template <
class...>
class ContainerT>
196 template<
template <
class...>
class ContainerT>
198 const ContainerT<KeyT>& container,
268 const std::string table_name = config.table_name.empty() ?
"key_store" : config.table_name;
269 const std::string temp_table_name = config.table_name.empty() ?
"key_temp_store" : config.table_name +
"_temp";
272 const std::string create_table_sql =
273 "CREATE TABLE IF NOT EXISTS " + table_name +
" ("
278 const std::string create_temp_table_sql =
279 "CREATE TEMPORARY TABLE IF NOT EXISTS " + temp_table_name +
" ("
302 template<
template <
class...>
class ContainerT>
311 if (err == SQLITE_DONE) {
315 if (err == SQLITE_BUSY) {
322 std::string err_msg =
"SQLite error: ";
323 err_msg += std::to_string(err);
330 std::current_exception(),
332 "Unknown error occurred while loading data from database.");
341 bool is_found =
false;
349 if (err == SQLITE_DONE) {
354 if (err == SQLITE_BUSY) {
362 std::string err_msg =
"SQLite error: ";
363 err_msg += std::to_string(err);
370 std::current_exception(),
372 "Unknown error occurred while retrieving value for the provided key.");
381 std::size_t
count = 0;
388 if (err == SQLITE_DONE) {
392 if (err == SQLITE_BUSY) {
399 std::string err_msg =
"SQLite error: ";
400 err_msg += std::to_string(err);
407 std::current_exception(),
409 "Unknown error occurred while counting keys in the database.");
420 template<
template <
class...>
class ContainerT>
428 for (
const auto& item : container) {
448 std::current_exception(), {
452 "Unknown error occurred while reconciling data.");
460 template<
template <
class...>
class ContainerT>
463 for (
const auto& item : container) {
471 std::current_exception(),
473 "Unknown error occurred while appending key-value pairs to the database.");
488 std::current_exception(),
490 "Unknown error occurred while inserting key-value pair into the database.");
505 std::current_exception(),
507 "Unknown error occurred while removing key.");
519 std::current_exception(),
521 "Unknown error occurred while clearing the database tables.");
Base class for SQLite database management in sqlite_containers.
#define SQLITE_CONTAINERS_BUSY_RETRY_DELAY_MS
Base class for SQLite database management.
Config get_config() const
Gets the current configuration of the database.
void db_rollback()
Rolls back the current transaction.
void db_commit()
Commits the current transaction.
void execute_in_transaction(Func operation, const TransactionMode &mode)
Executes an operation inside a transaction.
void db_begin(const TransactionMode &mode=TransactionMode::DEFERRED)
Begins a transaction with the given mode.
void set_config(const Config &config)
Sets the configuration for the database.
std::mutex m_sqlite_mutex
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.
Configuration class for SQLite database settings.
TransactionMode default_txn_mode
Template class for managing keys in a SQLite database.
SqliteStmt m_stmt_merge_temp
Statement for merging data from the temporary table into the main table.
ContainerT< KeyT > operator()()
Loads all keys from the database into a container (e.g., std::set, std::unordered_set,...
SqliteStmt m_stmt_purge_main
Statement for purging stale data from the main table.
SqliteStmt m_stmt_clear
Statement for clearing the table.
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 db_load(ContainerT< KeyT > &container)
Loads data from the database into the container.
bool find(const KeyT &key)
Finds if a key exists in the database.
void reconcile(const ContainerT< KeyT > &container)
Reconciles the database with the container.
ContainerT< KeyT > retrieve_all()
Retrieves all keys from the database.
SqliteStmt m_stmt_insert_temp
Statement for inserting data into the temporary table.
void db_reconcile(const ContainerT< KeyT > &container)
Reconciles the content of the database with the container. Synchronizes the main table with the conte...
void load(ContainerT< KeyT > &container)
Loads data from the database into the container.
void db_remove(const KeyT &key)
Removes a key from the database.
std::size_t db_count() const
Returns the total number of keys stored in the database.
void append(const ContainerT< KeyT > &container, const TransactionMode &mode)
Appends the content of the container to the database with a transaction.
void db_append(const ContainerT< KeyT > &container)
Appends the content of the container to the database.
void reconcile(const ContainerT< KeyT > &container, const TransactionMode &mode)
Reconciles the database with the container using a transaction.
SqliteStmt m_stmt_load
Statement for loading data from the database.
SqliteStmt m_stmt_clear_temp
Statement for clearing the temporary table.
void db_clear()
Clears all keys from the database.
void db_create_table(const Config &config) override final
Creates the table in the database.
void append(const ContainerT< KeyT > &container)
Appends the content of the container to the database.
KeyDB()
Default constructor.
void insert(const KeyT &key)
Inserts a key into the database.
SqliteStmt m_stmt_remove
Statement for removing a key.
void load(ContainerT< KeyT > &container, const TransactionMode &mode)
Loads data from the database into the container with a transaction.
ContainerT< KeyT > retrieve_all(const TransactionMode &mode)
Retrieves all keys from the database with a transaction.
void db_insert(const KeyT &key)
Inserts a key into the database.
bool db_find(const KeyT &key)
Finds if a key exists in the database.
SqliteStmt m_stmt_count
Statement for counting the number of keys in the database.
SqliteStmt m_stmt_find
Statement for finding a key.
void clear()
Clears all keys from the database.
KeyDB(const Config &config)
Constructor with configuration.
~KeyDB() override final=default
Destructor.
void remove(const KeyT &key)
Removes a key from the database.
SqliteStmt m_stmt_replace
Statement for replacing key-value pairs in the database.
Class for managing SQLite prepared statements.
void clear_bindings()
Clears all bindings on the prepared statement.
void execute(sqlite3 *sqlite_db)
Executes the prepared statement.
T extract_column(const int &index, typename std::enable_if< std::is_integral< T >::value >::type *=0)
Extracts a value from a SQLite statement column.
void reset()
Resets the prepared statement.
void init(sqlite3 *sqlite_db, const char *query)
Initializes the statement.
bool bind_value(const int &index, const T &value, typename std::enable_if< std::is_integral< T >::value >::type *=0)
Binds a value to a SQLite statement.
Exception class for SQLite errors.
void execute(sqlite3_stmt *stmt)
Executes a SQLite statement.
std::enable_if< std::is_trivially_copyable< typenameT::value_type >::value &&std::is_same< T, std::vector< typenameT::value_type > >::value, std::string >::type get_sqlite_type()
TransactionMode
Defines SQLite transaction modes.
void add_value(ContainerT< T > &container, T &value, typename std::enable_if< std::is_same< ContainerT< T >, std::set< T > >::value||std::is_same< ContainerT< T >, std::unordered_set< T > >::value >::type *=0)
Adds a value to a container (set or unordered_set).