<commit_before>﻿using Discord.Data;
using Discord.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using ChannelModel = Discord.API.Channel;
using ExtendedModel = Discord.API.Gateway.ExtendedGuild;
using MemberModel = Discord.API.GuildMember;
using Model = Discord.API.Guild;
using PresenceModel = Discord.API.Presence;

namespace Discord
{
    internal class CachedGuild : Guild, ICachedEntity<ulong>
    {
        private ConcurrentHashSet<ulong> _channels;
        private ConcurrentDictionary<ulong, CachedGuildUser> _members;
        private ConcurrentDictionary<ulong, Presence> _presences;
        private int _userCount;

        public bool Available { get; private set; } //TODO: Add to IGuild

        public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
        public CachedGuildUser CurrentUser => GetCachedUser(Discord.CurrentUser.Id);
        public IReadOnlyCollection<ICachedGuildChannel> Channels => _channels.Select(x => GetCachedChannel(x)).ToReadOnlyCollection(_channels);
        public IReadOnlyCollection<CachedGuildUser> Members => _members.ToReadOnlyCollection();

        public CachedGuild(DiscordSocketClient discord, Model model) : base(discord, model)
        {
        }

        public void Update(ExtendedModel model, UpdateSource source, DataStore dataStore)
        {
            if (source == UpdateSource.Rest && IsAttached) return;
            
            Available = !(model.Unavailable ?? false);
            if (!Available)
            {
                if (_channels == null)
                    _channels = new ConcurrentHashSet<ulong>();
                if (_members == null)
                    _members = new ConcurrentDictionary<ulong, CachedGuildUser>();
                if (_presences == null)
                    _presences = new ConcurrentDictionary<ulong, Presence>();
                if (_roles == null)
                    _roles = new ConcurrentDictionary<ulong, Role>();
                if (Emojis == null)
                    Emojis = ImmutableArray.Create<Emoji>();
                if (Features == null)
                    Features = ImmutableArray.Create<string>();
                return;
            }

            base.Update(model as Model, source);

            _userCount = model.MemberCount;

            var channels = new ConcurrentHashSet<ulong>();
            if (model.Channels != null)
            {
                for (int i = 0; i < model.Channels.Length; i++)
                    AddCachedChannel(model.Channels[i], channels, dataStore);
            }
            _channels = channels;

            var presences = new ConcurrentDictionary<ulong, Presence>();
            if (model.Presences != null)
            {
                for (int i = 0; i < model.Presences.Length; i++)
                    AddCachedPresence(model.Presences[i], presences);
            }
            _presences = presences;

            var members = new ConcurrentDictionary<ulong, CachedGuildUser>();
            if (model.Members != null)
            {
                for (int i = 0; i < model.Members.Length; i++)
                    AddCachedUser(model.Members[i], members, dataStore);
            }
            _members = members;
        }

        public override Task<IGuildChannel> GetChannel(ulong id) => Task.FromResult<IGuildChannel>(GetCachedChannel(id));
        public override Task<IReadOnlyCollection<IGuildChannel>> GetChannels() => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
        public ICachedGuildChannel AddCachedChannel(ChannelModel model, ConcurrentHashSet<ulong> channels = null, DataStore dataStore = null)
        {
            var channel = ToChannel(model);
            (dataStore ?? Discord.DataStore).AddChannel(channel);
            (channels ?? _channels).TryAdd(model.Id);
            return channel;
        }
        public ICachedGuildChannel GetCachedChannel(ulong id)
        {
            return Discord.DataStore.GetChannel(id) as ICachedGuildChannel;
        }
        public ICachedGuildChannel RemoveCachedChannel(ulong id, ConcurrentHashSet<ulong> channels = null, DataStore dataStore = null)
        {
            (channels ?? _channels).TryRemove(id);
            return (dataStore ?? Discord.DataStore).RemoveChannel(id) as ICachedGuildChannel;
        }

