IPlacementBuilder IPlacementBuilder.PlaceByJumpConsistentHash<TAffinity, TKey>(Func<TKey, ulong> preHash) { if (!process.Affinities.TryGetValue(typeof(TAffinity), out var keyInfo)) { throw new BuilderException($"undefined key {typeof(TAffinity).FullName}"); } ((PartitionedAffinityInfo<TAffinity, TKey>)keyInfo).SetPlacementFunction( new Func<TKey, uint, uint> ((key, n) => JumpConsistentHash.Compute(preHash(key), n))); return this; }
internal static object GetJumpConsistentHashFor <T>(Serializer serializer) { if (typeof(T) == typeof(sbyte)) { return(new Func <sbyte, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(short)) { return(new Func <short, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(int)) { return(new Func <int, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(long)) { return(new Func <long, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(byte)) { return(new Func <byte, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(ushort)) { return(new Func <ushort, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(uint)) { return(new Func <uint, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(ulong)) { return(new Func <ulong, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(char)) { return(new Func <char, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(bool)) { return(new Func <bool, uint, uint>((o, n) => JumpConsistentHash.Compute(o ? 1ul : 0ul, n))); } else if (typeof(T).IsEnum) { return(new Func <T, uint, uint>((o, n) => JumpConsistentHash.Compute(Convert.ToUInt64(o), n))); } else if (typeof(T) == typeof(double)) { return(new Func <double, uint, uint>((o, n) => JumpConsistentHash.Compute(BitConverter.GetBytes((double)o), n))); } else if (typeof(T) == typeof(float)) { return(new Func <float, uint, uint>((o, n) => JumpConsistentHash.Compute(BitConverter.GetBytes((float)o), n))); } else if (typeof(T) == typeof(Guid)) { return(new Func <Guid, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(string)) { return(new Func <string, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(ICustomKeyType <T>).IsAssignableFrom(typeof(T))) { return(((ICustomKeyType <T>)Activator.CreateInstance(typeof(T))).JumpConsistentHasher); } else { throw new BuilderException($"type {typeof(T)} cannot be used as a key."); } }
internal static object GetJumpConsistentHashFor <T>(Serializer serializer) { if (typeof(T) == typeof(sbyte)) { return(new Func <sbyte, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(short)) { return(new Func <short, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(int)) { return(new Func <int, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(long)) { return(new Func <long, uint, uint>((o, n) => JumpConsistentHash.Compute((ulong)o, n))); } else if (typeof(T) == typeof(byte)) { return(new Func <byte, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(ushort)) { return(new Func <ushort, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(uint)) { return(new Func <uint, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(ulong)) { return(new Func <ulong, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(char)) { return(new Func <char, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(bool)) { return(new Func <bool, uint, uint>((o, n) => JumpConsistentHash.Compute(o ? 1ul : 0ul, n))); } else if (typeof(T).IsEnum) { return(new Func <T, uint, uint>((o, n) => JumpConsistentHash.Compute(Convert.ToUInt64(o), n))); } else if (typeof(T) == typeof(double)) { return(new Func <double, uint, uint>((o, n) => JumpConsistentHash.Compute(BitConverter.GetBytes((double)o), n))); } else if (typeof(T) == typeof(float)) { return(new Func <float, uint, uint>((o, n) => JumpConsistentHash.Compute(BitConverter.GetBytes((float)o), n))); } else if (typeof(T) == typeof(Guid)) { return(new Func <Guid, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else if (typeof(T) == typeof(string)) { return(new Func <string, uint, uint>((o, n) => JumpConsistentHash.Compute(o, n))); } else { throw new BuilderException($"type {typeof(T)} cannot be used as a key"); //return new Func<sbyte, uint>((o) => ConsistentHashing.Compute(serializer.SerializeObject(o), num_buckets)); } }