Optimizing sorting storage

pull/3336/head
2dust 2023-02-20 20:15:08 +08:00
parent dd2d9133eb
commit 8ce476caf1
3 changed files with 60 additions and 26 deletions

View File

@ -117,7 +117,7 @@ namespace v2rayN.Base
}; };
progress.Report("......"); progress.Report("......");
await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token); using var stream = await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token);
downloadOpt = null; downloadOpt = null;
} }

View File

@ -1,4 +1,6 @@
using v2rayN.Base; using System.Collections.Concurrent;
using System.Reactive.Linq;
using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
namespace v2rayN.Handler namespace v2rayN.Handler
@ -6,8 +8,9 @@ namespace v2rayN.Handler
class ProfileExHandler class ProfileExHandler
{ {
private static readonly Lazy<ProfileExHandler> _instance = new(() => new()); private static readonly Lazy<ProfileExHandler> _instance = new(() => new());
private List<ProfileExItem> _lstProfileEx; private ConcurrentBag<ProfileExItem> _lstProfileEx;
public List<ProfileExItem> ProfileExs => _lstProfileEx; private Queue<string> _queIndexIds = new();
public ConcurrentBag<ProfileExItem> ProfileExs => _lstProfileEx;
public static ProfileExHandler Instance => _instance.Value; public static ProfileExHandler Instance => _instance.Value;
public ProfileExHandler() public ProfileExHandler()
@ -19,7 +22,33 @@ namespace v2rayN.Handler
{ {
SqliteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )"); SqliteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
_lstProfileEx = SqliteHelper.Instance.Table<ProfileExItem>().ToList(); _lstProfileEx = new(SqliteHelper.Instance.Table<ProfileExItem>());
Task.Run(async () =>
{
while (true)
{
var cnt = _queIndexIds.Count;
for (int i = 0; i < cnt; i++)
{
var id = _queIndexIds.Dequeue();
var item = _lstProfileEx.FirstOrDefault(t => t.indexId == id);
if (item is not null)
{
await SqliteHelper.Instance.Replacesync(item);
}
}
Thread.Sleep(1000 * 60);
}
});
}
private void IndexIdEnqueue(string indexId)
{
if (!Utils.IsNullOrEmpty(indexId) && !_queIndexIds.Contains(indexId))
{
_queIndexIds.Enqueue(indexId);
}
} }
private void AddProfileEx(string indexId, ref ProfileExItem profileEx) private void AddProfileEx(string indexId, ref ProfileExItem profileEx)
@ -32,7 +61,7 @@ namespace v2rayN.Handler
sort = 0 sort = 0
}; };
_lstProfileEx.Add(profileEx); _lstProfileEx.Add(profileEx);
//SqliteHelper.Instance.Replace(profileEx); IndexIdEnqueue(indexId);
} }
public void ClearAll() public void ClearAll()
@ -45,11 +74,11 @@ namespace v2rayN.Handler
{ {
try try
{ {
foreach (var item in _lstProfileEx) //foreach (var item in _lstProfileEx)
{ //{
SqliteHelper.Instance.Replace(item); // SqliteHelper.Instance.Replace(item);
} //}
//SqliteHelper.Instance.UpdateAll(_lstProfileEx); SqliteHelper.Instance.UpdateAll(_lstProfileEx);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -57,7 +86,7 @@ namespace v2rayN.Handler
} }
} }
public Task SetTestDelay(string indexId, string delayVal) public void SetTestDelay(string indexId, string delayVal)
{ {
var profileEx = _lstProfileEx.FirstOrDefault(t => t.indexId == indexId); var profileEx = _lstProfileEx.FirstOrDefault(t => t.indexId == indexId);
if (profileEx == null) if (profileEx == null)
@ -67,10 +96,10 @@ namespace v2rayN.Handler
int.TryParse(delayVal, out int delay); int.TryParse(delayVal, out int delay);
profileEx.delay = delay; profileEx.delay = delay;
return Task.CompletedTask; IndexIdEnqueue(indexId);
} }
public Task SetTestSpeed(string indexId, string speedVal) public void SetTestSpeed(string indexId, string speedVal)
{ {
var profileEx = _lstProfileEx.FirstOrDefault(t => t.indexId == indexId); var profileEx = _lstProfileEx.FirstOrDefault(t => t.indexId == indexId);
if (profileEx == null) if (profileEx == null)
@ -80,7 +109,7 @@ namespace v2rayN.Handler
decimal.TryParse(speedVal, out decimal speed); decimal.TryParse(speedVal, out decimal speed);
profileEx.speed = speed; profileEx.speed = speed;
return Task.CompletedTask; IndexIdEnqueue(indexId);
} }
public void SetSort(string indexId, int sort) public void SetSort(string indexId, int sort)
@ -91,6 +120,7 @@ namespace v2rayN.Handler
AddProfileEx(indexId, ref profileEx); AddProfileEx(indexId, ref profileEx);
} }
profileEx.sort = sort; profileEx.sort = sort;
IndexIdEnqueue(indexId);
} }
public int GetSort(string indexId) public int GetSort(string indexId)

View File

