From fdf5a546e1a33ea5efd507bfec0859b7ad56c174 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 May 2021 22:24:39 -0500 Subject: simplify api, don't persist results until search is complete --- src/slskd/Core/Search/ISearchService.cs | 2 +- src/slskd/Core/Search/SearchService.cs | 120 +++++++++++--------------------- 2 files changed, 41 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Search/ISearchService.cs b/src/slskd/Core/Search/ISearchService.cs index 7f44abb7..44d3edb9 100644 --- a/src/slskd/Core/Search/ISearchService.cs +++ b/src/slskd/Core/Search/ISearchService.cs @@ -24,7 +24,7 @@ namespace slskd.Search public interface ISearchService { - Task<(Guid Id, Task Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null); + Task SearchAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); bool TryCancel(Guid id); Task FindAsync(Expression> expression, bool includeResponses = false); } diff --git a/src/slskd/Core/Search/SearchService.cs b/src/slskd/Core/Search/SearchService.cs index 2e513b34..f1818423 100644 --- a/src/slskd/Core/Search/SearchService.cs +++ b/src/slskd/Core/Search/SearchService.cs @@ -19,6 +19,7 @@ namespace slskd.Search { using System; using System.Collections.Concurrent; + using System.Collections.Generic; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; @@ -42,85 +43,57 @@ namespace slskd.Search private ILogger Log { get; set; } private ISoulseekClient Client { get; } - private ConcurrentDictionary InProgress { get; } + private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); - public async Task<(Guid Id, Task Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null) + public async Task SearchAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null) { - scope ??= SearchScope.Network; - options ??= new SearchOptions(); - id ??= Guid.NewGuid(); - var token = Client.GetNextToken(); var cancellationTokenSource = new CancellationTokenSource(); - var record = new Search() + var search = new Search() { SearchText = query.SearchText, Token = token, - Id = id.Value, + Id = id, State = SearchStates.Requested, StartedAt = DateTime.UtcNow, }; - options = options.WithActions(stateChanged: (args) => UpdateState(record, args), (args) => UpdateState(record, args)); - - try - { - using var context = ContextFactory.CreateDbContext(); - context.Add(record); - await context.SaveChangesAsync(); - } - catch (Exception ex) - { - Log.LogError(ex, "Failed to save search: {Message}", ex.Message); - } - - InProgress.TryAdd(id.Value, cancellationTokenSource); - - var task = Task.Run(async () => - { - var completedSearch = await Client.SearchAsync( - query, - responseReceived: (response) => AddResponse(id.Value, response), - scope, - token, - options, - cancellationToken: cancellationTokenSource.Token); - - return Search.FromSoulseekSearch(completedSearch); - }); - - _ = task.ContinueWith(async task => - { - Console.WriteLine($"Continued..."); + options ??= new SearchOptions(); + options = options.WithActions(stateChanged: (args) => UpdateState(search, args.Search), (args) => UpdateState(search, args.Search)); - try - { - var search = await task; - record.State = search.State; - record.EndedAt = DateTime.UtcNow; + using var context = ContextFactory.CreateDbContext(); - Console.WriteLine($"Saving with state {record.State}"); + context.Add(search); + await context.SaveChangesAsync(); - using var context = ContextFactory.CreateDbContext(); - context.Update(record); - await context.SaveChangesAsync(); + CancellationTokens.TryAdd(id, cancellationTokenSource); + var responses = new List(); - Console.WriteLine($"Saved"); - } - finally + var soulseekSearch = await Client.SearchAsync( + query, + responseReceived: (response) => { - InProgress.TryRemove(id.Value, out _); - } - }); + responses.Add(Response.FromSoulseekSearchResponse(response)); + }, + scope, + token, + options, + cancellationToken: cancellationTokenSource.Token); + + search.FileCount = soulseekSearch.FileCount; + search.LockedFileCount = soulseekSearch.LockedFileCount; + search.ResponseCount = soulseekSearch.ResponseCount; + search.State = soulseekSearch.State; + search.Responses = responses; - return (id.Value, task); + return search; } public bool TryCancel(Guid id) { - if (InProgress.TryGetValue(id, out var cts)) + if (CancellationTokens.TryGetValue(id, out var cts)) { cts.Cancel(); return true; @@ -134,41 +107,28 @@ namespace slskd.Search using var context = ContextFactory.CreateDbContext(); var search = await context .Searches - .Include(s => s.Responses) - .ThenInclude(r => r.Files).AsSplitQuery() + .Select(search => search.) .FirstOrDefaultAsync(expression); - //if (search == default) - //{ - // return default; - //} - - //if (includeResponses) - //{ - // await context.Entry(search).Collection(s => s.Responses).LoadAsync(); - //} + if (!includeResponses) + { + } return search; } - private void AddResponse(Guid searchId, Soulseek.SearchResponse response) + private void UpdateState(Search search, Soulseek.Search soulseekSearch) { _ = Task.Run(async () => { - using var context = ContextFactory.CreateDbContext(); - context.Add(SearchResponse.FromSoulseekSearchResponse(response, searchId)); - await context.SaveChangesAsync(); - }).ContinueWith(task => - Log.LogError(task.Exception, "Failed to save search response for {SearchId} from {Username}", searchId, response.Username), - TaskContinuationOptions.OnlyOnFaulted); - } + search.FileCount = soulseekSearch.FileCount; + search.LockedFileCount = soulseekSearch.LockedFileCount; + search.ResponseCount = soulseekSearch.ResponseCount; + search.State = soulseekSearch.State; - private void UpdateState(Search search, SearchEventArgs args) - { - _ = Task.Run(async () => - { var context = ContextFactory.CreateDbContext(); - context.Update(search.WithSoulseekSearch(args.Search)); + + context.Update(search); await context.SaveChangesAsync(); }).ContinueWith(task => Log.LogError(task.Exception, "Failed to update state of {SearchId}", search.Id), -- cgit v1.2.3