diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-07-24 13:12:07 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-07-24 13:12:07 -0500 |
| commit | dbf9a8742745708d7d9da8f1de12e726f06bd7fc (patch) | |
| tree | ea4ca57c0bf5988303d9cb3a15270f81639e18de /src/slskd/Search | |
| parent | dab3ae049eacce25c9e44f16ac82ba27ab0357fc (diff) | |
| download | slskd-dbf9a8742745708d7d9da8f1de12e726f06bd7fc.tar.gz | |
docs
Diffstat (limited to 'src/slskd/Search')
| -rw-r--r-- | src/slskd/Search/ISearchService.cs | 30 | ||||
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 131 |
2 files changed, 79 insertions, 82 deletions
diff --git a/src/slskd/Search/ISearchService.cs b/src/slskd/Search/ISearchService.cs index 9a45280b..4fe15fae 100644 --- a/src/slskd/Search/ISearchService.cs +++ b/src/slskd/Search/ISearchService.cs @@ -21,9 +21,7 @@ namespace slskd.Search using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; - using SearchOptions = Soulseek.SearchOptions; - using SearchQuery = Soulseek.SearchQuery; - using SearchScope = Soulseek.SearchScope; + using Soulseek; /// <summary> /// Handles the lifecycle and persistence of searches. @@ -31,16 +29,6 @@ namespace slskd.Search public interface ISearchService { /// <summary> - /// Performs a search for the specified <paramref name="query"/> and <paramref name="scope"/>. - /// </summary> - /// <param name="id">A unique identifier for the search.</param> - /// <param name="query">The search query.</param> - /// <param name="scope">The search scope.</param> - /// <param name="options">Search options.</param> - /// <returns>The completed search.</returns> - Task<Search> StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); - - /// <summary> /// Deletes the specified ssearch. /// </summary> /// <param name="search">The search to delete.</param> @@ -53,17 +41,27 @@ namespace slskd.Search /// <param name="expression">The expression to use to match searches.</param> /// <param name="includeResponses">A value indicating whether to include search responses in the result.</param> /// <returns>The found search, or default if not found.</returns> - /// <exception cref="ArgumentException">Thrown an expression is not supplied.</exception> + /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> Task<Search> FindAsync(Expression<Func<Search, bool>> expression, bool includeResponses = false); /// <summary> - /// Returns a list of all completed and in-progress searches, with responses omitted. + /// Returns a list of all completed and in-progress searches, with responses omitted, matching the optional <paramref name="expression"/>. /// </summary> /// <param name="expression">An optional expression used to match searches.</param> - /// <returns>The list of searches matching the specified expression.</returns> + /// <returns>The list of searches matching the specified expression, or all searches if no expression is specified.</returns> Task<List<Search>> ListAsync(Expression<Func<Search, bool>> expression = null); /// <summary> + /// Performs a search for the specified <paramref name="query"/> and <paramref name="scope"/>. + /// </summary> + /// <param name="id">A unique identifier for the search.</param> + /// <param name="query">The search query.</param> + /// <param name="scope">The search scope.</param> + /// <param name="options">Search options.</param> + /// <returns>The completed search.</returns> + Task<Search> StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); + + /// <summary> /// Cancels the search matching the specified <paramref name="id"/>, if it is in progress. /// </summary> /// <param name="id">The unique identifier for the search.</param> diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 12c75fb1..54d8bac9 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -70,6 +70,68 @@ namespace slskd.Search private IHubContext<SearchHub> SearchHub { get; set; } /// <summary> + /// Deletes the specified ssearch. + /// </summary> + /// <param name="search">The search to delete.</param> + /// <returns>The operation context.</returns> + public Task DeleteAsync(Search search) + { + if (search == default) + { + throw new ArgumentNullException(nameof(search)); + } + + return DoDeleteAsync(search); + + async Task DoDeleteAsync(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Searches.Remove(search); + await context.SaveChangesAsync(); + + await SearchHub.BroadcastDeleteAsync(search); + } + } + + /// <summary> + /// Finds a single search matching the specified <paramref name="expression"/>. + /// </summary> + /// <param name="expression">The expression to use to match searches.</param> + /// <param name="includeResponses">A value indicating whether to include search responses in the result.</param> + /// <returns>The found search, or default if not found.</returns> + /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> + public Task<Search> FindAsync(Expression<Func<Search, bool>> expression, bool includeResponses = false) + { + if (expression == default) + { + throw new ArgumentException("An expression must be supplied.", nameof(expression)); + } + + using var context = ContextFactory.CreateDbContext(); + + var selector = context.Searches.Where(expression); + + if (!includeResponses) + { + selector = selector.WithoutResponses(); + } + + return selector.FirstOrDefaultAsync(); + } + + /// <summary> + /// Returns a list of all completed and in-progress searches, with responses omitted, matching the optional <paramref name="expression"/>. + /// </summary> + /// <param name="expression">An optional expression used to match searches.</param> + /// <returns>The list of searches matching the specified expression, or all searches if no expression is specified.</returns> + public Task<List<Search>> ListAsync(Expression<Func<Search, bool>> expression = null) + { + expression ??= s => true; + using var context = ContextFactory.CreateDbContext(); + return context.Searches.Where(expression).WithoutResponses().ToListAsync(); + } + + /// <summary> /// Performs a search for the specified <paramref name="query"/> and <paramref name="scope"/>. /// </summary> /// <param name="id">A unique identifier for the search.</param> @@ -117,10 +179,9 @@ namespace slskd.Search }, responseReceived: (args) => rateLimiter.Invoke(() => { - // note: this is rate limited, but has the potential to update - // the database every 250ms (or whatever the interval is set to) - // for the duration of the search. any issues with disk i/o or performance - // while searches are running should investigate this as a cause + // note: this is rate limited, but has the potential to update the database every 250ms (or whatever the + // interval is set to) for the duration of the search. any issues with disk i/o or performance while searches + // are running should investigate this as a cause search.ResponseCount = args.Search.ResponseCount; search.FileCount = args.Search.FileCount; search.LockedFileCount = args.Search.LockedFileCount; @@ -174,68 +235,6 @@ namespace slskd.Search } /// <summary> - /// Deletes the specified ssearch. - /// </summary> - /// <param name="search">The search to delete.</param> - /// <returns>The operation context.</returns> - public Task DeleteAsync(Search search) - { - if (search == default) - { - throw new ArgumentNullException(nameof(search)); - } - - return DoDeleteAsync(search); - - async Task DoDeleteAsync(Search search) - { - using var context = ContextFactory.CreateDbContext(); - context.Searches.Remove(search); - await context.SaveChangesAsync(); - - await SearchHub.BroadcastDeleteAsync(search); - } - } - - /// <summary> - /// Finds a single search matching the specified <paramref name="expression"/>. - /// </summary> - /// <param name="expression">The expression to use to match searches.</param> - /// <param name="includeResponses">A value indicating whether to include search responses in the result.</param> - /// <returns>The found search, or default if not found.</returns> - /// <exception cref="ArgumentException">Thrown an expression is not supplied.</exception> - public Task<Search> FindAsync(Expression<Func<Search, bool>> expression, bool includeResponses = false) - { - if (expression == default) - { - throw new ArgumentException("An expression must be supplied.", nameof(expression)); - } - - using var context = ContextFactory.CreateDbContext(); - - var selector = context.Searches.Where(expression); - - if (!includeResponses) - { - selector = selector.WithoutResponses(); - } - - return selector.FirstOrDefaultAsync(); - } - - /// <summary> - /// Returns a list of all completed and in-progress searches, with responses omitted. - /// </summary> - /// <param name="expression">An optional expression used to match searches.</param> - /// <returns>The list of searches matching the specified expression.</returns> - public Task<List<Search>> ListAsync(Expression<Func<Search, bool>> expression = null) - { - expression ??= s => true; - using var context = ContextFactory.CreateDbContext(); - return context.Searches.Where(expression).WithoutResponses().ToListAsync(); - } - - /// <summary> /// Cancels the search matching the specified <paramref name="id"/>, if it is in progress. /// </summary> /// <param name="id">The unique identifier for the search.</param> |
