Skip to content

Commit af087fb

Browse files
committed
Support multiple topmost records storage
1 parent 0d61908 commit af087fb

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public bool Exists()
5050
return File.Exists(FilePath);
5151
}
5252

53+
public void Delete()
54+
{
55+
if (File.Exists(FilePath))
56+
{
57+
File.Delete(FilePath);
58+
}
59+
if (File.Exists(BackupFilePath))
60+
{
61+
File.Delete(BackupFilePath);
62+
}
63+
if (File.Exists(TempFilePath))
64+
{
65+
File.Delete(TempFilePath);
66+
}
67+
}
68+
5369
public async Task<T> LoadAsync()
5470
{
5571
if (Data != null)

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
1-
using System.Collections.Concurrent;
1+
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Text.Json;
46
using System.Text.Json.Serialization;
57
using Flow.Launcher.Infrastructure.Storage;
68
using Flow.Launcher.Plugin;
79

810
namespace Flow.Launcher.Storage
911
{
10-
public class FlowLauncherJsonStorageTopMostRecord : ISavable
12+
public class FlowLauncherJsonStorageTopMostRecord
1113
{
1214
private readonly FlowLauncherJsonStorage<MultipleTopMostRecord> _topMostRecordStorage;
1315
private readonly MultipleTopMostRecord _topMostRecord;
1416

1517
public FlowLauncherJsonStorageTopMostRecord()
1618
{
19+
// Get old data & new data
1720
var topMostRecordStorage = new FlowLauncherJsonStorage<TopMostRecord>();
18-
var exist = topMostRecordStorage.Exists();
19-
if (exist)
20-
{
21-
// Get old data
22-
var topMostRecord = topMostRecordStorage.Load();
21+
_topMostRecordStorage = new FlowLauncherJsonStorage<MultipleTopMostRecord>();
22+
23+
// Check if data exist
24+
var oldDataExist = topMostRecordStorage.Exists();
25+
var newDataExist = _topMostRecordStorage.Exists();
2326

24-
// Convert to new data
25-
_topMostRecordStorage = new FlowLauncherJsonStorage<MultipleTopMostRecord>();
27+
// If new data exist, it means we have already migrated the old data
28+
// So we can safely delete the old data and load the new data
29+
if (newDataExist)
30+
{
31+
try
32+
{
33+
topMostRecordStorage.Delete();
34+
}
35+
catch
36+
{
37+
// Ignored
38+
}
2639
_topMostRecord = _topMostRecordStorage.Load();
27-
_topMostRecord.Add(topMostRecord);
2840
}
41+
// If new data does not exist and old data exist, we need to migrate the old data to the new data
42+
else if (oldDataExist)
43+
{
44+
// Migrate old data to new data
45+
_topMostRecord = _topMostRecordStorage.Load();
46+
_topMostRecord.Add(topMostRecordStorage.Load());
47+
48+
// Delete old data and save the new data
49+
try
50+
{
51+
topMostRecordStorage.Delete();
52+
}
53+
catch
54+
{
55+
// Ignored
56+
}
57+
Save();
58+
}
59+
// If both data do not exist, we just need to create a new data
2960
else
3061
{
31-
// Get new data
32-
_topMostRecordStorage = new FlowLauncherJsonStorage<MultipleTopMostRecord>();
3362
_topMostRecord = _topMostRecordStorage.Load();
3463
}
3564
}
@@ -109,6 +138,7 @@ internal void AddOrUpdate(Result result)
109138
public class MultipleTopMostRecord
110139
{
111140
[JsonInclude]
141+
[JsonConverter(typeof(ConcurrentDictionaryConcurrentBagConverter))]
112142
public ConcurrentDictionary<string, ConcurrentBag<Record>> records { get; private set; } = new();
113143

114144
internal void Add(TopMostRecord topMostRecord)
@@ -207,6 +237,30 @@ internal void AddOrUpdate(Result result)
207237
}
208238
}
209239

240+
public class ConcurrentDictionaryConcurrentBagConverter : JsonConverter<ConcurrentDictionary<string, ConcurrentBag<Record>>>
241+
{
242+
public override ConcurrentDictionary<string, ConcurrentBag<Record>> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
243+
{
244+
var dictionary = JsonSerializer.Deserialize<Dictionary<string, List<Record>>>(ref reader, options);
245+
var concurrentDictionary = new ConcurrentDictionary<string, ConcurrentBag<Record>>();
246+
foreach (var kvp in dictionary)
247+
{
248+
concurrentDictionary.TryAdd(kvp.Key, new ConcurrentBag<Record>(kvp.Value));
249+
}
250+
return concurrentDictionary;
251+
}
252+
253+
public override void Write(Utf8JsonWriter writer, ConcurrentDictionary<string, ConcurrentBag<Record>> value, JsonSerializerOptions options)
254+
{
255+
var dict = new Dictionary<string, List<Record>>();
256+
foreach (var kvp in value)
257+
{
258+
dict.Add(kvp.Key, kvp.Value.ToList());
259+
}
260+
JsonSerializer.Serialize(writer, dict, options);
261+
}
262+
}
263+
210264
public class Record
211265
{
212266
public string Title { get; init; }

0 commit comments

Comments
 (0)