diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2018-11-30 11:14:16 -0500 |
---|---|---|
committer | Henrik Edin <henrik.edin@mongodb.com> | 2018-12-20 09:18:05 -0500 |
commit | 6b641e86656df78e9f7ad2f364dec35e19df332e (patch) | |
tree | db8f5ae4deb29e82d63b6f653eb7632d2b66b22b /src/mongo/stdx | |
parent | fc1aa5592b8783dc802668659ec87826d789dd2f (diff) | |
download | mongo-6b641e86656df78e9f7ad2f364dec35e19df332e.tar.gz |
SERVER-38249 Implement stdx unordered_map and unordered_set as absl node hash map/set.
Remove stdx::unordered_multimap and multiset.
Custom hashers to stdx::unordered_map are not trusted by default, we
will rehash the produced hash with absl again to ensure we have a good
hash function.
Diffstat (limited to 'src/mongo/stdx')
-rw-r--r-- | src/mongo/stdx/trusted_hasher.h | 66 | ||||
-rw-r--r-- | src/mongo/stdx/unordered_map.h | 17 | ||||
-rw-r--r-- | src/mongo/stdx/unordered_set.h | 17 |
3 files changed, 76 insertions, 24 deletions
diff --git a/src/mongo/stdx/trusted_hasher.h b/src/mongo/stdx/trusted_hasher.h new file mode 100644 index 00000000000..8e3df6f07e0 --- /dev/null +++ b/src/mongo/stdx/trusted_hasher.h @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2018-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +// This file depends on internal implementation details in abseil. In a library upgrade you may have +// to re-write this file significantly. +#include <absl/container/internal/hash_function_defaults.h> + +/** + * To be safe we let abseil hash the produced hash one more time to protect ourselves against bad + * hash functions. If you know your hash function is good and can be trusted you get mark it as + * trusted by specializing a template like this: + * + * namespace mongo { + * template <> + * struct IsTrustedHasher<YourHash, YourKey> : std::true_type {}; + * } + */ + +namespace mongo { +template <typename Key> +using DefaultHasher = absl::container_internal::hash_default_hash<Key>; + +template <typename Hasher, typename Key> +struct IsTrustedHasher : std::is_same<Hasher, DefaultHasher<Key>> {}; + +template <typename Hasher, typename Key> +struct HashImprover : private Hasher { + HashImprover(const Hasher& hasher = Hasher()) : Hasher(hasher) {} + std::size_t operator()(const Key& k) const { + return absl::Hash<std::size_t>{}(Hasher::operator()(k)); + } +}; + +template <typename Hasher, typename Key> +using EnsureTrustedHasher = + std::conditional_t<IsTrustedHasher<Hasher, Key>::value, Hasher, HashImprover<Hasher, Key>>; + +} // namespace mongo diff --git a/src/mongo/stdx/unordered_map.h b/src/mongo/stdx/unordered_map.h index 0a207741b18..6f4e7a8728a 100644 --- a/src/mongo/stdx/unordered_map.h +++ b/src/mongo/stdx/unordered_map.h @@ -30,22 +30,15 @@ #pragma once -#if defined(_WIN32) -#include <boost/unordered_map.hpp> -#else -#include <unordered_map> -#endif +#include "mongo/stdx/trusted_hasher.h" + +#include <absl/container/node_hash_map.h> namespace mongo { namespace stdx { -#if defined(_WIN32) -using ::boost::unordered_map; // NOLINT -using ::boost::unordered_multimap; // NOLINT -#else -using ::std::unordered_map; // NOLINT -using ::std::unordered_multimap; // NOLINT -#endif +template <class Key, class Value, class Hasher = DefaultHasher<Key>, typename... Args> +using unordered_map = absl::node_hash_map<Key, Value, EnsureTrustedHasher<Hasher, Key>, Args...>; } // namespace stdx } // namespace mongo diff --git a/src/mongo/stdx/unordered_set.h b/src/mongo/stdx/unordered_set.h index dc24c3e7235..d88450cce75 100644 --- a/src/mongo/stdx/unordered_set.h +++ b/src/mongo/stdx/unordered_set.h @@ -30,22 +30,15 @@ #pragma once -#if defined(_WIN32) -#include <boost/unordered_set.hpp> -#else -#include <unordered_set> -#endif +#include "mongo/stdx/trusted_hasher.h" + +#include <absl/container/node_hash_set.h> namespace mongo { namespace stdx { -#if defined(_WIN32) -using ::boost::unordered_set; // NOLINT -using ::boost::unordered_multiset; // NOLINT -#else -using ::std::unordered_set; // NOLINT -using ::std::unordered_multiset; // NOLINT -#endif +template <class Key, class Hasher = DefaultHasher<Key>, typename... Args> +using unordered_set = absl::node_hash_set<Key, EnsureTrustedHasher<Hasher, Key>, Args...>; } // namespace stdx } // namespace mongo |