Skip to content

Commit e9b392d

Browse files
committed
Optimized sorted storage
1 parent 9d7c7e3 commit e9b392d

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

v2rayN/v2rayN/Handler/ProfileExHandler.cs

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,32 @@ namespace v2rayN.Handler
77
internal class ProfileExHandler
88
{
99
private static readonly Lazy<ProfileExHandler> _instance = new(() => new());
10-
private ConcurrentBag<ProfileExItem> _lstProfileEx;
10+
private ConcurrentBag<ProfileExItem> _lstProfileEx = [];
1111
private Queue<string> _queIndexIds = new();
1212
public ConcurrentBag<ProfileExItem> ProfileExs => _lstProfileEx;
1313
public static ProfileExHandler Instance => _instance.Value;
1414

1515
public ProfileExHandler()
1616
{
1717
Init();
18-
}
19-
20-
private void Init()
21-
{
22-
SQLiteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
23-
24-
_lstProfileEx = new(SQLiteHelper.Instance.Table<ProfileExItem>());
2518

2619
Task.Run(async () =>
2720
{
2821
while (true)
2922
{
30-
var cnt = _queIndexIds.Count;
31-
for (int i = 0; i < cnt; i++)
32-
{
33-
var id = _queIndexIds.Dequeue();
34-
var item = _lstProfileEx.FirstOrDefault(t => t.indexId == id);
35-
if (item is not null)
36-
{
37-
SQLiteHelper.Instance.Replace(item);
38-
}
39-
}
40-
await Task.Delay(1000 * 60);
23+
SaveQueueIndexIds();
24+
await Task.Delay(1000 * 600);
4125
}
4226
});
4327
}
4428

29+
private void Init()
30+
{
31+
SQLiteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
32+
33+
_lstProfileEx = new(SQLiteHelper.Instance.Table<ProfileExItem>());
34+
}
35+
4536
private void IndexIdEnqueue(string indexId)
4637
{
4738
if (!Utils.IsNullOrEmpty(indexId) && !_queIndexIds.Contains(indexId))
@@ -50,6 +41,49 @@ private void IndexIdEnqueue(string indexId)
5041
}
5142
}
5243

44+
private void SaveQueueIndexIds()
45+
{
46+
var cnt = _queIndexIds.Count;
47+
if (cnt > 0)
48+
{
49+
var lstExists = SQLiteHelper.Instance.Table<ProfileExItem>();
50+
List<ProfileExItem> lstInserts = [];
51+
List<ProfileExItem> lstUpdates = [];
52+
53+
for (int i = 0; i < cnt; i++)
54+
{
55+
var id = _queIndexIds.Dequeue();
56+
var item = lstExists.FirstOrDefault(t => t.indexId == id);
57+
var itemNew = _lstProfileEx?.FirstOrDefault(t => t.indexId == id);
58+
if (itemNew is null)
59+
{
60+
continue;
61+
}
62+
63+
if (item is not null)
64+
{
65+
lstUpdates.Add(itemNew);
66+
}
67+
else
68+
{
69+
lstInserts.Add(itemNew);
70+
}
71+
}
72+
try
73+
{
74+
if (lstInserts.Count() > 0)
75+
SQLiteHelper.Instance.InsertAll(lstInserts);
76+
77+
if (lstUpdates.Count() > 0)
78+
SQLiteHelper.Instance.UpdateAll(lstUpdates);
79+
}
80+
catch (Exception ex)
81+
{
82+
Logging.SaveLog("ProfileExHandler", ex);
83+
}
84+
}
85+
}
86+
5387
private void AddProfileEx(string indexId, ref ProfileExItem? profileEx)
5488
{
5589
profileEx = new()
@@ -73,11 +107,7 @@ public void SaveTo()
73107
{
74108
try
75109
{
76-
//foreach (var item in _lstProfileEx)
77-
//{
78-
// SQLiteHelper.Instance.Replace(item);
79-
//}
80-
SQLiteHelper.Instance.UpdateAll(_lstProfileEx);
110+
SaveQueueIndexIds();
81111
}
82112
catch (Exception ex)
83113
{

0 commit comments

Comments
 (0)