diff options
author | Faustoleyva54 <fausto.leyva@mongodb.com> | 2021-12-01 22:28:09 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-01 22:58:45 +0000 |
commit | 5574e83d9e5df11dd3ca9cf0151ada223edd3ec2 (patch) | |
tree | ead2f8d6e64b133ee83aa8eb97b7c0615073e69b /src | |
parent | f01e9aaa0d99f47bc6d96839e4ea3cc1108e715c (diff) | |
download | mongo-5574e83d9e5df11dd3ca9cf0151ada223edd3ec2.tar.gz |
SERVER-29262 Retire usage of `OwnedPointerVector` in `write_ops`.
Diffstat (limited to 'src')
29 files changed, 203 insertions, 834 deletions
diff --git a/src/mongo/base/SConscript b/src/mongo/base/SConscript index d6f19214df9..1eb4c56bf83 100644 --- a/src/mongo/base/SConscript +++ b/src/mongo/base/SConscript @@ -88,7 +88,6 @@ env.CppUnitTest( 'initializer_test.cpp', 'murmurhash3_test.cpp', 'owned_pointer_map_test.cpp', - 'owned_pointer_vector_test.cpp', 'parse_number_test.cpp', 'secure_allocator_test.cpp', 'status_test.cpp', diff --git a/src/mongo/base/owned_pointer_vector.h b/src/mongo/base/owned_pointer_vector.h deleted file mode 100644 index 929606ec520..00000000000 --- a/src/mongo/base/owned_pointer_vector.h +++ /dev/null @@ -1,192 +0,0 @@ -/** - * 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 - -#include <cstring> -#include <vector> - - -namespace mongo { - -/** - * An std::vector 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 OwnedPointerVector<T> wraps an std::vector<T*>. - */ -template <class T> -class OwnedPointerVector { - OwnedPointerVector(const OwnedPointerVector&) = delete; - OwnedPointerVector& operator=(const OwnedPointerVector&) = delete; - -public: - OwnedPointerVector() {} - ~OwnedPointerVector() { - clear(); - } - - /** - * Takes ownership of all pointers contained in 'other'. - * NOTE: argument is intentionally taken by value. - */ - OwnedPointerVector(std::vector<T*> other) { - _vector.swap(other); - } - - /** - * Takes ownership of all pointers contained in 'other'. - * NOTE: argument is intentionally taken by value. - */ - OwnedPointerVector& operator=(std::vector<T*> other) { - clear(); - _vector.swap(other); - return *this; - } - - typedef typename std::vector<T*>::const_iterator const_iterator; - typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator; - - /** Access the vector. */ - const std::vector<T*>& vector() const { - return _vector; - } - std::vector<T*>& mutableVector() { - return _vector; - } - - std::size_t size() const { - return _vector.size(); - } - bool empty() const { - return _vector.empty(); - } - - const_iterator begin() const { - return _vector.begin(); - } - const_reverse_iterator rbegin() const { - return _vector.rbegin(); - } - const_iterator end() const { - return _vector.end(); - } - const_reverse_iterator rend() const { - return _vector.rend(); - } - - T* operator[](size_t i) const { - return _vector[i]; - } - T* back() const { - return _vector.back(); - } - T* front() const { - return _vector.front(); - } - - void push_back(T* ptr) { - _vector.push_back(ptr); - } - - /** - * Deletes all pointers in the vector, then sets its size to 0. - */ - void clear(); - - /** - * Deletes the pointer at 'it', then erases it from the vector. - */ - void erase(const_iterator it) { - delete *it; - _vector.erase(toNonConstIter(it)); - } - - void erase(const_iterator begin, const_iterator end) { - for (const_iterator it = begin; it != end; ++it) { - delete *it; - } - _vector.erase(toNonConstIter(begin), toNonConstIter(end)); - } - - // - // extensions - // - - /** - * Releases the entire vector to allow you to transfer ownership. - * - * Leaves the OwnedPointerVector empty. - * Named after the similar method and pattern in std::unique_ptr. - */ - std::vector<T*> release() { - std::vector<T*> out; - out.swap(_vector); - return out; - } - - /** - * Releases ownership of a single element. - * - * Sets that element to NULL and does not change size(). - */ - T* releaseAt(size_t i) { - T* out = _vector[i]; - _vector[i] = nullptr; - return out; - } - - T* popAndReleaseBack() { - T* out = _vector.back(); - _vector.pop_back(); - return out; - } - - void popAndDeleteBack() { - delete popAndReleaseBack(); - } - -private: - typename std::vector<T*>::iterator toNonConstIter(const_iterator it) { - // This is needed for a few cases where c++03 vectors require non-const iterators that - // were relaxed in c++11 to allow const_iterators. It can go away when we require c++11. - return _vector.begin() + (it - begin()); - } - - std::vector<T*> _vector; -}; - -template <class T> -inline 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/base/owned_pointer_vector_test.cpp b/src/mongo/base/owned_pointer_vector_test.cpp deleted file mode 100644 index c11bda6fb6f..00000000000 --- a/src/mongo/base/owned_pointer_vector_test.cpp +++ /dev/null @@ -1,362 +0,0 @@ -/** - * 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. - */ - -/** Unit tests for OwnedPointerVector. */ - -#include "mongo/base/owned_pointer_vector.h" - -#include <string> -#include <vector> - -#include "mongo/unittest/unittest.h" - -namespace mongo { - -using std::string; - -namespace { - -/** Helper class that logs its constructor argument to a static vector on destruction. */ -class DestructionLogger { -public: - DestructionLogger(const string& name) : _name(name) {} - ~DestructionLogger() { - _destroyedNames.push_back(_name); - } - - static std::vector<string>& destroyedNames() { - return _destroyedNames; - } - -private: - string _name; - static std::vector<string> _destroyedNames; -}; - -std::vector<string> DestructionLogger::_destroyedNames; - -TEST(OwnedPointerVectorTest, OwnedPointerDestroyed) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> owned; - owned.mutableVector().push_back(new DestructionLogger("foo")); - // owned destroyed - } - ASSERT_EQUALS(1U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("foo", DestructionLogger::destroyedNames()[0]); -} - -TEST(OwnedPointerVectorTest, OwnedConstPointerDestroyed) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<const DestructionLogger> owned; - owned.push_back(new DestructionLogger("foo")); - // owned destroyed - } - ASSERT_EQUALS(1U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("foo", DestructionLogger::destroyedNames()[0]); -} - -TEST(OwnedPointerVectorTest, OwnedPointersDestroyedInOrder) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("first")); - owned.push_back(new DestructionLogger("second")); - // owned destroyed - } - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); -} - -TEST(OwnedPointerVectorTest, ClearDestroyedInOrder) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("first")); - owned.push_back(new DestructionLogger("second")); - - owned.clear(); - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - ASSERT_EQUALS(0U, owned.size()); - // owned destroyed - } - // no additional deletion should have occured when owned was destroyed - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); -} - -TEST(OwnedPointerVectorTest, EraseDestroysAsCalled) { - DestructionLogger::destroyedNames().clear(); - { - // names are order of erasure - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("third")); - owned.push_back(new DestructionLogger("first")); - owned.push_back(new DestructionLogger("second")); - owned.push_back(new DestructionLogger("fourth")); - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - - // erase "first", sliding "second" down to index 1 - owned.erase(owned.begin() + 1); - ASSERT_EQUALS(1U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames().back()); - ASSERT_EQUALS(3U, owned.size()); - - // erase "second" - owned.erase(owned.begin() + 1); - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames().back()); - ASSERT_EQUALS(2U, owned.size()); - - // erase "third" - owned.erase(owned.begin() + 0); - ASSERT_EQUALS(3U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("third", DestructionLogger::destroyedNames().back()); - ASSERT_EQUALS(1U, owned.size()); - - // owned destroyed - } - - // only "four" should have been deleted when owned was destroyed - ASSERT_EQUALS(4U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("fourth", DestructionLogger::destroyedNames().back()); -} - -TEST(OwnedPointerVectorTest, Accessors) { - OwnedPointerVector<int> owned; - ASSERT_TRUE(owned.empty()); - ASSERT_EQUALS(0U, owned.size()); - - owned.push_back(new int(0)); - owned.push_back(new int(1)); - owned.push_back(new int(2)); - - ASSERT_FALSE(owned.empty()); - ASSERT_EQUALS(3U, owned.size()); - - ASSERT_EQUALS(0, *owned[0]); - ASSERT_EQUALS(1, *owned[1]); - ASSERT_EQUALS(2, *owned[2]); - - ASSERT_EQUALS(0, *owned.front()); - ASSERT_EQUALS(2, *owned.back()); -} - -TEST(OwnedPointerVectorTest, TransferConstructor) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> source; - source.push_back(new DestructionLogger("first")); - source.push_back(new DestructionLogger("second")); - - { - OwnedPointerVector<DestructionLogger> dest(source.release()); - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(0U, source.size()); - ASSERT_EQUALS(2U, dest.size()); - // dest destroyed - } - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - - // source destroyed - } - // no additional deletions - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); -} - -TEST(OwnedPointerVectorTest, TransferConstructorDoesntModifyArgument) { - DestructionLogger::destroyedNames().clear(); - { - std::vector<DestructionLogger*> source; - source.push_back(new DestructionLogger("first")); - source.push_back(new DestructionLogger("second")); - - { - OwnedPointerVector<DestructionLogger> dest(source); - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(2U, source.size()); - ASSERT_EQUALS(2U, dest.size()); - ASSERT(source == dest.vector()); // can't use ASSERT_EQUALS - // dest destroyed - } - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - - ASSERT_EQUALS(2U, source.size()); - // source destroyed - } - // no additional deletions - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); -} - -TEST(OwnedPointerVectorTest, TransferAssignment) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> dest; - { - OwnedPointerVector<DestructionLogger> source; - source.push_back(new DestructionLogger("first")); - source.push_back(new DestructionLogger("second")); - - dest = source.release(); - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(0U, source.size()); - ASSERT_EQUALS(2U, dest.size()); - // source destroyed - } - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(2U, dest.size()); - // dest destroyed - } - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); -} - -TEST(OwnedPointerVectorTest, TransferAssignmentDoesntModifyArgument) { - DestructionLogger::destroyedNames().clear(); - { - OwnedPointerVector<DestructionLogger> dest; - { - std::vector<DestructionLogger*> source; - source.push_back(new DestructionLogger("first")); - source.push_back(new DestructionLogger("second")); - - dest = source; - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(2U, source.size()); - ASSERT_EQUALS(2U, dest.size()); - ASSERT(source == dest.vector()); // can't use ASSERT_EQUALS - // source destroyed - } - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(2U, dest.size()); - // dest destroyed - } - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); -} - -TEST(OwnedPointerVectorTest, ReleaseAt) { - DestructionLogger::destroyedNames().clear(); - - std::unique_ptr<DestructionLogger> holder; - { - // names are order of deletion - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("first")); - owned.push_back(new DestructionLogger("third")); - owned.push_back(new DestructionLogger("second")); - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - - // transfer ownership of "third" to holder - holder.reset(owned.releaseAt(1)); - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(3U, owned.size()); - ASSERT_EQUALS(static_cast<DestructionLogger*>(nullptr), owned[1]); - - // owned destroyed - } - // owned deleted "first" and "second", but not "third" - ASSERT_EQUALS(2U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames()[0]); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - - // delete "third" - holder.reset(); - ASSERT_EQUALS(3U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("third", DestructionLogger::destroyedNames().back()); -} - -TEST(OwnedPointerVectorTest, PopAndReleaseBack) { - DestructionLogger::destroyedNames().clear(); - - { - // names are order of deletion - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("second")); - owned.push_back(new DestructionLogger("third")); - owned.push_back(new DestructionLogger("first")); - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - - { - // transfer ownership of "third" to holder - std::unique_ptr<DestructionLogger> holder(owned.popAndReleaseBack()); - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS(2U, owned.size()); - // holder destroyed - } - ASSERT_EQUALS(1U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames().back()); - // owned destroyed - } - // owned destructor deleted "second" and "third", but not "first" - ASSERT_EQUALS(3U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - ASSERT_EQUALS("third", DestructionLogger::destroyedNames()[2]); -} - -TEST(OwnedPointerVectorTest, PopAndDeleteBack) { - DestructionLogger::destroyedNames().clear(); - - { - // names are order of deletion - OwnedPointerVector<DestructionLogger> owned; - owned.push_back(new DestructionLogger("second")); - owned.push_back(new DestructionLogger("third")); - owned.push_back(new DestructionLogger("first")); - - ASSERT_EQUALS(0U, DestructionLogger::destroyedNames().size()); - - owned.popAndDeleteBack(); - ASSERT_EQUALS(2U, owned.size()); - ASSERT_EQUALS(1U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("first", DestructionLogger::destroyedNames().back()); - // owned destroyed - } - // owned destructor deleted "second" and "third", but not "first" - ASSERT_EQUALS(3U, DestructionLogger::destroyedNames().size()); - ASSERT_EQUALS("second", DestructionLogger::destroyedNames()[1]); - ASSERT_EQUALS("third", DestructionLogger::destroyedNames()[2]); -} - -} // namespace -} // namespace mongo diff --git a/src/mongo/db/catalog/index_catalog_entry.h b/src/mongo/db/catalog/index_catalog_entry.h index 6e733338308..c5eac391739 100644 --- a/src/mongo/db/catalog/index_catalog_entry.h +++ b/src/mongo/db/catalog/index_catalog_entry.h @@ -33,7 +33,6 @@ #include <functional> #include <string> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/bson/ordering.h" #include "mongo/bson/timestamp.h" #include "mongo/db/index/multikey_paths.h" diff --git a/src/mongo/db/catalog/index_catalog_entry_impl.h b/src/mongo/db/catalog/index_catalog_entry_impl.h index 44607746903..60093abfeeb 100644 --- a/src/mongo/db/catalog/index_catalog_entry_impl.h +++ b/src/mongo/db/catalog/index_catalog_entry_impl.h @@ -32,7 +32,6 @@ #include <boost/optional.hpp> #include <string> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/bson/ordering.h" #include "mongo/bson/timestamp.h" #include "mongo/db/catalog/index_catalog_entry.h" diff --git a/src/mongo/db/exec/geo_near.cpp b/src/mongo/db/exec/geo_near.cpp index fdb731ff033..d83f63a2677 100644 --- a/src/mongo/db/exec/geo_near.cpp +++ b/src/mongo/db/exec/geo_near.cpp @@ -38,7 +38,6 @@ // For s2 search #include "third_party/s2/s2regionintersection.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/bson/dotted_path_support.h" #include "mongo/db/exec/document_value/value.h" #include "mongo/db/exec/fetch.h" diff --git a/src/mongo/db/exec/multi_plan.cpp b/src/mongo/db/exec/multi_plan.cpp index d1f7dfc9e64..7dfb6ffc5d7 100644 --- a/src/mongo/db/exec/multi_plan.cpp +++ b/src/mongo/db/exec/multi_plan.cpp @@ -37,8 +37,6 @@ #include <math.h> #include <memory> -#include "mongo/base/owned_pointer_vector.h" -#include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" #include "mongo/db/client.h" #include "mongo/db/concurrency/write_conflict_exception.h" diff --git a/src/mongo/db/exec/subplan.h b/src/mongo/db/exec/subplan.h index 34483a4408b..f1065c32e8d 100644 --- a/src/mongo/db/exec/subplan.h +++ b/src/mongo/db/exec/subplan.h @@ -33,7 +33,6 @@ #include <string> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/db/exec/requires_all_indices_stage.h" diff --git a/src/mongo/db/field_ref_set.h b/src/mongo/db/field_ref_set.h index 0019bf9b4ba..1100d0dbec9 100644 --- a/src/mongo/db/field_ref_set.h +++ b/src/mongo/db/field_ref_set.h @@ -32,7 +32,6 @@ #include <set> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/status.h" #include "mongo/db/field_ref.h" diff --git a/src/mongo/db/geo/big_polygon.cpp b/src/mongo/db/geo/big_polygon.cpp index e68191c7ff5..c4cffc35cea 100644 --- a/src/mongo/db/geo/big_polygon.cpp +++ b/src/mongo/db/geo/big_polygon.cpp @@ -32,7 +32,6 @@ #include <map> #include <memory> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/util/assert_util.h" #include "mongo/util/transitional_tools_do_not_use/vector_spooling.h" diff --git a/src/mongo/db/geo/geometry_container.cpp b/src/mongo/db/geo/geometry_container.cpp index 03a97ef86b0..2bf9dbba5c7 100644 --- a/src/mongo/db/geo/geometry_container.cpp +++ b/src/mongo/db/geo/geometry_container.cpp @@ -55,8 +55,7 @@ PointWithCRS GeometryContainer::getPoint() const { bool GeometryContainer::supportsContains() const { return nullptr != _polygon || nullptr != _box || nullptr != _cap || nullptr != _multiPolygon || (nullptr != _geometryCollection && - (_geometryCollection->polygons.vector().size() > 0 || - _geometryCollection->multiPolygons.vector().size() > 0)); + (!_geometryCollection->polygons.empty() || !_geometryCollection->multiPolygons.empty())); } bool GeometryContainer::hasS2Region() const { @@ -293,9 +292,8 @@ bool GeometryContainer::contains(const GeometryContainer& otherContainer) const } if (nullptr != otherContainer._multiLine) { - const vector<S2Polyline*>& lines = otherContainer._multiLine->lines.vector(); - for (size_t i = 0; i < lines.size(); ++i) { - if (!contains(*lines[i])) { + for (const auto& line : otherContainer._multiLine->lines) { + if (!contains(*line)) { return false; } } @@ -303,9 +301,8 @@ bool GeometryContainer::contains(const GeometryContainer& otherContainer) const } if (nullptr != otherContainer._multiPolygon) { - const vector<S2Polygon*>& polys = otherContainer._multiPolygon->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (!contains(*polys[i])) { + for (const auto& polygon : otherContainer._multiPolygon->polygons) { + if (!contains(*polygon)) { return false; } } @@ -321,23 +318,19 @@ bool GeometryContainer::contains(const GeometryContainer& otherContainer) const } } - const vector<LineWithCRS*>& lines = c.lines.vector(); - for (size_t i = 0; i < lines.size(); ++i) { - if (!contains(lines[i]->line)) { + for (const auto& line : c.lines) { + if (!contains(line->line)) { return false; } } - const vector<PolygonWithCRS*>& polys = c.polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (!contains(*polys[i]->s2Polygon)) { + for (const auto& polygon : c.polygons) { + if (!contains(*polygon->s2Polygon)) { return false; } } - const vector<MultiPointWithCRS*>& multipoints = c.multiPoints.vector(); - for (size_t i = 0; i < multipoints.size(); ++i) { - MultiPointWithCRS* mp = multipoints[i]; + for (const auto& mp : c.multiPoints) { for (size_t j = 0; j < mp->points.size(); ++j) { if (!contains(mp->cells[j], mp->points[j])) { return false; @@ -345,21 +338,17 @@ bool GeometryContainer::contains(const GeometryContainer& otherContainer) const } } - const vector<MultiLineWithCRS*>& multilines = c.multiLines.vector(); - for (size_t i = 0; i < multilines.size(); ++i) { - const vector<S2Polyline*>& lines = multilines[i]->lines.vector(); - for (size_t j = 0; j < lines.size(); ++j) { - if (!contains(*lines[j])) { + for (const auto& multiLine : c.multiLines) { + for (const auto& line : multiLine->lines) { + if (!contains(*line)) { return false; } } } - const vector<MultiPolygonWithCRS*>& multipolys = c.multiPolygons.vector(); - for (size_t i = 0; i < multipolys.size(); ++i) { - const vector<S2Polygon*>& polys = multipolys[i]->polygons.vector(); - for (size_t j = 0; j < polys.size(); ++j) { - if (!contains(*polys[j])) { + for (const auto& multiPolygon : c.multiPolygons) { + for (const auto& polygon : multiPolygon->polygons) { + if (!contains(*polygon)) { return false; } } @@ -396,28 +385,23 @@ bool GeometryContainer::contains(const S2Cell& otherCell, const S2Point& otherPo } if (nullptr != _multiPolygon) { - const vector<S2Polygon*>& polys = _multiPolygon->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsPoint(*polys[i], otherCell, otherPoint)) { + for (const auto& polys : _multiPolygon->polygons) { + if (containsPoint(*polys, otherCell, otherPoint)) { return true; } } } if (nullptr != _geometryCollection) { - const vector<PolygonWithCRS*>& polys = _geometryCollection->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsPoint(*polys[i]->s2Polygon, otherCell, otherPoint)) { + for (const auto& polygon : _geometryCollection->polygons) { + if (containsPoint(*polygon->s2Polygon, otherCell, otherPoint)) { return true; } } - const vector<MultiPolygonWithCRS*>& multipolys = - _geometryCollection->multiPolygons.vector(); - for (size_t i = 0; i < multipolys.size(); ++i) { - const vector<S2Polygon*>& innerpolys = multipolys[i]->polygons.vector(); - for (size_t j = 0; j < innerpolys.size(); ++j) { - if (containsPoint(*innerpolys[j], otherCell, otherPoint)) { + for (const auto& multiPolygon : _geometryCollection->multiPolygons) { + for (const auto& innerPolygon : multiPolygon->polygons) { + if (containsPoint(*innerPolygon, otherCell, otherPoint)) { return true; } } @@ -468,28 +452,23 @@ bool GeometryContainer::contains(const S2Polyline& otherLine) const { } if (nullptr != _multiPolygon) { - const vector<S2Polygon*>& polys = _multiPolygon->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsLine(*polys[i], otherLine)) { + for (const auto& polygon : _multiPolygon->polygons) { + if (containsLine(*polygon, otherLine)) { return true; } } } if (nullptr != _geometryCollection) { - const vector<PolygonWithCRS*>& polys = _geometryCollection->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsLine(*polys[i]->s2Polygon, otherLine)) { + for (const auto& polygon : _geometryCollection->polygons) { + if (containsLine(*polygon->s2Polygon, otherLine)) { return true; } } - const vector<MultiPolygonWithCRS*>& multipolys = - _geometryCollection->multiPolygons.vector(); - for (size_t i = 0; i < multipolys.size(); ++i) { - const vector<S2Polygon*>& innerpolys = multipolys[i]->polygons.vector(); - for (size_t j = 0; j < innerpolys.size(); ++j) { - if (containsLine(*innerpolys[j], otherLine)) { + for (const auto& multiPolygon : _geometryCollection->multiPolygons) { + for (const auto& innerPolygon : multiPolygon->polygons) { + if (containsLine(*innerPolygon, otherLine)) { return true; } } @@ -525,28 +504,23 @@ bool GeometryContainer::contains(const S2Polygon& otherPolygon) const { } if (nullptr != _multiPolygon) { - const vector<S2Polygon*>& polys = _multiPolygon->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsPolygon(*polys[i], otherPolygon)) { + for (const auto& polygon : _multiPolygon->polygons) { + if (containsPolygon(*polygon, otherPolygon)) { return true; } } } if (nullptr != _geometryCollection) { - const vector<PolygonWithCRS*>& polys = _geometryCollection->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (containsPolygon(*polys[i]->s2Polygon, otherPolygon)) { + for (const auto& polygon : _geometryCollection->polygons) { + if (containsPolygon(*polygon->s2Polygon, otherPolygon)) { return true; } } - const vector<MultiPolygonWithCRS*>& multipolys = - _geometryCollection->multiPolygons.vector(); - for (size_t i = 0; i < multipolys.size(); ++i) { - const vector<S2Polygon*>& innerpolys = multipolys[i]->polygons.vector(); - for (size_t j = 0; j < innerpolys.size(); ++j) { - if (containsPolygon(*innerpolys[j], otherPolygon)) { + for (const auto& multiPolygon : _geometryCollection->multiPolygons) { + for (const auto& innerPolygon : multiPolygon->polygons) { + if (containsPolygon(*innerPolygon, otherPolygon)) { return true; } } @@ -581,32 +555,32 @@ bool GeometryContainer::intersects(const GeometryContainer& otherContainer) cons } } - for (size_t i = 0; i < c.polygons.vector().size(); ++i) { - if (intersects(*c.polygons.vector()[i]->s2Polygon)) { + for (size_t i = 0; i < c.polygons.size(); ++i) { + if (intersects(*c.polygons[i]->s2Polygon)) { return true; } } - for (size_t i = 0; i < c.lines.vector().size(); ++i) { - if (intersects(c.lines.vector()[i]->line)) { + for (size_t i = 0; i < c.lines.size(); ++i) { + if (intersects(c.lines[i]->line)) { return true; } } - for (size_t i = 0; i < c.multiPolygons.vector().size(); ++i) { - if (intersects(*c.multiPolygons.vector()[i])) { + for (size_t i = 0; i < c.multiPolygons.size(); ++i) { + if (intersects(*c.multiPolygons[i])) { return true; } } - for (size_t i = 0; i < c.multiLines.vector().size(); ++i) { - if (intersects(*c.multiLines.vector()[i])) { + for (size_t i = 0; i < c.multiLines.size(); ++i) { + if (intersects(*c.multiLines[i])) { return true; } } - for (size_t i = 0; i < c.multiPoints.vector().size(); ++i) { - if (intersects(*c.multiPoints.vector()[i])) { + for (size_t i = 0; i < c.multiPoints.size(); ++i) { + if (intersects(*c.multiPoints[i])) { return true; } } @@ -625,8 +599,8 @@ bool GeometryContainer::intersects(const MultiPointWithCRS& otherMultiPoint) con } bool GeometryContainer::intersects(const MultiLineWithCRS& otherMultiLine) const { - for (size_t i = 0; i < otherMultiLine.lines.vector().size(); ++i) { - if (intersects(*otherMultiLine.lines.vector()[i])) { + for (size_t i = 0; i < otherMultiLine.lines.size(); ++i) { + if (intersects(*otherMultiLine.lines[i])) { return true; } } @@ -634,8 +608,8 @@ bool GeometryContainer::intersects(const MultiLineWithCRS& otherMultiLine) const } bool GeometryContainer::intersects(const MultiPolygonWithCRS& otherMultiPolygon) const { - for (size_t i = 0; i < otherMultiPolygon.polygons.vector().size(); ++i) { - if (intersects(*otherMultiPolygon.polygons.vector()[i])) { + for (size_t i = 0; i < otherMultiPolygon.polygons.size(); ++i) { + if (intersects(*otherMultiPolygon.polygons[i])) { return true; } } @@ -660,16 +634,14 @@ bool GeometryContainer::intersects(const S2Cell& otherPoint) const { } } } else if (nullptr != _multiLine) { - const vector<S2Polyline*>& lines = _multiLine->lines.vector(); - for (size_t i = 0; i < lines.size(); ++i) { - if (lines[i]->MayIntersect(otherPoint)) { + for (const auto& line : _multiLine->lines) { + if (line->MayIntersect(otherPoint)) { return true; } } } else if (nullptr != _multiPolygon) { - const vector<S2Polygon*>& polys = _multiPolygon->polygons.vector(); - for (size_t i = 0; i < polys.size(); ++i) { - if (polys[i]->MayIntersect(otherPoint)) { + for (const auto& polygon : _multiPolygon->polygons) { + if (polygon->MayIntersect(otherPoint)) { return true; } } @@ -682,20 +654,20 @@ bool GeometryContainer::intersects(const S2Cell& otherPoint) const { } } - for (size_t i = 0; i < c.polygons.vector().size(); ++i) { - if (c.polygons.vector()[i]->s2Polygon->MayIntersect(otherPoint)) { + for (size_t i = 0; i < c.polygons.size(); ++i) { + if (c.polygons[i]->s2Polygon->MayIntersect(otherPoint)) { return true; } } - for (size_t i = 0; i < c.lines.vector().size(); ++i) { - if (c.lines.vector()[i]->line.MayIntersect(otherPoint)) { + for (size_t i = 0; i < c.lines.size(); ++i) { + if (c.lines[i]->line.MayIntersect(otherPoint)) { return true; } } - for (size_t i = 0; i < c.multiPolygons.vector().size(); ++i) { - const vector<S2Polygon*>& innerPolys = c.multiPolygons.vector()[i]->polygons.vector(); + for (size_t i = 0; i < c.multiPolygons.size(); ++i) { + const auto& innerPolys = c.multiPolygons[i]->polygons; for (size_t j = 0; j < innerPolys.size(); ++j) { if (innerPolys[j]->MayIntersect(otherPoint)) { return true; @@ -703,8 +675,8 @@ bool GeometryContainer::intersects(const S2Cell& otherPoint) const { } } - for (size_t i = 0; i < c.multiLines.vector().size(); ++i) { - const vector<S2Polyline*>& innerLines = c.multiLines.vector()[i]->lines.vector(); + for (size_t i = 0; i < c.multiLines.size(); ++i) { + const auto& innerLines = c.multiLines[i]->lines; for (size_t j = 0; j < innerLines.size(); ++j) { if (innerLines[j]->MayIntersect(otherPoint)) { return true; @@ -712,8 +684,8 @@ bool GeometryContainer::intersects(const S2Cell& otherPoint) const { } } - for (size_t i = 0; i < c.multiPoints.vector().size(); ++i) { - const vector<S2Cell>& innerCells = c.multiPoints.vector()[i]->cells; + for (size_t i = 0; i < c.multiPoints.size(); ++i) { + const vector<S2Cell>& innerCells = c.multiPoints[i]->cells; for (size_t j = 0; j < innerCells.size(); ++j) { if (innerCells[j].MayIntersect(otherPoint)) { return true; @@ -752,14 +724,14 @@ bool GeometryContainer::intersects(const S2Polyline& otherLine) const { } } } else if (nullptr != _multiLine) { - for (size_t i = 0; i < _multiLine->lines.vector().size(); ++i) { - if (otherLine.Intersects(_multiLine->lines.vector()[i])) { + for (size_t i = 0; i < _multiLine->lines.size(); ++i) { + if (otherLine.Intersects(_multiLine->lines[i].get())) { return true; } } } else if (nullptr != _multiPolygon) { - for (size_t i = 0; i < _multiPolygon->polygons.vector().size(); ++i) { - if (polygonLineIntersection(otherLine, *_multiPolygon->polygons.vector()[i])) { + for (size_t i = 0; i < _multiPolygon->polygons.size(); ++i) { + if (polygonLineIntersection(otherLine, *_multiPolygon->polygons[i].get())) { return true; } } @@ -772,20 +744,20 @@ bool GeometryContainer::intersects(const S2Polyline& otherLine) const { } } - for (size_t i = 0; i < c.polygons.vector().size(); ++i) { - if (polygonLineIntersection(otherLine, *c.polygons.vector()[i]->s2Polygon)) { + for (size_t i = 0; i < c.polygons.size(); ++i) { + if (polygonLineIntersection(otherLine, *c.polygons[i]->s2Polygon)) { return true; } } - for (size_t i = 0; i < c.lines.vector().size(); ++i) { - if (c.lines.vector()[i]->line.Intersects(&otherLine)) { + for (size_t i = 0; i < c.lines.size(); ++i) { + if (c.lines[i]->line.Intersects(&otherLine)) { return true; } } - for (size_t i = 0; i < c.multiPolygons.vector().size(); ++i) { - const vector<S2Polygon*>& innerPolys = c.multiPolygons.vector()[i]->polygons.vector(); + for (size_t i = 0; i < c.multiPolygons.size(); ++i) { + const auto& innerPolys = c.multiPolygons[i]->polygons; for (size_t j = 0; j < innerPolys.size(); ++j) { if (polygonLineIntersection(otherLine, *innerPolys[j])) { return true; @@ -793,8 +765,8 @@ bool GeometryContainer::intersects(const S2Polyline& otherLine) const { } } - for (size_t i = 0; i < c.multiLines.vector().size(); ++i) { - const vector<S2Polyline*>& innerLines = c.multiLines.vector()[i]->lines.vector(); + for (size_t i = 0; i < c.multiLines.size(); ++i) { + const auto& innerLines = c.multiLines[i]->lines; for (size_t j = 0; j < innerLines.size(); ++j) { if (innerLines[j]->Intersects(&otherLine)) { return true; @@ -802,8 +774,8 @@ bool GeometryContainer::intersects(const S2Polyline& otherLine) const { } } - for (size_t i = 0; i < c.multiPoints.vector().size(); ++i) { - const vector<S2Cell>& innerCells = c.multiPoints.vector()[i]->cells; + for (size_t i = 0; i < c.multiPoints.size(); ++i) { + const vector<S2Cell>& innerCells = c.multiPoints[i]->cells; for (size_t j = 0; j < innerCells.size(); ++j) { if (otherLine.MayIntersect(innerCells[j])) { return true; @@ -832,14 +804,14 @@ bool GeometryContainer::intersects(const S2Polygon& otherPolygon) const { } } } else if (nullptr != _multiLine) { - for (size_t i = 0; i < _multiLine->lines.vector().size(); ++i) { - if (polygonLineIntersection(*_multiLine->lines.vector()[i], otherPolygon)) { + for (size_t i = 0; i < _multiLine->lines.size(); ++i) { + if (polygonLineIntersection(*_multiLine->lines[i], otherPolygon)) { return true; } } } else if (nullptr != _multiPolygon) { - for (size_t i = 0; i < _multiPolygon->polygons.vector().size(); ++i) { - if (otherPolygon.Intersects(_multiPolygon->polygons.vector()[i])) { + for (size_t i = 0; i < _multiPolygon->polygons.size(); ++i) { + if (otherPolygon.Intersects(_multiPolygon->polygons[i].get())) { return true; } } @@ -852,29 +824,29 @@ bool GeometryContainer::intersects(const S2Polygon& otherPolygon) const { } } - for (size_t i = 0; i < c.polygons.vector().size(); ++i) { - if (otherPolygon.Intersects(c.polygons.vector()[i]->s2Polygon.get())) { + for (size_t i = 0; i < c.polygons.size(); ++i) { + if (otherPolygon.Intersects(c.polygons[i]->s2Polygon.get())) { return true; } } - for (size_t i = 0; i < c.lines.vector().size(); ++i) { - if (polygonLineIntersection(c.lines.vector()[i]->line, otherPolygon)) { + for (size_t i = 0; i < c.lines.size(); ++i) { + if (polygonLineIntersection(c.lines[i]->line, otherPolygon)) { return true; } } - for (size_t i = 0; i < c.multiPolygons.vector().size(); ++i) { - const vector<S2Polygon*>& innerPolys = c.multiPolygons.vector()[i]->polygons.vector(); + for (size_t i = 0; i < c.multiPolygons.size(); ++i) { + const auto& innerPolys = c.multiPolygons[i]->polygons; for (size_t j = 0; j < innerPolys.size(); ++j) { - if (otherPolygon.Intersects(innerPolys[j])) { + if (otherPolygon.Intersects(innerPolys[j].get())) { return true; } } } - for (size_t i = 0; i < c.multiLines.vector().size(); ++i) { - const vector<S2Polyline*>& innerLines = c.multiLines.vector()[i]->lines.vector(); + for (size_t i = 0; i < c.multiLines.size(); ++i) { + const auto& innerLines = c.multiLines[i]->lines; for (size_t j = 0; j < innerLines.size(); ++j) { if (polygonLineIntersection(*innerLines[j], otherPolygon)) { return true; @@ -882,8 +854,8 @@ bool GeometryContainer::intersects(const S2Polygon& otherPolygon) const { } } - for (size_t i = 0; i < c.multiPoints.vector().size(); ++i) { - const vector<S2Cell>& innerCells = c.multiPoints.vector()[i]->cells; + for (size_t i = 0; i < c.multiPoints.size(); ++i) { + const vector<S2Cell>& innerCells = c.multiPoints[i]->cells; for (size_t j = 0; j < innerCells.size(); ++j) { if (otherPolygon.MayIntersect(innerCells[j])) { return true; @@ -925,13 +897,13 @@ Status GeometryContainer::parseFromGeoJSON(bool skipValidation) { _multiLine.reset(new MultiLineWithCRS()); status = GeoParser::parseMultiLine(obj, skipValidation, _multiLine.get()); for (size_t i = 0; i < _multiLine->lines.size(); ++i) { - regions.push_back(_multiLine->lines[i]); + regions.push_back(_multiLine->lines[i].get()); } } else if (GeoParser::GEOJSON_MULTI_POLYGON == type) { _multiPolygon.reset(new MultiPolygonWithCRS()); status = GeoParser::parseMultiPolygon(obj, skipValidation, _multiPolygon.get()); for (size_t i = 0; i < _multiPolygon->polygons.size(); ++i) { - regions.push_back(_multiPolygon->polygons[i]); + regions.push_back(_multiPolygon->polygons[i].get()); } } else if (GeoParser::GEOJSON_GEOMETRY_COLLECTION == type) { _geometryCollection.reset(new GeometryCollection()); @@ -948,21 +920,21 @@ Status GeometryContainer::parseFromGeoJSON(bool skipValidation) { regions.push_back(_geometryCollection->polygons[i]->s2Polygon.get()); } for (size_t i = 0; i < _geometryCollection->multiPoints.size(); ++i) { - MultiPointWithCRS* multiPoint = _geometryCollection->multiPoints[i]; + MultiPointWithCRS* multiPoint = _geometryCollection->multiPoints[i].get(); for (size_t j = 0; j < multiPoint->cells.size(); ++j) { regions.push_back(&multiPoint->cells[j]); } } for (size_t i = 0; i < _geometryCollection->multiLines.size(); ++i) { - const MultiLineWithCRS* multiLine = _geometryCollection->multiLines[i]; + const MultiLineWithCRS* multiLine = _geometryCollection->multiLines[i].get(); for (size_t j = 0; j < multiLine->lines.size(); ++j) { - regions.push_back(multiLine->lines[j]); + regions.push_back(multiLine->lines[j].get()); } } for (size_t i = 0; i < _geometryCollection->multiPolygons.size(); ++i) { - const MultiPolygonWithCRS* multiPolygon = _geometryCollection->multiPolygons[i]; + const MultiPolygonWithCRS* multiPolygon = _geometryCollection->multiPolygons[i].get(); for (size_t j = 0; j < multiPolygon->polygons.size(); ++j) { - regions.push_back(multiPolygon->polygons[j]); + regions.push_back(multiPolygon->polygons[j].get()); } } } else { @@ -1185,10 +1157,8 @@ static double s2MinDistanceRad(const S2Point& s2Point, const MultiPointWithCRS& static double s2MinDistanceRad(const S2Point& s2Point, const MultiLineWithCRS& s2MultiLine) { double minDistance = -1; - for (vector<S2Polyline*>::const_iterator it = s2MultiLine.lines.vector().begin(); - it != s2MultiLine.lines.vector().end(); - ++it) { - double nextDistance = S2Distance::minDistanceRad(s2Point, **it); + for (const auto& line : s2MultiLine.lines) { + double nextDistance = S2Distance::minDistanceRad(s2Point, *line); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } @@ -1199,10 +1169,8 @@ static double s2MinDistanceRad(const S2Point& s2Point, const MultiLineWithCRS& s static double s2MinDistanceRad(const S2Point& s2Point, const MultiPolygonWithCRS& s2MultiPolygon) { double minDistance = -1; - for (vector<S2Polygon*>::const_iterator it = s2MultiPolygon.polygons.vector().begin(); - it != s2MultiPolygon.polygons.vector().end(); - ++it) { - double nextDistance = S2Distance::minDistanceRad(s2Point, **it); + for (const auto& polygon : s2MultiPolygon.polygons) { + double nextDistance = S2Distance::minDistanceRad(s2Point, *polygon); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } @@ -1224,53 +1192,40 @@ static double s2MinDistanceRad(const S2Point& s2Point, } } - for (vector<LineWithCRS*>::const_iterator it = geometryCollection.lines.vector().begin(); - it != geometryCollection.lines.vector().end(); - ++it) { - invariant(SPHERE == (*it)->crs); - double nextDistance = S2Distance::minDistanceRad(s2Point, (*it)->line); + for (const auto& line : geometryCollection.lines) { + invariant(SPHERE == line->crs); + double nextDistance = S2Distance::minDistanceRad(s2Point, line->line); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } } - for (vector<PolygonWithCRS*>::const_iterator it = geometryCollection.polygons.vector().begin(); - it != geometryCollection.polygons.vector().end(); - ++it) { - invariant(SPHERE == (*it)->crs); + for (const auto& polygon : geometryCollection.polygons) { + invariant(SPHERE == polygon->crs); // We don't support distances for big polygons yet. - invariant(nullptr != (*it)->s2Polygon); - double nextDistance = S2Distance::minDistanceRad(s2Point, *((*it)->s2Polygon)); + invariant(polygon->s2Polygon); + double nextDistance = S2Distance::minDistanceRad(s2Point, *(polygon->s2Polygon)); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } } - for (vector<MultiPointWithCRS*>::const_iterator it = - geometryCollection.multiPoints.vector().begin(); - it != geometryCollection.multiPoints.vector().end(); - ++it) { - double nextDistance = s2MinDistanceRad(s2Point, **it); + for (const auto& multiPoint : geometryCollection.multiPoints) { + double nextDistance = s2MinDistanceRad(s2Point, *multiPoint); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } } - for (vector<MultiLineWithCRS*>::const_iterator it = - geometryCollection.multiLines.vector().begin(); - it != geometryCollection.multiLines.vector().end(); - ++it) { - double nextDistance = s2MinDistanceRad(s2Point, **it); + for (const auto& multiLine : geometryCollection.multiLines) { + double nextDistance = s2MinDistanceRad(s2Point, *multiLine); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } } - for (vector<MultiPolygonWithCRS*>::const_iterator it = - geometryCollection.multiPolygons.vector().begin(); - it != geometryCollection.multiPolygons.vector().end(); - ++it) { - double nextDistance = s2MinDistanceRad(s2Point, **it); + for (const auto& multiPolygon : geometryCollection.multiPolygons) { + double nextDistance = s2MinDistanceRad(s2Point, *multiPolygon); if (minDistance < 0 || nextDistance < minDistance) { minDistance = nextDistance; } diff --git a/src/mongo/db/geo/geoparser.cpp b/src/mongo/db/geo/geoparser.cpp index ea97c706851..005f6defef2 100644 --- a/src/mongo/db/geo/geoparser.cpp +++ b/src/mongo/db/geo/geoparser.cpp @@ -536,14 +536,14 @@ Status GeoParser::parseMultiLine(const BSONObj& obj, bool skipValidation, MultiL return BAD_VALUE("MultiLineString coordinates must be an array"); out->lines.clear(); - vector<S2Polyline*>& lines = out->lines.mutableVector(); + auto& lines = out->lines; BSONObjIterator it(coordElt.Obj()); // Iterate array while (it.more()) { - lines.push_back(new S2Polyline()); - status = parseGeoJSONLineCoordinates(it.next(), skipValidation, lines.back()); + lines.push_back(std::make_unique<S2Polyline>()); + status = parseGeoJSONLineCoordinates(it.next(), skipValidation, lines.back().get()); if (!status.isOK()) return status; } @@ -566,13 +566,13 @@ Status GeoParser::parseMultiPolygon(const BSONObj& obj, return BAD_VALUE("MultiPolygon coordinates must be an array"); out->polygons.clear(); - vector<S2Polygon*>& polygons = out->polygons.mutableVector(); + auto& polygons = out->polygons; BSONObjIterator it(coordElt.Obj()); // Iterate array while (it.more()) { - polygons.push_back(new S2Polygon()); - status = parseGeoJSONPolygonCoordinates(it.next(), skipValidation, polygons.back()); + polygons.push_back(std::make_unique<S2Polygon>()); + status = parseGeoJSONPolygonCoordinates(it.next(), skipValidation, polygons.back().get()); if (!status.isOK()) return status; } @@ -680,21 +680,20 @@ Status GeoParser::parseGeometryCollection(const BSONObj& obj, out->points.resize(out->points.size() + 1); status = parseGeoJSONPoint(geoObj, &out->points.back()); } else if (GEOJSON_LINESTRING == type) { - out->lines.mutableVector().push_back(new LineWithCRS()); - status = parseGeoJSONLine(geoObj, skipValidation, out->lines.vector().back()); + out->lines.push_back(std::make_unique<LineWithCRS>()); + status = parseGeoJSONLine(geoObj, skipValidation, out->lines.back().get()); } else if (GEOJSON_POLYGON == type) { - out->polygons.mutableVector().push_back(new PolygonWithCRS()); - status = parseGeoJSONPolygon(geoObj, skipValidation, out->polygons.vector().back()); + out->polygons.push_back(std::make_unique<PolygonWithCRS>()); + status = parseGeoJSONPolygon(geoObj, skipValidation, out->polygons.back().get()); } else if (GEOJSON_MULTI_POINT == type) { - out->multiPoints.mutableVector().push_back(new MultiPointWithCRS()); - status = parseMultiPoint(geoObj, out->multiPoints.mutableVector().back()); + out->multiPoints.push_back(std::make_unique<MultiPointWithCRS>()); + status = parseMultiPoint(geoObj, out->multiPoints.back().get()); } else if (GEOJSON_MULTI_LINESTRING == type) { - out->multiLines.mutableVector().push_back(new MultiLineWithCRS()); - status = parseMultiLine(geoObj, skipValidation, out->multiLines.mutableVector().back()); + out->multiLines.push_back(std::make_unique<MultiLineWithCRS>()); + status = parseMultiLine(geoObj, skipValidation, out->multiLines.back().get()); } else if (GEOJSON_MULTI_POLYGON == type) { - out->multiPolygons.mutableVector().push_back(new MultiPolygonWithCRS()); - status = parseMultiPolygon( - geoObj, skipValidation, out->multiPolygons.mutableVector().back()); + out->multiPolygons.push_back(std::make_unique<MultiPolygonWithCRS>()); + status = parseMultiPolygon(geoObj, skipValidation, out->multiPolygons.back().get()); } else { // Should not reach here. MONGO_UNREACHABLE; diff --git a/src/mongo/db/geo/shapes.h b/src/mongo/db/geo/shapes.h index b5da30d3e0d..56f1849304a 100644 --- a/src/mongo/db/geo/shapes.h +++ b/src/mongo/db/geo/shapes.h @@ -33,7 +33,6 @@ #include <string> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/geo/big_polygon.h" #include "mongo/db/geo/s2.h" #include "mongo/db/jsobj.h" @@ -313,31 +312,30 @@ struct MultiPointWithCRS { struct MultiLineWithCRS { MultiLineWithCRS() : crs(UNSET) {} - OwnedPointerVector<S2Polyline> lines; + std::vector<std::unique_ptr<S2Polyline>> lines; CRS crs; }; struct MultiPolygonWithCRS { MultiPolygonWithCRS() : crs(UNSET) {} - OwnedPointerVector<S2Polygon> polygons; + std::vector<std::unique_ptr<S2Polygon>> polygons; CRS crs; }; struct GeometryCollection { std::vector<PointWithCRS> points; - // The amount of indirection here is painful but we can't operator= unique_ptr or - // OwnedPointerVector. - OwnedPointerVector<LineWithCRS> lines; - OwnedPointerVector<PolygonWithCRS> polygons; - OwnedPointerVector<MultiPointWithCRS> multiPoints; - OwnedPointerVector<MultiLineWithCRS> multiLines; - OwnedPointerVector<MultiPolygonWithCRS> multiPolygons; + // The amount of indirection here is painful but we can't operator= unique_ptr. + std::vector<std::unique_ptr<LineWithCRS>> lines; + std::vector<std::unique_ptr<PolygonWithCRS>> polygons; + std::vector<std::unique_ptr<MultiPointWithCRS>> multiPoints; + std::vector<std::unique_ptr<MultiLineWithCRS>> multiLines; + std::vector<std::unique_ptr<MultiPolygonWithCRS>> multiPolygons; bool supportsContains() { // Only polygons (and multiPolygons) support containment. - return (polygons.vector().size() > 0 || multiPolygons.vector().size() > 0); + return (!polygons.empty() || !multiPolygons.empty()); } }; diff --git a/src/mongo/db/query/plan_cache_indexability.cpp b/src/mongo/db/query/plan_cache_indexability.cpp index dbf0250966c..4f1b0c176f8 100644 --- a/src/mongo/db/query/plan_cache_indexability.cpp +++ b/src/mongo/db/query/plan_cache_indexability.cpp @@ -34,7 +34,6 @@ #include <memory> #include "mongo/base/init.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/exec/projection_executor_utils.h" #include "mongo/db/index/wildcard_key_generator.h" #include "mongo/db/matcher/expression.h" diff --git a/src/mongo/db/query/planner_access.cpp b/src/mongo/db/query/planner_access.cpp index 0e22579b0f7..9af228bb8ac 100644 --- a/src/mongo/db/query/planner_access.cpp +++ b/src/mongo/db/query/planner_access.cpp @@ -36,7 +36,6 @@ #include <algorithm> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/bson/simple_bsonobj_comparator.h" #include "mongo/db/bson/dotted_path_support.h" #include "mongo/db/catalog/clustered_collection_util.h" diff --git a/src/mongo/db/query/query_planner_test_fixture.h b/src/mongo/db/query/query_planner_test_fixture.h index 2d612c7b11b..a34589548a3 100644 --- a/src/mongo/db/query/query_planner_test_fixture.h +++ b/src/mongo/db/query/query_planner_test_fixture.h @@ -34,7 +34,6 @@ #include <string> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/index/multikey_paths.h" #include "mongo/db/jsobj.h" #include "mongo/db/json.h" diff --git a/src/mongo/db/storage/key_string_test.cpp b/src/mongo/db/storage/key_string_test.cpp index 19322f583cc..ef9f7e744a5 100644 --- a/src/mongo/db/storage/key_string_test.cpp +++ b/src/mongo/db/storage/key_string_test.cpp @@ -40,7 +40,6 @@ #include <typeinfo> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/simple_string_data_comparator.h" #include "mongo/bson/bson_depth.h" #include "mongo/bson/bson_validate.h" diff --git a/src/mongo/db/storage/record_store.h b/src/mongo/db/storage/record_store.h index 77a6d6e2782..bcae0bf68d0 100644 --- a/src/mongo/db/storage/record_store.h +++ b/src/mongo/db/storage/record_store.h @@ -31,7 +31,6 @@ #include <boost/optional.hpp> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/bson/mutable/damage_vector.h" #include "mongo/db/exec/collection_scan_common.h" #include "mongo/db/namespace_string.h" diff --git a/src/mongo/db/update/path_support_test.cpp b/src/mongo/db/update/path_support_test.cpp index c8dcb73a8ab..26137bfcef0 100644 --- a/src/mongo/db/update/path_support_test.cpp +++ b/src/mongo/db/update/path_support_test.cpp @@ -35,7 +35,6 @@ #include <vector> #include "mongo/base/error_codes.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/simple_string_data_comparator.h" #include "mongo/base/status.h" #include "mongo/base/string_data.h" diff --git a/src/mongo/db/update/update_driver.h b/src/mongo/db/update/update_driver.h index 0f3fb02f455..d512d89b1cf 100644 --- a/src/mongo/db/update/update_driver.h +++ b/src/mongo/db/update/update_driver.h @@ -32,7 +32,6 @@ #include <string> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/status.h" #include "mongo/bson/mutable/document.h" #include "mongo/db/exec/document_value/value.h" diff --git a/src/mongo/db/update/update_driver_test.cpp b/src/mongo/db/update/update_driver_test.cpp index f133caf0d56..2d03e4b7886 100644 --- a/src/mongo/db/update/update_driver_test.cpp +++ b/src/mongo/db/update/update_driver_test.cpp @@ -32,7 +32,6 @@ #include <map> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/simple_string_data_comparator.h" #include "mongo/base/string_data.h" #include "mongo/bson/bsonelement_comparator.h" @@ -524,47 +523,47 @@ TEST_F(CreateFromQuery, ImmutableFieldsOp) { TEST_F(CreateFromQuery, ShardKeyRepl) { BSONObj query = fromjson("{a:{$eq:1}}, b:2}"); - OwnedPointerVector<FieldRef> immutablePathsVector; - immutablePathsVector.push_back(new FieldRef("a")); - immutablePathsVector.push_back(new FieldRef("_id")); + std::vector<std::unique_ptr<FieldRef>> immutablePathsVector; + immutablePathsVector.push_back(std::make_unique<FieldRef>("a")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("_id")); FieldRefSet immutablePaths; - immutablePaths.fillFrom(immutablePathsVector.vector()); + immutablePaths.fillFrom(immutablePathsVector); ASSERT_OK(driverRepl().populateDocumentWithQueryFields(opCtx(), query, immutablePaths, doc())); assertSameFields(fromjson("{a:1}"), doc().getObject()); } TEST_F(CreateFromQuery, NestedShardKeyRepl) { BSONObj query = fromjson("{a:{$eq:1},'b.c':2},d:2}"); - OwnedPointerVector<FieldRef> immutablePathsVector; - immutablePathsVector.push_back(new FieldRef("a")); - immutablePathsVector.push_back(new FieldRef("b.c")); - immutablePathsVector.push_back(new FieldRef("_id")); + std::vector<std::unique_ptr<FieldRef>> immutablePathsVector; + immutablePathsVector.push_back(std::make_unique<FieldRef>("a")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("b.c")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("_id")); FieldRefSet immutablePaths; - immutablePaths.fillFrom(immutablePathsVector.vector()); + immutablePaths.fillFrom(immutablePathsVector); ASSERT_OK(driverRepl().populateDocumentWithQueryFields(opCtx(), query, immutablePaths, doc())); assertSameFields(fromjson("{a:1,b:{c:2}}"), doc().getObject()); } TEST_F(CreateFromQuery, NestedShardKeyOp) { BSONObj query = fromjson("{a:{$eq:1},'b.c':2,d:{$all:[3]}},e:2}"); - OwnedPointerVector<FieldRef> immutablePathsVector; - immutablePathsVector.push_back(new FieldRef("a")); - immutablePathsVector.push_back(new FieldRef("b.c")); - immutablePathsVector.push_back(new FieldRef("_id")); + std::vector<std::unique_ptr<FieldRef>> immutablePathsVector; + immutablePathsVector.push_back(std::make_unique<FieldRef>("a")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("b.c")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("_id")); FieldRefSet immutablePaths; - immutablePaths.fillFrom(immutablePathsVector.vector()); + immutablePaths.fillFrom(immutablePathsVector); ASSERT_OK(driverOps().populateDocumentWithQueryFields(opCtx(), query, immutablePaths, doc())); assertSameFields(fromjson("{a:1,b:{c:2},d:3}"), doc().getObject()); } TEST_F(CreateFromQuery, NotFullShardKeyRepl) { BSONObj query = fromjson("{a:{$eq:1}, 'b.c':2}, d:2}"); - OwnedPointerVector<FieldRef> immutablePathsVector; - immutablePathsVector.push_back(new FieldRef("a")); - immutablePathsVector.push_back(new FieldRef("b")); - immutablePathsVector.push_back(new FieldRef("_id")); + std::vector<std::unique_ptr<FieldRef>> immutablePathsVector; + immutablePathsVector.push_back(std::make_unique<FieldRef>("a")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("b")); + immutablePathsVector.push_back(std::make_unique<FieldRef>("_id")); FieldRefSet immutablePaths; - immutablePaths.fillFrom(immutablePathsVector.vector()); + immutablePaths.fillFrom(immutablePathsVector); ASSERT_NOT_OK( driverRepl().populateDocumentWithQueryFields(opCtx(), query, immutablePaths, doc())); } diff --git a/src/mongo/dbtests/query_stage_near.cpp b/src/mongo/dbtests/query_stage_near.cpp index f5befb24e1d..e26ac67509c 100644 --- a/src/mongo/dbtests/query_stage_near.cpp +++ b/src/mongo/dbtests/query_stage_near.cpp @@ -37,7 +37,6 @@ #include <memory> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/client.h" #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" diff --git a/src/mongo/s/chunk_manager.cpp b/src/mongo/s/chunk_manager.cpp index 64249293630..847bac7b27f 100644 --- a/src/mongo/s/chunk_manager.cpp +++ b/src/mongo/s/chunk_manager.cpp @@ -33,7 +33,6 @@ #include "mongo/s/chunk_manager.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/bson/simple_bsonobj_comparator.h" #include "mongo/db/matcher/extensions_callback_noop.h" #include "mongo/db/query/collation/collation_index_key.h" diff --git a/src/mongo/s/commands/cluster_write_cmd.cpp b/src/mongo/s/commands/cluster_write_cmd.cpp index c0715bbf51c..2f176bed049 100644 --- a/src/mongo/s/commands/cluster_write_cmd.cpp +++ b/src/mongo/s/commands/cluster_write_cmd.cpp @@ -32,7 +32,6 @@ #include "mongo/platform/basic.h" #include "mongo/base/error_codes.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/client/remote_command_targeter.h" #include "mongo/db/catalog/document_validation.h" #include "mongo/db/commands.h" diff --git a/src/mongo/s/write_ops/batch_write_op.cpp b/src/mongo/s/write_ops/batch_write_op.cpp index e9f1bd1617b..4ac63ff2b1b 100644 --- a/src/mongo/s/write_ops/batch_write_op.cpp +++ b/src/mongo/s/write_ops/batch_write_op.cpp @@ -31,7 +31,6 @@ #include "mongo/s/write_ops/batch_write_op.h" -#include <memory> #include <numeric> #include "mongo/base/error_codes.h" @@ -89,9 +88,9 @@ void buildTargetError(const Status& errStatus, WriteErrorDetail* details) { /** * Helper to determine whether a number of targeted writes require a new targeted batch. */ -bool isNewBatchRequiredOrdered(const std::vector<TargetedWrite*>& writes, +bool isNewBatchRequiredOrdered(const std::vector<std::unique_ptr<TargetedWrite>>& writes, const TargetedBatchMap& batchMap) { - for (const auto write : writes) { + for (auto&& write : writes) { if (batchMap.find(&write->endpoint) == batchMap.end()) { return true; } @@ -105,10 +104,10 @@ bool isNewBatchRequiredOrdered(const std::vector<TargetedWrite*>& writes, * necessitates a new batch. This happens when a batch write incldues a multi target write and * a single target write. */ -bool isNewBatchRequiredUnordered(const std::vector<TargetedWrite*>& writes, +bool isNewBatchRequiredUnordered(const std::vector<std::unique_ptr<TargetedWrite>>& writes, const TargetedBatchMap& batchMap, const std::set<ShardId>& targetedShards) { - for (const auto write : writes) { + for (auto&& write : writes) { if (batchMap.find(&write->endpoint) == batchMap.end()) { if (targetedShards.find((&write->endpoint)->shardName) != targetedShards.end()) { return true; @@ -122,10 +121,10 @@ bool isNewBatchRequiredUnordered(const std::vector<TargetedWrite*>& writes, /** * Helper to determine whether a number of targeted writes require a new targeted batch. */ -bool wouldMakeBatchesTooBig(const std::vector<TargetedWrite*>& writes, +bool wouldMakeBatchesTooBig(const std::vector<std::unique_ptr<TargetedWrite>>& writes, int writeSizeBytes, const TargetedBatchMap& batchMap) { - for (const auto write : writes) { + for (auto&& write : writes) { TargetedBatchMap::const_iterator it = batchMap.find(&write->endpoint); if (it == batchMap.end()) { // If this is the first item in the batch, it can't be too big @@ -322,9 +321,7 @@ Status BatchWriteOp::targetBatch(const NSTargeter& targeter, // Get TargetedWrites from the targeter for the write operation // // TargetedWrites need to be owned once returned - - OwnedPointerVector<TargetedWrite> writesOwned; - std::vector<TargetedWrite*>& writes = writesOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> writes; Status targetStatus = Status::OK(); try { @@ -417,7 +414,7 @@ Status BatchWriteOp::targetBatch(const NSTargeter& targeter, // Targeting went ok, add to appropriate TargetedBatch // - for (const auto write : writes) { + for (auto&& write : writes) { TargetedBatchMap::iterator batchIt = batchMap.find(&write->endpoint); if (batchIt == batchMap.end()) { TargetedWriteBatch* newBatch = new TargetedWriteBatch(write->endpoint); @@ -426,11 +423,12 @@ Status BatchWriteOp::targetBatch(const NSTargeter& targeter, } TargetedWriteBatch* batch = batchIt->second; - batch->addWrite(write, std::max(writeSizeBytes, errorResponsePotentialSizeBytes)); + batch->addWrite(std::move(write), + std::max(writeSizeBytes, errorResponsePotentialSizeBytes)); } // Relinquish ownership of TargetedWrites, now the TargetedBatches own them - writesOwned.mutableVector().clear(); + writes.clear(); // // Break if we're ordered and we have more than one endpoint - later writes cannot be @@ -638,10 +636,7 @@ void BatchWriteOp::noteBatchResponse(const TargetedWriteBatch& targetedBatch, std::vector<WriteErrorDetail*>::iterator itemErrorIt = itemErrors.begin(); int index = 0; WriteErrorDetail* lastError = nullptr; - for (std::vector<TargetedWrite*>::const_iterator it = targetedBatch.getWrites().begin(); - it != targetedBatch.getWrites().end(); - ++it, ++index) { - const TargetedWrite* write = *it; + for (auto&& write : targetedBatch.getWrites()) { WriteOp& writeOp = _writeOps[write->writeOpRef.first]; dassert(writeOp.getWriteState() == WriteOpState_Pending); @@ -668,6 +663,7 @@ void BatchWriteOp::noteBatchResponse(const TargetedWriteBatch& targetedBatch, writeOp.noteWriteError(*write, *writeError); lastError = writeError; } + ++index; } // Track errors we care about, whether batch or individual errors @@ -977,8 +973,15 @@ const std::vector<ShardError>& TrackedErrors::getErrors(int errCode) const { return _errorMap.find(errCode)->second; } -void TargetedWriteBatch::addWrite(TargetedWrite* targetedWrite, int estWriteSize) { - _writes.mutableVector().push_back(targetedWrite); +std::vector<TargetedWrite*> TargetedWriteBatch::getWrites() const { + std::vector<TargetedWrite*> rv; + std::transform( + _writes.begin(), _writes.end(), std::back_inserter(rv), [](auto&& w) { return w.get(); }); + return rv; +} + +void TargetedWriteBatch::addWrite(std::unique_ptr<TargetedWrite> targetedWrite, int estWriteSize) { + _writes.push_back(std::move(targetedWrite)); _estimatedSizeBytes += estWriteSize; } diff --git a/src/mongo/s/write_ops/batch_write_op.h b/src/mongo/s/write_ops/batch_write_op.h index 375ae643079..70c3a3ba7b1 100644 --- a/src/mongo/s/write_ops/batch_write_op.h +++ b/src/mongo/s/write_ops/batch_write_op.h @@ -33,7 +33,6 @@ #include <set> #include <vector> -#include "mongo/base/owned_pointer_vector.h" #include "mongo/base/status.h" #include "mongo/db/logical_session_id.h" #include "mongo/rpc/write_concern_error_detail.h" @@ -268,9 +267,7 @@ public: return _endpoint; } - const std::vector<TargetedWrite*>& getWrites() const { - return _writes.vector(); - } + std::vector<TargetedWrite*> getWrites() const; size_t getNumOps() const { return _writes.size(); @@ -283,7 +280,7 @@ public: /** * TargetedWrite is owned here once given to the TargetedWriteBatch. */ - void addWrite(TargetedWrite* targetedWrite, int estWriteSize); + void addWrite(std::unique_ptr<TargetedWrite> targetedWrite, int estWriteSize); private: // Where to send the batch @@ -291,7 +288,7 @@ private: // Where the responses go // TargetedWrite*s are owned by the TargetedWriteBatch - OwnedPointerVector<TargetedWrite> _writes; + std::vector<std::unique_ptr<TargetedWrite>> _writes; // Conservatvely estimated size of the batch, for ensuring it doesn't grow past the maximum BSON // size diff --git a/src/mongo/s/write_ops/write_op.cpp b/src/mongo/s/write_ops/write_op.cpp index 2c37e7fda28..c0288b7080a 100644 --- a/src/mongo/s/write_ops/write_op.cpp +++ b/src/mongo/s/write_ops/write_op.cpp @@ -54,7 +54,7 @@ const WriteErrorDetail& WriteOp::getOpError() const { void WriteOp::targetWrites(OperationContext* opCtx, const NSTargeter& targeter, - std::vector<TargetedWrite*>* targetedWrites) { + std::vector<std::unique_ptr<TargetedWrite>>* targetedWrites) { auto endpoints = [&] { if (_itemRef.getOpType() == BatchedCommandRequest::BatchType_Insert) { return std::vector{targeter.targetInsert(opCtx, _itemRef.getDocument())}; @@ -91,9 +91,9 @@ void WriteOp::targetWrites(OperationContext* opCtx, endpoint.shardVersion = ChunkVersion::IGNORED(); } - targetedWrites->push_back(new TargetedWrite(std::move(endpoint), ref)); + targetedWrites->push_back(std::make_unique<TargetedWrite>(std::move(endpoint), ref)); - _childOps.back().pendingWrite = targetedWrites->back(); + _childOps.back().pendingWrite = targetedWrites->back().get(); _childOps.back().state = WriteOpState_Pending; } diff --git a/src/mongo/s/write_ops/write_op.h b/src/mongo/s/write_ops/write_op.h index 138b466a556..1eb9ead8fbf 100644 --- a/src/mongo/s/write_ops/write_op.h +++ b/src/mongo/s/write_ops/write_op.h @@ -121,7 +121,7 @@ public: */ void targetWrites(OperationContext* opCtx, const NSTargeter& targeter, - std::vector<TargetedWrite*>* targetedWrites); + std::vector<std::unique_ptr<TargetedWrite>>* targetedWrites); /** * Returns the number of child writes that were last targeted. diff --git a/src/mongo/s/write_ops/write_op_test.cpp b/src/mongo/s/write_ops/write_op_test.cpp index 1957ef3b5f2..5444a801548 100644 --- a/src/mongo/s/write_ops/write_op_test.cpp +++ b/src/mongo/s/write_ops/write_op_test.cpp @@ -29,7 +29,6 @@ #include "mongo/platform/basic.h" -#include "mongo/base/owned_pointer_vector.h" #include "mongo/db/service_context_test_fixture.h" #include "mongo/s/concurrency/locker_mongos_client_observer.h" #include "mongo/s/mock_ns_targeter.h" @@ -60,9 +59,10 @@ write_ops::DeleteOpEntry buildDelete(const BSONObj& query, bool multi) { return entry; } -void sortByEndpoint(std::vector<TargetedWrite*>* writes) { +void sortByEndpoint(std::vector<std::unique_ptr<TargetedWrite>>* writes) { struct EndpointComp { - bool operator()(const TargetedWrite* writeA, const TargetedWrite* writeB) const { + bool operator()(const std::unique_ptr<TargetedWrite>& writeA, + const std::unique_ptr<TargetedWrite>& writeB) const { return writeA->endpoint.shardName.compare(writeB->endpoint.shardName) < 0; } }; @@ -119,8 +119,7 @@ TEST_F(WriteOpTest, TargetSingle) { MockNSTargeter targeter(kNss, {MockRange(endpoint, BSON("x" << MINKEY), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 1u); @@ -154,8 +153,7 @@ TEST_F(WriteOpTest, TargetMultiOneShard) { MockRange(endpointB, BSON("x" << 0), BSON("x" << 10)), MockRange(endpointC, BSON("x" << 10), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 1u); @@ -190,8 +188,7 @@ TEST_F(WriteOpTest, TargetMultiAllShards) { MockRange(endpointB, BSON("x" << 0), BSON("x" << 10)), MockRange(endpointC, BSON("x" << 10), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 3u); @@ -230,8 +227,7 @@ TEST_F(WriteOpTest, TargetMultiAllShardsAndErrorSingleChildOp) { {MockRange(endpointA, BSON("x" << MINKEY), BSON("x" << 0)), MockRange(endpointB, BSON("x" << 0), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 2u); @@ -272,8 +268,7 @@ TEST_F(WriteOpTest, ErrorSingle) { MockNSTargeter targeter(kNss, {MockRange(endpoint, BSON("x" << MINKEY), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 1u); @@ -306,8 +301,7 @@ TEST_F(WriteOpTest, CancelSingle) { MockNSTargeter targeter(kNss, {MockRange(endpoint, BSON("x" << MINKEY), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 1u); @@ -339,8 +333,7 @@ TEST_F(WriteOpTest, RetrySingleOp) { MockNSTargeter targeter(kNss, {MockRange(endpoint, BSON("x" << MINKEY), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 1u); @@ -385,8 +378,7 @@ TEST_F(WriteOpTransactionTest, TargetMultiDoesNotTargetAllShards) { MockRange(endpointB, BSON("x" << 0), BSON("x" << 10)), MockRange(endpointC, BSON("x" << 10), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); // The write should only target shardA and shardB and send real shard versions to each. @@ -431,8 +423,7 @@ TEST_F(WriteOpTransactionTest, TargetMultiAllShardsAndErrorSingleChildOp) { {MockRange(endpointA, BSON("x" << MINKEY), BSON("x" << 0)), MockRange(endpointB, BSON("x" << 0), BSON("x" << MAXKEY))}); - OwnedPointerVector<TargetedWrite> targetedOwned; - std::vector<TargetedWrite*>& targeted = targetedOwned.mutableVector(); + std::vector<std::unique_ptr<TargetedWrite>> targeted; writeOp.targetWrites(_opCtx, targeter, &targeted); ASSERT_EQUALS(writeOp.getWriteState(), WriteOpState_Pending); ASSERT_EQUALS(targeted.size(), 2u); |