aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/consistent_hashing/consistent_hashing.h
diff options
context:
space:
mode:
authorvitalyisaev <vitalyisaev@ydb.tech>2023-11-14 09:58:56 +0300
committervitalyisaev <vitalyisaev@ydb.tech>2023-11-14 10:20:20 +0300
commitc2b2dfd9827a400a8495e172a56343462e3ceb82 (patch)
treecd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /library/cpp/consistent_hashing/consistent_hashing.h
parentd4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff)
downloadydb-c2b2dfd9827a400a8495e172a56343462e3ceb82.tar.gz
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'library/cpp/consistent_hashing/consistent_hashing.h')
-rw-r--r--library/cpp/consistent_hashing/consistent_hashing.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/library/cpp/consistent_hashing/consistent_hashing.h b/library/cpp/consistent_hashing/consistent_hashing.h
new file mode 100644
index 0000000000..8e4d299150
--- /dev/null
+++ b/library/cpp/consistent_hashing/consistent_hashing.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <util/system/defaults.h>
+
+/*
+ * Maps random ui64 x (in fact hash of some string) to n baskets/shards.
+ * Output value is id of a basket. 0 <= ConsistentHashing(x, n) < n.
+ * Probability of all baskets must be equal. Also, it should be consistent
+ * in terms, that with different n_1 < n_2 probability of
+ * ConsistentHashing(x, n_1) != ConsistentHashing(x, n_2) must be equal to
+ * (n_2 - n_1) / n_2 - the least possible with previous conditions.
+ * It requires O(1) memory and cpu to calculate. So, it is faster than classic
+ * consistent hashing algos with points on circle.
+ */
+size_t ConsistentHashing(ui64 x, size_t n); // Works good for n < 65536
+size_t ConsistentHashing(ui64 lo, ui64 hi, size_t n); // Works good for n < 4294967296