diff options
author | Greg Studer <greg@10gen.com> | 2012-11-30 16:32:43 -0500 |
---|---|---|
committer | Greg Studer <greg@10gen.com> | 2012-12-03 17:01:20 -0500 |
commit | ceddaa1ac65b1c06e62a217f4a78086e0a354f23 (patch) | |
tree | 6830b27095d2aabf5050e5c1e195c2faadee607b | |
parent | 64408b78c89a65a4a727b57d552b64bf5f7eb803 (diff) | |
download | mongo-ceddaa1ac65b1c06e62a217f4a78086e0a354f23.tar.gz |
SERVER-939 enhance owned pointer vector and add owned pointer map
-rw-r--r-- | src/mongo/base/owned_pointer_map.h | 64 | ||||
-rw-r--r-- | src/mongo/base/owned_pointer_vector.h | 11 | ||||
-rw-r--r-- | src/mongo/db/repl/rs.cpp | 16 |
3 files changed, 82 insertions, 9 deletions
diff --git a/src/mongo/base/owned_pointer_map.h b/src/mongo/base/owned_pointer_map.h new file mode 100644 index 00000000000..4f2f3d5dd45 --- /dev/null +++ b/src/mongo/base/owned_pointer_map.h @@ -0,0 +1,64 @@ +/* Copyright 2012 10gen 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 <map> + +#include "mongo/base/disallow_copying.h" + +namespace mongo { + + /** + * An std::map wrapper that deletes pointers within a vector on destruction. The objects + * referenced by the vector's pointers are 'owned' by an object of this class. + * NOTE that an OwnedPointerMap<K,T> wraps an std::map<K,T*>. + */ + template<class K, class T> + class OwnedPointerMap { + MONGO_DISALLOW_COPYING(OwnedPointerMap); + + public: + OwnedPointerMap(); + ~OwnedPointerMap(); + + /** Access the map. */ + const std::map<K, T*>& map() { return _map; } + std::map<K, T*>& mutableMap() { return _map; } + + void clear(); + + private: + std::map<K, T*> _map; + }; + + template<class K, class T> + OwnedPointerMap<K, T>::OwnedPointerMap() { + } + + template<class K, class T> + OwnedPointerMap<K, T>::~OwnedPointerMap() { + clear(); + } + + template<class K, class T> + void OwnedPointerMap<K, T>::clear() { + for( typename std::map<K, T*>::iterator i = _map.begin(); i != _map.end(); ++i ) { + delete i->second; + } + _map.clear(); + } + +} // namespace mongo diff --git a/src/mongo/base/owned_pointer_vector.h b/src/mongo/base/owned_pointer_vector.h index 75bae2af7cd..4c3f83ae9d5 100644 --- a/src/mongo/base/owned_pointer_vector.h +++ b/src/mongo/base/owned_pointer_vector.h @@ -35,7 +35,10 @@ namespace mongo { ~OwnedPointerVector(); /** Access the vector. */ - std::vector<T*>& vector() { return _vector; } + const std::vector<T*>& vector() { return _vector; } + std::vector<T*>& mutableVector() { return _vector; } + + void clear(); private: std::vector<T*> _vector; @@ -47,9 +50,15 @@ namespace mongo { template<class T> OwnedPointerVector<T>::~OwnedPointerVector() { + clear(); + } + + template<class T> + void OwnedPointerVector<T>::clear() { for( typename std::vector<T*>::iterator i = _vector.begin(); i != _vector.end(); ++i ) { delete *i; } + _vector.clear(); } } // namespace mongo diff --git a/src/mongo/db/repl/rs.cpp b/src/mongo/db/repl/rs.cpp index a90581928d3..18103bf526a 100644 --- a/src/mongo/db/repl/rs.cpp +++ b/src/mongo/db/repl/rs.cpp @@ -684,14 +684,14 @@ namespace mongo { try { OwnedPointerVector<ReplSetConfig> configs; try { - configs.vector().push_back(ReplSetConfig::makeDirect()); + configs.mutableVector().push_back(ReplSetConfig::makeDirect()); } catch(DBException& e) { log() << "replSet exception loading our local replset configuration object : " << e.toString() << rsLog; } for( vector<HostAndPort>::const_iterator i = _seeds->begin(); i != _seeds->end(); i++ ) { try { - configs.vector().push_back( ReplSetConfig::make(*i) ); + configs.mutableVector().push_back( ReplSetConfig::make(*i) ); } catch( DBException& e ) { log() << "replSet exception trying to load config from " << *i << " : " << e.toString() << rsLog; @@ -704,7 +704,7 @@ namespace mongo { i != replSettings.discoveredSeeds.end(); i++) { try { - configs.vector().push_back( ReplSetConfig::make(HostAndPort(*i)) ); + configs.mutableVector().push_back( ReplSetConfig::make(HostAndPort(*i)) ); } catch( DBException& ) { LOG(1) << "replSet exception trying to load config from discovered seed " << *i << rsLog; @@ -716,7 +716,7 @@ namespace mongo { if (!replSettings.reconfig.isEmpty()) { try { - configs.vector().push_back(ReplSetConfig::make(replSettings.reconfig, + configs.mutableVector().push_back(ReplSetConfig::make(replSettings.reconfig, true)); } catch( DBException& re) { @@ -727,8 +727,8 @@ namespace mongo { int nok = 0; int nempty = 0; - for( vector<ReplSetConfig*>::iterator i = configs.vector().begin(); - i != configs.vector().end(); i++ ) { + for( vector<ReplSetConfig*>::iterator i = configs.mutableVector().begin(); + i != configs.mutableVector().end(); i++ ) { if( (*i)->ok() ) nok++; if( (*i)->empty() ) @@ -736,7 +736,7 @@ namespace mongo { } if( nok == 0 ) { - if( nempty == (int) configs.vector().size() ) { + if( nempty == (int) configs.mutableVector().size() ) { startupStatus = EMPTYCONFIG; startupStatusMsg.set("can't get " + rsConfigNs + " config from self or any seed (EMPTYCONFIG)"); log() << "replSet can't get " << rsConfigNs << " config from self or any seed (EMPTYCONFIG)" << rsLog; @@ -758,7 +758,7 @@ namespace mongo { continue; } - if( !_loadConfigFinish(configs.vector()) ) { + if( !_loadConfigFinish(configs.mutableVector()) ) { log() << "replSet info Couldn't load config yet. Sleeping 20sec and will try again." << rsLog; sleepsecs(20); continue; |