From 8feb789ba5dd4729ea0a7fbafdccd86e9e06f74c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 26 May 2021 23:48:13 -0500 Subject: rough in SearchService --- src/slskd/Core/Search/ISearchService.cs | 5 +- src/slskd/Core/Search/SearchService.cs | 94 ++++++++++++++++++++++----------- 2 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/slskd/Core/Search/ISearchService.cs b/src/slskd/Core/Search/ISearchService.cs index 0182beab..1936b30f 100644 --- a/src/slskd/Core/Search/ISearchService.cs +++ b/src/slskd/Core/Search/ISearchService.cs @@ -18,13 +18,14 @@ namespace slskd.Search { using System; + using System.Linq.Expressions; using System.Threading.Tasks; using Soulseek; public interface ISearchService { - Task BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null); + Task<(Guid Id, Task Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null); bool TryCancel(Guid id); - Task FindAsync(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 0f69f632..f0632211 100644 --- a/src/slskd/Core/Search/SearchService.cs +++ b/src/slskd/Core/Search/SearchService.cs @@ -19,76 +19,86 @@ namespace slskd.Search { using System; using System.Collections.Concurrent; + using System.Linq.Expressions; + using System.Text.Json; using System.Threading; using System.Threading.Tasks; + using Microsoft.EntityFrameworkCore; using Soulseek; public class SearchService : ISearchService { - public SearchService(SearchDbContext context, ISoulseekClient client) + public SearchService(IDbContextFactory contextFactory, ISoulseekClient client) { - Context = context; + ContextFactory = contextFactory; Client = client; - - Client.SearchStateChanged += Client_SearchStateChanged; - Client.SearchRequestReceived += Client_SearchRequestReceived; } - private void Client_SearchRequestReceived(object sender, SearchRequestEventArgs e) - { - throw new NotImplementedException(); - } - - private void Client_SearchStateChanged(object sender, SearchStateChangedEventArgs e) - { - throw new NotImplementedException(); - } - - private SearchDbContext Context { get; } + private IDbContextFactory ContextFactory { get; } private ISoulseekClient Client { get; } private ConcurrentDictionary Lookup { get; } = new ConcurrentDictionary(); private ConcurrentDictionary InProgress { get; } = new ConcurrentDictionary(); - public async Task BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null) + public async Task<(Guid Id, Task Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null) { scope = scope ?? SearchScope.Network; var token = Client.GetNextToken(); - options = options ?? new SearchOptions(); + id = id ?? Guid.NewGuid(); + var cts = new CancellationTokenSource(); - var id = Guid.NewGuid(); var record = new Search() { SearchText = query.SearchText, Token = token, - Id = id, + Id = id.Value, State = SearchStates.Requested, + StartedAt = DateTime.UtcNow, }; - Context.Add(record); - await Context.SaveChangesAsync(); + options = options ?? new SearchOptions(stateChanged: (args) => + { + record.State = args.Search.State; + ContextFactory.CreateDbContext().Update(record); + }); + + using var context = ContextFactory.CreateDbContext(); + context.Add(record); + context.SaveChanges(); InProgress.TryAdd(token, cts); - Lookup.TryAdd(id, token); + Lookup.TryAdd(id.Value, token); + + var task = Client.SearchAsync(query, responseReceived: (response) => AddResponse(id.Value, response), scope, token, options, cancellationToken: cts.Token); - _ = Client.SearchAsync(query, scope, token, options, cancellationToken: cts.Token).ContinueWith(async task => + _ = task.ContinueWith(async task => { + Console.WriteLine($"Continued..."); + try { - var result = await task; + var search = await task; + record.State = search.State; + record.EndedAt = DateTime.UtcNow; + + Console.WriteLine($"Saving with state {record.State}"); - // todo: update db with status and end time + using var context = ContextFactory.CreateDbContext(); + context.Update(record); + context.SaveChanges(); + + Console.WriteLine($"Saved"); } finally { InProgress.TryRemove(token, out _); - Lookup.TryRemove(id, out _); + Lookup.TryRemove(id.Value, out _); } }); - return id; + return (id.Value, task); } public bool TryCancel(Guid id) @@ -102,9 +112,33 @@ namespace slskd.Search return false; } - public Task FindAsync(Guid id) + public async Task FindAsync(Expression> expression, bool includeResponses = true) + { + using var context = ContextFactory.CreateDbContext(); + var search = await context + .Searches + .Include(s => s.Responses) + .ThenInclude(r => r.Files) + .FirstOrDefaultAsync(expression); + + //if (search == default) + //{ + // return default; + //} + + //if (includeResponses) + //{ + // await context.Entry(search).Collection(s => s.Responses).LoadAsync(); + //} + + return search; + } + + private void AddResponse(Guid searchId, Soulseek.SearchResponse soulseekResponse) { - return Context.Searches.FindAsync(id).AsTask(); + using var context = ContextFactory.CreateDbContext(); + context.Add(SearchResponse.FromSoulseekSearchResponse(soulseekResponse, searchId)); + context.SaveChanges(); } } } -- cgit v1.2.3