aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/cityhash
diff options
context:
space:
mode:
authorthegeorg <thegeorg@yandex-team.com>2023-10-03 11:19:48 +0300
committerthegeorg <thegeorg@yandex-team.com>2023-10-03 11:43:28 +0300
commitcda0c13f23f6b169fb0a49dc504b40a0aaecea09 (patch)
tree26476e92e5af2c856e017afb1df8f8dff42495bf /library/python/cityhash
parent4854116da9c5e3c95bb8440f2ea997c54b6e1a61 (diff)
downloadydb-cda0c13f23f6b169fb0a49dc504b40a0aaecea09.tar.gz
Move contrib/tools/jdk to build/platform/java/jdk/testing
Diffstat (limited to 'library/python/cityhash')
-rw-r--r--library/python/cityhash/cityhash.pyx75
-rw-r--r--library/python/cityhash/hash.cpp32
-rw-r--r--library/python/cityhash/hash.h6
-rw-r--r--library/python/cityhash/ya.make16
4 files changed, 129 insertions, 0 deletions
diff --git a/library/python/cityhash/cityhash.pyx b/library/python/cityhash/cityhash.pyx
new file mode 100644
index 0000000000..6f0046f0d7
--- /dev/null
+++ b/library/python/cityhash/cityhash.pyx
@@ -0,0 +1,75 @@
+from libcpp.pair cimport pair
+
+cdef extern from "util/system/types.h":
+ ctypedef unsigned long ui64
+
+
+cdef extern from "util/digest/city.h":
+ ui64 CityHash64(const char* buf, size_t len) nogil
+ pair[ui64, ui64] CityHash128(const char* buf, size_t len) nogil
+ ui64 CityHash64WithSeed(const char* buf, size_t len, ui64 seed) nogil
+
+
+cdef extern from "library/python/cityhash/hash.h":
+ ui64 FileCityHash128WithSeedHigh64(const char* fpath) nogil except+
+ ui64 FileCityHash64(const char* fpath) nogil except+
+
+
+def hash64(content):
+ cdef const char* s = content
+ cdef size_t size = len(content)
+ cdef ui64 res = 0
+
+ if size > 128:
+ with nogil:
+ res = CityHash64(s, size)
+ else:
+ res = CityHash64(s, size)
+
+ return res
+
+def hash128(content):
+ cdef const char* s = content
+ cdef size_t size = len(content)
+ cdef pair[ui64, ui64] res = pair[ui64, ui64](0, 0)
+
+ if size > 128:
+ with nogil:
+ res = CityHash128(s, size)
+ else:
+ res = CityHash128(s, size)
+ return res
+
+
+def hash64seed(content, seed):
+ cdef const char* s = content
+ cdef size_t size = len(content)
+ cdef ui64 _seed = seed;
+
+ if size > 128:
+ with nogil:
+ res = CityHash64WithSeed(s, size, _seed)
+ else:
+ res = CityHash64WithSeed(s, size, _seed)
+
+ return res
+
+
+def filehash64(path):
+ cdef const char* p = path
+ cdef ui64 res = 0
+
+ with nogil:
+ res = FileCityHash64(p)
+
+ return res
+
+
+def filehash128high64(path):
+ cdef const char* p = path
+ cdef ui64 res = 0
+
+ with nogil:
+ res = FileCityHash128WithSeedHigh64(p)
+
+ return res
diff --git a/library/python/cityhash/hash.cpp b/library/python/cityhash/hash.cpp
new file mode 100644
index 0000000000..17bd3a75f3
--- /dev/null
+++ b/library/python/cityhash/hash.cpp
@@ -0,0 +1,32 @@
+#include "hash.h"
+
+#include <util/digest/city.h>
+#include <util/generic/string.h>
+#include <util/memory/blob.h>
+#include <util/system/file.h>
+#include <util/system/fstat.h>
+
+void ReadFile(const char* fpath, TBlob& blob) {
+ TFile f(TString{fpath}, RdOnly | Seq);
+ const TFileStat fs(f);
+ auto size = fs.Size;
+
+ if (size < (64 << 10)) {
+ blob = TBlob::FromFileContent(f, 0, size);
+ } else {
+ blob = TBlob::FromFile(f);
+ }
+}
+
+ui64 FileCityHash128WithSeedHigh64(const char* fpath) {
+ TBlob blob;
+ ReadFile(fpath, blob);
+ const uint128 hash = CityHash128WithSeed((const char*)blob.Data(), blob.Size(), uint128(0, blob.Size()));
+ return Uint128High64(hash);
+}
+
+ui64 FileCityHash64(const char* fpath) {
+ TBlob blob;
+ ReadFile(fpath, blob);
+ return CityHash64(static_cast<const char*>(blob.Data()), blob.Size());
+}
diff --git a/library/python/cityhash/hash.h b/library/python/cityhash/hash.h
new file mode 100644
index 0000000000..64b22ba74b
--- /dev/null
+++ b/library/python/cityhash/hash.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <util/system/defaults.h>
+
+ui64 FileCityHash128WithSeedHigh64(const char* fpath);
+ui64 FileCityHash64(const char* fpath);
diff --git a/library/python/cityhash/ya.make b/library/python/cityhash/ya.make
new file mode 100644
index 0000000000..7948e19389
--- /dev/null
+++ b/library/python/cityhash/ya.make
@@ -0,0 +1,16 @@
+PY23_LIBRARY()
+
+SRCS(
+ hash.cpp
+)
+
+PY_SRCS(
+ TOP_LEVEL
+ cityhash.pyx
+)
+
+END()
+
+RECURSE_FOR_TESTS(
+ test
+)