@ -55,12 +55,16 @@ namespace v2rayN.Handler
case ESpeedActionType.Tcping: case ESpeedActionType.Tcping:
case ESpeedActionType.Realping: case ESpeedActionType.Realping:
UpdateFunc(it.indexId, ResUI.Speedtesting, ""); UpdateFunc(it.indexId, ResUI.Speedtesting, "");
ProfileExHandler.Instance.SetTestDelay(it.indexId, "0");
break; break;
case ESpeedActionType.Speedtest: case ESpeedActionType.Speedtest:
UpdateFunc(it.indexId, "", ResUI.Speedtesting); UpdateFunc(it.indexId, "", ResUI.Speedtesting);
ProfileExHandler.Instance.SetTestSpeed(it.indexId, "0");
break; break;
case ESpeedActionType.Mixedtest: case ESpeedActionType.Mixedtest:
UpdateFunc(it.indexId, ResUI.Speedtesting, ResUI.Speedtesting); UpdateFunc(it.indexId, ResUI.Speedtesting, ResUI.Speedtesting);
ProfileExHandler.Instance.SetTestDelay(it.indexId, "0");
ProfileExHandler.Instance.SetTestSpeed(it.indexId, "0");
break; break;
} }
} }
@ -112,24 +116,24 @@ namespace v2rayN.Handler
private void RunPing() private void RunPing()
{ {
RunPingSub(async (ServerTestItem it) => RunPingSub((ServerTestItem it) =>
{ {
long time = Ping(it.address); long time = Ping(it.address);
var output = FormatOut(time, Global.DelayUnit); var output = FormatOut(time, Global.DelayUnit);
await ProfileExHandler.Instance.SetTestDelay(it.indexId, output); ProfileExHandler.Instance.SetTestDelay(it.indexId, output);
UpdateFunc(it.indexId, output); UpdateFunc(it.indexId, output);
}); });
} }
private void RunTcping() private void RunTcping()
{ {
RunPingSub(async (ServerTestItem it) => RunPingSub((ServerTestItem it) =>
{ {
int time = GetTcpingTime(it.address, it.port); int time = GetTcpingTime(it.address, it.port);
var output = FormatOut(time, Global.DelayUnit); var output = FormatOut(time, Global.DelayUnit);
await ProfileExHandler.Instance.SetTestDelay(it.indexId, output); ProfileExHandler.Instance.SetTestDelay(it.indexId, output);
UpdateFunc(it.indexId, output); UpdateFunc(it.indexId, output);
}); });
} }
@ -161,7 +165,7 @@ namespace v2rayN.Handler
{ {
continue; continue;
} }
tasks.Add(Task.Run(async () => tasks.Add(Task.Run(() =>
{ {
try try
{ {
@ -169,7 +173,7 @@ namespace v2rayN.Handler
WebProxy webProxy = new(Global.Loopback, it.port); WebProxy webProxy = new(Global.Loopback, it.port);
string output = GetRealPingTime(downloadHandle, webProxy); string output = GetRealPingTime(downloadHandle, webProxy);
await ProfileExHandler.Instance.SetTestDelay(it.indexId, output); ProfileExHandler.Instance.SetTestDelay(it.indexId, output);
UpdateFunc(it.indexId, output); UpdateFunc(it.indexId, output);
int.TryParse(output, out int delay); int.TryParse(output, out int delay);
it.delay = delay; it.delay = delay;
@ -231,7 +235,7 @@ namespace v2rayN.Handler
// UpdateFunc(it.indexId, "", ResUI.SpeedtestingSkip); // UpdateFunc(it.indexId, "", ResUI.SpeedtestingSkip);
// continue; // continue;
//} //}
await ProfileExHandler.Instance.SetTestSpeed(it.indexId, "-1"); ProfileExHandler.Instance.SetTestSpeed(it.indexId, "-1");
var item = LazyConfig.Instance.GetProfileItem(it.indexId); var item = LazyConfig.Instance.GetProfileItem(it.indexId);
if (item is null) continue; if (item is null) continue;
@ -243,7 +247,7 @@ namespace v2rayN.Handler
decimal.TryParse(msg, out decimal dec); decimal.TryParse(msg, out decimal dec);
if (dec > 0) if (dec > 0)
{ {
await ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg); ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg);
} }
UpdateFunc(it.indexId, "", msg); UpdateFunc(it.indexId, "", msg);
}); });
@ -282,7 +286,7 @@ namespace v2rayN.Handler
{ {
continue; continue;
} }
await ProfileExHandler.Instance.SetTestSpeed(it.indexId, "-1"); ProfileExHandler.Instance.SetTestSpeed(it.indexId, "-1");
var item = LazyConfig.Instance.GetProfileItem(it.indexId); var item = LazyConfig.Instance.GetProfileItem(it.indexId);
if (item is null) continue; if (item is null) continue;
@ -293,7 +297,7 @@ namespace v2rayN.Handler
decimal.TryParse(msg, out decimal dec); decimal.TryParse(msg, out decimal dec);
if (dec > 0) if (dec > 0)
{ {
await ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg); ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg);
} }
UpdateFunc(it.indexId, "", msg); UpdateFunc(it.indexId, "", msg);
}); });