<commit_before>﻿using EventCentric.Log;
using EventCentric.Publishing;
using EventCentric.Publishing.Dto;
using EventCentric.Transport;
using EventCentric.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace EventCentric.Messaging
{
    public class InMemoryEventPublisher : IInMemoryEventPublisher
    {
        private readonly ILogger log;
        private readonly ConcurrentDictionary<string, Func<long, string, PollResponse>> sourcesByStreamType = new ConcurrentDictionary<string, Func<long, string, PollResponse>>();
        private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, Func<PollResponse, ServerStatus>>> ocassionallyConnectedSourcesByConsumer = new ConcurrentDictionary<string, ConcurrentDictionary<string, Func<PollResponse, ServerStatus>>>();

        public InMemoryEventPublisher(ILogger log)
        {
            Ensure.NotNull(log, nameof(log));

            this.log = log;
        }

        public void Register(IPollableEventSource publisher)
        {
            sourcesByStreamType.TryAdd(publisher.SourceName, (version, name) => publisher.PollEvents(version, name));
            if (publisher is IOcassionallyConnectedSourceConsumer)
            {
                var consumer = publisher as IOcassionallyConnectedSourceConsumer;

                this.ocassionallyConnectedSourcesByConsumer.TryAdd(consumer.ConsumerName, new ConcurrentDictionary<string, Func<PollResponse, ServerStatus>>());
                this.ocassionallyConnectedSourcesByConsumer[consumer.ConsumerName].TryAdd(consumer.SourceName, (response) => consumer.UpdateConsumer(response));
            }
        }

        public PollResponse PollEvents(string streamType, long fromVersion, string consumerName)
        {
            return this.sourcesByStreamType[streamType].Invoke(fromVersion, consumerName);
        }

        public bool TryUpdateConsumer(string consumerName, PollResponse response, out ServerStatus status)
        {
            status = null;
            if (!this.ocassionallyConnectedSourcesByConsumer.ContainsKey(consumerName))
                throw new KeyNotFoundException($"The consumer of name {nameof(consumerName)} is not registered in the system");

            var sources = this.ocassionallyConnectedSourcesByConsumer[consumerName];
            if (!sources.ContainsKey(response.StreamType))
                return false;

            status = sources[response.StreamType].Invoke(response);
            return true;
        }
    }
}<commit_msg>consumer stuff<commit_after>﻿using EventCentric.Log;
using EventCentric.Publishing;
using EventCentric.Publishing.Dto;
using EventCentric.Transport;
using EventCentric.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace EventCentric.Messaging
{
    public class InMemoryEventPublisher : IInMemoryEventPublisher
    {
        private readonly ILogger log;
        private readonly ConcurrentDictionary<string, Func<long, string, PollResponse>> sourcesByStreamType = new ConcurrentDictionary<string, Func<long, string, PollResponse>>();
        private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, IOcassionallyConnectedSourceConsumer>> ocassionallyConnectedSourcesByConsumer = new ConcurrentDictionary<string, ConcurrentDictionary<string, IOcassionallyConnectedSourceConsumer>>();

        public InMemoryEventPublisher(ILogger log)
        {
            Ensure.NotNull(log, nameof(log));

            this.log = log;
        }

        public void Register(IPollableEventSource publisher)
        {
            sourcesByStreamType.TryAdd(publisher.SourceName, (version, name) => publisher.PollEvents(version, name));
            if (publisher is IOcassionallyConnectedSourceConsumer)
            {
                var consumer = publisher as IOcassionallyConnectedSourceConsumer;

                this.ocassionallyConnectedSourcesByConsumer.TryAdd(consumer.ConsumerName, new ConcurrentDictionary<string, IOcassionallyConnectedSourceConsumer>());
                this.ocassionallyConnectedSourcesByConsumer[consumer.ConsumerName].TryAdd(consumer.SourceName, consumer);
            }
        }

        public PollResponse PollEvents(string streamType, long fromVersion, string consumerName)
        {
            return this.sourcesByStreamType[streamType].Invoke(fromVersion, consumerName);
        }

        public bool TryUpdateConsumer(string consumerName, PollResponse response, out ServerStatus status)
        {
            status = null;
            if (!this.ocassionallyConnectedSourcesByConsumer.ContainsKey(consumerName))
                throw new KeyNotFoundException($"The consumer of name {nameof(consumerName)} is not registered in the system");

            var sources = this.ocassionallyConnectedSourcesByConsumer[consumerName];
            if (!sources.ContainsKey(response.StreamType))
                return false;

            var consumer = sources[response.StreamType];
            lock (consumer)
            {
                status = consumer.UpdateConsumer(response);
            }
            return true;
        }
    }
}
{'fraction_non_alphanumeric': 0.07577807848443843, 'fraction_numerical': 0.0, 'mean_word_length': 3.3699324324324325, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 27, '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': '3750448', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1247, 'n_words': 264}