public void InsertGet_Wildcards_SecondAndThird()
        {
            var target = new StringTrie <int>();

            target.GetOrInsert("a", new[] { "a", "c" }, () => 1);
            target.GetOrInsert("a", new[] { "a", "d" }, () => 2);
            target.GetOrInsert("a", new[] { "b", "c" }, () => 3);
            target.GetOrInsert("a", new[] { "b", "d" }, () => 4);

            target.Get("a", new[] { "*", "*" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(1, 2, 3, 4);
        }
        public void InsertGet_Test()
        {
            var target = new StringTrie <int>();

            target.GetOrInsert("a", new string[0], () => 1);
            target.GetOrInsert("a", new[] { "b", }, () => 2);
            target.GetOrInsert("a", new[] { "b", "c" }, () => 3);
            target.GetOrInsert("a", new[] { "b", "c" }, () => 4);
            target.GetOrInsert("a", new[] { "b", "d" }, () => 5);

            target.Get("a", new string[] { }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(1);
            target.Get("a", new[] { "b" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(2);
            target.Get("a", new[] { "b", "c" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(3);
            target.Get("a", new[] { "b", "d" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(5);
        }
示例#3
0
        public IDisposable Participate <TRequest, TResponse>(string topic, IParticipant <TRequest, TResponse> participant)
        {
            var channelObj = _channels.GetOrInsert(typeof(TRequest).FullName, typeof(TResponse).FullName, topic.Split('.'), () => new Channel <TRequest, TResponse>());

            if (!(channelObj is Channel <TRequest, TResponse> channel))
            {
                throw new Exception("Could not get channel");
            }
            participant.Id = Guid.NewGuid();
            channel.Add(participant);
            return(new Token <TRequest, TResponse>(this, typeof(TRequest).Name, typeof(TResponse).Name, topic, participant.Id));
        }
        private void AddTopicSubscription <TPayload>(ISubscription <TPayload> subscription, string topic)
        {
            var channel = _topicChannels.GetOrInsert(typeof(TPayload).FullName, topic.Split('.'), () => new Channel <TPayload>());

            if (channel == null)
            {
                throw new Exception("Channel not found");
            }
            if (!(channel is Channel <TPayload> typedChannel))
            {
                throw new Exception($"Expected channel of type {typeof(TPayload).FullName} but found {channel.GetType().FullName}");
            }
            typedChannel.AddSubscription(subscription.Id, subscription);
        }
        public IDisposable Listen <TRequest, TResponse>(string topic, IListener <TRequest, TResponse> listener)
        {
            listener.Id = Guid.NewGuid();
            var inserted = _listeners.GetOrInsert(typeof(TRequest).FullName, typeof(TResponse).FullName, topic.Split('.'), () => listener);

            if (inserted == null)
            {
                throw new Exception("Could not get channel");
            }
            if (inserted != listener)
            {
                throw new Exception("Could not add a second listener to this channel");
            }
            return(new Token <TRequest, TResponse>(this, typeof(TRequest).Name, typeof(TResponse).Name, topic, listener.Id));
        }