utils: hashing: add simple_xx_hasher

And a respective unit test.

Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
This commit is contained in:
Benny Halevy
2023-04-17 12:29:20 +03:00
parent b638dddf1b
commit f4fefec343
3 changed files with 50 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
#include <seastar/testing/thread_test_case.hh>
#include "utils/hashers.hh"
#include "utils/xx_hasher.hh"
#include "utils/simple_hashers.hh"
#include "gc_clock.hh"
#include "test/lib/simple_schema.hh"
#include "reader_concurrency_semaphore.hh"
@@ -102,3 +103,17 @@ SEASTAR_THREAD_TEST_CASE(mutation_fragment_sanity_check) {
check_hash(f, 0x5092daca1b27ea26ull);
}
}
BOOST_AUTO_TEST_CASE(basic_xx_hasher_sanity_check) {
simple_xx_hasher hasher1;
hasher1.update(reinterpret_cast<const char*>(std::data(text_part1)), std::size(text_part1));
hasher1.update(reinterpret_cast<const char*>(std::data(text_part2)), std::size(text_part2));
auto hash1 = hasher1.finalize();
bytes_view_hasher hasher2;
hasher2.update(reinterpret_cast<const char*>(std::data(text_part1)), std::size(text_part1));
hasher2.update(reinterpret_cast<const char*>(std::data(text_part2)), std::size(text_part2));
auto hash2 = hasher2.finalize();
BOOST_CHECK_EQUAL(hash1, hash2);
}

View File

@@ -8,6 +8,7 @@
#include "utils/hashers.hh"
#include "utils/xx_hasher.hh"
#include "utils/simple_hashers.hh"
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
@@ -19,6 +20,8 @@ static_assert(HasherReturningBytes<md5_hasher>);
static_assert(HasherReturningBytes<sha256_hasher>);
static_assert(HasherReturningBytes<xx_hasher>);
static_assert(SimpleHasher<simple_xx_hasher>);
template <typename T> struct hasher_traits;
template <> struct hasher_traits<md5_hasher> { using impl_type = CryptoPP::Weak::MD5; };
template <> struct hasher_traits<sha256_hasher> { using impl_type = CryptoPP::SHA256; };

32
utils/simple_hashers.hh Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2023-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#include <xxhash.h>
#pragma GCC diagnostic pop
#include "utils/hashing.hh"
template<typename H>
concept SimpleHasher = HasherReturning<H, size_t>;
struct simple_xx_hasher : public hasher {
XXH64_state_t _state;
simple_xx_hasher(uint64_t seed = 0) noexcept {
XXH64_reset(&_state, seed);
}
void update(const char* ptr, size_t length) noexcept override {
XXH64_update(&_state, ptr, length);
}
size_t finalize() {
return static_cast<size_t>(XXH64_digest(&_state));
}
};