diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-09-10 17:39:44 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-09-10 17:39:44 -0500 |
| commit | 7c4922e890bf0267ceb55ff568d03b0485edbd4d (patch) | |
| tree | a885fb0bb67319b7c33f5483d06748b94992e32a /src/slskd/Shares/SharedFileCache.cs | |
| parent | cad5239f816bc3f116a6ad8d3dea9e1e3f831a65 (diff) | |
| download | slskd-7c4922e890bf0267ceb55ff568d03b0485edbd4d.tar.gz | |
backup and restore share cache from disk
Diffstat (limited to 'src/slskd/Shares/SharedFileCache.cs')
| -rw-r--r-- | src/slskd/Shares/SharedFileCache.cs | 113 |
1 files changed, 80 insertions, 33 deletions
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index c7c1d30a..2c14024e 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -22,6 +22,7 @@ namespace slskd.Shares using System; using System.Collections.Concurrent; using System.Collections.Generic; + using System.Data; using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; @@ -39,9 +40,11 @@ namespace slskd.Shares /// <summary> /// Initializes a new instance of the <see cref="SharedFileCache"/> class. /// </summary> + /// <param name="storageMode"></param> /// <param name="soulseekFileFactory"></param> - public SharedFileCache(ISoulseekFileFactory soulseekFileFactory = null) + public SharedFileCache(StorageMode storageMode, ISoulseekFileFactory soulseekFileFactory = null) { + StorageMode = storageMode; SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); } @@ -50,6 +53,7 @@ namespace slskd.Shares /// </summary> public IStateMonitor<SharedFileCacheState> StateMonitor => State; + private StorageMode StorageMode { get; } private ILogger Log { get; } = Serilog.Log.ForContext<SharedFileCache>(); private List<Share> Shares { get; set; } private ISoulseekFileFactory SoulseekFileFactory { get; } @@ -144,19 +148,24 @@ namespace slskd.Shares try { + // drop and recreate the tables to clear them + // note: leave the backup in place. this will leave the app in a better position if the app stops during the scan. + CreateTables(); + State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0, + Directories = 0, + Files = 0, + ExcludedDirectories = 0, }); Log.Debug("Starting shared file scan"); await Task.Yield(); - ResetCache(); - var sw = new Stopwatch(); var swSnapshot = 0L; sw.Start(); @@ -301,6 +310,10 @@ namespace slskd.Shares Log.Debug("Inserted {Files} records in {Elapsed}ms", cached, sw.ElapsedMilliseconds - swSnapshot); + Log.Debug("Backing up shared file cache database..."); + Backup(); + Log.Debug("Shared file cache database backup complete"); + State.SetValue(state => state with { Filling = false, @@ -437,24 +450,48 @@ namespace slskd.Shares { try { - if (TableExists("directories") && TableExists("filenames") && TableExists("files") && TableExists("exclusions")) + // see if we need to 'restore' the database from disk + if (StorageMode == StorageMode.Memory || (StorageMode == StorageMode.Disk && !DatabaseExists(Program.ConnectionStrings.Shares))) { - // todo: check columns + Log.Debug($"Share cache {(StorageMode == StorageMode.Memory ? "StorageMode is 'Memory'" : "database is missing from disk")}. Attempting to load from backup..."); - State.SetValue(state => state with + // the backup is missing; we can't do anything but recreate it from scratch + if (!DatabaseExists(Program.ConnectionStrings.SharesBackup)) { - Filling = false, - Faulted = false, - Filled = true, - FillProgress = 1, - Directories = CountDirectories(), - Files = CountFiles(), - }); + Log.Debug("Share cache backup is missing; unable to restore"); + return false; + } - return true; + Log.Debug("Share cache backup located. Attempting to restore..."); + + using var backupConn = GetConnection(Program.ConnectionStrings.SharesBackup); + using var conn = GetConnection(); + backupConn.BackupDatabase(conn); + + Log.Debug("Share cache successfully restored from backup"); } - return false; + Log.Debug("Validating share cache database"); + + if (!TableExists("directories") || !TableExists("filenames") || !TableExists("files")) + { + Log.Warning("One or more tables are missing from the share cache database"); + return false; + } + + // todo: check columns + + State.SetValue(state => state with + { + Filling = false, + Faulted = false, + Filled = true, + FillProgress = 1, + Directories = CountDirectories(), + Files = CountFiles(), + }); + + return true; } catch (Exception ex) { @@ -463,6 +500,32 @@ namespace slskd.Shares } } + private bool DatabaseExists(string connectionString) + { + try + { + using var conn = GetConnection(connectionString); + + if (conn.State == ConnectionState.Open) + { + return true; + } + + return false; + } + catch + { + return false; + } + } + + private void Backup() + { + using var sourceConn = GetConnection(); + using var backupConn = GetConnection(Program.ConnectionStrings.SharesBackup); + sourceConn.BackupDatabase(backupConn); + } + private int CountDirectories() { using var conn = GetConnection(); @@ -494,9 +557,9 @@ namespace slskd.Shares "extension TEXT, attributeJson TEXT NOT NULL);"); } - private SqliteConnection GetConnection() + private SqliteConnection GetConnection(string connectionString = null) { - var conn = new SqliteConnection(Program.ConnectionStrings.Shares); + var conn = new SqliteConnection(connectionString ?? Program.ConnectionStrings.Shares); conn.Open(); return conn; } @@ -595,22 +658,6 @@ namespace slskd.Shares } } - private void ResetCache() - { - CreateTables(); - - try - { - System.IO.File.Delete(Path.Combine(Program.DataDirectory, Filenames.BrowseCache)); - } - catch (FileNotFoundException) - { - // noop - } - - State.SetValue(state => state with { Directories = 0, Files = 0 }); - } - private bool TableExists(string table) { using var conn = GetConnection(); |
