mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 16:12:01 +00:00
Every function in its own file, part 10
This commit is contained in:
parent
261abd6c41
commit
66acafe70e
@ -1,15 +0,0 @@
|
||||
#include "FunctionsConsistentHashing.h"
|
||||
#include <Functions/FunctionFactory.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
void registerFunctionsConsistentHashing(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionYandexConsistentHash>();
|
||||
factory.registerFunction<FunctionJumpConsistentHash>();
|
||||
factory.registerFunction<FunctionSumburConsistentHash>();
|
||||
}
|
||||
|
||||
}
|
@ -8,9 +8,6 @@
|
||||
#include <Common/typeid_cast.h>
|
||||
#include <common/likely.h>
|
||||
|
||||
#include <sumbur.h>
|
||||
#include <consistent_hashing.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
@ -23,69 +20,6 @@ namespace ErrorCodes
|
||||
}
|
||||
|
||||
|
||||
/// An O(1) time and space consistent hash algorithm by Konstantin Oblakov
|
||||
struct YandexConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "yandexConsistentHash";
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// Code from https://arxiv.org/pdf/1406.2294.pdf
|
||||
static inline int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets)
|
||||
{
|
||||
int64_t b = -1, j = 0;
|
||||
while (j < num_buckets)
|
||||
{
|
||||
b = j;
|
||||
key = key * 2862933555777941757ULL + 1;
|
||||
j = static_cast<int64_t>((b + 1) * (double(1LL << 31) / double((key >> 33) + 1)));
|
||||
}
|
||||
return static_cast<int32_t>(b);
|
||||
}
|
||||
|
||||
struct JumpConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "jumpConsistentHash";
|
||||
|
||||
using HashType = UInt64;
|
||||
using ResultType = Int32;
|
||||
using BucketsType = ResultType;
|
||||
static constexpr auto max_buckets = static_cast<UInt64>(std::numeric_limits<BucketsType>::max());
|
||||
|
||||
static inline ResultType apply(UInt64 hash, BucketsType n)
|
||||
{
|
||||
return JumpConsistentHash(hash, n);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct SumburConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "sumburConsistentHash";
|
||||
|
||||
using HashType = UInt32;
|
||||
using ResultType = UInt16;
|
||||
using BucketsType = ResultType;
|
||||
static constexpr auto max_buckets = static_cast<UInt64>(std::numeric_limits<BucketsType>::max());
|
||||
|
||||
static inline ResultType apply(HashType hash, BucketsType n)
|
||||
{
|
||||
return static_cast<ResultType>(sumburConsistentHash(hash, n));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Impl>
|
||||
class FunctionConsistentHashImpl : public IFunction
|
||||
{
|
||||
@ -221,10 +155,4 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
using FunctionYandexConsistentHash = FunctionConsistentHashImpl<YandexConsistentHashImpl>;
|
||||
using FunctionJumpConsistentHash = FunctionConsistentHashImpl<JumpConsistentHashImpl>;
|
||||
using FunctionSumburConsistentHash = FunctionConsistentHashImpl<SumburConsistentHashImpl>;
|
||||
|
||||
|
||||
}
|
||||
|
44
dbms/src/Functions/jumpConsistentHash.cpp
Normal file
44
dbms/src/Functions/jumpConsistentHash.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "FunctionsConsistentHashing.h"
|
||||
#include <Functions/FunctionFactory.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/// Code from https://arxiv.org/pdf/1406.2294.pdf
|
||||
static inline int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets)
|
||||
{
|
||||
int64_t b = -1, j = 0;
|
||||
while (j < num_buckets)
|
||||
{
|
||||
b = j;
|
||||
key = key * 2862933555777941757ULL + 1;
|
||||
j = static_cast<int64_t>((b + 1) * (double(1LL << 31) / double((key >> 33) + 1)));
|
||||
}
|
||||
return static_cast<int32_t>(b);
|
||||
}
|
||||
|
||||
struct JumpConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "jumpConsistentHash";
|
||||
|
||||
using HashType = UInt64;
|
||||
using ResultType = Int32;
|
||||
using BucketsType = ResultType;
|
||||
static constexpr auto max_buckets = static_cast<UInt64>(std::numeric_limits<BucketsType>::max());
|
||||
|
||||
static inline ResultType apply(UInt64 hash, BucketsType n)
|
||||
{
|
||||
return JumpConsistentHash(hash, n);
|
||||
}
|
||||
};
|
||||
|
||||
using FunctionJumpConsistentHash = FunctionConsistentHashImpl<JumpConsistentHashImpl>;
|
||||
|
||||
void registerFunctionJumpConsistentHash(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionJumpConsistentHash>();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ void registerFunctionsNull(FunctionFactory &);
|
||||
void registerFunctionsFindCluster(FunctionFactory &);
|
||||
void registerFunctionsJSON(FunctionFactory &);
|
||||
void registerFunctionsIntrospection(FunctionFactory &);
|
||||
void registerFunctionsConsistentHashing(FunctionFactory & factory)
|
||||
|
||||
void registerFunctions()
|
||||
{
|
||||
@ -80,6 +81,7 @@ void registerFunctions()
|
||||
registerFunctionsFindCluster(factory);
|
||||
registerFunctionsJSON(factory);
|
||||
registerFunctionsIntrospection(factory);
|
||||
registerFunctionsConsistentHashing(factory);
|
||||
}
|
||||
|
||||
}
|
||||
|
18
dbms/src/Functions/registerFunctionsConsistentHashing.cpp
Normal file
18
dbms/src/Functions/registerFunctionsConsistentHashing.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
class FunctionFactory;
|
||||
|
||||
void registerFunctionYandexConsistentHash(FunctionFactory & factory);
|
||||
void registerFunctionJumpConsistentHash(FunctionFactory & factory);
|
||||
void registerFunctionSumburConsistentHash(FunctionFactory & factory);
|
||||
|
||||
void registerFunctionsConsistentHashing(FunctionFactory & factory)
|
||||
{
|
||||
registerFunctionYandexConsistentHash(factory);
|
||||
registerFunctionJumpConsistentHash(factory);
|
||||
registerFunctionSumburConsistentHash(factory);
|
||||
}
|
||||
|
||||
}
|
||||
|
34
dbms/src/Functions/sumburConsistentHash.cpp
Normal file
34
dbms/src/Functions/sumburConsistentHash.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "FunctionsConsistentHashing.h"
|
||||
#include <Functions/FunctionFactory.h>
|
||||
|
||||
#include <sumbur.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
struct SumburConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "sumburConsistentHash";
|
||||
|
||||
using HashType = UInt32;
|
||||
using ResultType = UInt16;
|
||||
using BucketsType = ResultType;
|
||||
static constexpr auto max_buckets = static_cast<UInt64>(std::numeric_limits<BucketsType>::max());
|
||||
|
||||
static inline ResultType apply(HashType hash, BucketsType n)
|
||||
{
|
||||
return static_cast<ResultType>(sumburConsistentHash(hash, n));
|
||||
}
|
||||
};
|
||||
|
||||
using FunctionSumburConsistentHash = FunctionConsistentHashImpl<SumburConsistentHashImpl>;
|
||||
|
||||
void registerFunctionSumburConsistentHash(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionSumburConsistentHash>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
34
dbms/src/Functions/yandexConsistentHash.cpp
Normal file
34
dbms/src/Functions/yandexConsistentHash.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#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
|
||||
struct YandexConsistentHashImpl
|
||||
{
|
||||
static constexpr auto name = "yandexConsistentHash";
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
using FunctionYandexConsistentHash = FunctionConsistentHashImpl<YandexConsistentHashImpl>;
|
||||
|
||||
void registerFunctionYandexConsistentHash(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionYandexConsistentHash>();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user