mirror of
https://github.com/2dust/v2rayN.git
synced 2026-04-17 21:15:47 +00:00
Optimize db read
This commit is contained in:
parent
cb001afb02
commit
6852cd287b
5 changed files with 32 additions and 53 deletions
|
|
@ -242,6 +242,30 @@ public sealed class AppManager
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Dictionary<string, ProfileItem>> GetProfileItemsByIndexIdsAsMap(IEnumerable<string> indexIds)
|
||||||
|
{
|
||||||
|
var items = await GetProfileItemsByIndexIds(indexIds);
|
||||||
|
return items.ToDictionary(it => it.IndexId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<ProfileItem>> GetProfileItemsOrderedByIndexIds(IEnumerable<string> indexIds)
|
||||||
|
{
|
||||||
|
var idList = indexIds.Where(id => !id.IsNullOrEmpty()).Distinct().ToList();
|
||||||
|
if (idList.Count == 0)
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
var items = await SQLiteHelper.Instance.TableAsync<ProfileItem>()
|
||||||
|
.Where(it => idList.Contains(it.IndexId))
|
||||||
|
.ToListAsync();
|
||||||
|
var itemMap = items.ToDictionary(it => it.IndexId);
|
||||||
|
|
||||||
|
return idList.Select(id => itemMap.GetValueOrDefault(id))
|
||||||
|
.Where(item => item != null)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
|
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
|
||||||
{
|
{
|
||||||
if (remarks.IsNullOrEmpty())
|
if (remarks.IsNullOrEmpty())
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,10 @@ public class GroupProfileManager
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var child in childIds)
|
var childItems = await AppManager.Instance.GetProfileItemsByIndexIds(childIds);
|
||||||
|
foreach (var childItem in childItems)
|
||||||
{
|
{
|
||||||
var childItem = await AppManager.Instance.GetProfileItem(child);
|
if (await HasCycle(childItem.IndexId, childItem?.GetProtocolExtra(), visited, stack))
|
||||||
if (await HasCycle(child, childItem?.GetProtocolExtra(), visited, stack))
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -103,26 +103,7 @@ public class GroupProfileManager
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var childProfiles = await AppManager.Instance.GetProfileItemsByIndexIds(childProfileIds);
|
var ordered = await AppManager.Instance.GetProfileItemsOrderedByIndexIds(childProfileIds);
|
||||||
if (childProfiles == null || childProfiles.Count == 0)
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
var profileMap = childProfiles
|
|
||||||
.Where(p => p != null && !p.IndexId.IsNullOrEmpty())
|
|
||||||
.GroupBy(p => p!.IndexId!)
|
|
||||||
.ToDictionary(g => g.Key, g => g.First());
|
|
||||||
|
|
||||||
var ordered = new List<ProfileItem>(childProfileIds.Count);
|
|
||||||
foreach (var id in childProfileIds)
|
|
||||||
{
|
|
||||||
if (id != null && profileMap.TryGetValue(id, out var item) && item != null)
|
|
||||||
{
|
|
||||||
ordered.Add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ordered;
|
return ordered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,15 +99,8 @@ public class AddGroupServerViewModel : MyReactiveObject
|
||||||
Filter = protocolExtra?.Filter;
|
Filter = protocolExtra?.Filter;
|
||||||
|
|
||||||
var childIndexIds = Utils.String2List(protocolExtra?.ChildItems) ?? [];
|
var childIndexIds = Utils.String2List(protocolExtra?.ChildItems) ?? [];
|
||||||
foreach (var item in childIndexIds)
|
var childItemList = await AppManager.Instance.GetProfileItemsOrderedByIndexIds(childIndexIds);
|
||||||
{
|
ChildItemsObs.AddRange(childItemList);
|
||||||
var child = await AppManager.Instance.GetProfileItem(item);
|
|
||||||
if (child == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ChildItemsObs.Add(child);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ChildRemoveAsync()
|
public async Task ChildRemoveAsync()
|
||||||
|
|
|
||||||
|
|
@ -255,19 +255,7 @@ public class ProfilesSelectViewModel : MyReactiveObject
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var lst = new List<ProfileItem>();
|
var lst = await AppManager.Instance.GetProfileItemsOrderedByIndexIds(SelectedProfiles.Select(sp => sp?.IndexId));
|
||||||
foreach (var sp in SelectedProfiles)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(sp?.IndexId))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var item = await AppManager.Instance.GetProfileItem(sp.IndexId);
|
|
||||||
if (item != null)
|
|
||||||
{
|
|
||||||
lst.Add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lst.Count == 0)
|
if (lst.Count == 0)
|
||||||
{
|
{
|
||||||
NoticeManager.Instance.Enqueue(ResUI.PleaseSelectServer);
|
NoticeManager.Instance.Enqueue(ResUI.PleaseSelectServer);
|
||||||
|
|
|
||||||
|
|
@ -481,14 +481,7 @@ public class ProfilesViewModel : MyReactiveObject
|
||||||
var orderProfiles = SelectedProfiles?.OrderBy(t => t.Sort);
|
var orderProfiles = SelectedProfiles?.OrderBy(t => t.Sort);
|
||||||
if (latest)
|
if (latest)
|
||||||
{
|
{
|
||||||
foreach (var profile in orderProfiles)
|
lstSelected.AddRange(await AppManager.Instance.GetProfileItemsOrderedByIndexIds(orderProfiles.Select(sp => sp?.IndexId)));
|
||||||
{
|
|
||||||
var item = await AppManager.Instance.GetProfileItem(profile.IndexId);
|
|
||||||
if (item is not null)
|
|
||||||
{
|
|
||||||
lstSelected.Add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue