Skip to content

Commit

Permalink
Update hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
acvictor committed Apr 29, 2024
1 parent 59c21b6 commit f2d65d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
22 changes: 9 additions & 13 deletions velox/connectors/hive/FileHandleKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,19 @@ class FileHandleKey {
};
} // namespace facebook::velox

template <class T>
inline void hash_combine(std::size_t& seed, const T& v) {
std::hash<T> hasher;
// See boost::hash_combine
// https://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

namespace std {
template <>
struct hash<facebook::velox::FileHandleKey> {
size_t operator()(const facebook::velox::FileHandleKey& key) const {
size_t seed = 0;
hash_combine(seed, key.getFilePath());
hash_combine(seed, key.getFileSize());
hash_combine(seed, key.getModificationTime());
return seed;
auto hasher = folly::Hash{};
uint64_t combinedHash = 0;
combinedHash = folly::hash::commutative_hash_combine_value_generic(
combinedHash, hasher, key.getFileSize());
combinedHash = folly::hash::commutative_hash_combine_value_generic(
combinedHash, hasher, key.getModificationTime());
combinedHash = folly::hash::commutative_hash_combine_value_generic(
combinedHash, hasher, key.getFilePath());
return combinedHash;
}
};
} // namespace std
20 changes: 8 additions & 12 deletions velox/connectors/hive/tests/FileHandleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ TEST(FileHandleTest, retrieveCachedLocalFile) {

auto tempFile = exec::test::TempFilePath::create();
const auto& filename = tempFile->getPath();
remove(filename.c_str());

{
LocalWriteFile writeFile(filename);
writeFile.append("foo");
}

FileHandleFactory factory(
std::make_unique<
Expand All @@ -104,14 +98,16 @@ TEST(FileHandleTest, retrieveCachedLocalFile) {

factory.retrieveCached(
{FileHandleKey(
tempFile->fileSize(), tempFile->fileModifiedTime(), filename),
FileHandleKey(
tempFile->fileSize() + 1,
tempFile->fileModifiedTime() + 1,
filename)},
tempFile->fileSize(), tempFile->fileModifiedTime(), filename)},
&cached,
&missing);

ASSERT_EQ(1, cached.size());
ASSERT_EQ(0, missing.size());

factory.retrieveCached(
{FileHandleKey(
tempFile->fileSize() + 1, tempFile->fileModifiedTime(), filename)},
&cached,
&missing);
ASSERT_EQ(1, missing.size());
}

0 comments on commit f2d65d7

Please sign in to comment.