예제 #1
0
        /// <summary>
        /// Removes a node from the hash ring.
        ///
        /// Note that <see cref="ConsistentHash{T}"/> is immutable and
        /// this operation returns a new instance.
        /// </summary>
        public static ConsistentHash <T> operator -(ConsistentHash <T> hash, T node)
        {
            var nodeHash = ConsistentHash.HashFor(node.ToString());

            return(new ConsistentHash <T>(hash._nodes.CopyAndRemove(Enumerable.Range(1, hash._virtualNodesFactor).Select(r => new KeyValuePair <int, T>(ConsistentHash.ConcatenateNodeHash(nodeHash, r), node))),
                                          hash._virtualNodesFactor));
        }
예제 #2
0
        /// <summary>
        /// Get the node responsible for the data key.
        /// Can only be used if nodes exist in the node ring.
        /// Otherwise throws <see cref="ArgumentException"/>.
        /// </summary>
        public T NodeFor(string key)
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException(string.Format("Can't get node for [{0}] from an empty node ring", key));
            }

            return(NodeRing[Idx(Array.BinarySearch(NodeHashRing, ConsistentHash.HashFor(key)))]);
        }
예제 #3
0
        /// <summary>
        /// Get the node responsible for the data key.
        /// Can only be used if nodes exist in the node ring.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// This exception is thrown if the node ring is empty.
        /// </exception>
        public T NodeFor(byte[] key)
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException($"Can't get node for [{key}] from an empty node ring");
            }

            return(NodeRing[Idx(Array.BinarySearch(NodeHashRing, ConsistentHash.HashFor(key)))]);
        }