aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Shares
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-09-13 19:34:57 -0500
committerJP Dillingham <jp@dillingham.me>2026-09-13 19:34:57 -0500
commitf857fa37a94bac0967f31d56b839e699b39db427 (patch)
treea5519ed4082fed48ee64300effbe1f5d51fd31d1 /src/slskd/Shares
parent5699bcf9fde961105b917350b720803c869566f2 (diff)
parent92167f6f1657703959d6ea94e4af82169c5c5fba (diff)
downloadslskd-f857fa37a94bac0967f31d56b839e699b39db427.tar.gz
Merge remote-tracking branch 'origin/master' into enumerate
Diffstat (limited to 'src/slskd/Shares')
-rw-r--r--src/slskd/Shares/IShareRepository.cs5
-rw-r--r--src/slskd/Shares/ShareScanner.cs4
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs54
3 files changed, 58 insertions, 5 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/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs
index 84ea80e3..98a3f092 100644
--- a/src/slskd/Shares/ShareScanner.cs
+++ b/src/slskd/Shares/ShareScanner.cs
@@ -403,6 +403,10 @@ namespace slskd.Shares
Log.Debug("Vacuuming...");
repository.Vacuum();
Log.Debug("Vacuumed successfully");
+
+ Log.Debug("Checkpointing...");
+ repository.Checkpoint();
+ Log.Debug("Checkpointed successfully");
}
catch (OperationCanceledException)
{
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index ed594209..6d84308a 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -84,14 +84,58 @@ namespace slskd.Shares
/// <param name="repository">The destination repository.</param>
public void BackupTo(IShareRepository repository)
{
- using var sourceConn = GetConnection(ConnectionString);
- using var backupConn = GetConnection(repository.ConnectionString);
- sourceConn.BackupDatabase(backupConn);
+ using (var sourceConn = GetConnection(ConnectionString))
+ using (var backupConn = GetConnection(repository.ConnectionString))
+ {
+ sourceConn.BackupDatabase(backupConn);
+ }
+
+ using var backupRepository = new SqliteShareRepository(repository.ConnectionString);
Log.Debug("Vacuuming backup");
- using var cmd = new SqliteCommand("VACUUM", backupConn);
- cmd.ExecuteNonQuery();
+ backupRepository.Vacuum();
Log.Debug("Backup vacuumed successfully");
+
+ Log.Debug("Checkpointing backup");
+ backupRepository.Checkpoint();
+ Log.Debug("Backup checkpointed 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>