Skip to content

Commit

Permalink
Improve database updating procedure by avoiding opening a dedicated /…
Browse files Browse the repository at this point in the history
… separate database connection for updating the query counters when we already have the disk database attached to our in-memory database under the disk.* namespace. This also brings a bit of code duplication and removal of functions called only once.

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 18, 2024
1 parent 72bfc98 commit 66141d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
30 changes: 0 additions & 30 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,17 +678,6 @@ bool db_set_FTL_property(sqlite3 *db, const enum ftl_table_props ID, const int v
return true;
}

bool db_set_FTL_property_double(sqlite3 *db, const enum ftl_table_props ID, const double value)
{
int ret = dbquery(db, "INSERT OR REPLACE INTO ftl (id, value) VALUES ( %u, %f );", ID, value);
if(ret != SQLITE_OK)
{
checkFTLDBrc(ret);
return false;
}
return true;
}

bool db_set_counter(sqlite3 *db, const enum counters_table_props ID, const int value)
{
int ret = dbquery(db, "INSERT OR REPLACE INTO counters (id, value) VALUES ( %u, %d );", ID, value);
Expand All @@ -700,25 +689,6 @@ bool db_set_counter(sqlite3 *db, const enum counters_table_props ID, const int v
return true;
}

bool db_update_counters(sqlite3 *db, const int total, const int blocked)
{
int ret = dbquery(db, "UPDATE counters SET value = value + %i WHERE id = %i;", total, DB_TOTALQUERIES);
if(ret != SQLITE_OK)
{
checkFTLDBrc(ret);
return false;
}

ret = dbquery(db, "UPDATE counters SET value = value + %i WHERE id = %i;", total, DB_TOTALQUERIES);
if(ret != SQLITE_OK)
{
checkFTLDBrc(ret);
return false;
}

return true;
}

int db_query_int(sqlite3 *db, const char* querystr)
{
log_debug(DEBUG_DATABASE, "dbquery: \"%s\"", querystr);
Expand Down
2 changes: 0 additions & 2 deletions src/database/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ int db_get_int(sqlite3* db, const enum ftl_table_props ID);
int db_get_FTL_property(sqlite3* db, const enum ftl_table_props ID);
double db_get_FTL_property_double(sqlite3* db, const enum ftl_table_props ID);
bool db_set_FTL_property(sqlite3* db, const enum ftl_table_props ID, const int value);
bool db_set_FTL_property_double(sqlite3* db, const enum ftl_table_props ID, const double value);

/// Execute a formatted SQL query and get the return code
int dbquery(sqlite3* db, const char *format, ...) __attribute__ ((format (printf, 2, 3)));;
Expand All @@ -53,7 +52,6 @@ int db_query_int_from_until_type(sqlite3 *db, const char* querystr, const double

void SQLite3LogCallback(void *pArg, int iErrCode, const char *zMsg);
bool db_set_counter(sqlite3 *db, const enum counters_table_props ID, const int value);
bool db_update_counters(sqlite3 *db, const int total, const int blocked);
const char *get_sqlite3_version(void);

extern bool DBdeleteoldqueries;
Expand Down
22 changes: 14 additions & 8 deletions src/database/query-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ bool export_queries_to_disk(bool final)
// Finalize statement
sqlite3_finalize(stmt);


// Update last_disk_db_idx
// Prepare SQLite3 statement
log_debug(DEBUG_DATABASE, "Accessing in-memory database");
Expand Down Expand Up @@ -691,15 +690,22 @@ bool export_queries_to_disk(bool final)
// All temp queries were stored to disk, update the IDs
last_disk_db_idx += insertions;

/*
* If there are any insertions, we:
* 1. Insert (or replace) the last timestamp into the `disk.ftl` table.
* 2. Update the total queries counter in the `disk.counters` table.
* 3. Update the blocked queries counter in the `disk.counters` table.
*/
if(insertions > 0)
{
sqlite3 *db = dbopen(false, false);
if(db != NULL)
{
db_set_FTL_property_double(db, DB_LASTTIMESTAMP, new_last_timestamp);
db_update_counters(db, new_total, new_blocked);
dbclose(&db);
}
if((rc = dbquery(memdb, "INSERT OR REPLACE INTO disk.ftl (id, value) VALUES ( %i, %f );", DB_LASTTIMESTAMP, new_last_timestamp)) != SQLITE_OK)
log_err("export_queries_to_disk(): Cannot update timestamp: %s", sqlite3_errstr(rc));

if((rc = dbquery(memdb, "UPDATE disk.counters SET value = value + %u WHERE id = %i;", new_total, DB_TOTALQUERIES)) != SQLITE_OK)
log_err("export_queries_to_disk(): Cannot update total queries counter: %s", sqlite3_errstr(rc));

if((rc = dbquery(memdb, "UPDATE disk.counters SET value = value + %u WHERE id = %i;", new_blocked, DB_BLOCKEDQUERIES)) != SQLITE_OK)
log_err("export_queries_to_disk(): Cannot update blocked queries counter: %s", sqlite3_errstr(rc));
}

log_debug(DEBUG_DATABASE, "Exported %u rows for disk.query_storage (took %.1f ms, last SQLite ID %lu)",
Expand Down

0 comments on commit 66141d8

Please sign in to comment.