ClickHouse
47 строк · 1.1 Кб
1#include "FunctionsConsistentHashing.h"2#include <Functions/FunctionFactory.h>3
4
5namespace DB6{
7namespace
8{
9
10/// Code from https://arxiv.org/pdf/1406.2294.pdf
11inline int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets)12{
13int64_t b = -1, j = 0;14while (j < num_buckets)15{16b = j;17key = key * 2862933555777941757ULL + 1;18j = static_cast<int64_t>((b + 1) * (static_cast<double>(1LL << 31) / static_cast<double>((key >> 33) + 1)));19}20return static_cast<int32_t>(b);21}
22
23struct JumpConsistentHashImpl24{
25static constexpr auto name = "jumpConsistentHash";26
27using HashType = UInt64;28using ResultType = Int32;29using BucketsType = ResultType;30static constexpr auto max_buckets = static_cast<UInt64>(std::numeric_limits<BucketsType>::max());31
32static inline ResultType apply(UInt64 hash, BucketsType n)33{34return JumpConsistentHash(hash, n);35}36};37
38using FunctionJumpConsistentHash = FunctionConsistentHashImpl<JumpConsistentHashImpl>;39
40}
41
42REGISTER_FUNCTION(JumpConsistentHash)43{
44factory.registerFunction<FunctionJumpConsistentHash>();45}
46
47}
48