Exemple #1
0
        /// <summary>
        /// init
        /// </summary>
        /// <param name="arrNodes"></param>
        public void Init(string[] arrNodes)
        {
            if (arrNodes == null || arrNodes.Length == 0)
            {
                throw new ArgumentNullException("arrNodes");
            }

            this._arrNodes = arrNodes.Distinct().ToArray();
            this._dic.Clear();
            this._keys = null;

            foreach (var node in arrNodes)
            {
                for (int i = 0; i < 250; i++)
                {
                    var hashCode = Ketama.GetHashCode(string.Concat(node, "-", i.ToString()));
                    this._dic[hashCode] = node;
                }
            }

            var listKeys = this._dic.Keys.ToList();

            listKeys.Sort();
            this._keys = listKeys.ToArray();
        }
Exemple #2
0
        /// <summary>
        /// calc
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetNode(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var i = Array.BinarySearch(this._keys, Ketama.GetHashCode(key));

            if (i < 0)
            {
                i = ~i;
                if (i >= this._keys.Length)
                {
                    i = 0;
                }
            }
            return(this._dic[this._keys[i]]);
        }