From 90fa214a651f42185ae660091f599edf3bb422e4 Mon Sep 17 00:00:00 2001 From: Billy Donahue Date: Mon, 22 Jan 2018 18:24:04 -0500 Subject: SERVER-32846 NamespaceString: add and remove relational operators relops for NamespaceString fix client sites relying on NamespaceString vs string --- src/mongo/db/catalog/collection_impl.cpp | 4 +-- src/mongo/db/catalog/database_impl.cpp | 2 +- src/mongo/db/namespace_string.h | 41 +++++++++++----------- src/mongo/db/query/query_yield.cpp | 2 +- src/mongo/db/repl/oplog.cpp | 4 +-- src/mongo/db/s/collection_sharding_state.cpp | 8 ++--- src/mongo/db/s/shard_metadata_util_test.cpp | 2 +- src/mongo/dbtests/storage_timestamp_tests.cpp | 2 +- ...rding_catalog_assign_key_range_to_zone_test.cpp | 6 ++-- src/mongo/s/client/parallel.cpp | 2 +- src/mongo/s/move_chunk_request_test.cpp | 2 +- 11 files changed, 37 insertions(+), 38 deletions(-) (limited to 'src/mongo') diff --git a/src/mongo/db/catalog/collection_impl.cpp b/src/mongo/db/catalog/collection_impl.cpp index eab807fbf82..ff2989fa218 100644 --- a/src/mongo/db/catalog/collection_impl.cpp +++ b/src/mongo/db/catalog/collection_impl.cpp @@ -335,7 +335,7 @@ Status CollectionImpl::insertDocuments(OperationContext* opCtx, const BSONObj& data = extraData.getData(); const auto collElem = data["collectionNS"]; // If the failpoint specifies no collection or matches the existing one, fail. - if (!collElem || _ns == collElem.str()) { + if (!collElem || _ns.ns() == collElem.str()) { const std::string msg = str::stream() << "Failpoint (failCollectionInserts) has been enabled (" << data << "), so rejecting insert (first doc): " << begin->doc; @@ -394,7 +394,7 @@ Status CollectionImpl::insertDocument(OperationContext* opCtx, const BSONObj& data = extraData.getData(); const auto collElem = data["collectionNS"]; // If the failpoint specifies no collection or matches the existing one, fail. - if (!collElem || _ns == collElem.str()) { + if (!collElem || _ns.ns() == collElem.str()) { const std::string msg = str::stream() << "Failpoint (failCollectionInserts) has been enabled (" << data << "), so rejecting insert: " << doc; diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp index b8b5eba740f..781acd85b14 100644 --- a/src/mongo/db/catalog/database_impl.cpp +++ b/src/mongo/db/catalog/database_impl.cpp @@ -456,7 +456,7 @@ Status DatabaseImpl::dropCollection(OperationContext* opCtx, "turn off profiling before dropping system.profile collection"); } else if (!(nss.isSystemDotViews() || nss.isHealthlog() || nss == SessionsCollection::kSessionsNamespaceString || - nss == NamespaceString::kSystemKeysCollectionName)) { + nss.ns() == NamespaceString::kSystemKeysCollectionName)) { return Status(ErrorCodes::IllegalOperation, str::stream() << "can't drop system collection " << fullns); } diff --git a/src/mongo/db/namespace_string.h b/src/mongo/db/namespace_string.h index 21be2b4c133..2bc60d7ef97 100644 --- a/src/mongo/db/namespace_string.h +++ b/src/mongo/db/namespace_string.h @@ -293,27 +293,6 @@ public: return validDBName(db(), DollarInDbNameBehavior::Allow) && !coll().empty(); } - bool operator==(const std::string& nsIn) const { - return nsIn == _ns; - } - bool operator==(StringData nsIn) const { - return nsIn == _ns; - } - bool operator==(const NamespaceString& nsIn) const { - return nsIn._ns == _ns; - } - - bool operator!=(const std::string& nsIn) const { - return nsIn != _ns; - } - bool operator!=(const NamespaceString& nsIn) const { - return nsIn._ns != _ns; - } - - bool operator<(const NamespaceString& rhs) const { - return _ns < rhs._ns; - } - /** ( foo.bar ).getSisterNS( "blah" ) == foo.blah */ std::string getSisterNS(StringData local) const; @@ -400,6 +379,26 @@ public: */ static bool validCollectionName(StringData coll); + // Relops among `NamespaceString`. + friend bool operator==(const NamespaceString& a, const NamespaceString& b) { + return a.ns() == b.ns(); + } + friend bool operator!=(const NamespaceString& a, const NamespaceString& b) { + return a.ns() != b.ns(); + } + friend bool operator<(const NamespaceString& a, const NamespaceString& b) { + return a.ns() < b.ns(); + } + friend bool operator>(const NamespaceString& a, const NamespaceString& b) { + return a.ns() > b.ns(); + } + friend bool operator<=(const NamespaceString& a, const NamespaceString& b) { + return a.ns() <= b.ns(); + } + friend bool operator>=(const NamespaceString& a, const NamespaceString& b) { + return a.ns() >= b.ns(); + } + private: std::string _ns; size_t _dotIndex; diff --git a/src/mongo/db/query/query_yield.cpp b/src/mongo/db/query/query_yield.cpp index e731383a92d..68df4a0cb52 100644 --- a/src/mongo/db/query/query_yield.cpp +++ b/src/mongo/db/query/query_yield.cpp @@ -74,7 +74,7 @@ void QueryYield::yieldAllLocks(OperationContext* opCtx, MONGO_FAIL_POINT_BLOCK(setYieldAllLocksWait, customWait) { const BSONObj& data = customWait.getData(); BSONElement customWaitNS = data["namespace"]; - if (!customWaitNS || planExecNS == customWaitNS.str()) { + if (!customWaitNS || planExecNS.ns() == customWaitNS.str()) { sleepFor(Milliseconds(data["waitForMillis"].numberInt())); } } diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index 0deb0def76f..4d52075cffa 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -1073,7 +1073,7 @@ Status applyOperation_inlock(OperationContext* opCtx, // During upgrade from 3.4 to 3.6, the feature compatibility version cannot change during // initial sync because we cannot do some operations with UUIDs and others without. if ((mode == OplogApplication::Mode::kInitialSync) && - requestNss == FeatureCompatibilityVersion::kCollection) { + requestNss.ns() == FeatureCompatibilityVersion::kCollection) { std::string oID; auto status = bsonExtractStringField(o, "_id", &oID); if (status.isOK() && oID == FeatureCompatibilityVersion::kParameterName) { @@ -1526,7 +1526,7 @@ Status applyCommand_inlock(OperationContext* opCtx, if ((mode == OplogApplication::Mode::kInitialSync) && (std::find(whitelistedOps.begin(), whitelistedOps.end(), o.firstElementFieldName()) == whitelistedOps.end()) && - parseNs(nss.ns(), o) == FeatureCompatibilityVersion::kCollection) { + parseNs(nss.ns(), o).ns() == FeatureCompatibilityVersion::kCollection) { return Status(ErrorCodes::OplogOperationUnsupported, str::stream() << "Applying command to feature compatibility version " "collection not supported in initial sync: " diff --git a/src/mongo/db/s/collection_sharding_state.cpp b/src/mongo/db/s/collection_sharding_state.cpp index 9b554195cd7..9b4d84756aa 100644 --- a/src/mongo/db/s/collection_sharding_state.cpp +++ b/src/mongo/db/s/collection_sharding_state.cpp @@ -320,7 +320,7 @@ void CollectionShardingState::onUpdateOp(OperationContext* opCtx, dassert(opCtx->lockState()->isCollectionLockedForMode(_nss.ns(), MODE_IX)); if (serverGlobalParams.clusterRole == ClusterRole::ShardServer) { - if (_nss == NamespaceString::kShardConfigCollectionsCollectionName) { + if (_nss.ns() == NamespaceString::kShardConfigCollectionsCollectionName) { _onConfigCollectionsUpdateOp(opCtx, query, update, updatedDoc); } @@ -348,7 +348,7 @@ void CollectionShardingState::onDeleteOp(OperationContext* opCtx, dassert(opCtx->lockState()->isCollectionLockedForMode(_nss.ns(), MODE_IX)); if (serverGlobalParams.clusterRole == ClusterRole::ShardServer) { - if (_nss == NamespaceString::kShardConfigCollectionsCollectionName) { + if (_nss.ns() == NamespaceString::kShardConfigCollectionsCollectionName) { _onConfigDeleteInvalidateCachedMetadataAndNotify(opCtx, deleteState.documentKey); } @@ -370,7 +370,7 @@ void CollectionShardingState::onDeleteOp(OperationContext* opCtx, } if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) { - if (_nss == VersionType::ConfigNS) { + if (_nss.ns() == VersionType::ConfigNS) { if (!repl::ReplicationCoordinator::get(opCtx)->getMemberState().rollback()) { uasserted(40302, "cannot delete config.version document while in --configsvr mode"); } else { @@ -407,7 +407,7 @@ void CollectionShardingState::onDropCollection(OperationContext* opCtx, } if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) { - if (_nss == VersionType::ConfigNS) { + if (_nss.ns() == VersionType::ConfigNS) { if (!repl::ReplicationCoordinator::get(opCtx)->getMemberState().rollback()) { uasserted(40303, "cannot drop config.version document while in --configsvr mode"); } else { diff --git a/src/mongo/db/s/shard_metadata_util_test.cpp b/src/mongo/db/s/shard_metadata_util_test.cpp index fed6991ed32..fa86f74a4ec 100644 --- a/src/mongo/db/s/shard_metadata_util_test.cpp +++ b/src/mongo/db/s/shard_metadata_util_test.cpp @@ -201,7 +201,7 @@ TEST_F(ShardMetadataUtilTest, PersistedRefreshSignalStartAndFinish) { assertGet(readShardCollectionsEntry(operationContext(), kNss)); ASSERT_EQUALS(*shardCollectionsEntry.getUUID(), uuid); - ASSERT_EQUALS(shardCollectionsEntry.getNss(), kNss.ns()); + ASSERT_EQUALS(shardCollectionsEntry.getNss().ns(), kNss.ns()); ASSERT_EQUALS(shardCollectionsEntry.getEpoch(), maxCollVersion.epoch()); ASSERT_BSONOBJ_EQ(shardCollectionsEntry.getKeyPattern().toBSON(), keyPattern.toBSON()); ASSERT_BSONOBJ_EQ(shardCollectionsEntry.getDefaultCollation(), defaultCollation); diff --git a/src/mongo/dbtests/storage_timestamp_tests.cpp b/src/mongo/dbtests/storage_timestamp_tests.cpp index 352b04051bf..d30157e4d4e 100644 --- a/src/mongo/dbtests/storage_timestamp_tests.cpp +++ b/src/mongo/dbtests/storage_timestamp_tests.cpp @@ -1075,7 +1075,7 @@ public: // The next logOp() call will get 'futureTs', which will be the timestamp at which we do // the write. Thus we expect the write to appear at 'futureTs' and not before. ASSERT_EQ(op.getTimestamp(), futureTs) << op.toBSON(); - ASSERT_EQ(op.getNamespace(), nss.getCommandNS().ns()) << op.toBSON(); + ASSERT_EQ(op.getNamespace().ns(), nss.getCommandNS().ns()) << op.toBSON(); ASSERT_BSONOBJ_EQ(op.getObject(), BSON("create" << nss.coll())); assertNamespaceInIdents(_opCtx, nss, pastTs, false); diff --git a/src/mongo/s/catalog/sharding_catalog_assign_key_range_to_zone_test.cpp b/src/mongo/s/catalog/sharding_catalog_assign_key_range_to_zone_test.cpp index f4ff94429c7..50ff6368f2a 100644 --- a/src/mongo/s/catalog/sharding_catalog_assign_key_range_to_zone_test.cpp +++ b/src/mongo/s/catalog/sharding_catalog_assign_key_range_to_zone_test.cpp @@ -103,7 +103,7 @@ public: ASSERT_OK(tagDocStatus.getStatus()); auto tagDoc = tagDocStatus.getValue(); - ASSERT_EQ(ns, tagDoc.getNS()); + ASSERT_EQ(ns.ns(), tagDoc.getNS()); ASSERT_BSONOBJ_EQ(range.getMin(), tagDoc.getMinKey()); ASSERT_BSONOBJ_EQ(range.getMax(), tagDoc.getMaxKey()); ASSERT_EQ(zoneName, tagDoc.getTag()); @@ -655,7 +655,7 @@ TEST_F(AssignKeyRangeWithOneRangeFixture, RemoveThatIsOnlyMinPrefixOfExistingSho ASSERT_OK(tagDocStatus.getStatus()); auto tagDoc = tagDocStatus.getValue(); - ASSERT_EQ(ns, tagDoc.getNS()); + ASSERT_EQ(ns.ns(), tagDoc.getNS()); ASSERT_BSONOBJ_EQ(existingRange.getMin(), tagDoc.getMinKey()); ASSERT_BSONOBJ_EQ(existingRange.getMax(), tagDoc.getMaxKey()); ASSERT_EQ(zoneName(), tagDoc.getTag()); @@ -709,7 +709,7 @@ TEST_F(AssignKeyRangeWithOneRangeFixture, RemoveThatIsOnlyMaxPrefixOfExistingSho ASSERT_OK(tagDocStatus.getStatus()); auto tagDoc = tagDocStatus.getValue(); - ASSERT_EQ(ns, tagDoc.getNS()); + ASSERT_EQ(ns.ns(), tagDoc.getNS()); ASSERT_BSONOBJ_EQ(existingRange.getMin(), tagDoc.getMinKey()); ASSERT_BSONOBJ_EQ(existingRange.getMax(), tagDoc.getMaxKey()); ASSERT_EQ(zoneName(), tagDoc.getTag()); diff --git a/src/mongo/s/client/parallel.cpp b/src/mongo/s/client/parallel.cpp index 78dbb885fd6..7c56b69c053 100644 --- a/src/mongo/s/client/parallel.cpp +++ b/src/mongo/s/client/parallel.cpp @@ -838,7 +838,7 @@ void ParallelSortClusteredCursor::finishInit(OperationContext* opCtx) { << causedBy(redact(ex)); // This is somewhat strange - if (staleNS != ns) { + if (staleNS.ns() != ns) { warning() << "versioned ns " << ns << " doesn't match stale config namespace " << staleNS; } diff --git a/src/mongo/s/move_chunk_request_test.cpp b/src/mongo/s/move_chunk_request_test.cpp index 5b51e720934..79ee1727ba9 100644 --- a/src/mongo/s/move_chunk_request_test.cpp +++ b/src/mongo/s/move_chunk_request_test.cpp @@ -71,7 +71,7 @@ TEST(MoveChunkRequest, Roundtrip) { auto request = assertGet( MoveChunkRequest::createFromCommand(NamespaceString(cmdObj["moveChunk"].String()), cmdObj)); - ASSERT_EQ(kNs, request.getNss().ns()); + ASSERT_EQ(kNs.ns(), request.getNss().ns()); ASSERT_EQ(kFromShard, request.getFromShardId()); ASSERT_EQ(kToShard, request.getToShardId()); ASSERT_BSONOBJ_EQ(kMin, request.getMinKey()); -- cgit v1.2.1