ClickHouse
34 строки · 914.0 Байт
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 KostikConsistentHashImpl
11{
12static constexpr auto name = "kostikConsistentHash";
13
14using HashType = UInt64;
15/// Actually it supports UInt64, but it is efficient only if n <= 32768
16using ResultType = UInt16;
17using BucketsType = ResultType;
18static constexpr auto max_buckets = 32768;
19
20static inline ResultType apply(UInt64 hash, BucketsType n)
21{
22return ConsistentHashing(hash, n);
23}
24};
25
26using FunctionKostikConsistentHash = FunctionConsistentHashImpl<KostikConsistentHashImpl>;
27
28REGISTER_FUNCTION(KostikConsistentHash)
29{
30factory.registerFunction<FunctionKostikConsistentHash>();
31factory.registerAlias("yandexConsistentHash", "kostikConsistentHash");
32}
33
34}
35