From: Michael Tremer Date: Thu, 21 Jan 2021 11:34:37 +0000 (+0000) Subject: libpakfire: db: Set database to WAL mode X-Git-Tag: 0.9.28~1285^2~827 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=257532904640d50d443aa08ac2b2229dbdfb0b2e;p=pakfire.git libpakfire: db: Set database to WAL mode We disable autocheckpointing since we are expected to writer rather large transactions and we would like to keep the database as compact as possible, too. That is achieved by truncating the WAL log in the end where the data is being copied into the main database and the WAL log being emptied after. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 3f36a3add..bb30cf755 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -56,14 +56,26 @@ static int pakfire_db_execute(struct pakfire_db* db, const char* stmt) { r = sqlite3_exec(db->handle, stmt, NULL, NULL, &error); } while (r == SQLITE_BUSY); - return (r == SQLITE_OK); + return r; +} + +/* + This function performs any fast optimization and tries to truncate the WAL log file + to keep the database as compact as possible on disk. +*/ +static void pakfire_db_optimize(struct pakfire_db* db) { + pakfire_db_execute(db, "PRAGMA optmize"); + pakfire_db_execute(db, "PRAGMA wal_checkpoint = TRUNCATE"); } static void pakfire_db_free(struct pakfire_db* db) { DEBUG(db->pakfire, "Releasing database at %p\n", db); - // Close database handle if (db->handle) { + // Optimize the database before it is being closed + pakfire_db_optimize(db); + + // Close database handle int r = sqlite3_close(db->handle); if (r != SQLITE_OK) { ERROR(db->pakfire, "Could not close database handle: %s\n", @@ -77,6 +89,8 @@ static void pakfire_db_free(struct pakfire_db* db) { } static int pakfire_db_setup(struct pakfire_db* db) { + int r; + // Setup logging sqlite3_config(SQLITE_CONFIG_LOG, logging_callback, db->pakfire); @@ -90,6 +104,22 @@ static int pakfire_db_setup(struct pakfire_db* db) { // Disable secure delete pakfire_db_execute(db, "PRAGMA secure_delete = OFF"); + // Set database journal to WAL + r = pakfire_db_execute(db, "PRAGMA journal_mode = WAL"); + if (r != SQLITE_OK) { + ERROR(db->pakfire, "Could not set journal mode to WAL: %s\n", + sqlite3_errmsg(db->handle)); + return 1; + } + + // Disable autocheckpoint + r = sqlite3_wal_autocheckpoint(db->handle, 0); + if (r != SQLITE_OK) { + ERROR(db->pakfire, "Could not disable autocheckpoint: %s\n", + sqlite3_errmsg(db->handle)); + return 1; + } + // XXX Create schema return 0;