diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-09-10 12:40:03 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-10 12:40:03 -0500 |
| commit | 3d657ed87cfdab4089c72d9e6532d9b2cc83aa4e (patch) | |
| tree | 848ead506fa36e922bf9db2d3f45f6398fdac8b8 | |
| parent | 25191b8b7c8fa08484e2b7eb4efe4a1398cb1ab5 (diff) | |
| parent | 1955793824df698ce1f4aa3a130d327e20875efd (diff) | |
| download | slskd-3d657ed87cfdab4089c72d9e6532d9b2cc83aa4e.tar.gz | |
Merge pull request #637 from slskd/db-metadata
Refactor shared file cache to store all data on disk
| -rw-r--r-- | src/slskd/Application.cs | 57 | ||||
| -rw-r--r-- | src/slskd/Common/SqliteConnectionExtensions.cs | 32 | ||||
| -rw-r--r-- | src/slskd/Core/ConnectionStrings.cs | 29 | ||||
| -rw-r--r-- | src/slskd/Core/Filenames.cs | 30 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Core/State.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 32 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareService.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Shares/ISharedFileCache.cs | 78 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 22 | ||||
| -rw-r--r-- | src/slskd/Shares/SharedFileCache.cs | 421 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 2 |
12 files changed, 535 insertions, 185 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index e4d47bb1..f3feec23 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -369,13 +369,22 @@ namespace slskd _ = CheckVersionAsync(); } - if (OptionsAtStartup.Flags.NoShareScan) + // try to load the share cache from disk. + // if not successful, the file is missing one or more tables, or is not present at all + if (!await Shares.TryLoadFromDiskAsync()) { - Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); - } - else - { - _ = Shares.StartScanAsync(); + Log.Warning("Unable to load share cache from disk. A share scan is required."); + + State.SetValue(state => state with { Shares = state.Shares with { ScanPending = true } }); + + if (OptionsAtStartup.Flags.NoShareScan) + { + Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); + } + else + { + _ = Shares.StartScanAsync(); + } } if (OptionsAtStartup.Flags.NoConnect) @@ -417,7 +426,21 @@ namespace slskd var sw = new Stopwatch(); sw.Start(); - var directories = await Shares.BrowseAsync(); + BrowseResponse response = default; + + var cacheFilename = Path.Combine(Program.DataDirectory, Filenames.BrowseCache); + var cacheFileInfo = new FileInfo(cacheFilename); + + if (!cacheFileInfo.Exists) + { + Log.Warning("Browse response not cached. Rebuilding..."); + await CacheBrowseResponse(); + } + + var stream = new FileStream(cacheFilename, FileMode.Open, FileAccess.Read); + response = new RawBrowseResponse(cacheFileInfo.Length, stream); + + Log.Information("Sent browse response to {User}", username); sw.Stop(); @@ -425,7 +448,7 @@ namespace slskd Metrics.Browse.CurrentResponseLatency.Update(sw.ElapsedMilliseconds); Metrics.Browse.ResponsesSent.Inc(1); - return new BrowseResponse(directories); + return response; } catch (Exception ex) { @@ -981,6 +1004,7 @@ namespace slskd { State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } }); Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Directories, current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); + _ = CacheBrowseResponse(); SharesRefreshStarted = default; @@ -994,6 +1018,13 @@ namespace slskd } } } + else if (!previous.Ready && current.Ready) + { + // the share transitioned into ready without completing a scan; it was loaded from disk + State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } }); + Log.Information("Shares loaded from disk successfully. Sharing {Directories} directories and {Files}", current.Directories, current.Files); + _ = CacheBrowseResponse(); + } else { // the scan is neither starting nor finishing; progress update @@ -1008,6 +1039,16 @@ namespace slskd } } + private async Task CacheBrowseResponse() + { + var directories = await Shares.BrowseAsync(); + var response = new BrowseResponse(directories); + + Log.Information("Warming browse response cache..."); + System.IO.File.WriteAllBytes(Path.Combine(Program.DataDirectory, Filenames.BrowseCache), response.ToByteArray()); + Log.Information("Browse response cached successfully"); + } + private void State_OnChange((State Previous, State Current) state) { _ = ApplicationHub.BroadcastStateAsync(state.Current); diff --git a/src/slskd/Common/SqliteConnectionExtensions.cs b/src/slskd/Common/SqliteConnectionExtensions.cs new file mode 100644 index 00000000..3944c10b --- /dev/null +++ b/src/slskd/Common/SqliteConnectionExtensions.cs @@ -0,0 +1,32 @@ +// <copyright file="SqliteConnectionExtensions.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using Microsoft.Data.Sqlite; + + public static class SqliteConnectionExtensions + { + public static int ExecuteNonQuery(this SqliteConnection conn, string query, Action<SqliteCommand> action = null) + { + using var cmd = new SqliteCommand(query, conn); + action?.Invoke(cmd); + return cmd.ExecuteNonQuery(); + } + } +} diff --git a/src/slskd/Core/ConnectionStrings.cs b/src/slskd/Core/ConnectionStrings.cs new file mode 100644 index 00000000..e5bd4f05 --- /dev/null +++ b/src/slskd/Core/ConnectionStrings.cs @@ -0,0 +1,29 @@ +// <copyright file="ConnectionStrings.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + /// <summary> + /// Application connection strings. + /// </summary> + public record ConnectionStrings + { + public string Transfers { get; init; } + public string Shares { get; init; } + public string Search { get; init; } + } +} diff --git a/src/slskd/Core/Filenames.cs b/src/slskd/Core/Filenames.cs new file mode 100644 index 00000000..0fc55fc5 --- /dev/null +++ b/src/slskd/Core/Filenames.cs @@ -0,0 +1,30 @@ +// <copyright file="Filenames.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + /// <summary> + /// Application files. + /// </summary> + public static class Filenames + { + public static readonly string TransfersDb = "transfers.db"; + public static readonly string SharesDb = "shares.db"; + public static readonly string SearchDb = "search.db"; + public static readonly string BrowseCache = "browse.cache"; + } +} diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 259977b6..d6d4b04c 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -755,12 +755,12 @@ namespace slskd /// Gets metrics endpoint authentication options. /// </summary> [Validate] - public AuthenticationOptions Authentication { get; init; } = new AuthenticationOptions(); + public MetricsAuthenticationOptions Authentication { get; init; } = new MetricsAuthenticationOptions(); /// <summary> /// Metrics endpoint authentication options. /// </summary> - public class AuthenticationOptions + public class MetricsAuthenticationOptions { /// <summary> /// Gets a value indicating whether authentication should be disabled. @@ -1114,12 +1114,12 @@ namespace slskd /// Gets authentication options. /// </summary> [Validate] - public AuthenticationOptions Authentication { get; init; } = new AuthenticationOptions(); + public WebAuthenticationOptions Authentication { get; init; } = new WebAuthenticationOptions(); /// <summary> /// Authentication options. /// </summary> - public class AuthenticationOptions + public class WebAuthenticationOptions { /// <summary> /// Gets a value indicating whether authentication should be disabled. diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index a1da2be8..1b798ad3 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -102,6 +102,7 @@ namespace slskd { public bool ScanPending { get; init; } public bool Scanning { get; init; } + public bool Ready { get; init; } public bool Faulted { get; init; } public double ScanProgress { get; init; } public int Directories { get; init; } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 541f0f00..f4e59456 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -45,7 +45,6 @@ namespace slskd using Microsoft.Extensions.FileProviders.Physical; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; - using Prometheus; using Prometheus.DotNetRuntime; using Prometheus.SystemMetrics; using Serilog; @@ -161,6 +160,16 @@ namespace slskd public static string ConfigurationFile { get; private set; } = null; /// <summary> + /// Gets the connection strings for application databases. + /// </summary> + public static ConnectionStrings ConnectionStrings { get; private set; } = null; + + /// <summary> + /// Gets the path where persistent data is saved. + /// </summary> + public static string DataDirectory { get; private set; } = null; + + /// <summary> /// Gets the default fully qualified path to the configuration file. /// </summary> public static string DefaultConfigurationFile { get; private set; } @@ -247,6 +256,15 @@ namespace slskd // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; + DataDirectory = Path.Combine(AppDirectory, "data"); + + ConnectionStrings = new() + { + Search = $"Data Source={Path.Combine(DataDirectory, Filenames.SearchDb)};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, Filenames.TransfersDb)};Cache=shared;Pooling=True;", + Shares = $"Data Source={Path.Combine(DataDirectory, Filenames.SharesDb)};Cache=shared", + }; + DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete"); @@ -260,7 +278,7 @@ namespace slskd try { VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DataDirectory, createIfMissing: true, verifyWriteable: true); VerifyDirectory(DefaultDownloadsDirectory, createIfMissing: true, verifyWriteable: true); VerifyDirectory(DefaultIncompleteDirectory, createIfMissing: true, verifyWriteable: true); } @@ -449,8 +467,8 @@ namespace slskd ConfigureSQLite(); - services.AddDbContext<SearchDbContext>("search.db"); - services.AddDbContext<TransfersDbContext>("transfers.db"); + services.AddDbContext<SearchDbContext>(ConnectionStrings.Search); + services.AddDbContext<TransfersDbContext>(ConnectionStrings.Transfers); services.AddSingleton<IBrowseTracker, BrowseTracker>(); services.AddSingleton<IConversationTracker, ConversationTracker>(); @@ -512,7 +530,7 @@ namespace slskd using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); services.AddDataProtection() - .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(AppDirectory, "data", ".DataProtection-Keys"))); + .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, ".DataProtection-Keys"))); var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key)); @@ -838,7 +856,7 @@ namespace slskd commandLine: Environment.CommandLine); } - private static IServiceCollection AddDbContext<T>(this IServiceCollection services, string filename) + private static IServiceCollection AddDbContext<T>(this IServiceCollection services, string connectionString) where T : DbContext { Log.Debug("Initializing database context {Name}", typeof(T).Name); @@ -847,7 +865,7 @@ namespace slskd { services.AddDbContextFactory<T>(options => { - options.UseSqlite($"Data Source={Path.Combine(AppDirectory, "data", filename)};Cache=shared;Pooling=True;"); + options.UseSqlite(connectionString); if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL) { diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index a2eeed92..962d4c1e 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -75,5 +75,11 @@ namespace slskd.Shares /// <returns>The operation context.</returns> /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> Task StartScanAsync(); + + /// <summary> + /// Attempt to load shares from disk. + /// </summary> + /// <returns>A value indicating whether shares were loaded.</returns> + Task<bool> TryLoadFromDiskAsync(); } -} +}
\ No newline at end of file diff --git a/src/slskd/Shares/ISharedFileCache.cs b/src/slskd/Shares/ISharedFileCache.cs new file mode 100644 index 00000000..7043b030 --- /dev/null +++ b/src/slskd/Shares/ISharedFileCache.cs @@ -0,0 +1,78 @@ +// <copyright file="ISharedFileCache.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Shares +{ + using System.Collections.Generic; + using System.Text.RegularExpressions; + using System.Threading.Tasks; + using Soulseek; + + /// <summary> + /// Shared file cache. + /// </summary> + public interface ISharedFileCache + { + /// <summary> + /// Gets the cache state monitor. + /// </summary> + IStateMonitor<SharedFileCacheState> StateMonitor { get; } + + /// <summary> + /// Returns the contents of the cache. + /// </summary> + /// <returns>The contents of the cache.</returns> + IEnumerable<Directory> Browse(); + + /// <summary> + /// Scans the configured shares and fills the cache. + /// </summary> + /// <remarks>Initiates the scan, then yields execution back to the caller; does not wait for the operation to complete.</remarks> + /// <param name="shares">The list of shares from which to fill the cache.</param> + /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param> + /// <returns>The operation context.</returns> + Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters); + + /// <summary> + /// Returns the contents of the specified <paramref name="directory"/>. + /// </summary> + /// <param name="directory">The directory for which the contents are to be listed.</param> + /// <returns>The contents of the directory.</returns> + Directory List(string directory); + + /// <summary> + /// Substitutes the mask in the specified <paramref name="filename"/> with the original path, if the mask is tracked + /// by the cache. + /// </summary> + /// <param name="filename">The fully qualified filename to unmask.</param> + /// <returns>The unmasked filename.</returns> + public string Resolve(string filename); + + /// <summary> + /// Searches the cache for the specified <paramref name="query"/> and returns the matching files. + /// </summary> + /// <param name="query">The query for which to search.</param> + /// <returns>The matching files.</returns> + IEnumerable<File> Search(SearchQuery query); + + /// <summary> + /// Attempts to load the cache from disk. + /// </summary> + /// <returns>A value indicating whether the load was successful.</returns> + bool TryLoad(); + } +} diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 60b69967..316e6b2f 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -52,6 +52,7 @@ namespace slskd.Shares ScanPending = current.Faulted || (!(previous.Filling && !current.Filling) && state.ScanPending), Scanning = current.Filling, Faulted = current.Faulted, + Ready = current.Filled, ScanProgress = current.FillProgress, Directories = current.Directories, Files = current.Files, @@ -142,16 +143,16 @@ namespace slskd.Shares /// </summary> /// <param name="query">The query for which to search.</param> /// <returns>The matching files.</returns> - public async Task<IEnumerable<File>> SearchAsync(SearchQuery query) + public Task<IEnumerable<File>> SearchAsync(SearchQuery query) { - var results = await Cache.SearchAsync(query); + var results = Cache.Search(query); - return results.Select(r => new File( + return Task.FromResult(results.Select(r => new File( r.Code, r.Filename.NormalizePath(), r.Size, r.Extension, - r.Attributes)); + r.Attributes))); } /// <summary> @@ -160,7 +161,18 @@ namespace slskd.Shares /// <returns>The operation context.</returns> /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> public Task StartScanAsync() - => Cache.FillAsync(Shares, FilterRegexes); + { + return Cache.FillAsync(Shares, FilterRegexes); + } + + /// <summary> + /// Attempt to load shares from disk. + /// </summary> + /// <returns>A value indicating whether shares were loaded.</returns> + public Task<bool> TryLoadFromDiskAsync() + { + return Task.FromResult(Cache.TryLoad()); + } private void Configure(Options options) { diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 9902c951..c7c1d30a 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -34,54 +34,6 @@ namespace slskd.Shares /// <summary> /// Shared file cache. /// </summary> - public interface ISharedFileCache - { - /// <summary> - /// Gets the cache state monitor. - /// </summary> - IStateMonitor<SharedFileCacheState> StateMonitor { get; } - - /// <summary> - /// Returns the contents of the cache. - /// </summary> - /// <returns>The contents of the cache.</returns> - IEnumerable<Directory> Browse(); - - /// <summary> - /// Scans the configured shares and fills the cache. - /// </summary> - /// <remarks>Initiates the scan, then yields execution back to the caller; does not wait for the operation to complete.</remarks> - /// <param name="shares">The list of shares from which to fill the cache.</param> - /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param> - /// <returns>The operation context.</returns> - Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters); - - /// <summary> - /// Returns the contents of the specified <paramref name="directory"/>. - /// </summary> - /// <param name="directory">The directory for which the contents are to be listed.</param> - /// <returns>The contents of the directory.</returns> - Directory List(string directory); - - /// <summary> - /// Substitutes the mask in the specified <paramref name="filename"/> with the original path, if the mask is tracked - /// by the cache. - /// </summary> - /// <param name="filename">The fully qualified filename to unmask.</param> - /// <returns>The unmasked filename.</returns> - public string Resolve(string filename); - - /// <summary> - /// Searches the cache for the specified <paramref name="query"/> and returns the matching files. - /// </summary> - /// <param name="query">The query for which to search.</param> - /// <returns>The matching files.</returns> - Task<IEnumerable<File>> SearchAsync(SearchQuery query); - } - - /// <summary> - /// Shared file cache. - /// </summary> public class SharedFileCache : ISharedFileCache { /// <summary> @@ -99,11 +51,8 @@ namespace slskd.Shares public IStateMonitor<SharedFileCacheState> StateMonitor => State; private ILogger Log { get; } = Serilog.Log.ForContext<SharedFileCache>(); - private HashSet<string> MaskedDirectories { get; set; } - private Dictionary<string, File> MaskedFiles { get; set; } private List<Share> Shares { get; set; } private ISoulseekFileFactory SoulseekFileFactory { get; } - private SqliteConnection SQLite { get; set; } private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); @@ -130,23 +79,43 @@ namespace slskd.Shares // in the root. to get around this, prime a dictionary with all known directories, and an empty Soulseek.Directory. if // there are any files in the directory, this entry will be overwritten with a new Soulseek.Directory containing the // files. if not they'll be left as is. - foreach (var directory in MaskedDirectories) + foreach (var directory in ListDirectories()) { directories.TryAdd(directory, new Directory(directory)); } - var groups = MaskedFiles - .GroupBy(f => Path.GetDirectoryName(f.Key)) - .Select(g => new Directory(g.Key, g.Select(g => + var files = new List<File>(); + + using var conn = GetConnection(); + using var cmd = new SqliteCommand("SELECT maskedFilename, code, size, extension, attributeJson FROM files ORDER BY maskedFilename ASC;", conn); + var reader = cmd.ExecuteReader(); + + while (reader.Read()) + { + var filename = reader.GetString(0); + var code = reader.GetInt32(1); + var size = reader.GetInt64(2); + var extension = reader.GetString(3); + var attributeJson = reader.GetString(4); + + var attributeList = attributeJson.FromJson<List<FileAttribute>>(); + + var file = new File(code, filename, size, extension, attributeList); + + files.Add(file); + } + + var groups = files + .GroupBy(file => Path.GetDirectoryName(file.Filename)) + .Select(group => new Directory(group.Key, group.Select(f => { - var f = g.Value; return new File( f.Code, Path.GetFileName(f.Filename), // we can send the full path, or just the filename. save bandwidth and omit the path. f.Size, f.Extension, f.Attributes); - }))); + }).OrderBy(f => f.Filename))); // merge the dictionary containing all directories with the Soulseek.Directory instances containing their files. // entries with no files will remain untouched. @@ -155,7 +124,7 @@ namespace slskd.Shares directories.AddOrUpdate(group.Name, group, (_, _) => group); } - return directories.Values.OrderBy(f => f.Name); + return directories.Values.OrderBy(d => d.Name); } /// <summary> @@ -210,9 +179,9 @@ namespace slskd.Shares Log.Debug("Enumerating shared directories"); swSnapshot = sw.ElapsedMilliseconds; - // derive a list of all directories from all shares - // skip hidden and system directories, as well as anything that can't be accessed due to security restrictions. - // it's necessary to enumerate these directories up front so we can deduplicate directories and apply exclusions + // derive a list of all directories from all shares skip hidden and system directories, as well as anything that + // can't be accessed due to security restrictions. it's necessary to enumerate these directories up front so we + // can deduplicate directories and apply exclusions var unmaskedDirectories = Shares .SelectMany(share => { @@ -244,10 +213,10 @@ namespace slskd.Shares Log.Debug("Found {Directories} shared directories (and {Excluded} were excluded) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; - var files = new Dictionary<string, File>(); var maskedDirectories = new HashSet<string>(); - var current = 0L; - var filtered = 0L; + var current = 0; + var cached = 0; + var filtered = 0; foreach (var directory in unmaskedDirectories) { @@ -259,52 +228,49 @@ namespace slskd.Shares continue; } - var addedFiles = 0L; - var filteredFiles = 0L; + var addedFiles = 0; + var filteredFiles = 0; var share = Shares.First(share => directory.StartsWith(share.LocalPath)); maskedDirectories.Add(directory.ReplaceFirst(share.LocalPath, share.RemotePath)); + InsertDirectory(directory.ReplaceFirst(share.LocalPath, share.RemotePath)); // recursively find all files in the directory and stick a record in a dictionary, keyed on the sanitized // filename and with a value of a Soulseek.File object try { - // enumerate files in this directory only (no subdirectories) - // exclude hidden and system files and anything that can't be accessed due to security restrictions + // enumerate files in this directory only (no subdirectories) exclude hidden and system files and anything + // that can't be accessed due to security restrictions var newFiles = System.IO.Directory.GetFiles(directory, "*", new EnumerationOptions() { AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, IgnoreInaccessible = true, RecurseSubdirectories = false, - }) - .Select(filename => SoulseekFileFactory.Create(filename, maskedFilename: filename.ReplaceFirst(share.LocalPath, share.RemotePath))) - .ToDictionary(file => file.Filename, file => file); + }); - addedFiles = newFiles.Count; + addedFiles = newFiles.Count(); // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. - foreach (var file in newFiles) + foreach (var originalFilename in newFiles) { - if (filters.Any(filter => filter.IsMatch(file.Key))) + var info = new FileInfo(originalFilename); + var file = SoulseekFileFactory.Create(originalFilename, maskedFilename: originalFilename.ReplaceFirst(share.LocalPath, share.RemotePath)); + + if (filters.Any(filter => filter.IsMatch(originalFilename))) { filteredFiles++; continue; } - if (Path.DirectorySeparatorChar == '/' && file.Value.Filename.Contains('\\')) + if (Path.DirectorySeparatorChar == '/' && file.Filename.Contains('\\')) { - Log.Warning("Filename {Filename} contains one or more backslashes, which are not currently supported. This file will not be shared.", file.Value.Filename); + Log.Warning("Filename {Filename} contains one or more backslashes, which are not currently supported. This file will not be shared.", file.Filename); continue; } - if (files.ContainsKey(file.Key)) - { - Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); - } - - files[file.Key] = file.Value; + InsertFile(maskedFilename: file.Filename, originalFilename, touchedAt: info.LastWriteTimeUtc, file); } } catch (Exception ex) @@ -315,12 +281,13 @@ namespace slskd.Shares current++; filtered += filteredFiles; + cached += addedFiles; Log.Debug("Finished scanning {Directory}: {Added} files added and {Filtered} filtered", directory, addedFiles, filteredFiles); try { - State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = files.Count }); + State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = cached }); } catch (Exception ex) { @@ -329,19 +296,10 @@ namespace slskd.Shares } } - Log.Debug("Directory scan found {Files} files (and {Filtered} were filtered) in {Elapsed}ms. Populating filename database", files.Count, filtered, sw.ElapsedMilliseconds - swSnapshot); + Log.Debug("Directory scan found {Files} files (and {Filtered} were filtered) in {Elapsed}ms. Populating filename database", cached, filtered, sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; - // potentially optimize with multi-valued insert https://stackoverflow.com/questions/16055566/insert-multiple-rows-in-sqlite - foreach (var file in files) - { - InsertFilename(file.Key); - } - - Log.Debug("Inserted {Files} records in {Elapsed}ms", files.Count, sw.ElapsedMilliseconds - swSnapshot); - - MaskedDirectories = maskedDirectories; - MaskedFiles = files; + Log.Debug("Inserted {Files} records in {Elapsed}ms", cached, sw.ElapsedMilliseconds - swSnapshot); State.SetValue(state => state with { @@ -349,11 +307,11 @@ namespace slskd.Shares Faulted = false, Filled = true, FillProgress = 1, - Directories = MaskedDirectories.Count, - Files = MaskedFiles.Count, + Directories = CountDirectories(), + Files = CountFiles(), }); - Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); + Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {cached}"); } catch (Exception ex) { @@ -384,17 +342,7 @@ namespace slskd.Shares return null; } - var files = MaskedFiles.Where(f => f.Key.StartsWith(directory)).Select(kvp => - { - var f = kvp.Value; - return new File( - f.Code, - Path.GetFileName(f.Filename), - f.Size, - f.Extension, - f.Attributes); - }); - + var files = ListFiles(directory); return new Directory(directory, files); } @@ -406,35 +354,19 @@ namespace slskd.Shares /// <returns>The unmasked filename.</returns> public string Resolve(string filename) { - // ensure this is a tracked file - if (!MaskedFiles.TryGetValue(filename, out _)) - { - return null; - } - - var resolved = filename; - - // a well-formed path will consist of a mask, either an alias or a local directory, and a fully qualified path to a - // file. split the requested filename so we can examine the first two segments - var parts = filename.Split(new[] { '/', '\\' }); + using var conn = GetConnection(); + using var cmd = new SqliteCommand("SELECT originalFilename FROM files WHERE maskedFilename = @maskedFilename;", conn); + cmd.Parameters.AddWithValue("maskedFilename", filename); - if (parts.Length < 2) - { - Log.Warning($"Failed to resolve shared file {filename}; filename is badly formed"); - return null; - } + var reader = cmd.ExecuteReader(); - // find the share with a matching mask and alias/local directory - var share = Shares.FirstOrDefault(share => share.Alias == parts[0]); - - if (share == default) + if (!reader.Read()) { - Log.Warning("Failed to resolve shared file {Filename}; unable to resolve share from alias '{Alias}'", filename, parts[0]); + Log.Warning("Failed to resolve shared file {Filename}", filename); return null; } - resolved = resolved.ReplaceFirst(share.RemotePath, share.LocalPath); - + var resolved = reader.GetString(0); Log.Debug($"Resolved requested shared file {filename} to {resolved}"); return resolved; } @@ -444,7 +376,7 @@ namespace slskd.Shares /// </summary> /// <param name="query">The query for which to search.</param> /// <returns>The matching files.</returns> - public async Task<IEnumerable<File>> SearchAsync(SearchQuery query) + public IEnumerable<File> Search(SearchQuery query) { if (!State.CurrentValue.Filled) { @@ -460,68 +392,239 @@ namespace slskd.Shares var match = string.Join(" AND ", query.Terms.Select(token => $"\"{Clean(token)}\"")); var exclusions = string.Join(" OR ", query.Exclusions.Select(exclusion => $"\"{Clean(exclusion)}\"")); - var sql = $"SELECT * FROM cache WHERE cache MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}'"; - var results = new List<string>(); + var sql = $"SELECT files.maskedFilename, files.code, files.size, files.extension, files.attributeJson FROM filenames " + + "INNER JOIN files ON filenames.maskedFilename = files.maskedFilename " + + $"WHERE filenames MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}' " + + "ORDER BY filenames.maskedFilename ASC;"; + + var results = new List<File>(); + SqliteDataReader reader = default; try { - using var conn = new SqliteConnection($"Data Source={Path.Combine(Program.AppDirectory, "data", "shares.db")};cache=shared"); + using var conn = GetConnection(); using var cmd = new SqliteCommand(sql, conn); - await conn.OpenAsync(); - - var reader = await cmd.ExecuteReaderAsync(); + reader = cmd.ExecuteReader(); while (reader.Read()) { - results.Add(reader.GetString(0)); + var filename = reader.GetString(0); + var code = reader.GetInt32(1); + var size = reader.GetInt64(2); + var extension = reader.GetString(3); + var attributeJson = reader.GetString(4); + + var attributeList = attributeJson.FromJson<List<FileAttribute>>(); + + var file = new File(code, filename, size, extension, attributeList); + results.Add(file); } + + return results.OrderBy(f => f.Filename); } catch (Exception ex) { Log.Warning(ex, "Failed to execute shared file query '{Query}': {Message}", query, ex.Message); return Enumerable.Empty<File>(); } + } + /// <summary> + /// Attempts to load the cache from disk. + /// </summary> + /// <returns>A value indicating whether the load was successful.</returns> + public bool TryLoad() + { try { - return results - .Select(r => MaskedFiles[r]) - .ToList(); + if (TableExists("directories") && TableExists("filenames") && TableExists("files") && TableExists("exclusions")) + { + // todo: check columns + + State.SetValue(state => state with + { + Filling = false, + Faulted = false, + Filled = true, + FillProgress = 1, + Directories = CountDirectories(), + Files = CountFiles(), + }); + + return true; + } + + return false; } catch (Exception ex) { - Log.Warning(ex, "Failed to map shared file result: {Message}", ex.Message); - return Enumerable.Empty<File>(); + Log.Debug(ex, "Error loading shared file cache: {Message}", ex.Message); + return false; } } - private void CreateTable() + private int CountDirectories() + { + using var conn = GetConnection(); + using var cmd = new SqliteCommand("SELECT COUNT(*) FROM directories;", conn); + var reader = cmd.ExecuteReader(); + reader.Read(); + return reader.GetInt32(0); + } + + private int CountFiles() { - if (SQLite != null) + using var conn = GetConnection(); + using var cmd = new SqliteCommand("SELECT COUNT(*) FROM files;", conn); + var reader = cmd.ExecuteReader(); + reader.Read(); + return reader.GetInt32(0); + } + + private void CreateTables() + { + using var conn = GetConnection(); + + conn.ExecuteNonQuery("DROP TABLE IF EXISTS directories; CREATE TABLE directories (name TEXT PRIMARY KEY);"); + + conn.ExecuteNonQuery("DROP TABLE IF EXISTS filenames; CREATE VIRTUAL TABLE filenames USING fts5(maskedFilename);"); + + conn.ExecuteNonQuery("DROP TABLE IF EXISTS files; CREATE TABLE files " + + "(maskedFilename TEXT PRIMARY KEY, originalFilename TEXT NOT NULL, size BIGINT NOT NULL, touchedAt TEXT NOT NULL, code INTEGER DEFAULT 1 NOT NULL, " + + "extension TEXT, attributeJson TEXT NOT NULL);"); + } + + private SqliteConnection GetConnection() + { + var conn = new SqliteConnection(Program.ConnectionStrings.Shares); + conn.Open(); + return conn; + } + + private void InsertDirectory(string name) + { + using var conn = GetConnection(); + + conn.ExecuteNonQuery("INSERT INTO directories VALUES(@name) ON CONFLICT DO NOTHING;", cmd => { - SQLite.Dispose(); - } + cmd.Parameters.AddWithValue("name", name); + }); + } + + private void InsertFile(string maskedFilename, string originalFilename, DateTime touchedAt, File file) + { + using var conn = GetConnection(); - SQLite = new SqliteConnection($"Data Source={Path.Combine(Program.AppDirectory, "data", "shares.db")};cache=shared"); - SQLite.Open(); + conn.ExecuteNonQuery("INSERT INTO files (maskedFilename, originalFilename, size, touchedAt, code, extension, attributeJson) " + + "VALUES(@maskedFilename, @originalFilename, @size, @touchedAt, @code, @extension, @attributeJson) " + + "ON CONFLICT DO UPDATE SET originalFilename = excluded.originalFilename, size = excluded.size, touchedAt = excluded.touchedAt, code = excluded.code, extension = excluded.extension, attributeJson = excluded.attributeJson;", cmd => + { + cmd.Parameters.AddWithValue("maskedFilename", maskedFilename); + cmd.Parameters.AddWithValue("originalFilename", originalFilename); + cmd.Parameters.AddWithValue("size", file.Size); + cmd.Parameters.AddWithValue("touchedAt", touchedAt.ToLongDateString()); + cmd.Parameters.AddWithValue("code", file.Code); + cmd.Parameters.AddWithValue("extension", file.Extension); + cmd.Parameters.AddWithValue("attributeJson", file.Attributes.ToJson()); + }); - using var cmd = new SqliteCommand("DROP TABLE IF EXISTS cache; CREATE VIRTUAL TABLE cache USING fts5(filename);", SQLite); - cmd.ExecuteNonQuery(); + conn.ExecuteNonQuery("INSERT INTO filenames (maskedFilename) VALUES(@maskedFilename);", cmd => + { + cmd.Parameters.AddWithValue("maskedFilename", maskedFilename); + }); } - private void InsertFilename(string filename) + private IEnumerable<string> ListDirectories() { - using var cmd = new SqliteCommand($"INSERT INTO cache(filename) VALUES(@filename)", SQLite); - cmd.Parameters.AddWithValue("filename", filename); - cmd.ExecuteNonQuery(); + var results = new List<string>(); + + using var conn = GetConnection(); + + using var cmd = new SqliteCommand("SELECT name FROM directories ORDER BY name ASC;", conn); + var reader = cmd.ExecuteReader(); + + while (reader.Read()) + { + results.Add(reader.GetString(0)); + } + + return results; + } + + private IEnumerable<File> ListFiles(string directory = null) + { + var results = new List<File>(); + + SqliteCommand cmd = default; + using var conn = GetConnection(); + + try + { + if (string.IsNullOrEmpty(directory)) + { + cmd = new SqliteCommand("SELECT maskedFilename, code, size, extension, attributeJson FROM files ORDER BY maskedFilename ASC;", conn); + } + else + { + cmd = new SqliteCommand("SELECT maskedFilename, code, size, extension, attributeJson FROM files WHERE maskedFilename LIKE @match ORDER BY maskedFilename ASC;", conn); + cmd.Parameters.AddWithValue("match", directory + '%'); + } + + var reader = cmd.ExecuteReader(); + + while (reader.Read()) + { + var filename = reader.GetString(0); + var code = reader.GetInt32(1); + var size = reader.GetInt64(2); + var extension = reader.GetString(3); + var attributeJson = reader.GetString(4); + + var attributeList = attributeJson.FromJson<List<FileAttribute>>(); + + var file = new File(code, filename: Path.GetFileName(filename), size, extension, attributeList); + + results.Add(file); + } + + return results; + } + finally + { + cmd?.Dispose(); + } } private void ResetCache() { - CreateTable(); - MaskedDirectories = new HashSet<string>(); - MaskedFiles = new Dictionary<string, File>(); + 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(); + using var cmd = new SqliteCommand("SELECT name from sqlite_master WHERE type = 'table' AND name = @table;", conn); + cmd.Parameters.AddWithValue("table", table); + var reader = cmd.ExecuteReader(); + + if (reader.Read()) + { + var fetchedTable = reader.GetString(0); + return table == fetchedTable; + } + + return false; + } } }
\ No newline at end of file diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index bfb2f3f0..f35a6485 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -68,7 +68,7 @@ <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> - <PackageReference Include="Soulseek" Version="5.1.3" /> + <PackageReference Include="Soulseek" Version="5.1.5" /> <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.354"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
