From d2fb26b10317e0d90d05039e639b523cc305f115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 19 Apr 2021 13:06:03 +0800 Subject: [PATCH] Bring Legacy records together and remove some useless code for JsonStorage --- .../Storage/JsonStorage.cs | 13 +-- Flow.Launcher/Storage/UserSelectedRecord.cs | 97 +++++++++++-------- Flow.Launcher/ViewModel/MainViewModel.cs | 17 +--- 3 files changed, 64 insertions(+), 63 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index 37cfec25258..833ec087d6e 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -11,7 +11,6 @@ namespace Flow.Launcher.Infrastructure.Storage /// public class JsonStrorage where T : new() { - private readonly JsonSerializerOptions _serializerSettings; protected T _data; // need a new directory name public const string DirectoryName = "Settings"; @@ -20,16 +19,6 @@ namespace Flow.Launcher.Infrastructure.Storage public string DirectoryPath { get; set; } - internal JsonStrorage() - { - // use property initialization instead of DefaultValueAttribute - // easier and flexible for default value of object - _serializerSettings = new JsonSerializerOptions - { - IgnoreNullValues = false - }; - } - public T Load() { if (File.Exists(FilePath)) @@ -55,7 +44,7 @@ private void Deserialize(string searlized) { try { - _data = JsonSerializer.Deserialize(searlized, _serializerSettings); + _data = JsonSerializer.Deserialize(searlized); } catch (JsonException e) { diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index 668376dac22..8d8956cfa4e 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -5,6 +5,7 @@ using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Plugin; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.Storage { @@ -14,72 +15,90 @@ public class UserSelectedRecord private const int HASH_INITIAL = 23; [JsonInclude] - public Dictionary records { get; private set; } + public Dictionary recordsWithQuery { get; private set; } + + [JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public Dictionary records { get; private set; } + public UserSelectedRecord() { - records = new Dictionary(); + recordsWithQuery = new Dictionary(); } - private static int GenerateCustomHashCode(Query query, Result result) + private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL) { - int hashcode = HASH_INITIAL; - unchecked { // skip the empty space // https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast // optimization without information lost when int overflow - - for (int i = 0; i < query.ActionKeyword.Length; i++) - { - char item = query.ActionKeyword[i]; - hashcode = hashcode * HASH_MULTIPLIER + item; - } - - for (int i = 0; i < query.Search.Length; i++) - { - char item = query.Search[i]; - hashcode = hashcode * HASH_MULTIPLIER + item; - } - for (int i = 0; i < result.Title.Length; i++) + for (int i = 0; i < s.Length; i++) { - char item = result.Title[i]; - hashcode = hashcode * HASH_MULTIPLIER + item; + start = start * HASH_MULTIPLIER + s[i]; } - for (int i = 0; i < result.SubTitle.Length; i++) + return start; + } + } + + private static int GenerateResultHashCode(Result result) + { + int hashcode = GenerateStaticHashCode(result.Title); + return GenerateStaticHashCode(result.SubTitle, hashcode); + } + + private static int GenerateQueryAndResultHashCode(Query query, Result result) + { + int hashcode = GenerateStaticHashCode(query.ActionKeyword); + hashcode = GenerateStaticHashCode(query.Search, hashcode); + hashcode = GenerateStaticHashCode(result.Title, hashcode); + hashcode = GenerateStaticHashCode(result.SubTitle, hashcode); + + return hashcode; + } + + private void TransformOldRecords() + { + if (records != null) + { + var localRecords = records; + records = null; + + foreach (var pair in localRecords) { - char item = result.SubTitle[i]; - hashcode = hashcode * HASH_MULTIPLIER + item; + recordsWithQuery.TryAdd(GenerateStaticHashCode(pair.Key), pair.Value); } - return hashcode; - } } public void Add(Result result) { - var key = GenerateCustomHashCode(result.OriginQuery, result); - if (records.ContainsKey(key)) - { - records[key]++; - } - else - { - records.Add(key, 1); + TransformOldRecords(); - } + var keyWithQuery = GenerateQueryAndResultHashCode(result.OriginQuery, result); + + if (!recordsWithQuery.TryAdd(keyWithQuery, 1)) + recordsWithQuery[keyWithQuery]++; + + var keyWithoutQuery = GenerateResultHashCode(result); + + if (!recordsWithQuery.TryAdd(keyWithoutQuery, 1)) + recordsWithQuery[keyWithoutQuery]++; } public int GetSelectedCount(Result result) { - if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value)) - { - return value; - } - return 0; + var selectedCount = 0; + + recordsWithQuery.TryGetValue(GenerateQueryAndResultHashCode(result.OriginQuery, result), out int value); + selectedCount += value * 5; + + recordsWithQuery.TryGetValue(GenerateResultHashCode(result), out value); + selectedCount += value; + + return selectedCount; } } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index c1382e51eec..56ab0ff1d4f 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -43,7 +43,6 @@ public class MainViewModel : BaseModel, ISavable private CancellationTokenSource _updateSource; private CancellationToken _updateToken; - private bool _saved; private readonly Internationalization _translator = InternationalizationManager.Instance; @@ -56,7 +55,6 @@ public class MainViewModel : BaseModel, ISavable public MainViewModel(Settings settings) { - _saved = false; _queryTextBeforeLeaveResults = ""; _queryText = ""; _lastQuery = new Query(); @@ -540,7 +538,7 @@ async Task QueryTask(PluginPair plugin) var results = await PluginManager.QueryForPlugin(plugin, query, currentCancellationToken); if (currentCancellationToken.IsCancellationRequested || results == null) return; - + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken))) { Log.Error("MainViewModel", "Unable to add item to Result Update Queue"); @@ -765,14 +763,9 @@ private void ToggleFlowLauncher() public void Save() { - if (!_saved) - { - _historyItemsStorage.Save(); - _userSelectedRecordStorage.Save(); - _topMostRecordStorage.Save(); - - _saved = true; - } + _historyItemsStorage.Save(); + _userSelectedRecordStorage.Save(); + _topMostRecordStorage.Save(); } /// @@ -813,7 +806,7 @@ public void UpdateResultView(IEnumerable resultsForUpdates) else { var priorityScore = metaResults.Metadata.Priority * 150; - result.Score += _userSelectedRecord.GetSelectedCount(result) * 5 + priorityScore; + result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; } } }