2019-08-04 14:01:04 +00:00
|
|
|
#include "FunctionsConsistentHashing.h"
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
|
|
|
#include <consistent_hashing.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// An O(1) time and space consistent hash algorithm by Konstantin Oblakov
|
2022-03-24 01:18:25 +00:00
|
|
|
struct KostikConsistentHashImpl
|
2019-08-04 14:01:04 +00:00
|
|
|
{
|
2022-03-24 01:18:25 +00:00
|
|
|
static constexpr auto name = "kostikConsistentHash";
|
2019-08-04 14:01:04 +00:00
|
|
|
|
|
|
|
using HashType = UInt64;
|
|
|
|
/// Actually it supports UInt64, but it is efficient only if n <= 32768
|
|
|
|
using ResultType = UInt16;
|
|
|
|
using BucketsType = ResultType;
|
|
|
|
static constexpr auto max_buckets = 32768;
|
|
|
|
|
|
|
|
static inline ResultType apply(UInt64 hash, BucketsType n)
|
|
|
|
{
|
|
|
|
return ConsistentHashing(hash, n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-24 01:18:25 +00:00
|
|
|
using FunctionKostikConsistentHash = FunctionConsistentHashImpl<KostikConsistentHashImpl>;
|
2019-08-04 14:01:04 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(KostikConsistentHash)
|
2019-08-04 14:01:04 +00:00
|
|
|
{
|
2022-03-24 01:18:25 +00:00
|
|
|
factory.registerFunction<FunctionKostikConsistentHash>();
|
2022-03-24 01:21:57 +00:00
|
|
|
factory.registerAlias("yandexConsistentHash", "kostikConsistentHash");
|
2019-08-04 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|