aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-09-10 17:39:44 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-09-10 17:39:44 -0500
commit7c4922e890bf0267ceb55ff568d03b0485edbd4d (patch)
treea885fb0bb67319b7c33f5483d06748b94992e32a /src
parentcad5239f816bc3f116a6ad8d3dea9e1e3f831a65 (diff)
downloadslskd-7c4922e890bf0267ceb55ff568d03b0485edbd4d.tar.gz
backup and restore share cache from disk
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Shares/ShareService.cs8
-rw-r--r--src/slskd/Shares/SharedFileCache.cs113
2 files changed, 84 insertions, 37 deletions
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs
index 316e6b2f..dd808360 100644
--- a/src/slskd/Shares/ShareService.cs
+++ b/src/slskd/Shares/ShareService.cs
@@ -41,7 +41,7 @@ namespace slskd.Shares
IOptionsMonitor<Options> optionsMonitor,
ISharedFileCache sharedFileCache = null)
{
- Cache = sharedFileCache ?? new SharedFileCache();
+ Cache = sharedFileCache ?? new SharedFileCache(storageMode: optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum<StorageMode>());
Cache.StateMonitor.OnChange(cacheState =>
{
var (previous, current) = cacheState;
@@ -180,21 +180,21 @@ namespace slskd.Shares
try
{
- var optionsHash = Compute.Sha1Hash(string.Join(';', options.Directories.Shared));
+ var optionsHash = Compute.Sha1Hash(string.Join(';', options.Shares.Directories));
if (optionsHash == LastOptionsHash)
{
return;
}
- var shares = options.Directories.Shared
+ var shares = options.Shares.Directories
.Select(share => share.TrimEnd('/', '\\'))
.ToHashSet() // remove duplicates
.Select(share => new Share(share)) // convert to Shares
.OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent
.ToList();
- var regexes = options.Filters.Share
+ var regexes = options.Shares.Filters
.Select(filter => new Regex(filter, RegexOptions.Compiled))
.ToList();
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();