1#include "FunctionsConsistentHashing.h"
2#include <Functions/FunctionFactory.h>
3
4#include <consistent_hashing.h>
5
6namespace DB
7{
8
9/// An O(1) time and space consistent hash algorithm by Konstantin Oblakov
10struct 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
26using FunctionYandexConsistentHash = FunctionConsistentHashImpl<YandexConsistentHashImpl>;
27
28void registerFunctionYandexConsistentHash(FunctionFactory & factory)
29{
30 factory.registerFunction<FunctionYandexConsistentHash>();
31}
32
33}
34
35