        public Presence AddCachedPresence(PresenceModel model, ConcurrentDictionary<ulong, Presence> presences = null)
        {
            var game = model.Game != null ? new Game(model.Game) : (Game?)null;
            var presence = new Presence(model.Status, game);
            (presences ?? _presences)[model.User.Id] = presence;
            return presence;
        }
        public Presence? GetCachedPresence(ulong id)
        {
            Presence presence;
            if (_presences.TryGetValue(id, out presence))<commit_msg>Fixed a few datastore issues<commit_after>﻿using Discord.Data;
using Discord.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using ChannelModel = Discord.API.Channel;
using ExtendedModel = Discord.API.Gateway.ExtendedGuild;
using MemberModel = Discord.API.GuildMember;
using Model = Discord.API.Guild;
using PresenceModel = Discord.API.Presence;

namespace Discord
{
    internal class CachedGuild : Guild, ICachedEntity<ulong>
    {
        private ConcurrentHashSet<ulong> _channels;
        private ConcurrentDictionary<ulong, CachedGuildUser> _members;
        private ConcurrentDictionary<ulong, Presence> _presences;
        private int _userCount;

        public bool Available { get; private set; } //TODO: Add to IGuild

        public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
        public CachedGuildUser CurrentUser => GetCachedUser(Discord.CurrentUser.Id);
        public IReadOnlyCollection<ICachedGuildChannel> Channels => _channels.Select(x => GetCachedChannel(x)).ToReadOnlyCollection(_channels);
        public IReadOnlyCollection<CachedGuildUser> Members => _members.ToReadOnlyCollection();

        public CachedGuild(DiscordSocketClient discord, Model model) : base(discord, model)
        {
        }

        public void Update(ExtendedModel model, UpdateSource source, DataStore dataStore)
        {
            if (source == UpdateSource.Rest && IsAttached) return;
            
            Available = !(model.Unavailable ?? false);
            if (!Available)
            {
                if (_channels == null)
                    _channels = new ConcurrentHashSet<ulong>();
                if (_members == null)
                    _members = new ConcurrentDictionary<ulong, CachedGuildUser>();
                if (_presences == null)
                    _presences = new ConcurrentDictionary<ulong, Presence>();
                if (_roles == null)
                    _roles = new ConcurrentDictionary<ulong, Role>();
                if (Emojis == null)
                    Emojis = ImmutableArray.Create<Emoji>();
                if (Features == null)
                    Features = ImmutableArray.Create<string>();
                return;
            }

            base.Update(model as Model, source);

            _userCount = model.MemberCount;

            var channels = new ConcurrentHashSet<ulong>();
            if (model.Channels != null)
            {
                for (int i = 0; i < model.Channels.Length; i++)
                    AddCachedChannel(model.Channels[i], channels);
            }
            _channels = channels;

            var presences = new ConcurrentDictionary<ulong, Presence>();
            if (model.Presences != null)
            {
                for (int i = 0; i < model.Presences.Length; i++)
                    AddCachedPresence(model.Presences[i], presences);
            }
            _presences = presences;

            var members = new ConcurrentDictionary<ulong, CachedGuildUser>();
            if (model.Members != null)
            {
                for (int i = 0; i < model.Members.Length; i++)
                    AddCachedUser(model.Members[i], members, dataStore);
            }
            _members = members;
        }

        public override Task<IGuildChannel> GetChannel(ulong id) => Task.FromResult<IGuildChannel>(GetCachedChannel(id));
        public override Task<IReadOnlyCollection<IGuildChannel>> GetChannels() => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
        public ICachedGuildChannel AddCachedChannel(ChannelModel model, ConcurrentHashSet<ulong> channels = null)
        {
            var channel = ToChannel(model);
            (channels ?? _channels).TryAdd(model.Id);
            return channel;
        }
        public ICachedGuildChannel GetCachedChannel(ulong id)
        {
            return Discord.DataStore.GetChannel(id) as ICachedGuildChannel;
        }
        public void RemoveCachedChannel(ulong id, ConcurrentHashSet<ulong> channels = null)
        {
            (channels ?? _channels).TryRemove(id);
        }

        public Presence AddCachedPresence(PresenceModel model, ConcurrentDictionary<ulong, Presence> presences = null)
        {
            var game = model.Game != null ? new Game(model.Game) : (Game?)null;
            var presence = new Presence(model.Status, game);
            (presences ?? _presences)[model.User.Id] = presence;
            return presence;
        }
        public Presence? GetCachedPresence(ulong id)
        {
            Presence presence;
            if (_presences.TryGetValue(id, out presence))
{'fraction_non_alphanumeric': 0.09255264509501798, 'fraction_numerical': 0.0006163328197226503, 'mean_word_length': 2.308188922867822, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4434644', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2519, 'n_words': 587}