Merge pull request #293 from taooceros/SelectedCountOptimize

fix TopMostRecord and UserSelectedRecord
This commit is contained in:
Jeremy Wu 2021-01-17 16:28:55 +11:00 committed by GitHub
commit 5430756770
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 12 deletions

View file

@ -9,7 +9,7 @@ namespace Flow.Launcher.Infrastructure.Storage
/// <summary>
/// Serialize object using json format.
/// </summary>
public class JsonStrorage<T>
public class JsonStrorage<T> where T : new()
{
private readonly JsonSerializerOptions _serializerSettings;
private T _data;
@ -76,7 +76,7 @@ namespace Flow.Launcher.Infrastructure.Storage
BackupOriginFile();
}
_data = JsonSerializer.Deserialize<T>("{}", _serializerSettings);
_data = new T();
Save();
}

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Storage
@ -8,20 +9,24 @@ namespace Flow.Launcher.Storage
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
{
private Dictionary<string, Record> records = new Dictionary<string, Record>();
/// <summary>
/// You should not directly access this field
/// <para>
/// It is public due to System.Text.Json limitation in version 3.1
/// </para>
/// </summary>
/// TODO: Set it to private
public Dictionary<string, Record> records { get; set; } = new Dictionary<string, Record>();
internal bool IsTopMost(Result result)
{
if (records.Count == 0)
if (records.Count == 0 || !records.ContainsKey(result.OriginQuery.RawQuery))
{
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);
// since this dictionary should be very small (or empty) going over it should be pretty fast.
return records[result.OriginQuery.RawQuery].Equals(result);
}
internal void Remove(Result result)
@ -53,5 +58,12 @@ namespace Flow.Launcher.Storage
public string Title { get; set; }
public string SubTitle { get; set; }
public string PluginID { get; set; }
public bool Equals(Result r)
{
return Title == r.Title
&& SubTitle == r.SubTitle
&& PluginID == r.PluginID;
}
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin;
@ -6,14 +7,27 @@ namespace Flow.Launcher.Storage
{
public class UserSelectedRecord
{
private Dictionary<string, int> records = new Dictionary<string, int>();
/// <summary>
/// You should not directly access this field
/// <para>
/// It is public due to System.Text.Json limitation in version 3.1
/// </para>
/// </summary>
/// TODO: Set it to private
[JsonPropertyName("records")]
public Dictionary<string, int> records { get; set; }
public UserSelectedRecord()
{
records = new Dictionary<string, int>();
}
public void Add(Result result)
{
var key = result.ToString();
if (records.TryGetValue(key, out int value))
if (records.ContainsKey(key))
{
records[key] = value + 1;
records[key]++;
}
else
{