From 57d6753f80fb28f085c5ef0be093002b0faa43a8 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Fri, 6 Nov 2020 10:28:15 +1100 Subject: [PATCH] Merge contents of SQLite WAL file into main database file on sync completion (#1128) * When using WAL and SHM files, certain thresholds are used to automatically determine when WAL and SHM data is committed to the main database file. If these thresholds are not met, the data is not written. Before terminating the program, commit the data, and at the end of each monitor loop, commit the data * Only try and write data to DB file and destroy variable if it was set to begin with * Only try and destroy 'onedrive' variable if set, rather than set to null --- src/itemdb.d | 7 +++++++ src/main.d | 43 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/itemdb.d b/src/itemdb.d index 11b875b0..1c929fe8 100644 --- a/src/itemdb.d +++ b/src/itemdb.d @@ -474,4 +474,11 @@ final class ItemDatabase } return items; } + + // Perform a vacuum on the database, commit WAL / SHM to file + void performVacuum() + { + auto stmt = db.prepare("VACUUM;"); + stmt.exec(); + } } diff --git a/src/main.d b/src/main.d index df6fb637..e0fe92b8 100644 --- a/src/main.d +++ b/src/main.d @@ -62,10 +62,16 @@ int main(string[] args) if (onedriveInitialised) { oneDrive.shutdown(); } - // Make sure the .wal file is incorporated into the main db before we exit - destroy(itemDb); + // was itemDb initialised? + if (itemDb !is null) { + // Make sure the .wal file is incorporated into the main db before we exit + itemDb.performVacuum(); + destroy(itemDb); + } // free API instance - oneDrive = null; + if (oneDrive !is null) { + destroy(oneDrive); + } // Perform Garbage Cleanup GC.collect(); // Display memory details @@ -83,10 +89,16 @@ int main(string[] args) if (onedriveInitialised) { oneDrive.shutdown(); } - // Make sure the .wal file is incorporated into the main db before we exit - destroy(itemDb); + // was itemDb initialised? + if (itemDb !is null) { + // Make sure the .wal file is incorporated into the main db before we exit + itemDb.performVacuum(); + destroy(itemDb); + } // free API instance - oneDrive = null; + if (oneDrive !is null) { + destroy(oneDrive); + } // Perform Garbage Cleanup GC.collect(); // Display memory details @@ -901,6 +913,10 @@ int main(string[] args) // fullScanRequired = false, for final true-up // but if we have sync_list configured, use syncListConfigured which = true performSync(sync, cfg.getValueString("single_directory"), cfg.getValueBool("download_only"), cfg.getValueBool("local_first"), cfg.getValueBool("upload_only"), LOG_NORMAL, false, syncListConfigured, displaySyncOptions, cfg.getValueBool("monitor"), m); + + // Write WAL and SHM data to file for this sync + log.vdebug("Merge contents of WAL and SHM files into main database file"); + itemDb.performVacuum(); } } @@ -1112,8 +1128,15 @@ int main(string[] args) if (displayMemoryUsage) { log.displayMemoryUsagePostGC(); } + + // Write WAL and SHM data to file for this loop + log.vdebug("Merge contents of WAL and SHM files into main database file"); + itemDb.performVacuum(); + // monitor loop complete logOutputMessage = "################################################ LOOP COMPLETE ###############################################"; + + // Handle display options if (displaySyncOptions) { log.log(logOutputMessage); } else { @@ -1419,8 +1442,12 @@ extern(C) nothrow @nogc @system void exitHandler(int value) { try { assumeNoGC ( () { log.log("Got termination signal, shutting down db connection"); - // make sure the .wal file is incorporated into the main db - destroy(itemDb); + // was itemDb initialised? + if (itemDb !is null) { + // Make sure the .wal file is incorporated into the main db before we exit + itemDb.performVacuum(); + destroy(itemDb); + } // Use exit scopes to shutdown OneDrive API })(); } catch(Exception e) {}