From c90dd0e818201131bf578f6f904c4c8a59113b90 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Tue, 3 Dec 2019 23:02:24 +0200 Subject: [PATCH] updated top most record --- Wox.Infrastructure/Storage/WoxJsonStorage.cs | 4 +- Wox.Plugin/Result.cs | 2 +- Wox/Storage/TopMostRecord.cs | 49 +++++++++++--------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Wox.Infrastructure/Storage/WoxJsonStorage.cs b/Wox.Infrastructure/Storage/WoxJsonStorage.cs index f117aee229..da0dbd073d 100644 --- a/Wox.Infrastructure/Storage/WoxJsonStorage.cs +++ b/Wox.Infrastructure/Storage/WoxJsonStorage.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Wox.Infrastructure.Storage { - class WoxJsonStorage : JsonStrorage where T : new() + public class WoxJsonStorage : JsonStrorage where T : new() { public WoxJsonStorage() { @@ -18,4 +18,4 @@ namespace Wox.Infrastructure.Storage FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}"); } } -} +} \ No newline at end of file diff --git a/Wox.Plugin/Result.cs b/Wox.Plugin/Result.cs index 2f74591703..592bcac5c6 100644 --- a/Wox.Plugin/Result.cs +++ b/Wox.Plugin/Result.cs @@ -108,7 +108,7 @@ namespace Wox.Plugin public object ContextData { get; set; } /// - /// Plugin ID that generate this result + /// Plugin ID that generated this result /// public string PluginID { get; internal set; } } diff --git a/Wox/Storage/TopMostRecord.cs b/Wox/Storage/TopMostRecord.cs index 4d33f91ebb..38cfbab8e0 100644 --- a/Wox/Storage/TopMostRecord.cs +++ b/Wox/Storage/TopMostRecord.cs @@ -1,47 +1,52 @@ using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; +using Newtonsoft.Json; using Wox.Infrastructure.Storage; using Wox.Plugin; namespace Wox.Storage { + // todo this class is not thread safe.... but used from multiple threads. public class TopMostRecord { - public Dictionary records = new Dictionary(); + [JsonProperty] + private Dictionary records = new Dictionary(); internal bool IsTopMost(Result result) { + if (records.Count == 0) + { + return false; + } + + // since this dictionary should be very small (or empty) going over it should be pretty fast. return records.Any(o => o.Value.Title == result.Title - && o.Value.SubTitle == result.SubTitle - && o.Value.PluginID == result.PluginID - && o.Key == result.OriginQuery.RawQuery); + && o.Value.SubTitle == result.SubTitle + && o.Value.PluginID == result.PluginID + && o.Key == result.OriginQuery.RawQuery); } internal void Remove(Result result) { - if (records.ContainsKey(result.OriginQuery.RawQuery)) - { - records.Remove(result.OriginQuery.RawQuery); - } + records.Remove(result.OriginQuery.RawQuery); } internal void AddOrUpdate(Result result) { - if (records.ContainsKey(result.OriginQuery.RawQuery)) + var record = new Record { - records[result.OriginQuery.RawQuery].Title = result.Title; - records[result.OriginQuery.RawQuery].SubTitle = result.SubTitle; - records[result.OriginQuery.RawQuery].PluginID = result.PluginID; - } - else - { - records.Add(result.OriginQuery.RawQuery, new Record - { - PluginID = result.PluginID, - Title = result.Title, - SubTitle = result.SubTitle - }); - } + PluginID = result.PluginID, + Title = result.Title, + SubTitle = result.SubTitle + }; + records[result.OriginQuery.RawQuery] = record; + + } + + public void Load(Dictionary dictionary) + { + records = dictionary; } }