public static ulong ComputeHash64(this Murmur128 murmur, byte[] data) { var hash128 = murmur.ComputeHash(data); var hash64 = BitConverter.ToUInt64(hash128, 0); // take first 64 bits of 128 bit hash return(hash64); }
public ExistenceStatCollector(string memberName, Configuration configuration, out bool cancel) { MemberName = memberName; _byteConverter = ByteConverterHelpers.GetByteConverter <T>(); _murmurHash = MurmurHash.Create128(managed: true, preference: AlgorithmPreference.X64); cancel = false; }
/// <summary> /// 获取节点 /// </summary> /// <param name="k"></param> /// <returns></returns> public StoreNode GetPrimary(byte[] k) { Murmur128 murmur = MurmurHashFactory.MurmurHash.Create128((uint)Environment.TickCount); byte[] digest = murmur.ComputeHash(k); ulong key = MurmurHashFactory.Hash(digest); return(GetNodeForKey(key)); }
public ulong Hash(string key, uint seed) { Murmur128 murmur128 = MurmurHash.Create128(seed: seed, preference: AlgorithmPreference.X64); byte[] keyToBytes = Encoding.ASCII.GetBytes(key); byte[] seedResult = murmur128.ComputeHash(keyToBytes); return(BitConverter.ToUInt64(seedResult, 0)); }
private HashAlgorithm GetHash() { switch (Type) { case ChecksumType.Murmur128: return(Murmur128.CreateMurmur()); case ChecksumType.Md5: return(MD5.Create()); default: throw new NotImplementedException("Unknown checksum type: " + Type); } }
//将传入的字符串通过murmurhash函数生成32位长的字符串 private static string GetHashAsString(string stringToHash) { Murmur128 urlHash = MurmurHash.Create128(managed: false); byte[] urlbyte = System.Text.Encoding.UTF8.GetBytes(stringToHash); byte[] hash = urlHash.ComputeHash(urlbyte); //以下代码也可以用 BitConverter.ToString(hash)代替 var builder = new StringBuilder(16); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } return(builder.ToString()); }
private long GetRingPosition(long key) { using (Murmur128 hashAlgorithm = MurmurHash.Create128()) { byte[] hash = hashAlgorithm.ComputeHash(BitConverter.GetBytes(key)); long total = 0; for (int startIndex = 0; startIndex < hash.Length; startIndex += 8) { unchecked { total += BitConverter.ToInt64(hash, startIndex); } } return(total); } }
internal static void Run() { const int iterations = 0x20000; const int testSize = 0x10000; List <long> timeMurmur = new List <long>(iterations); List <long> timeAdler = new List <long>(iterations); List <long> timeMd5 = new List <long>(iterations); byte[] randBytes = new byte[testSize]; var rnd = new Random(); var watchMurmur = new Stopwatch(); var watchAdler = new Stopwatch(); var watchMd5 = new Stopwatch(); bool dry = true; DoRun: for (int i = 0; i < iterations; i++) { rnd.NextBytes(randBytes); { var murmur = Murmur128.CreateMurmur(); watchMurmur.Restart(); murmur.ComputeHash(randBytes); watchMurmur.Stop(); if (!dry) { timeMurmur.Add(watchMurmur.ElapsedTicks); } } { var adler = new Adler32(); watchAdler.Restart(); adler.Update(randBytes); watchAdler.Stop(); if (!dry) { timeAdler.Add(watchAdler.ElapsedTicks); } } { var md5 = MD5.Create(); watchMd5.Restart(); md5.ComputeHash(randBytes); watchMd5.Stop(); if (!dry) { timeMd5.Add(watchMd5.ElapsedTicks); } } if (i == 0x1000 && dry) { dry = false; goto DoRun; } } var avgMurmur = TimeSpan.FromTicks(Convert.ToInt64(timeMurmur.Average())); var avgAdler = TimeSpan.FromTicks(Convert.ToInt64(timeAdler.Average())); var avgMd5 = TimeSpan.FromTicks(Convert.ToInt64(timeMd5.Average())); Console.WriteLine("Average murmur:\t" + avgMurmur); Console.WriteLine("Average adler:\t" + avgAdler); Console.WriteLine("Average md5:\t" + avgMd5); Console.ReadKey(); }
private static List <RecommendationProduct> Build(Murmur128 hash, int partitionId, int partitionCount, List <SmCalcProduct> products, int userId) { var entities = new List <RecommendationProduct>(); foreach (var product in products) { var productEntity = new RecommendationProduct(hash) { ClientId = product.ClientId, ScenarioId = product.ScenarioId, ModelId = product.ModelId, ProductId = product.ProductId, ProductName = product.ProductName, PriceLadderId = product.PriceLadderId, PartitionNumber = partitionId, PartitionCount = partitionCount, HierarchyId = product.HierarchyId, HierarchyName = product.HierarchyName, ScheduleCount = product.ScheduleCount, ScheduleCrossProductCount = product.ScheduleCrossProductCount, ScheduleProductMaskFilterCount = product.ScheduleProductMaskFilterCount, ScheduleMaxMarkdownFilterCount = product.ScheduleMaxMarkdownFilterCount, HighPredictionCount = product.HighPredictionCount, NegativeRevenueCount = product.NegativeRevenueCount, InvalidMarkdownTypeCount = product.InvalidMarkdownTypeCount, CurrentMarkdownCount = product.CurrentMarkdownCount, CurrentMarkdownTypeId = (int)product.CurrentMarkdownType, CurrentSellingPrice = product.CurrentSellingPrice, OriginalSellingPrice = product.OriginalSellingPrice, CurrentCostPrice = product.CurrentCostPrice, CurrentStock = product.CurrentStock, CurrentSalesQuantity = product.CurrentSalesQuantity, SellThroughTarget = product.SellThroughTarget, CurrentMarkdownDepth = product.CurrentMarkdownDepth, CurrentDiscountLadderDepth = product.CurrentDiscountLadderDepth, StateName = product.State.ToString(), DecisionStateName = product.DecisionState.ToString(), CreatedBy = userId }; foreach (var recommendation in product.Recommendations) { var recommendationEntity = new Recommendation(hash) { ClientId = product.ClientId, ScenarioId = product.ScenarioId, ScheduleId = recommendation.ScheduleId, ScheduleMask = recommendation.ScheduleMask, ScheduleMarkdownCount = recommendation.ScheduleMarkdownCount, IsCsp = recommendation.IsCsp, PricePathPrices = string.Join(";", recommendation.PricePath), PricePathHashCode = string.Join(";", recommendation.PricePath).GetHashCode(), RevisionId = recommendation.RevisionId, Rank = recommendation.Rank, TotalMarkdownCount = recommendation.TotalMarkdownCount, TerminalStock = recommendation.TerminalStock, TotalRevenue = recommendation.TotalRevenue, TotalCost = recommendation.TotalCost, TotalMarkdownCost = recommendation.TotalMarkdownCost, FinalDiscount = recommendation.FinalDiscount, StockValue = recommendation.StockValue, EstimatedProfit = recommendation.EstimatedProfit, EstimatedSales = recommendation.EstimatedSales, SellThroughRate = recommendation.SellThroughRate, SellThroughTarget = recommendation.SellThroughTarget, FinalMarkdownTypeId = (int)recommendation.FinalMarkdownType, Product = productEntity, CreatedBy = userId }; recommendationEntity.Projections = recommendation .Projections .Select(x => new RecommendationProjection(hash) { ClientId = product.ClientId, ScenarioId = product.ScenarioId, Week = x.Week, Discount = x.Discount, Price = x.Price, Quantity = x.Quantity, Revenue = x.Revenue, Stock = x.Stock, MarkdownCost = x.MarkdownCost, AccumulatedMarkdownCount = x.AccumulatedMarkdownCount, MarkdownCount = x.MarkdownCount, Elasticity = x.Elasticity, Decay = x.Decay, MarkdownTypeId = (int)x.MarkdownType, Recommendation = recommendationEntity }) .ToList(); productEntity.Recommendations.Add(recommendationEntity); } entities.Add(productEntity); } return(entities); }
private static List <Recommendation> Build(Murmur128 hash, SmRecommendationProductSummary product, IEnumerable <SmCalcRecommendation> recommendations, int userId) { var results = new List <Recommendation>(); foreach (var recommendation in recommendations) { var recommendationEntity = new Recommendation(hash) { ClientId = product.ClientId, ScenarioId = product.ScenarioId, ScheduleId = recommendation.ScheduleId, ScheduleMask = recommendation.ScheduleMask, ScheduleMarkdownCount = recommendation.ScheduleMarkdownCount, IsCsp = recommendation.IsCsp, PricePathPrices = string.Join(";", recommendation.PricePath), PricePathHashCode = string.Join(";", recommendation.PricePath).GetHashCode(), RevisionId = recommendation.RevisionId, Rank = recommendation.Rank, TotalMarkdownCount = recommendation.TotalMarkdownCount, TerminalStock = recommendation.TerminalStock, TotalRevenue = recommendation.TotalRevenue, TotalCost = recommendation.TotalCost, TotalMarkdownCost = recommendation.TotalMarkdownCost, FinalDiscount = recommendation.FinalDiscount, StockValue = recommendation.StockValue, EstimatedProfit = recommendation.EstimatedProfit, EstimatedSales = recommendation.EstimatedSales, SellThroughRate = recommendation.SellThroughRate, SellThroughTarget = recommendation.SellThroughTarget, FinalMarkdownTypeId = (int)recommendation.FinalMarkdownType, RecommendationProductGuid = product.RecommendationProductGuid, CreatedBy = userId }; recommendationEntity.Projections = recommendation .Projections .Select(x => new RecommendationProjection(hash) { ClientId = product.ClientId, ScenarioId = product.ScenarioId, Week = x.Week, Discount = x.Discount, Price = x.Price, Quantity = x.Quantity, Revenue = x.Revenue, Stock = x.Stock, MarkdownCost = x.MarkdownCost, AccumulatedMarkdownCount = x.AccumulatedMarkdownCount, MarkdownCount = x.MarkdownCount, Elasticity = x.Elasticity, Decay = x.Decay, MarkdownTypeId = (int)x.MarkdownType, Recommendation = recommendationEntity }) .ToList(); results.Add(recommendationEntity); } return(results); }
public void TestGetHashSize() { Murmur128 murmur128 = new Murmur128(1); murmur128.HashSize.Should().Be(128); }
public Hasher() { _byteConverter = ByteConverterHelpers.GetByteConverter <T>(); _murmurHash = MurmurHash.Create128(managed: true, preference: AlgorithmPreference.X64); }