Replaced all hashsets which are used concurrently to Concurrent hashsets, some slight db tweaks

This commit is contained in:
Kwoth
2016-10-10 06:12:22 +02:00
parent aa96ed884e
commit e60034728c
16 changed files with 1219 additions and 63 deletions

View File

@@ -55,7 +55,7 @@ namespace NadekoBot.Modules.Administration
private string GetText(IGuild server, ITextChannel channel, IGuildUser user, IUserMessage message) =>
$"**{server.Name} | {channel.Name}** `{user.Username}`: " + message.Content;
public static readonly ConcurrentDictionary<int, HashSet<ITextChannel>> Subscribers = new ConcurrentDictionary<int, HashSet<ITextChannel>>();
public static readonly ConcurrentDictionary<int, ConcurrentHashSet<ITextChannel>> Subscribers = new ConcurrentDictionary<int, ConcurrentHashSet<ITextChannel>>();
private Logger _log { get; }
[NadekoCommand, Usage, Description, Aliases]
@@ -65,7 +65,7 @@ namespace NadekoBot.Modules.Administration
{
var channel = (ITextChannel)msg.Channel;
var token = new NadekoRandom().Next();
var set = new HashSet<ITextChannel>();
var set = new ConcurrentHashSet<ITextChannel>();
if (Subscribers.TryAdd(token, set))
{
set.Add(channel);
@@ -80,7 +80,7 @@ namespace NadekoBot.Modules.Administration
{
var channel = (ITextChannel)imsg.Channel;
HashSet<ITextChannel> set;
ConcurrentHashSet<ITextChannel> set;
if (!Subscribers.TryGetValue(token, out set))
return;
set.Add(channel);
@@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Administration
foreach (var subscriber in Subscribers)
{
subscriber.Value.Remove(channel);
subscriber.Value.TryRemove(channel);
}
await channel.SendMessageAsync(":ok:").ConfigureAwait(false);
}

View File

@@ -12,6 +12,7 @@ using NadekoBot.Services.Database.Models;
using Newtonsoft.Json;
using NLog;
using NadekoBot.Modules.Administration.Commands.Migration;
using System.Collections.Concurrent;
namespace NadekoBot.Modules.Administration
{
@@ -99,12 +100,12 @@ namespace NadekoBot.Modules.Administration
botConfig.RotatingStatusMessages = messages;
//races
var races = new List<RaceAnimal>();
var races = new HashSet<RaceAnimal>();
oldData.RaceAnimals.ForEach(i => races.Add(new RaceAnimal() { Icon = i, Name = i }));
botConfig.RaceAnimals = races;
//Prefix
var prefix = new List<ModulePrefix>
var prefix = new HashSet<ModulePrefix>
{
new ModulePrefix()
{
@@ -158,7 +159,7 @@ namespace NadekoBot.Modules.Administration
botConfig.Blacklist = new HashSet<BlacklistItem>(blacklist);
//Eightball
botConfig.EightBallResponses = oldData._8BallResponses.Select(response => new EightBallResponse() {Text = response}).ToList();
botConfig.EightBallResponses = new HashSet<EightBallResponse>(oldData._8BallResponses.Select(response => new EightBallResponse() {Text = response}));
//NOW save it
botConfig.MigrationVersion = 1;

View File

@@ -6,6 +6,7 @@ using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -76,7 +77,7 @@ namespace NadekoBot.Modules.Administration
{
var channel = (ITextChannel)umsg.Channel;
var toRemove = new HashSet<SelfAssignedRole>();
var toRemove = new ConcurrentHashSet<SelfAssignedRole>();
var removeMsg = new StringBuilder();
var msg = new StringBuilder();
using (var uow = DbHandler.UnitOfWork())

View File

@@ -17,15 +17,15 @@ namespace NadekoBot.Modules.CustomReactions
[NadekoModule("CustomReactions",".")]
public class CustomReactions : DiscordModule
{
public static HashSet<CustomReaction> GlobalReactions { get; } = new HashSet<CustomReaction>();
public static ConcurrentDictionary<ulong, HashSet<CustomReaction>> GuildReactions { get; } = new ConcurrentDictionary<ulong, HashSet<CustomReaction>>();
public static ConcurrentHashSet<CustomReaction> GlobalReactions { get; } = new ConcurrentHashSet<CustomReaction>();
public static ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>> GuildReactions { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>>();
static CustomReactions()
{
using (var uow = DbHandler.UnitOfWork())
{
var items = uow.CustomReactions.GetAll();
GuildReactions = new ConcurrentDictionary<ulong, HashSet<CustomReaction>>(items.Where(g => g.GuildId != null).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => new HashSet<CustomReaction>(g)));
GlobalReactions = new HashSet<CustomReaction>(items.Where(g => g.GuildId == null));
GuildReactions = new ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>>(items.Where(g => g.GuildId != null).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => new ConcurrentHashSet<CustomReaction>(g)));
GlobalReactions = new ConcurrentHashSet<CustomReaction>(items.Where(g => g.GuildId == null));
}
}
public CustomReactions(ILocalization loc, CommandService cmds, ShardedDiscordClient client) : base(loc, cmds, client)
@@ -43,7 +43,7 @@ namespace NadekoBot.Modules.CustomReactions
var t = Task.Run(async () =>
{
var content = umsg.Content.ToLowerInvariant();
HashSet<CustomReaction> reactions;
ConcurrentHashSet<CustomReaction> reactions;
GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
if (reactions != null && reactions.Any())
{
@@ -101,7 +101,7 @@ namespace NadekoBot.Modules.CustomReactions
}
else
{
var reactions = GuildReactions.GetOrAdd(channel.Guild.Id, new HashSet<CustomReaction>());
var reactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CustomReaction>());
reactions.Add(cr);
}
@@ -115,11 +115,11 @@ namespace NadekoBot.Modules.CustomReactions
if (page < 1 || page > 1000)
return;
HashSet<CustomReaction> customReactions;
ConcurrentHashSet<CustomReaction> customReactions;
if (channel == null)
customReactions = GlobalReactions;
else
customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new HashSet<CustomReaction>());
customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CustomReaction>());
if (customReactions == null || !customReactions.Any())
await imsg.Channel.SendMessageAsync("`No custom reactions found`").ConfigureAwait(false);
@@ -150,13 +150,16 @@ namespace NadekoBot.Modules.CustomReactions
if (toDelete.GuildId == null && channel == null)
{
uow.CustomReactions.Remove(toDelete);
GlobalReactions.RemoveWhere(cr => cr.Id == toDelete.Id);
var toRemove = GlobalReactions.FirstOrDefault(cr => cr.Id == toDelete.Id);
GlobalReactions.TryRemove(toRemove);
success = true;
}
else if (toDelete.GuildId != null && channel?.Guild.Id == toDelete.GuildId)
{
uow.CustomReactions.Remove(toDelete);
GuildReactions.GetOrAdd(channel.Guild.Id, new HashSet<CustomReaction>()).RemoveWhere(cr => cr.Id == toDelete.Id);
var crs = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CustomReaction>());
var toRemove = crs.FirstOrDefault(cr => cr.Id == toDelete.Id);
success = true;
}
if(success)

View File

@@ -2,6 +2,7 @@
using NadekoBot.Services;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -11,7 +12,7 @@ namespace NadekoBot.Modules.Games.Trivia
public class TriviaQuestionPool
{
public static TriviaQuestionPool Instance { get; } = new TriviaQuestionPool();
public HashSet<TriviaQuestion> pool = new HashSet<TriviaQuestion>();
public ConcurrentHashSet<TriviaQuestion> pool = new ConcurrentHashSet<TriviaQuestion>();
private Random rng { get; } = new NadekoRandom();
@@ -39,7 +40,7 @@ namespace NadekoBot.Modules.Games.Trivia
pool.Add(tq);
}
var r = new NadekoRandom();
pool = new HashSet<TriviaQuestion>(pool.OrderBy(x => r.Next()));
pool = new ConcurrentHashSet<TriviaQuestion>(pool.OrderBy(x => r.Next()));
}
}
}

View File

@@ -6,6 +6,7 @@ using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -25,13 +26,13 @@ namespace NadekoBot.Modules.Permissions
[Group]
public class BlacklistCommands
{
public static HashSet<BlacklistItem> BlacklistedItems { get; set; } = new HashSet<BlacklistItem>();
public static ConcurrentHashSet<BlacklistItem> BlacklistedItems { get; set; } = new ConcurrentHashSet<BlacklistItem>();
static BlacklistCommands()
{
using (var uow = DbHandler.UnitOfWork())
{
BlacklistedItems = uow.BotConfig.GetOrCreate().Blacklist;
BlacklistedItems = new ConcurrentHashSet<BlacklistItem>(uow.BotConfig.GetOrCreate().Blacklist);
}
}
@@ -75,7 +76,9 @@ namespace NadekoBot.Modules.Permissions
else
{
uow.BotConfig.GetOrCreate().Blacklist.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
BlacklistedItems.RemoveWhere(bi => bi.ItemId == id && bi.Type == type);
var toRemove = BlacklistedItems.FirstOrDefault(bi => bi.ItemId == id && bi.Type == type);
if (toRemove != null)
BlacklistedItems.TryRemove(toRemove);
}
await uow.CompleteAsync().ConfigureAwait(false);
}

View File

@@ -26,15 +26,15 @@ namespace NadekoBot.Modules.Permissions
[Group]
public class CmdCdsCommands
{
public static ConcurrentDictionary<ulong, HashSet<CommandCooldown>> commandCooldowns { get; }
private static ConcurrentDictionary<ulong, HashSet<ActiveCooldown>> activeCooldowns = new ConcurrentDictionary<ulong, HashSet<ActiveCooldown>>();
public static ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>> commandCooldowns { get; }
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> activeCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>();
static CmdCdsCommands()
{
using (var uow = DbHandler.UnitOfWork())
{
var configs = uow.GuildConfigs.GetAll();
commandCooldowns = new ConcurrentDictionary<ulong, HashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => v.CommandCooldowns));
commandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => v.CommandCooldowns));
}
}
[NadekoCommand, Usage, Description, Aliases]
@@ -51,7 +51,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(channel.Guild.Id);
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new HashSet<CommandCooldown>());
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
config.CommandCooldowns.RemoveWhere(cc => cc.CommandName == command.Text.ToLowerInvariant());
localSet.RemoveWhere(cc => cc.CommandName == command.Text.ToLowerInvariant());
@@ -69,7 +69,7 @@ namespace NadekoBot.Modules.Permissions
}
if (secs == 0)
{
var activeCds = activeCooldowns.GetOrAdd(channel.Guild.Id, new HashSet<ActiveCooldown>());
var activeCds = activeCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<ActiveCooldown>());
activeCds.RemoveWhere(ac => ac.Command == command.Text.ToLowerInvariant());
await channel.SendMessageAsync($"Command **{command}** has no coooldown now and all existing cooldowns have been cleared.").ConfigureAwait(false);
}
@@ -82,7 +82,7 @@ namespace NadekoBot.Modules.Permissions
public async Task AllCmdCooldowns(IUserMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new HashSet<CommandCooldown>());
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
if (!localSet.Any())
await channel.SendMessageAsync("`No command cooldowns set.`").ConfigureAwait(false);
@@ -94,11 +94,11 @@ namespace NadekoBot.Modules.Permissions
{
if (guild == null)
return false;
var cmdcds = CmdCdsCommands.commandCooldowns.GetOrAdd(guild.Id, new HashSet<CommandCooldown>());
var cmdcds = CmdCdsCommands.commandCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<CommandCooldown>());
CommandCooldown cdRule;
if ((cdRule = cmdcds.FirstOrDefault(cc => cc.CommandName == cmd.Text.ToLowerInvariant())) != null)
{
var activeCdsForGuild = activeCooldowns.GetOrAdd(guild.Id, new HashSet<ActiveCooldown>());
var activeCdsForGuild = activeCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<ActiveCooldown>());
if (activeCdsForGuild.FirstOrDefault(ac => ac.UserId == user.Id && ac.Command == cmd.Text.ToLowerInvariant()) != null)
{
return true;

View File

@@ -17,26 +17,26 @@ namespace NadekoBot.Modules.Permissions
[Group]
public class FilterCommands
{
public static HashSet<ulong> InviteFilteringChannels { get; set; }
public static HashSet<ulong> InviteFilteringServers { get; set; }
public static ConcurrentHashSet<ulong> InviteFilteringChannels { get; set; }
public static ConcurrentHashSet<ulong> InviteFilteringServers { get; set; }
//serverid, filteredwords
private static ConcurrentDictionary<ulong, HashSet<string>> ServerFilteredWords { get; set; }
private static ConcurrentDictionary<ulong, ConcurrentHashSet<string>> ServerFilteredWords { get; set; }
public static HashSet<ulong> WordFilteringChannels { get; set; }
public static HashSet<ulong> WordFilteringServers { get; set; }
public static ConcurrentHashSet<ulong> WordFilteringChannels { get; set; }
public static ConcurrentHashSet<ulong> WordFilteringServers { get; set; }
public static HashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
public static ConcurrentHashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
{
HashSet<string> words = new HashSet<string>();
ConcurrentHashSet<string> words = new ConcurrentHashSet<string>();
if(WordFilteringChannels.Contains(channelId))
ServerFilteredWords.TryGetValue(guildId, out words);
return words;
}
public static HashSet<string> FilteredWordsForServer(ulong guildId)
public static ConcurrentHashSet<string> FilteredWordsForServer(ulong guildId)
{
var words = new HashSet<string>();
var words = new ConcurrentHashSet<string>();
if(WordFilteringServers.Contains(guildId))
ServerFilteredWords.TryGetValue(guildId, out words);
return words;
@@ -48,17 +48,17 @@ namespace NadekoBot.Modules.Permissions
{
var guildConfigs = uow.GuildConfigs.GetAll();
InviteFilteringServers = new HashSet<ulong>(guildConfigs.Where(gc => gc.FilterInvites).Select(gc => gc.GuildId));
InviteFilteringChannels = new HashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterInvitesChannelIds.Select(fci => fci.ChannelId)));
InviteFilteringServers = new ConcurrentHashSet<ulong>(guildConfigs.Where(gc => gc.FilterInvites).Select(gc => gc.GuildId));
InviteFilteringChannels = new ConcurrentHashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterInvitesChannelIds.Select(fci => fci.ChannelId)));
var dict = guildConfigs.ToDictionary(gc => gc.GuildId, gc => new HashSet<string>(gc.FilteredWords.Select(fw => fw.Word)));
var dict = guildConfigs.ToDictionary(gc => gc.GuildId, gc => new ConcurrentHashSet<string>(gc.FilteredWords.Select(fw => fw.Word)));
ServerFilteredWords = new ConcurrentDictionary<ulong, HashSet<string>>(dict);
ServerFilteredWords = new ConcurrentDictionary<ulong, ConcurrentHashSet<string>>(dict);
var serverFiltering = guildConfigs.Where(gc => gc.FilterWords);
WordFilteringServers = new HashSet<ulong>(serverFiltering.Select(gc => gc.GuildId));
WordFilteringServers = new ConcurrentHashSet<ulong>(serverFiltering.Select(gc => gc.GuildId));
WordFilteringChannels = new HashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterWordsChannelIds.Select(fwci => fwci.ChannelId)));
WordFilteringChannels = new ConcurrentHashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterWordsChannelIds.Select(fwci => fwci.ChannelId)));
}
}
@@ -205,7 +205,7 @@ namespace NadekoBot.Modules.Permissions
await uow.CompleteAsync().ConfigureAwait(false);
}
var filteredWords = ServerFilteredWords.GetOrAdd(channel.Guild.Id, new HashSet<string>());
var filteredWords = ServerFilteredWords.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<string>());
if (removed == 0)
{
@@ -227,7 +227,7 @@ namespace NadekoBot.Modules.Permissions
{
var channel = (ITextChannel)imsg.Channel;
HashSet<string> filteredWords;
ConcurrentHashSet<string> filteredWords;
ServerFilteredWords.TryGetValue(channel.Guild.Id, out filteredWords);
await channel.SendMessageAsync($"`List of banned words:`\n" + string.Join(",\n", filteredWords))

View File

@@ -140,10 +140,7 @@ namespace NadekoBot.Modules.Searches.IMDB
List<string> list = new List<string>();
string recUrl = "http://www.imdb.com/widget/recommendations/_ajax/get_more_recs?specs=p13nsims%3A" + mov.Id;
string json = await GetUrlDataAsync(recUrl);
list = MatchAll(@"title=\\""(.*?)\\""", json);
HashSet<String> set = new HashSet<string>();
foreach (String rec in list) set.Add(rec);
return new List<string>(set.ToList());
return MatchAll(@"title=\\""(.*?)\\""", json);
}
/*******************************[ Helper Methods ]********************************/
//Match single instance