| 1 | #include "FunctionsConsistentHashing.h" |
|---|---|
| 2 | #include <Functions/FunctionFactory.h> |
| 3 | |
| 4 | #include <consistent_hashing.h> |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | /// An O(1) time and space consistent hash algorithm by Konstantin Oblakov |
| 10 | struct YandexConsistentHashImpl |
| 11 | { |
| 12 | static constexpr auto name = "yandexConsistentHash"; |
| 13 | |
| 14 | using HashType = UInt64; |
| 15 | /// Actually it supports UInt64, but it is efficient only if n <= 32768 |
| 16 | using ResultType = UInt16; |
| 17 | using BucketsType = ResultType; |
| 18 | static constexpr auto max_buckets = 32768; |
| 19 | |
| 20 | static inline ResultType apply(UInt64 hash, BucketsType n) |
| 21 | { |
| 22 | return ConsistentHashing(hash, n); |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | using FunctionYandexConsistentHash = FunctionConsistentHashImpl<YandexConsistentHashImpl>; |
| 27 | |
| 28 | void registerFunctionYandexConsistentHash(FunctionFactory & factory) |
| 29 | { |
| 30 | factory.registerFunction<FunctionYandexConsistentHash>(); |
| 31 | } |
| 32 | |
| 33 | } |
| 34 | |
| 35 |