diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs index a45d16a6..db612319 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs @@ -24,9 +24,8 @@ public class IndexFunctions private readonly IndexHeadersCache _indexHeadersCache; private readonly InputParser _inputParser; private readonly ProgressionsSearch _progressionsSearch; - private readonly SearchOrderCache _searchOrderCache; - public IndexFunctions(ILogger logger, DownstreamApiClient downstreamApiClient, ProgressionsCache progressionsCache, LoopsStatisticsCache loopsStatisticsCache, SecurityContext securityContext, IndexHeadersCache indexHeadersCache, InputParser inputParser, ProgressionsSearch progressionsSearch, SearchOrderCache searchOrderCache) + public IndexFunctions(ILogger logger, DownstreamApiClient downstreamApiClient, ProgressionsCache progressionsCache, LoopsStatisticsCache loopsStatisticsCache, SecurityContext securityContext, IndexHeadersCache indexHeadersCache, InputParser inputParser, ProgressionsSearch progressionsSearch) { _logger = logger; _downstreamApiClient = downstreamApiClient; @@ -35,7 +34,6 @@ public IndexFunctions(ILogger logger, DownstreamApiClient downst _indexHeadersCache = indexHeadersCache; _inputParser = inputParser; _progressionsSearch = progressionsSearch; - _searchOrderCache = searchOrderCache; securityContext.InitService(); } @@ -165,20 +163,4 @@ public async Task VDevFindAndCount([HttpTrigger(AuthorizationLeve { return new OkObjectResult(_progressionsSearch.Search((await _progressionsCache.Get()).Values, _inputParser.Parse(searchQuery)).Count); } - - [Function(nameof(VDevFindTop100))] - public async Task VDevFindTop100([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, string searchQuery) - { - var progressions = await _progressionsCache.Get(); - var progressionsReverse = progressions.ToDictionary(x => (ISearchableChordsProgression)x.Value, x => x.Key); - var searchOrder = await _searchOrderCache.Get(); - var headers = await _indexHeadersCache.Get(); - var found = _progressionsSearch.Search(searchOrder.Select(x => progressions[x]), _inputParser.Parse(searchQuery), 100); - - return new OkObjectResult(found.Select(x => new - { - Header = headers.Headers[progressionsReverse[x.Key]], - Coverage = x.Value, - }).ToList()); - } } \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VExternal1/Search.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VExternal1/Search.cs index 0a1ba176..46caf68a 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VExternal1/Search.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VExternal1/Search.cs @@ -23,19 +23,17 @@ public class Search : ServiceFunctionBase private readonly ProgressionsCache _progressionsCache; private readonly IndexHeadersCache _indexHeadersCache; private readonly ProgressionsSearch _progressionsSearch; - private readonly SearchOrderCache _searchOrderCache; private readonly InputParser _inputParser; private readonly DownstreamApiClient _downstreamApiClient; private readonly IndexApiOptions _options; private readonly IndexApiClient _indexApiClient; - public Search(ILoggerFactory loggerFactory, SecurityContext securityContext, ProgressionsCache progressionsCache, IndexHeadersCache indexHeadersCache, ProgressionsSearch progressionsSearch, SearchOrderCache searchOrderCache, InputParser inputParser, DownstreamApiClient downstreamApiClient, ConcurrencyLimiter concurrencyLimiter, IOptions options, IndexApiClient indexApiClient) + public Search(ILoggerFactory loggerFactory, SecurityContext securityContext, ProgressionsCache progressionsCache, IndexHeadersCache indexHeadersCache, ProgressionsSearch progressionsSearch, InputParser inputParser, DownstreamApiClient downstreamApiClient, ConcurrencyLimiter concurrencyLimiter, IOptions options, IndexApiClient indexApiClient) : base(loggerFactory, securityContext, concurrencyLimiter, options.Value.RedirectCachesToIndex) { _progressionsCache = progressionsCache; _indexHeadersCache = indexHeadersCache; _progressionsSearch = progressionsSearch; - _searchOrderCache = searchOrderCache; _inputParser = inputParser; _downstreamApiClient = downstreamApiClient; _indexApiClient = indexApiClient; @@ -54,9 +52,12 @@ protected override async Task Execute(SearchRequest request) } var progressions = await _progressionsCache.Get(); - var searchOrder = await _searchOrderCache.Get(); var headers = await _indexHeadersCache.Get(); - var found = _progressionsSearch.Search(searchOrder.Select(x => progressions[x]), _inputParser.Parse(request.Query)); + var found = _progressionsSearch.Search( + headers.Headers.Values.Join(progressions, h => h.ExternalId, p => p.Key, (h, p) => (h, p)) + .Where(x => !string.IsNullOrWhiteSpace(x.h.Title) && x.h.Artists?.Any(a => !string.IsNullOrWhiteSpace(a)) == true && !string.IsNullOrWhiteSpace(_downstreamApiClient.GetSourceTitle(x.h.Source))) + .Select(x => x.p.Value), + _inputParser.Parse(request.Query)); var results = headers.Headers .Select(h => progressions.TryGetValue(h.Key, out var progression) && found.TryGetValue(progression, out var coverage) diff --git a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/IServiceCollectionExtensions.cs b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/IServiceCollectionExtensions.cs index c1afc9ec..c46ae115 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/IServiceCollectionExtensions.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/IServiceCollectionExtensions.cs @@ -13,7 +13,6 @@ public static IServiceCollection AddIndexBusinessLogic(this IServiceCollection s .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddIndexAnalysis() .Configure(o => configuration.GetSection(nameof(FileCacheBaseOptions)).Bind(o)); } \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/MemoryCacheBase.cs b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/MemoryCacheBase.cs deleted file mode 100644 index 58702bb0..00000000 --- a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/MemoryCacheBase.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Microsoft.Extensions.Logging; -using Nito.AsyncEx; - -namespace HarmonyDB.Index.BusinessLogic.Services; - -public abstract class MemoryCacheBase -{ - private readonly AsyncLock _lock = new(); - private TPresentationModel? _presentationModel; - - protected MemoryCacheBase(ILogger> logger) - { - Logger = logger; - } - - protected abstract string Key { get; } - - protected ILogger> Logger { get; } - - protected abstract Task GetInputModel(); - - protected abstract TPresentationModel GetPresentationModel(TInputModel inputModel); - - public async Task Get() - { - if (_presentationModel != null) return _presentationModel; - using var _ = await _lock.LockAsync(); - if (_presentationModel != null) return _presentationModel; - - var started = DateTime.Now; - var inputModel = await GetInputModel(); - Logger.LogInformation("Getting the input model for the {type} memory cache took {time:N0} ms", Key, (DateTime.Now - started).TotalMilliseconds); - started = DateTime.Now; - _presentationModel = GetPresentationModel(inputModel); - Logger.LogInformation("Building the presentation model for the {type} memory cache took {time:N0} ms", Key, (DateTime.Now - started).TotalMilliseconds); - - return _presentationModel; - } -} \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/SearchOrderCache.cs b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/SearchOrderCache.cs deleted file mode 100644 index 996749a6..00000000 --- a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/SearchOrderCache.cs +++ /dev/null @@ -1,33 +0,0 @@ -using HarmonyDB.Index.BusinessLogic.Models; -using Microsoft.Extensions.Logging; - -namespace HarmonyDB.Index.BusinessLogic.Services; - -public class SearchOrderCache : MemoryCacheBase<(IndexHeaders indexHeaders, IReadOnlyList progressionsExternalIds), IReadOnlyList> -{ - private readonly ProgressionsCache _progressionsCache; - private readonly IndexHeadersCache _indexHeadersCache; - - public SearchOrderCache(ILogger), IReadOnlyList>> logger, ProgressionsCache progressionsCache, IndexHeadersCache indexHeadersCache) - : base(logger) - { - _progressionsCache = progressionsCache; - _indexHeadersCache = indexHeadersCache; - } - - protected override string Key => "SearchOrder"; - - protected override async Task<(IndexHeaders, IReadOnlyList)> GetInputModel() - { - return (await _indexHeadersCache.Get(), (await _progressionsCache.Get()).Keys.ToList()); - } - - protected override IReadOnlyList GetPresentationModel((IndexHeaders indexHeaders, IReadOnlyList progressionsExternalIds) inputModel) - { - return inputModel.indexHeaders.Headers - .Join(inputModel.progressionsExternalIds, x => x.Key, x => x, (x, y) => x.Value) - .OrderByDescending(x => x.Rating) - .Select(x => x.ExternalId) - .ToList(); - } -} \ No newline at end of file