Optimize add and delete performance

pull/3492/head^2
2dust 2023-03-07 20:24:26 +08:00
parent 961bd6140c
commit a883ba8808
3 changed files with 73 additions and 61 deletions

View File

@ -25,11 +25,18 @@ namespace v2rayN.Base
return _db.CreateTable<T>(); return _db.CreateTable<T>();
} }
public int Add(object model) public int Insert(object model)
{ {
return _db.Insert(model); return _db.Insert(model);
} }
public async Task<int> AddAsync(object model) public int InsertAll(IEnumerable models)
{
lock (objLock)
{
return _db.InsertAll(models);
}
}
public async Task<int> InsertAsync(object model)
{ {
return await _dbAsync.InsertAsync(model); return await _dbAsync.InsertAsync(model);
} }

View File

@ -352,7 +352,7 @@ namespace v2rayN.Handler
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="profileItem"></param> /// <param name="profileItem"></param>
/// <returns></returns> /// <returns></returns>
public static int AddServer(ref Config config, ProfileItem profileItem) public static int AddServer(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configType = EConfigType.VMess; profileItem.configType = EConfigType.VMess;
@ -370,7 +370,7 @@ namespace v2rayN.Handler
return -1; return -1;
} }
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, toFile);
return 0; return 0;
} }
@ -383,11 +383,15 @@ namespace v2rayN.Handler
/// <returns></returns> /// <returns></returns>
public static int RemoveServer(Config config, List<ProfileItem> indexs) public static int RemoveServer(Config config, List<ProfileItem> indexs)
{ {
var subid = "TempRemoveSubId";
foreach (var item in indexs) foreach (var item in indexs)
{ {
RemoveProfileItem(config, item.indexId); item.subid = subid;
} }
SqliteHelper.Instance.UpdateAll(indexs);
RemoveServerViaSubid(ref config, subid, false);
return 0; return 0;
} }
@ -414,7 +418,7 @@ namespace v2rayN.Handler
} }
else else
{ {
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, true);
} }
} }
@ -447,8 +451,7 @@ namespace v2rayN.Handler
{ {
return 0; return 0;
} }
var allItems = LazyConfig.Instance.ProfileItemIndexs(""); if (SqliteHelper.Instance.Table<ProfileItem>().Where(t => t.indexId == config.indexId).Any())
if (allItems.Where(t => t == config.indexId).Any())
{ {
return 0; return 0;
} }
@ -456,11 +459,7 @@ namespace v2rayN.Handler
{ {
return SetDefaultServerIndex(ref config, lstProfile[0].indexId); return SetDefaultServerIndex(ref config, lstProfile[0].indexId);
} }
if (allItems.Count > 0) return SetDefaultServerIndex(ref config, SqliteHelper.Instance.Table<ProfileItem>().Select(t => t.indexId).FirstOrDefault());
{
return SetDefaultServerIndex(ref config, allItems.FirstOrDefault());
}
return -1;
} }
public static ProfileItem? GetDefaultServer(ref Config config) public static ProfileItem? GetDefaultServer(ref Config config)
{ {
@ -588,7 +587,7 @@ namespace v2rayN.Handler
} }
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, true);
return 0; return 0;
@ -620,7 +619,7 @@ namespace v2rayN.Handler
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="profileItem"></param> /// <param name="profileItem"></param>
/// <returns></returns> /// <returns></returns>
public static int AddShadowsocksServer(ref Config config, ProfileItem profileItem) public static int AddShadowsocksServer(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configType = EConfigType.Shadowsocks; profileItem.configType = EConfigType.Shadowsocks;
@ -633,7 +632,7 @@ namespace v2rayN.Handler
return -1; return -1;
} }
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, toFile);
return 0; return 0;
} }
@ -644,13 +643,13 @@ namespace v2rayN.Handler
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="profileItem"></param> /// <param name="profileItem"></param>
/// <returns></returns> /// <returns></returns>
public static int AddSocksServer(ref Config config, ProfileItem profileItem) public static int AddSocksServer(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configType = EConfigType.Socks; profileItem.configType = EConfigType.Socks;
profileItem.address = profileItem.address.TrimEx(); profileItem.address = profileItem.address.TrimEx();
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, toFile);
return 0; return 0;
} }
@ -661,7 +660,7 @@ namespace v2rayN.Handler
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="profileItem"></param> /// <param name="profileItem"></param>
/// <returns></returns> /// <returns></returns>
public static int AddTrojanServer(ref Config config, ProfileItem profileItem) public static int AddTrojanServer(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configType = EConfigType.Trojan; profileItem.configType = EConfigType.Trojan;
@ -672,7 +671,7 @@ namespace v2rayN.Handler
profileItem.streamSecurity = Global.StreamSecurity; profileItem.streamSecurity = Global.StreamSecurity;
} }
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, toFile);
return 0; return 0;
} }
@ -777,7 +776,7 @@ namespace v2rayN.Handler
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="profileItem"></param> /// <param name="profileItem"></param>
/// <returns></returns> /// <returns></returns>
public static int AddVlessServer(ref Config config, ProfileItem profileItem) public static int AddVlessServer(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configType = EConfigType.VLESS; profileItem.configType = EConfigType.VLESS;
@ -790,7 +789,7 @@ namespace v2rayN.Handler
profileItem.path = profileItem.path.TrimEx(); profileItem.path = profileItem.path.TrimEx();
profileItem.streamSecurity = profileItem.streamSecurity.TrimEx(); profileItem.streamSecurity = profileItem.streamSecurity.TrimEx();
AddServerCommon(ref config, profileItem); AddServerCommon(ref config, profileItem, toFile);
return 0; return 0;
} }
@ -800,27 +799,27 @@ namespace v2rayN.Handler
List<ProfileItem> source = lstProfile; List<ProfileItem> source = lstProfile;
bool keepOlder = config.guiItem.keepOlderDedupl; bool keepOlder = config.guiItem.keepOlderDedupl;
List<ProfileItem> list = new(); List<ProfileItem> lstKeep = new();
List<ProfileItem> lstRemove = new();
if (!keepOlder) source.Reverse(); // Remove the early items first if (!keepOlder) source.Reverse(); // Remove the early items first
foreach (ProfileItem item in source) foreach (ProfileItem item in source)
{ {
if (!list.Exists(i => CompareProfileItem(i, item, false))) if (!lstKeep.Exists(i => CompareProfileItem(i, item, false)))
{ {
list.Add(item); lstKeep.Add(item);
} }
else else
{ {
RemoveProfileItem(config, item.indexId); lstRemove.Add(item);
} }
} }
//if (!keepOlder) list.Reverse(); RemoveServer(config, lstRemove);
//config.vmess = list;
return list.Count; return lstKeep.Count;
} }
public static int AddServerCommon(ref Config config, ProfileItem profileItem) public static int AddServerCommon(ref Config config, ProfileItem profileItem, bool toFile = true)
{ {
profileItem.configVersion = 2; profileItem.configVersion = 2;
@ -847,18 +846,12 @@ namespace v2rayN.Handler
var maxSort = ProfileExHandler.Instance.GetMaxSort(); var maxSort = ProfileExHandler.Instance.GetMaxSort();
ProfileExHandler.Instance.SetSort(profileItem.indexId, maxSort + 1); ProfileExHandler.Instance.SetSort(profileItem.indexId, maxSort + 1);
} }
else if (profileItem.indexId == config.indexId)
{
}
if (SqliteHelper.Instance.Replace(profileItem) > 0) if (toFile)
{ {
return 0; SqliteHelper.Instance.Replace(profileItem);
}
else
{
return -1;
} }
return 0;
} }
private static bool CompareProfileItem(ProfileItem o, ProfileItem n, bool remarks) private static bool CompareProfileItem(ProfileItem o, ProfileItem n, bool remarks)
@ -935,11 +928,7 @@ namespace v2rayN.Handler
} }
int countServers = 0; int countServers = 0;
//var maxSort = 0; List<ProfileItem> lstAdd = new();
//if (SqliteHelper.Instance.Table<ProfileItem>().Count() > 0)
//{
// maxSort = SqliteHelper.Instance.Table<ProfileItem>().Max(t => t.sort);
//}
string[] arrData = clipboardData.Split(Environment.NewLine.ToCharArray()); string[] arrData = clipboardData.Split(Environment.NewLine.ToCharArray());
foreach (string str in arrData) foreach (string str in arrData)
{ {
@ -977,45 +966,47 @@ namespace v2rayN.Handler
} }
profileItem.subid = subid; profileItem.subid = subid;
profileItem.isSub = isSub; profileItem.isSub = isSub;
//profileItem.sort = maxSort + countServers + 1;
if (profileItem.configType == EConfigType.VMess) if (profileItem.configType == EConfigType.VMess)
{ {
if (AddServer(ref config, profileItem) == 0) if (AddServer(ref config, profileItem, false) == 0)
{ {
countServers++; countServers++;
} }
} }
else if (profileItem.configType == EConfigType.Shadowsocks) else if (profileItem.configType == EConfigType.Shadowsocks)
{ {
if (AddShadowsocksServer(ref config, profileItem) == 0) if (AddShadowsocksServer(ref config, profileItem, false) == 0)
{ {
countServers++; countServers++;
} }
} }
else if (profileItem.configType == EConfigType.Socks) else if (profileItem.configType == EConfigType.Socks)
{ {
if (AddSocksServer(ref config, profileItem) == 0) if (AddSocksServer(ref config, profileItem, false) == 0)
{ {
countServers++; countServers++;
} }
} }
else if (profileItem.configType == EConfigType.Trojan) else if (profileItem.configType == EConfigType.Trojan)
{ {
if (AddTrojanServer(ref config, profileItem) == 0) if (AddTrojanServer(ref config, profileItem, false) == 0)
{ {
countServers++; countServers++;
} }
} }
else if (profileItem.configType == EConfigType.VLESS) else if (profileItem.configType == EConfigType.VLESS)
{ {
if (AddVlessServer(ref config, profileItem) == 0) if (AddVlessServer(ref config, profileItem, false) == 0)
{ {
countServers++; countServers++;
} }
} }
lstAdd.Add(profileItem);
} }
SqliteHelper.Instance.InsertAll(lstAdd);
ToJsonFile(config); ToJsonFile(config);
return countServers; return countServers;
} }
@ -1265,6 +1256,7 @@ namespace v2rayN.Handler
{ {
return -1; return -1;
} }
var customProfile = SqliteHelper.Instance.Table<ProfileItem>().Where(t => t.subid == subid && t.configType == EConfigType.Custom).ToList();
if (isSub) if (isSub)
{ {
SqliteHelper.Instance.Execute($"delete from ProfileItem where isSub = 1 and subid = '{subid}'"); SqliteHelper.Instance.Execute($"delete from ProfileItem where isSub = 1 and subid = '{subid}'");
@ -1273,6 +1265,10 @@ namespace v2rayN.Handler
{ {
SqliteHelper.Instance.Execute($"delete from ProfileItem where subid = '{subid}'"); SqliteHelper.Instance.Execute($"delete from ProfileItem where subid = '{subid}'");
} }
foreach (var item in customProfile)
{
File.Delete(Utils.GetConfigPath(item.address));
}
return 0; return 0;
} }

