Example #1
0
            public int Update(Peer peer, BindingKey subscription, UpdateAction action)
            {
                if (IsLeaf(subscription))
                {
                    var update = UpdateList(peer, action);
                    _peerCountIncludingChildren += update;

                    return(update);
                }

                var nextPart = subscription.GetPart(_nextPartIndex);

                if (subscription.IsSharp(_nextPartIndex) || nextPart == null)
                {
                    var sharpNode = GetOrCreateSharpNode();
                    return(UpdateChildNode(sharpNode, peer, subscription, action, null, _removeSharpNode));
                }

                if (subscription.IsStar(_nextPartIndex))
                {
                    var starNode = GetOrCreateStarNode();
                    return(UpdateChildNode(starNode, peer, subscription, action, null, _removeStarNode));
                }

                var childNode = GetOrAddChildNode(nextPart);

                return(UpdateChildNode(childNode, peer, subscription, action, nextPart, _removeNode));
            }
Example #2
0
            public void Accept(PeerCollector peerCollector, BindingKey routingKey)
            {
                if (IsLeaf(routingKey) || _matchesAll)
                {
                    peerCollector.Offer(_peers);
                    return;
                }

                _sharpNode?.Accept(peerCollector, routingKey);
                _starNode?.Accept(peerCollector, routingKey);

                var nextPart = routingKey.GetPart(_nextPartIndex);

                if (nextPart == null)
                {
                    return;
                }

                if (_childrenNodes == null)
                {
                    return;
                }

                SubscriptionNode childNode;

                if (_childrenNodes.TryGetValue(nextPart, out childNode))
                {
                    childNode.Accept(peerCollector, routingKey);
                }
            }
Example #3
0
        public IList <Peer> GetPeers(BindingKey routingKey)
        {
            var peerCollector = new PeerCollector(_peersMatchingAllMessages);

            _rootNode.Accept(peerCollector, routingKey);

            return(peerCollector.GetPeers());
        }
Example #4
0
 private void UpdatePeerSubscription(Peer peer, BindingKey subscription, UpdateAction action)
 {
     if (subscription.IsEmpty)
     {
         UpdatePeersMatchingAllMessages(peer, action);
     }
     else
     {
         _rootNode.Update(peer, subscription, action);
     }
 }
Example #5
0
            private int UpdateChildNode(SubscriptionNode childNode, Peer peer, BindingKey subscription, UpdateAction action, string childNodePart, Action <SubscriptionNode, string> remover)
            {
                var update = childNode.Update(peer, subscription, action);

                _peerCountIncludingChildren += update;

                if (childNode.IsEmpty)
                {
                    remover(this, childNodePart);
                }

                return(update);
            }
Example #6
0
            private bool IsLeaf(BindingKey bindingKey)
            {
                if (_nextPartIndex == 0)
                {
                    return(false);
                }

                if (bindingKey.IsEmpty)
                {
                    return(_nextPartIndex == 1);
                }

                return(_nextPartIndex == bindingKey.PartCount);
            }
Example #7
0
        private void RemoveFromGlobalSubscriptionsIndex(MessageTypeId messageTypeId, BindingKey bindingKey)
        {
            var subscriptionTree = _globalSubscriptionsIndex.GetValueOrDefault(messageTypeId);

            if (subscriptionTree == null)
            {
                return;
            }

            subscriptionTree.Remove(Peer, bindingKey);

            if (subscriptionTree.IsEmpty)
            {
                _globalSubscriptionsIndex.Remove(messageTypeId);
            }
        }
        public static Func <IMessage, bool> GetPredicate(Type messageType, BindingKey bindingKey)
        {
            if (bindingKey.IsEmpty)
            {
                return(_ => true);
            }

            var cacheItem = GetOrCreateCacheItem(messageType);

            var count         = Math.Min(cacheItem.MembersToStringExpressions.Count, bindingKey.PartCount);
            var subPredicates = new List <Expression>();

            for (var index = 0; index < count; index++)
            {
                if (bindingKey.IsSharp(index))
                {
                    break;
                }

                if (bindingKey.IsStar(index))
                {
                    continue;
                }

                var part = bindingKey.GetPart(index);
                var memberToStringExpression = cacheItem.MembersToStringExpressions[index];
                subPredicates.Add(Expression.MakeBinary(ExpressionType.Equal, memberToStringExpression, Expression.Constant(part)));
            }

            if (!subPredicates.Any())
            {
                return(_ => true);
            }

            var finalExpression = subPredicates.Aggregate((Expression)null, (final, exp) => final == null ? exp : Expression.AndAlso(final, exp));

            return((Func <IMessage, bool>)Expression.Lambda(finalExpression, cacheItem.ParameterExpression).Compile());
        }
Example #9
0
        public bool Matches(BindingKey routingKey)
        {
            if (BindingKey.IsEmpty)
            {
                return(true);
            }

            for (var i = 0; i < routingKey.PartCount; i++)
            {
                var evaluatedPart = BindingKey.GetPart(i);
                if (evaluatedPart == "#")
                {
                    return(true);
                }

                if (evaluatedPart != "*" && routingKey.GetPart(i) != evaluatedPart)
                {
                    return(false);
                }
            }

            return(routingKey.PartCount == BindingKey.PartCount);
        }
Example #10
0
 public void Remove(Peer peer, BindingKey subscription)
 {
     UpdatePeerSubscription(peer, subscription, UpdateAction.Remove);
 }
Example #11
0
 public void Add(Peer peer, BindingKey subscription)
 {
     UpdatePeerSubscription(peer, subscription, UpdateAction.Add);
 }
Example #12
0
        private void AddToGlobalSubscriptionsIndex(MessageTypeId messageTypeId, BindingKey bindingKey)
        {
            var subscriptionTree = _globalSubscriptionsIndex.GetOrAdd(messageTypeId, _ => new PeerSubscriptionTree());

            subscriptionTree.Add(Peer, bindingKey);
        }
Example #13
0
 public static MessageBinding FromMessage(IMessage message)
 => new MessageBinding(message.TypeId(), BindingKey.Create(message));
Example #14
0
 public MessageBinding(MessageTypeId messageTypeId, BindingKey routingKey)
 {
     MessageTypeId = messageTypeId;
     RoutingKey    = routingKey;
 }
Example #15
0
 public Subscription(MessageTypeId messageTypeId, BindingKey bindingKey)
 {
     MessageTypeId = messageTypeId;
     BindingKey    = bindingKey;
 }