Commit 6b34b1b7 authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

packaging for farmhash

Summary:
This diff updates the unmodified checkin of farmhash in
folly/external/farmhash to be compatible with the folly namespaces,
build system, and unit tests, and exposes it via folly/hash/FarmHash.h.
The primary entry point is the function folly::hash::farmhash::Hash.

Changes to the original farmhash code have been kept as small as possible
while still removing the tests from the main hash implementation; many
opportunities for cleanup have been skipped.

Reviewed By: yfeldblum

Differential Revision: D13469230

fbshipit-source-id: 48dfe7d4e7d7f02ed5552b4e9e92dbad27592e45
parent 01f7795b
...@@ -434,6 +434,9 @@ if (BUILD_TESTS) ...@@ -434,6 +434,9 @@ if (BUILD_TESTS)
#TEST async_io_test SOURCES AsyncIOTest.cpp #TEST async_io_test SOURCES AsyncIOTest.cpp
TEST fs_util_test SOURCES FsUtilTest.cpp TEST fs_util_test SOURCES FsUtilTest.cpp
DIRECTORY external/farmhash/test/
TEST farmhash_test SOURCES farmhash_test.cpp
DIRECTORY logging/test/ DIRECTORY logging/test/
TEST async_file_writer_test SOURCES AsyncFileWriterTest.cpp TEST async_file_writer_test SOURCES AsyncFileWriterTest.cpp
TEST config_parser_test SOURCES ConfigParserTest.cpp TEST config_parser_test SOURCES ConfigParserTest.cpp
...@@ -516,6 +519,7 @@ if (BUILD_TESTS) ...@@ -516,6 +519,7 @@ if (BUILD_TESTS)
DIRECTORY hash/test/ DIRECTORY hash/test/
TEST checksum_test SOURCES ChecksumTest.cpp TEST checksum_test SOURCES ChecksumTest.cpp
TEST farm_hash_test SOURCES FarmHashTest.cpp
TEST hash_test WINDOWS_DISABLED TEST hash_test WINDOWS_DISABLED
SOURCES HashTest.cpp SOURCES HashTest.cpp
TEST spooky_hash_v1_test SOURCES SpookyHashV1Test.cpp TEST spooky_hash_v1_test SOURCES SpookyHashV1Test.cpp
......
This diff is collapsed.
...@@ -40,8 +40,9 @@ ...@@ -40,8 +40,9 @@
// of a+b is easily derived from the hashes of a and b. This property // of a+b is easily derived from the hashes of a and b. This property
// doesn't hold for any hash functions in this file. // doesn't hold for any hash functions in this file.
#ifndef FARM_HASH_H_ // clang-format off
#define FARM_HASH_H_
#pragma once
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
...@@ -49,13 +50,15 @@ ...@@ -49,13 +50,15 @@
#include <string.h> // for memcpy and memset #include <string.h> // for memcpy and memset
#include <utility> #include <utility>
#ifndef NAMESPACE_FOR_HASH_FUNCTIONS #include <folly/portability/Config.h>
#define NAMESPACE_FOR_HASH_FUNCTIONS util
#endif namespace folly {
namespace external {
namespace farmhash {
namespace NAMESPACE_FOR_HASH_FUNCTIONS { #if FOLLY_HAVE_INT128_T
using uint128_t = unsigned __int128;
#if defined(FARMHASH_UINT128_T_DEFINED)
inline uint64_t Uint128Low64(const uint128_t x) { inline uint64_t Uint128Low64(const uint128_t x) {
return static_cast<uint64_t>(x); return static_cast<uint64_t>(x);
} }
...@@ -175,8 +178,6 @@ inline uint64_t Fingerprint(uint64_t x) { ...@@ -175,8 +178,6 @@ inline uint64_t Fingerprint(uint64_t x) {
return b; return b;
} }
#ifndef FARMHASH_NO_CXX_STRING
// Convenience functions to hash or fingerprint C++ strings. // Convenience functions to hash or fingerprint C++ strings.
// These require that Str::data() return a pointer to the first char // These require that Str::data() return a pointer to the first char
// (as a const char*) and that Str::length() return the string's length; // (as a const char*) and that Str::length() return the string's length;
...@@ -283,8 +284,77 @@ inline uint128_t Fingerprint128(const Str& s) { ...@@ -283,8 +284,77 @@ inline uint128_t Fingerprint128(const Str& s) {
return Fingerprint128(s.data(), s.length()); return Fingerprint128(s.data(), s.length());
} }
#endif //// internal variants
} // namespace NAMESPACE_FOR_HASH_FUNCTIONS namespace test {
extern bool returnZeroIfMisconfigured;
}
namespace farmhashna {
uint64_t Hash64(const char* s, size_t len);
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
uint64_t
Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1);
} // namespace farmhashna
namespace farmhashuo {
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
uint64_t Hash64(const char* s, size_t len);
} // namespace farmhashuo
namespace farmhashxo {
uint64_t Hash64(const char* s, size_t len);
uint64_t
Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1);
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
} // namespace farmhashxo
namespace farmhashte {
uint64_t Hash64(const char* s, size_t len);
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
uint64_t Hash64(const char* s, size_t len);
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
uint64_t
Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1);
} // namespace farmhashte
namespace farmhashnt {
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
} // namespace farmhashnt
namespace farmhashmk {
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
} // namespace farmhashmk
namespace farmhashsu {
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
} // namespace farmhashsu
namespace farmhashsa {
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
} // namespace farmhashsa
namespace farmhashcc {
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
uint128_t CityHash128WithSeed(const char* s, size_t len, uint128_t seed);
uint128_t Fingerprint128(const char* s, size_t len);
uint32_t Hash32(const char* s, size_t len);
uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
uint64_t Hash64(const char* s, size_t len);
size_t Hash(const char* s, size_t len);
uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
uint64_t
Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1);
uint128_t Hash128(const char* s, size_t len);
uint128_t Hash128WithSeed(const char* s, size_t len, uint128_t seed);
uint32_t Fingerprint32(const char* s, size_t len);
uint64_t Fingerprint64(const char* s, size_t len);
uint128_t Fingerprint128(const char* s, size_t len);
} // namespace farmhashcc
#endif // FARM_HASH_H_ } // namespace farmhash
} // namespace external
} // namespace folly
/*
* Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/external/farmhash/farmhash.h>
namespace folly {
namespace hash {
namespace farmhash {
// The values returned by Hash, Hash32, and Hash64 are only guaranteed to
// be the same within the same process. Fingerpring32 and Fingerprint64
// are fixed algorithms that always give the same result.
// std::size_t Hash(char const*, std::size_t)
using external::farmhash::Hash;
// uint32_t Hash32(char const*, std::size_t)
using external::farmhash::Hash32;
// uint64_t Hash64(char const*, std::size_t)
using external::farmhash::Hash64;
// uint32_t Fingerprint32(char const*, std::size_t)
using external::farmhash::Fingerprint32;
// uint64_t Fingerprint64(char const*, std::size_t)
using external::farmhash::Fingerprint64;
} // namespace farmhash
} // namespace hash
} // namespace folly
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/hash/FarmHash.h>
#include <folly/portability/GTest.h>
TEST(farmhash, simple) {
EXPECT_NE(
folly::hash::farmhash::Hash("foo", 3),
folly::hash::farmhash::Hash("bar", 3));
EXPECT_NE(
folly::hash::farmhash::Hash32("foo", 3),
folly::hash::farmhash::Hash32("bar", 3));
EXPECT_NE(
folly::hash::farmhash::Hash64("foo", 3),
folly::hash::farmhash::Hash64("bar", 3));
EXPECT_NE(
folly::hash::farmhash::Fingerprint32("foo", 3),
folly::hash::farmhash::Fingerprint32("bar", 3));
EXPECT_NE(
folly::hash::farmhash::Fingerprint64("foo", 3),
folly::hash::farmhash::Fingerprint64("bar", 3));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment