aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-09-13 02:20:19 -0500
committerJP Dillingham <jp@dillingham.me>2026-09-13 02:20:19 -0500
commitfab938740106725aa8486eb4503cf91cfe8d81e2 (patch)
treedab1b7af672e89e24e72cdc6b1cb2fd95ff55859
parentf71b893e6b625b12906537166b332d550ed5f115 (diff)
downloadslskd-fab938740106725aa8486eb4503cf91cfe8d81e2.tar.gz
add Checkpoint()
-rw-r--r--src/slskd/Shares/IShareRepository.cs5
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs36
2 files changed, 41 insertions, 0 deletions
diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs
index 3b57c16f..e9799663 100644
--- a/src/slskd/Shares/IShareRepository.cs
+++ b/src/slskd/Shares/IShareRepository.cs
@@ -53,6 +53,11 @@ namespace slskd.Shares
void BackupTo(IShareRepository repository);
/// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ void Checkpoint();
+
+ /// <summary>
/// Counts the number of directories in the database.
/// </summary>
/// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param>
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index ed594209..a4be81b4 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -92,6 +92,42 @@ namespace slskd.Shares
using var cmd = new SqliteCommand("VACUUM", backupConn);
cmd.ExecuteNonQuery();
Log.Debug("Backup vacuumed successfully");
+
+ /// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ public void Checkpoint()
+ {
+ const int maxAttempts = 3;
+
+ using var conn = GetConnection();
+
+ for (var attempt = 1; attempt <= maxAttempts; attempt++)
+ {
+ using var cmd = new SqliteCommand("PRAGMA wal_checkpoint(TRUNCATE);", conn);
+ using var reader = cmd.ExecuteReader();
+
+ reader.Read();
+
+ var busy = reader.GetInt32(0); // another operation prevented the checkpoint (true/false)
+ var log = reader.GetInt32(1); // number of frames in the WAL at the start
+ var checkpointed = reader.GetInt32(2); // number of frames moved from the WAL to the db
+
+ if (busy == 0 && checkpointed == log)
+ {
+ Log.Debug("WAL checkpoint complete; {Log} frames in log, {Checkpointed} checkpointed", log, checkpointed);
+ return;
+ }
+
+ Log.Warning("WAL checkpoint attempt {Attempt} of {MaxAttempts} did not fully complete; {Log} frames in log, {Checkpointed} checkpointed", attempt, maxAttempts, log, checkpointed);
+
+ if (attempt < maxAttempts)
+ {
+ System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
+ }
+ }
+
+ throw new TimeoutException($"Failed to fully checkpoint the shares database after {maxAttempts} attempts");
}
/// <summary>