]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: db: Set database to WAL mode
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2021 11:34:37 +0000 (11:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2021 11:34:37 +0000 (11:34 +0000)
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 <michael.tremer@ipfire.org>
src/libpakfire/db.c

index 3f36a3add015554e53915bb06859e1a25d3e6e18..bb30cf75549da276b6825734c51b53f8182d74d0 100644 (file)
@@ -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;