diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-06-01 10:56:34 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-06-01 10:56:34 -0500 |
| commit | 1da4e1f198aff7f5ad7f61381d481acc8dc7119c (patch) | |
| tree | 8d1439876f0348d62b455bf2a54c4bf958ea446f /src | |
| parent | ccd0e524b7c2d60362ea272140080e5cc52ab08a (diff) | |
| download | slskd-1da4e1f198aff7f5ad7f61381d481acc8dc7119c.tar.gz | |
pass filters to cache
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 22 | ||||
| -rw-r--r-- | src/slskd/Shares/SharedFileCache.cs | 19 |
2 files changed, 25 insertions, 16 deletions
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<Options> 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); } /// <summary> @@ -65,6 +68,8 @@ namespace slskd.Shares /// </summary> public IReadOnlyList<Share> Shares => SharesList.AsReadOnly(); + public IReadOnlyList<Regex> FilterRegexes => FilterRegexList.AsReadOnly(); + /// <summary> /// Gets the state monitor for the service. /// </summary> @@ -76,6 +81,7 @@ namespace slskd.Shares private List<Share> SharesList { get; set; } = new List<Share>(); private IManagedState<ShareState> State { get; } = new ManagedState<ShareState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private List<Regex> FilterRegexList { get; set; } = new List<Regex>(); /// <summary> /// Returns the entire contents of the share. @@ -127,7 +133,7 @@ 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); + => 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 /// </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); + Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters); /// <summary> /// Returns the contents of the specified <paramref name="directory"/>. @@ -87,13 +88,9 @@ namespace slskd.Shares /// <summary> /// Initializes a new instance of the <see cref="SharedFileCache"/> class. /// </summary> - /// <param name="optionsMonitor"></param> /// <param name="soulseekFileFactory"></param> - public SharedFileCache( - IOptionsMonitor<Options> 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<SharedFileCache>(); private HashSet<string> MaskedDirectories { get; set; } private Dictionary<string, File> MaskedFiles { get; set; } - private IOptionsMonitor<Options> OptionsMonitor { get; set; } private List<Share> Shares { get; set; } private ISoulseekFileFactory SoulseekFileFactory { get; } private SqliteConnection SQLite { get; set; } @@ -118,6 +114,7 @@ namespace slskd.Shares /// <returns>The contents of the cache.</returns> public IEnumerable<Directory> 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. /// </summary> /// <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> - public async Task FillAsync(IEnumerable<Share> shares) + public async Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> 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<string, File>(); var maskedDirectories = new HashSet<string>(); var current = 0; |
