diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 00:22:50 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 10:56:02 -0400 |
commit | 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch) | |
tree | 3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/client/read_preference.cpp | |
parent | 01965cf52bce6976637ecb8f4a622aeb05ab256a (diff) | |
download | mongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz |
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/client/read_preference.cpp')
-rw-r--r-- | src/mongo/client/read_preference.cpp | 199 |
1 files changed, 93 insertions, 106 deletions
diff --git a/src/mongo/client/read_preference.cpp b/src/mongo/client/read_preference.cpp index e088504fe15..2a1ee9d6420 100644 --- a/src/mongo/client/read_preference.cpp +++ b/src/mongo/client/read_preference.cpp @@ -44,19 +44,19 @@ namespace mongo { namespace { - const BSONArray tagsMatchesAll = BSON_ARRAY(BSONObj()); +const BSONArray tagsMatchesAll = BSON_ARRAY(BSONObj()); - const char kModeFieldName[] = "mode"; - const char kTagsFieldName[] = "tags"; +const char kModeFieldName[] = "mode"; +const char kTagsFieldName[] = "tags"; - const char kPrimaryOnly[] = "primary"; - const char kPrimaryPreferred[] = "primaryPreferred"; - const char kSecondaryOnly[] = "secondary"; - const char kSecondaryPreferred[] = "secondaryPreferred"; - const char kNearest[] = "nearest"; +const char kPrimaryOnly[] = "primary"; +const char kPrimaryPreferred[] = "primaryPreferred"; +const char kSecondaryOnly[] = "secondary"; +const char kSecondaryPreferred[] = "secondaryPreferred"; +const char kNearest[] = "nearest"; - StringData readPreferenceName(ReadPreference pref) { - switch (pref) { +StringData readPreferenceName(ReadPreference pref) { + switch (pref) { case ReadPreference::PrimaryOnly: return StringData(kPrimaryOnly); case ReadPreference::PrimaryPreferred: @@ -69,123 +69,110 @@ namespace { return StringData(kNearest); default: MONGO_UNREACHABLE; - } } - - StatusWith<ReadPreference> parseReadPreferenceMode(StringData prefStr) { - if (prefStr == kPrimaryOnly) { - return ReadPreference::PrimaryOnly; - } - else if (prefStr == kPrimaryPreferred) { - return ReadPreference::PrimaryPreferred; - } - else if (prefStr == kSecondaryOnly) { - return ReadPreference::SecondaryOnly; - } - else if (prefStr == kSecondaryPreferred) { - return ReadPreference::SecondaryPreferred; - } - else if (prefStr == kNearest) { - return ReadPreference::Nearest; - } - return Status(ErrorCodes::FailedToParse, - str::stream() << "Could not parse $readPreference mode '" - << prefStr << "'. Only the modes '" - << kPrimaryOnly << "', '" - << kPrimaryPreferred << "', " - << kSecondaryOnly << "', '" - << kSecondaryPreferred << "', and '" - << kNearest << "' are supported."); +} + +StatusWith<ReadPreference> parseReadPreferenceMode(StringData prefStr) { + if (prefStr == kPrimaryOnly) { + return ReadPreference::PrimaryOnly; + } else if (prefStr == kPrimaryPreferred) { + return ReadPreference::PrimaryPreferred; + } else if (prefStr == kSecondaryOnly) { + return ReadPreference::SecondaryOnly; + } else if (prefStr == kSecondaryPreferred) { + return ReadPreference::SecondaryPreferred; + } else if (prefStr == kNearest) { + return ReadPreference::Nearest; } - - // Slight kludge here: if we weren't passed a TagSet, we default to the empty - // TagSet if ReadPreference is Primary, or the default (wildcard) TagSet otherwise. - // This maintains compatibility with existing code, while preserving the ability to round - // trip. - TagSet defaultTagSetForMode(ReadPreference mode) { - switch (mode) { + return Status(ErrorCodes::FailedToParse, + str::stream() << "Could not parse $readPreference mode '" << prefStr + << "'. Only the modes '" << kPrimaryOnly << "', '" + << kPrimaryPreferred << "', " << kSecondaryOnly << "', '" + << kSecondaryPreferred << "', and '" << kNearest + << "' are supported."); +} + +// Slight kludge here: if we weren't passed a TagSet, we default to the empty +// TagSet if ReadPreference is Primary, or the default (wildcard) TagSet otherwise. +// This maintains compatibility with existing code, while preserving the ability to round +// trip. +TagSet defaultTagSetForMode(ReadPreference mode) { + switch (mode) { case ReadPreference::PrimaryOnly: return TagSet::primaryOnly(); default: return TagSet(); - } } +} } // namespace - TagSet::TagSet() : _tags(tagsMatchesAll) {} +TagSet::TagSet() : _tags(tagsMatchesAll) {} - TagSet TagSet::primaryOnly() { - return TagSet{BSONArray()}; - } +TagSet TagSet::primaryOnly() { + return TagSet{BSONArray()}; +} - ReadPreferenceSetting::ReadPreferenceSetting(ReadPreference pref, TagSet tags) - : pref(std::move(pref)) - , tags(std::move(tags)) - {} - - StatusWith<ReadPreferenceSetting> ReadPreferenceSetting::fromBSON(const BSONObj& readPrefObj) { - std::string modeStr; - auto modeExtractStatus = bsonExtractStringField(readPrefObj, - kModeFieldName, - &modeStr); - if (!modeExtractStatus.isOK()) { - return modeExtractStatus; - } +ReadPreferenceSetting::ReadPreferenceSetting(ReadPreference pref, TagSet tags) + : pref(std::move(pref)), tags(std::move(tags)) {} - ReadPreference mode; - auto swReadPrefMode = parseReadPreferenceMode(modeStr); - if (!swReadPrefMode.isOK()) { - return swReadPrefMode.getStatus(); - } - mode = std::move(swReadPrefMode.getValue()); - - TagSet tags; - BSONElement tagsElem; - auto tagExtractStatus = bsonExtractTypedField(readPrefObj, - kTagsFieldName, - mongo::Array, - &tagsElem); - if (tagExtractStatus.isOK()) { - tags = TagSet{BSONArray(tagsElem.Obj().getOwned())}; - - // In accordance with the read preference spec, passing the default wildcard tagset - // '[{}]' is the same as not passing a TagSet at all. Furthermore, passing an empty - // TagSet with a non-primary ReadPreference is equivalent to passing the wildcard - // ReadPreference. - if (tags == TagSet() || tags == TagSet::primaryOnly()) { - tags = defaultTagSetForMode(mode); - } - - // If we are using a user supplied TagSet, check that it is compatible with - // the readPreference mode. - else if (ReadPreference::PrimaryOnly == mode && (tags != TagSet::primaryOnly())) { - return Status(ErrorCodes::BadValue, - "Only empty tags are allowed with primary read preference"); - } - } +StatusWith<ReadPreferenceSetting> ReadPreferenceSetting::fromBSON(const BSONObj& readPrefObj) { + std::string modeStr; + auto modeExtractStatus = bsonExtractStringField(readPrefObj, kModeFieldName, &modeStr); + if (!modeExtractStatus.isOK()) { + return modeExtractStatus; + } - else if (ErrorCodes::NoSuchKey == tagExtractStatus) { + ReadPreference mode; + auto swReadPrefMode = parseReadPreferenceMode(modeStr); + if (!swReadPrefMode.isOK()) { + return swReadPrefMode.getStatus(); + } + mode = std::move(swReadPrefMode.getValue()); + + TagSet tags; + BSONElement tagsElem; + auto tagExtractStatus = + bsonExtractTypedField(readPrefObj, kTagsFieldName, mongo::Array, &tagsElem); + if (tagExtractStatus.isOK()) { + tags = TagSet{BSONArray(tagsElem.Obj().getOwned())}; + + // In accordance with the read preference spec, passing the default wildcard tagset + // '[{}]' is the same as not passing a TagSet at all. Furthermore, passing an empty + // TagSet with a non-primary ReadPreference is equivalent to passing the wildcard + // ReadPreference. + if (tags == TagSet() || tags == TagSet::primaryOnly()) { tags = defaultTagSetForMode(mode); } - else { - return tagExtractStatus; - } - return ReadPreferenceSetting(mode, tags); + // If we are using a user supplied TagSet, check that it is compatible with + // the readPreference mode. + else if (ReadPreference::PrimaryOnly == mode && (tags != TagSet::primaryOnly())) { + return Status(ErrorCodes::BadValue, + "Only empty tags are allowed with primary read preference"); + } } - BSONObj ReadPreferenceSetting::toBSON() const { - BSONObjBuilder bob; - bob.append(kModeFieldName, readPreferenceName(pref)); - if (tags != defaultTagSetForMode(pref)) { - bob.append(kTagsFieldName, tags.getTagBSON()); - } - return bob.obj(); + else if (ErrorCodes::NoSuchKey == tagExtractStatus) { + tags = defaultTagSetForMode(mode); + } else { + return tagExtractStatus; } - std::string ReadPreferenceSetting::toString() const { - return toBSON().toString(); + return ReadPreferenceSetting(mode, tags); +} + +BSONObj ReadPreferenceSetting::toBSON() const { + BSONObjBuilder bob; + bob.append(kModeFieldName, readPreferenceName(pref)); + if (tags != defaultTagSetForMode(pref)) { + bob.append(kTagsFieldName, tags.getTagBSON()); } + return bob.obj(); +} + +std::string ReadPreferenceSetting::toString() const { + return toBSON().toString(); +} } // namespace mongo |