aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-27 22:24:39 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-27 22:24:39 -0500
commitfdf5a546e1a33ea5efd507bfec0859b7ad56c174 (patch)
treee76c788591bc470e01f58ef1af37ce6f199011c8
parent1774865e4027b1233d497d66396677699ee25341 (diff)
downloadslskd-fdf5a546e1a33ea5efd507bfec0859b7ad56c174.tar.gz
simplify api, don't persist results until search is complete
-rw-r--r--src/slskd/Core/Search/ISearchService.cs2
-rw-r--r--src/slskd/Core/Search/SearchService.cs120
2 files changed, 41 insertions, 81 deletions
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<Search> Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null);
+ Task<Search> SearchAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null);
bool TryCancel(Guid id);
Task<Search> FindAsync(Expression<Func<Search, bool>> 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<SearchService> Log { get; set; }
private ISoulseekClient Client { get; }
- private ConcurrentDictionary<Guid, CancellationTokenSource> InProgress { get; }
+ private ConcurrentDictionary<Guid, CancellationTokenSource> CancellationTokens { get; }
= new ConcurrentDictionary<Guid, CancellationTokenSource>();
- public async Task<(Guid Id, Task<Search> Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null)
+ public async Task<Search> 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<Response>();
- 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),