From f9a5d666e40d462cfd13fb31e590b0aa25db3133 Mon Sep 17 00:00:00 2001 From: Spencer T Brody Date: Wed, 16 Jul 2014 17:42:56 -0400 Subject: SERVER-14587 Add hash function to OID so it can be stored in an unordered_map --- src/mongo/bson/oid.cpp | 6 ++++++ src/mongo/bson/oid.h | 10 ++++++++++ src/mongo/db/repl/repl_coordinator_impl.h | 5 ++--- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mongo/bson/oid.cpp b/src/mongo/bson/oid.cpp index 92664fd753c..b073c8efa49 100644 --- a/src/mongo/bson/oid.cpp +++ b/src/mongo/bson/oid.cpp @@ -50,6 +50,12 @@ namespace mongo { boost::hash_combine(seed, z); } + size_t OID::Hasher::operator() (const OID& oid) const { + size_t seed = 0; + oid.hash_combine(seed); + return seed; + } + // machine # before folding in the process id OID::MachineAndPid OID::ourMachine; diff --git a/src/mongo/bson/oid.h b/src/mongo/bson/oid.h index b98e070eeac..d5063dc5f1b 100644 --- a/src/mongo/bson/oid.h +++ b/src/mongo/bson/oid.h @@ -53,6 +53,16 @@ namespace mongo { */ class OID { public: + + /** + * Functor compatible with std::hash for std::unordered_{map,set} + * Warning: The hash function is subject to change. Do not use in cases where hashes need + * to be consistent across versions. + */ + struct Hasher { + size_t operator() (const OID& oid) const; + }; + OID() : a(0), b(0) { } enum { diff --git a/src/mongo/db/repl/repl_coordinator_impl.h b/src/mongo/db/repl/repl_coordinator_impl.h index 62e043f9d0c..97260d0fae7 100644 --- a/src/mongo/db/repl/repl_coordinator_impl.h +++ b/src/mongo/db/repl/repl_coordinator_impl.h @@ -31,7 +31,6 @@ #include #include #include -#include #include #include "mongo/base/status.h" @@ -40,6 +39,7 @@ #include "mongo/db/repl/repl_coordinator.h" #include "mongo/db/repl/replication_executor.h" #include "mongo/db/repl/topology_coordinator_impl.h" +#include "mongo/platform/unordered_map.h" #include "mongo/util/net/hostandport.h" namespace mongo { @@ -189,8 +189,7 @@ namespace repl { boost::scoped_ptr _topCoordDriverThread; // Maps nodes in this replication group to the last oplog operation they have committed - // TODO(spencer): change to unordered_map - typedef std::map SlaveOpTimeMap; + typedef unordered_map SlaveOpTimeMap; SlaveOpTimeMap _slaveOpTimeMap; // Current ReplicaSet state -- cgit v1.2.1