21 template<
class KeyT,
class ValueT>
44 template<template <class...> class ContainerT>
45 KeyValueDB& operator=(const ContainerT<KeyT, ValueT>& container) {
60 template<
template <
class...>
class ContainerT = std::map>
62 ContainerT<KeyT, ValueT> container;
78 template<
template <
class...>
class ContainerT>
79 void load(ContainerT<KeyT, ValueT>& container) {
89 template<
template <
class...>
class ContainerT>
91 ContainerT<KeyT, ValueT>& container,
102 template<
template <
class...>
class ContainerT>
104 ContainerT<KeyT, ValueT> container;
115 template<
template <
class...>
class ContainerT>
117 ContainerT<KeyT, ValueT> container;
128 template<
template <
class...>
class ContainerT>
129 void append(
const ContainerT<KeyT, ValueT>& container) {
139 template<
template <
class...>
class ContainerT>
141 const ContainerT<KeyT, ValueT>& container,
152 template<
template <
class...>
class ContainerT>
153 void reconcile(
const ContainerT<KeyT, ValueT>& container) {
163 template<
template <
class...>
class ContainerT>
165 const ContainerT<KeyT, ValueT>& container,
176 void insert(
const KeyT &key,
const ValueT &value) {
184 void insert(
const std::pair<KeyT, ValueT> &pair) {
194 bool find(
const KeyT &key, ValueT &value) {
246 const std::string table_name = config.table_name.empty() ?
"kv_store" : config.table_name;
247 const std::string temp_table_name = config.table_name.empty() ?
"kv_temp_store" : config.table_name +
"_temp";
250 const std::string create_table_sql =
251 "CREATE TABLE IF NOT EXISTS " + table_name +
" ("
257 const std::string create_temp_table_sql =
258 "CREATE TEMPORARY TABLE IF NOT EXISTS " + temp_table_name +
" ("
282 template<
template <
class...>
class ContainerT>
283 void db_load(ContainerT<KeyT, ValueT>& container) {
290 container.emplace(std::move(key), std::move(value));
292 if (err == SQLITE_DONE) {
296 if (err == SQLITE_BUSY) {
303 std::string err_msg =
"SQLite error: ";
304 err_msg += std::to_string(err);
311 std::current_exception(),
313 "Unknown error occurred while loading data from database.");
322 bool db_find(
const KeyT& key, ValueT& value) {
323 bool is_found =
false;
332 if (err == SQLITE_DONE) {
337 if (err == SQLITE_BUSY) {
345 std::string err_msg =
"SQLite error: ";
346 err_msg += std::to_string(err);
353 std::current_exception(),
355 "Unknown error occurred while retrieving value for the provided key.");
364 std::size_t
count = 0;
371 if (err == SQLITE_DONE) {
375 if (err == SQLITE_BUSY) {
382 std::string err_msg =
"SQLite error: ";
383 err_msg += std::to_string(err);
390 std::current_exception(),
392 "Unknown error occurred while counting key-value pairs in the database.");
401 template<
template <
class...>
class ContainerT>
402 void db_append(
const ContainerT<KeyT, ValueT>& container) {
404 for (
const auto& pair : container) {
413 std::current_exception(),
415 "Unknown error occurred while appending key-value pairs to the database.");
425 template<
template <
class...>
class ContainerT>
433 for (
const auto& pair : container) {
455 std::current_exception(), {
459 "Unknown error occurred while reconciling data.");
476 std::current_exception(),
478 "Unknown error occurred while inserting key-value pair into the database.");
493 std::current_exception(),
495 "Unknown error occurred while removing key.");
507 std::current_exception(),
509 "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 execute_in_transaction(Func operation, const TransactionMode &mode)
Executes an operation inside a transaction.
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 key-value pairs in a SQLite database.
SqliteStmt m_stmt_purge_main
Statement for purging stale data from the main table.
void db_load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
KeyValueDB()
Default constructor.
void db_reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the content of the database with the container. Synchronizes the main table with the conte...
std::size_t count() const
Returns the number of elements in the database.
SqliteStmt m_stmt_replace
Statement for replacing key-value pairs in the database.
SqliteStmt m_stmt_clear_main
Statement for clearing the main table.
SqliteStmt m_stmt_load
Statement for loading data from the database.
void db_insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
void clear()
Clears all key-value pairs from the database.
std::size_t db_count() const
Returns the number of elements in the database.
void remove(const KeyT &key)
Removes a key-value pair from the database.
bool db_find(const KeyT &key, ValueT &value)
Finds a value by key in the database.
SqliteStmt m_stmt_clear_temp
Statement for clearing the temporary table.
void reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the database with the container.
void insert(const std::pair< KeyT, ValueT > &pair)
Inserts a key-value pair into the database.
void insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
SqliteStmt m_stmt_remove
Statement for removing key-value pair from the database.
~KeyValueDB() override final=default
Destructor.
void db_remove(const KeyT &key)
Removes a key-value pair from the database.
ContainerT< KeyT, ValueT > retrieve_all()
Retrieves all key-value pairs.
bool empty() const
Checks if the database is empty.
bool find(const KeyT &key, ValueT &value)
Finds a value by key.
KeyValueDB(const Config &config)
Constructor with configuration.
void append(const ContainerT< KeyT, ValueT > &container)
Appends data to the database.
void load(ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Loads data with a transaction.
ContainerT< KeyT, ValueT > operator()()
Loads all key-value pairs from the database into a container (e.g., std::map or std::unordered_map).
SqliteStmt m_stmt_insert_temp
Statement for inserting data into the temporary table.
SqliteStmt m_stmt_merge_temp
Statement for merging data from the temporary table into the main table.
void db_clear()
Clears all key-value pairs from the database.
void append(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Appends data with a transaction.
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 ta...
SqliteStmt m_stmt_get_value
Statement for retrieving value by key from the database.
ContainerT< KeyT, ValueT > retrieve_all(const TransactionMode &mode)
Retrieves all key-value pairs with a transaction.
void reconcile(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Reconciles the database with the container using a transaction.
void db_append(const ContainerT< KeyT, ValueT > &container)
Appends the content of the container to the database.
void load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
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.