From 1da4e1f198aff7f5ad7f61381d481acc8dc7119c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 1 Jun 2022 10:56:34 -0500 Subject: pass filters to cache --- src/slskd/Shares/ShareService.cs | 22 +++++++++++++++++----- src/slskd/Shares/SharedFileCache.cs | 19 ++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 7c08ad13..9cae0d42 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -21,6 +21,7 @@ namespace slskd.Shares { using System.Collections.Generic; using System.Linq; + using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Soulseek; @@ -39,10 +40,7 @@ namespace slskd.Shares IOptionsMonitor optionsMonitor, ISharedFileCache sharedFileCache = null) { - OptionsMonitor = optionsMonitor; - OptionsMonitor.OnChange(Configure); - - Cache = sharedFileCache ?? new SharedFileCache(OptionsMonitor); + Cache = sharedFileCache ?? new SharedFileCache(); Cache.StateMonitor.OnChange(cacheState => { var (previous, current) = cacheState; @@ -58,6 +56,11 @@ namespace slskd.Shares Files = current.Files, }); }); + + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(options => Configure(options)); + + Configure(OptionsMonitor.CurrentValue); } /// @@ -65,6 +68,8 @@ namespace slskd.Shares /// public IReadOnlyList Shares => SharesList.AsReadOnly(); + public IReadOnlyList FilterRegexes => FilterRegexList.AsReadOnly(); + /// /// Gets the state monitor for the service. /// @@ -76,6 +81,7 @@ namespace slskd.Shares private List SharesList { get; set; } = new List(); private IManagedState State { get; } = new ManagedState(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private List FilterRegexList { get; set; } = new List(); /// /// Returns the entire contents of the share. @@ -127,7 +133,7 @@ namespace slskd.Shares /// The operation context. /// Thrown when a scan is already in progress. public Task StartScanAsync() - => Cache.FillAsync(Shares); + => Cache.FillAsync(Shares, FilterRegexes); private void Configure(Options options) { @@ -149,7 +155,13 @@ namespace slskd.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 + .Select(filter => new Regex(filter, RegexOptions.Compiled)) + .ToList(); + SharesList = shares; + FilterRegexList = regexes; + State.SetValue(state => state with { ScanPending = true }); LastOptionsHash = optionsHash; diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index b06f7be6..cb1ccdee 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -53,8 +53,9 @@ namespace slskd.Shares /// /// Initiates the scan, then yields execution back to the caller; does not wait for the operation to complete. /// The list of shares from which to fill the cache. + /// The list of regular expressions used to exclude files or paths from scanning. /// The operation context. - Task FillAsync(IEnumerable shares); + Task FillAsync(IEnumerable shares, IEnumerable filters); /// /// Returns the contents of the specified . @@ -87,13 +88,9 @@ namespace slskd.Shares /// /// Initializes a new instance of the class. /// - /// /// - public SharedFileCache( - IOptionsMonitor optionsMonitor, - ISoulseekFileFactory soulseekFileFactory = null) + public SharedFileCache(ISoulseekFileFactory soulseekFileFactory = null) { - OptionsMonitor = optionsMonitor; SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); } @@ -105,7 +102,6 @@ namespace slskd.Shares private ILogger Log { get; } = Serilog.Log.ForContext(); private HashSet MaskedDirectories { get; set; } private Dictionary MaskedFiles { get; set; } - private IOptionsMonitor OptionsMonitor { get; set; } private List Shares { get; set; } private ISoulseekFileFactory SoulseekFileFactory { get; } private SqliteConnection SQLite { get; set; } @@ -118,6 +114,7 @@ namespace slskd.Shares /// The contents of the cache. public IEnumerable Browse() { + Console.WriteLine(State.ToJson()); if (!State.CurrentValue.Filled) { if (State.CurrentValue.Filling) @@ -167,9 +164,12 @@ namespace slskd.Shares /// Scans the configured shares and fills the cache. /// /// The list of shares from which to fill the cache. + /// The list of regular expressions used to exclude files or paths from scanning. /// The operation context. - public async Task FillAsync(IEnumerable shares) + public async Task FillAsync(IEnumerable shares, IEnumerable filters) { + Console.WriteLine($"Filling: {shares.ToJson()}"); + // obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running. if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0)) { @@ -234,9 +234,6 @@ 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 filters = OptionsMonitor.CurrentValue.Filters.Share - .Select(filter => new Regex(filter, RegexOptions.Compiled)); - var files = new Dictionary(); var maskedDirectories = new HashSet(); var current = 0; -- cgit v1.2.3