@@ -368,7 +368,7 @@ static bool get_memdb_size(sqlite3 *db, size_t *memsize, int *queries)
368368 * memsize = page_count * page_size ;
369369
370370 // Get number of queries in the memory table
371- if ((* queries = get_number_of_queries_in_DB (db , "query_storage" )) == DB_FAILED )
371+ if ((* queries = get_number_of_queries_in_DB (db , "query_storage" , NULL )) == DB_FAILED )
372372 return false;
373373
374374 return true;
@@ -506,21 +506,29 @@ bool detach_database(sqlite3* db, const char **message, const char *alias)
506506
507507// Get number of queries either in the temp or in the on-diks database
508508// This routine is used by the API routines.
509- int get_number_of_queries_in_DB (sqlite3 * db , const char * tablename )
509+ int get_number_of_queries_in_DB (sqlite3 * db , const char * tablename , double * earliest_timestamp )
510510{
511511 int rc = 0 , num = 0 ;
512512 sqlite3_stmt * stmt = NULL ;
513513
514- // Count number of rows
515- const size_t buflen = 42 + strlen (tablename );
516- char * querystr = calloc (buflen , sizeof (char ));
517- snprintf (querystr , buflen , "SELECT COUNT(*) FROM %s" , tablename );
518-
519514 // The database pointer may be NULL, meaning we want the memdb
520515 if (db == NULL )
521516 db = get_memdb ();
522517
523- // PRAGMA page_size
518+ // Build query string based on whether we need the earliest timestamp too
519+ const size_t buflen = 37 + strlen (tablename );
520+ char * querystr = calloc (buflen , sizeof (char ));
521+ if (earliest_timestamp != NULL )
522+ {
523+ // Get both count and earliest timestamp
524+ snprintf (querystr , buflen , "SELECT COUNT(*), MIN(timestamp) FROM %s" , tablename );
525+ }
526+ else
527+ {
528+ // Get only count
529+ snprintf (querystr , buflen , "SELECT COUNT(*) FROM %s" , tablename );
530+ }
531+
524532 rc = sqlite3_prepare_v2 (db , querystr , -1 , & stmt , NULL );
525533 if ( rc != SQLITE_OK )
526534 {
@@ -532,7 +540,13 @@ int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename)
532540 }
533541 rc = sqlite3_step (stmt );
534542 if ( rc == SQLITE_ROW )
543+ {
544+ // Get count from first column
535545 num = sqlite3_column_int (stmt , 0 );
546+ // Get timestamp from second column if requested
547+ if (earliest_timestamp != NULL && sqlite3_column_type (stmt , 1 ) != SQLITE_NULL )
548+ * earliest_timestamp = sqlite3_column_double (stmt , 1 );
549+ }
536550 sqlite3_finalize (stmt );
537551 free (querystr );
538552
@@ -617,8 +631,8 @@ bool import_queries_from_disk(void)
617631 }
618632
619633 // Get number of queries on disk before detaching
620- disk_db_num = get_number_of_queries_in_DB (memdb , "disk.query_storage" );
621- mem_db_num = get_number_of_queries_in_DB (memdb , "query_storage" );
634+ disk_db_num = get_number_of_queries_in_DB (memdb , "disk.query_storage" , NULL );
635+ mem_db_num = get_number_of_queries_in_DB (memdb , "query_storage" , NULL );
622636
623637 log_info ("Imported %u queries from the on-disk database (it has %u rows)" , mem_db_num , disk_db_num );
624638
@@ -710,7 +724,7 @@ bool export_queries_to_disk(const bool final)
710724 }
711725
712726 // Update number of queries in the disk database
713- disk_db_num = get_number_of_queries_in_DB (memdb , "disk.query_storage" );
727+ disk_db_num = get_number_of_queries_in_DB (memdb , "disk.query_storage" , NULL );
714728 }
715729
716730 // Export linking tables and current AUTOINCREMENT values to the disk database
@@ -778,7 +792,7 @@ bool delete_old_queries_from_db(const bool use_memdb, const double mintime)
778792 mintime , sqlite3_errstr (rc ));
779793
780794 // Update number of queries in in-memory database
781- const int new_num = get_number_of_queries_in_DB (NULL , "query_storage" );
795+ const int new_num = get_number_of_queries_in_DB (NULL , "query_storage" , NULL );
782796 log_debug (DEBUG_GC , "delete_old_queries_from_db(): Deleted %i (%u) queries, new number of queries in memory: %i" ,
783797 sqlite3_changes (db ), (mem_db_num - new_num ), new_num );
784798 mem_db_num = new_num ;
@@ -1738,7 +1752,7 @@ bool queries_to_database(void)
17381752 }
17391753
17401754 // Update number of queries in in-memory database
1741- mem_db_num = get_number_of_queries_in_DB (NULL , "query_storage" );
1755+ mem_db_num = get_number_of_queries_in_DB (NULL , "query_storage" , NULL );
17421756
17431757 if (config .debug .database .v .b && updated + added > 0 )
17441758 {
0 commit comments