View File

@ -741,6 +741,7 @@ namespace v2rayN.ViewModels
security = t.security, security = t.security,
network = t.network, network = t.network,
streamSecurity = t.streamSecurity, streamSecurity = t.streamSecurity,
subid = t.subid,
subRemarks = t.subRemarks, subRemarks = t.subRemarks,
isActive = t.indexId == _config.indexId, isActive = t.indexId == _config.indexId,
sort = t33 == null ? 0 : t33.sort, sort = t33 == null ? 0 : t33.sort,
@ -830,21 +831,29 @@ namespace v2rayN.ViewModels
#endregion #endregion
#region Add Servers #region Add Servers
private int GetProfileItems(out List<ProfileItem> lstSelecteds) private int GetProfileItems(out List<ProfileItem> lstSelecteds, bool latest)
{ {
lstSelecteds = new List<ProfileItem>(); lstSelecteds = new List<ProfileItem>();
if (SelectedProfiles == null || SelectedProfiles.Count <= 0) if (SelectedProfiles == null || SelectedProfiles.Count <= 0)
{ {
return -1; return -1;
} }
foreach (var profile in SelectedProfiles) if (latest)
{ {
var item = LazyConfig.Instance.GetProfileItem(profile.indexId); foreach (var profile in SelectedProfiles)
if (item is not null)
{ {
lstSelecteds.Add(item); var item = LazyConfig.Instance.GetProfileItem(profile.indexId);
if (item is not null)
{
lstSelecteds.Add(item);
}
} }
} }
else
{
lstSelecteds = Utils.FromJson<List<ProfileItem>>(Utils.ToJson(SelectedProfiles));
}
return 0; return 0;
} }
@ -931,7 +940,7 @@ namespace v2rayN.ViewModels
} }
public void RemoveServer() public void RemoveServer()
{ {
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, false) < 0)
{ {
return; return;
} }
@ -962,7 +971,7 @@ namespace v2rayN.ViewModels
} }
private void CopyServer() private void CopyServer()
{ {
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
{ {
return; return;
} }
@ -1094,7 +1103,7 @@ namespace v2rayN.ViewModels
return; return;
} }
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, false) < 0)
{ {
return; return;
} }
@ -1145,7 +1154,7 @@ namespace v2rayN.ViewModels
{ {
SelectedProfiles = _profileItems; SelectedProfiles = _profileItems;
} }
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, false) < 0)
{ {
return; return;
} }
@ -1177,7 +1186,7 @@ namespace v2rayN.ViewModels
public void Export2ShareUrl() public void Export2ShareUrl()
{ {
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
{ {
return; return;
} }
@ -1202,7 +1211,7 @@ namespace v2rayN.ViewModels
private void Export2SubContent() private void Export2SubContent()
{ {
if (GetProfileItems(out List<ProfileItem> lstSelecteds) < 0) if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
{ {
return; return;
} }