Пример #1
0
        /// <summary>
        /// 通过哈希算法计算出对应的节点
        /// </summary>
        /// <param name="item">值</param>
        /// <returns>返回节点</returns>
        public T GetItemNode(string item)
        {
            var hashOfItem          = _hashAlgorithm.Hash(item);
            var nearestNodePosition = GetClockwiseNearestNode(_nodeKeysInRing, hashOfItem);

            return(_ring[_nodeKeysInRing[nearestNodePosition]]);
        }
Пример #2
0
 private void AddNode(T node)
 {
     for (int i = 0; i < _virtualNodeReplicationFactor; i++)
     {
         int hashOfVirtualNode = _hashAlgorithm.Hash(node.GetHashCode().ToString() + i);
         _ring[hashOfVirtualNode] = node;
     }
 }
Пример #3
0
        /// <summary>
        /// Calculates a hash checksum for a file.
        /// </summary>
        /// <param name="filePath">Path to file.</param>
        /// <returns>Hash checksum of the file as string.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="filePath" /> is null.</exception>
        /// <exception cref="T:System.ArgumentException"><paramref name="filePath" /> is empty.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">File is not found.</exception>
        public string Calculate(string filePath)
        {
            using (var stream = File.OpenRead(filePath))
            {
                var hash = _hashAlgorithm.Hash(stream);

                return(_encoder.Encode(hash));
            }
        }
Пример #4
0
 /// <summary>
 /// 添加节点
 /// </summary>
 /// <param name="node">节点</param>
 private void AddNode(T node)
 {
     for (var i = 0; i < VirtualNodeReplicationFactor; i++)
     {
         var hashOfVirtualNode =
             _hashAlgorithm.Hash(node.GetHashCode().ToString(CultureInfo.InvariantCulture) + i);
         _ring[hashOfVirtualNode] = node;
     }
 }
Пример #5
0
 /// <summary>
 /// Oblicz hash has³a
 /// </summary>
 /// <returns></returns>
 public string Hash()
 {
     for (int i = 0; i < length; i++)
     {
         passbyte[i] = (byte)charset[digits[i]];
     }
     return(_iha.Hash(passbyte, 0, length));
 }
Пример #6
0
        private int GetHashCode(ServiceCommand command, IDictionary <string, object> parameters)
        {
            var result = 0;

            if (command.ShuntStrategy == AddressSelectorMode.HashAlgorithm)
            {
                var parameter = parameters.Values.FirstOrDefault();
                result = _hashAlgorithm.Hash(parameter?.ToString());
            }
            return(result);
        }
Пример #7
0
        public void Add(CacheItem <K, V> cacheItem)
        {
            var startIndex = (_hashAlgorithm.Hash(cacheItem.Key) % _sets) * _entries;
            var added      = false;

            for (var slot = startIndex; slot < startIndex + _entries; slot++)
            {
                if (_cache[slot] == null)
                {
                    _cache[slot] = cacheItem;
                    added        = true;
                    break;
                }
            }

            if (!added)
            {
                var slot = _evictionPolicy.Evict(_cache, startIndex, _entries);
                _cache[slot] = cacheItem;
            }
        }
Пример #8
0
        /// <summary>
        /// Hashes the given phrase.
        /// </summary>
        /// <param name="phrase">Phrase to hash.</param>
        /// <returns>Hash of <paramref name="phrase" /> as a string.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="phrase" /> is null.</exception>
        public string Hash(ClearPhrase phrase)
        {
            if (phrase == null)
            {
                throw new ArgumentNullException(nameof(phrase));
            }

            byte[] bytes = phrase.ToBytes();

            var hash = _hashAlgorithm.Hash(bytes);

            return(_encoder.Encode(hash));
        }
Пример #9
0
        public async Task <string> Locate(string routePath, string key)
        {
            var route = await _serviceRouteProvider.SearchRoute(routePath);

            AddressModel result = new IpAddressModel();

            if (route != null)
            {
                result = await _addressSelector.SelectAsync(new AddressSelectContext()
                {
                    Address    = route.Address,
                    Descriptor = route.ServiceDescriptor,
                    HashCode   = _hashAlgorithm.Hash(key)
                });
            }
            return(result.ToString());
        }
Пример #10
0
        public IActionResult Create([FromBody] CreateLink command)
        {
            if (!(command.Link.IsValidHttpLink() || command.Link.IsValidHttpsLink()))
            {
                return(BadRequest());
            }

            Link linkInformation = new Link {
                OriginalLink = command.Link, Visitors = 0
            };

            _repository.Add(linkInformation);

            linkInformation.Hash = _hashAlgorithm.Hash(linkInformation.Id);
            _repository.Update(linkInformation);

            return(Ok(new { message = "Created" }));
        }
Пример #11
0
 public static string Hash(this byte[] rawData, IHashAlgorithm hashAlgorithm)
 {
     return(hashAlgorithm.Hash(rawData));
 }