diff options
Diffstat (limited to 'src/mongo/db/write_concern_options.cpp')
-rw-r--r-- | src/mongo/db/write_concern_options.cpp | 251 |
1 files changed, 111 insertions, 140 deletions
diff --git a/src/mongo/db/write_concern_options.cpp b/src/mongo/db/write_concern_options.cpp index 8f0c8e9c25a..5fba6321b5e 100644 --- a/src/mongo/db/write_concern_options.cpp +++ b/src/mongo/db/write_concern_options.cpp @@ -32,180 +32,151 @@ namespace mongo { - using std::string; +using std::string; namespace { - /** - * Controls how much a client cares about writes and serves as initializer for the pre-defined - * write concern options. - * - * Default is NORMAL. - */ - enum WriteConcern { - W_NONE = 0 , - W_NORMAL = 1 - }; - - const BSONField<bool> mongosSecondaryThrottleField("_secondaryThrottle", true); - const BSONField<bool> secondaryThrottleField("secondaryThrottle", true); - const BSONField<BSONObj> writeConcernField("writeConcern"); - -} // namespace - - const char WriteConcernOptions::kMajority[] = "majority"; - - const BSONObj WriteConcernOptions::Default = BSONObj(); - const BSONObj WriteConcernOptions::Acknowledged(BSON("w" << W_NORMAL)); - const BSONObj WriteConcernOptions::Unacknowledged(BSON("w" << W_NONE)); - const BSONObj WriteConcernOptions::Majority(BSON("w" << WriteConcernOptions::kMajority)); - - - WriteConcernOptions::WriteConcernOptions(int numNodes, - SyncMode sync, - int timeout): - syncMode(sync), - wNumNodes(numNodes), - wTimeout(timeout) { - } +/** + * Controls how much a client cares about writes and serves as initializer for the pre-defined + * write concern options. + * + * Default is NORMAL. + */ +enum WriteConcern { W_NONE = 0, W_NORMAL = 1 }; - WriteConcernOptions::WriteConcernOptions(const std::string& mode, - SyncMode sync, - int timeout): - syncMode(sync), - wNumNodes(0), - wMode(mode), - wTimeout(timeout) { - } +const BSONField<bool> mongosSecondaryThrottleField("_secondaryThrottle", true); +const BSONField<bool> secondaryThrottleField("secondaryThrottle", true); +const BSONField<BSONObj> writeConcernField("writeConcern"); - Status WriteConcernOptions::parse( const BSONObj& obj ) { - if ( obj.isEmpty() ) { - return Status( ErrorCodes::FailedToParse, "write concern object cannot be empty" ); - } +} // namespace - BSONElement jEl = obj["j"]; - if ( !jEl.eoo() && !jEl.isNumber() && jEl.type() != Bool ) { - return Status( ErrorCodes::FailedToParse, "j must be numeric or a boolean value" ); - } +const char WriteConcernOptions::kMajority[] = "majority"; - const bool j = jEl.trueValue(); +const BSONObj WriteConcernOptions::Default = BSONObj(); +const BSONObj WriteConcernOptions::Acknowledged(BSON("w" << W_NORMAL)); +const BSONObj WriteConcernOptions::Unacknowledged(BSON("w" << W_NONE)); +const BSONObj WriteConcernOptions::Majority(BSON("w" << WriteConcernOptions::kMajority)); - BSONElement fsyncEl = obj["fsync"]; - if ( !fsyncEl.eoo() && !fsyncEl.isNumber() && fsyncEl.type() != Bool ) { - return Status( ErrorCodes::FailedToParse, "fsync must be numeric or a boolean value" ); - } - const bool fsync = fsyncEl.trueValue(); +WriteConcernOptions::WriteConcernOptions(int numNodes, SyncMode sync, int timeout) + : syncMode(sync), wNumNodes(numNodes), wTimeout(timeout) {} - if ( j && fsync ) - return Status( ErrorCodes::FailedToParse, - "fsync and j options cannot be used together" ); +WriteConcernOptions::WriteConcernOptions(const std::string& mode, SyncMode sync, int timeout) + : syncMode(sync), wNumNodes(0), wMode(mode), wTimeout(timeout) {} - if ( j ) { - syncMode = JOURNAL; - } - if ( fsync ) { - syncMode = FSYNC; - } +Status WriteConcernOptions::parse(const BSONObj& obj) { + if (obj.isEmpty()) { + return Status(ErrorCodes::FailedToParse, "write concern object cannot be empty"); + } - BSONElement e = obj["w"]; - if ( e.isNumber() ) { - wNumNodes = e.numberInt(); - } - else if ( e.type() == String ) { - wMode = e.valuestrsafe(); - } - else if ( e.eoo() || - e.type() == jstNULL || - e.type() == Undefined ) { - wNumNodes = 1; - } - else { - return Status( ErrorCodes::FailedToParse, "w has to be a number or a string" ); - } + BSONElement jEl = obj["j"]; + if (!jEl.eoo() && !jEl.isNumber() && jEl.type() != Bool) { + return Status(ErrorCodes::FailedToParse, "j must be numeric or a boolean value"); + } - wTimeout = obj["wtimeout"].numberInt(); + const bool j = jEl.trueValue(); - return Status::OK(); + BSONElement fsyncEl = obj["fsync"]; + if (!fsyncEl.eoo() && !fsyncEl.isNumber() && fsyncEl.type() != Bool) { + return Status(ErrorCodes::FailedToParse, "fsync must be numeric or a boolean value"); } - Status WriteConcernOptions::parseSecondaryThrottle(const BSONObj& doc, - BSONObj* rawWriteConcernObj) { - string errMsg; - bool isSecondaryThrottle; - FieldParser::FieldState fieldState = FieldParser::extract(doc, - secondaryThrottleField, - &isSecondaryThrottle, - &errMsg); - if (fieldState == FieldParser::FIELD_INVALID) { - return Status(ErrorCodes::FailedToParse, errMsg); - } + const bool fsync = fsyncEl.trueValue(); - if (fieldState != FieldParser::FIELD_SET) { - fieldState = FieldParser::extract(doc, - mongosSecondaryThrottleField, - &isSecondaryThrottle, - &errMsg); + if (j && fsync) + return Status(ErrorCodes::FailedToParse, "fsync and j options cannot be used together"); - if (fieldState == FieldParser::FIELD_INVALID) { - return Status(ErrorCodes::FailedToParse, errMsg); - } - } + if (j) { + syncMode = JOURNAL; + } + if (fsync) { + syncMode = FSYNC; + } - BSONObj dummyBSON; - if (!rawWriteConcernObj) { - rawWriteConcernObj = &dummyBSON; - } + BSONElement e = obj["w"]; + if (e.isNumber()) { + wNumNodes = e.numberInt(); + } else if (e.type() == String) { + wMode = e.valuestrsafe(); + } else if (e.eoo() || e.type() == jstNULL || e.type() == Undefined) { + wNumNodes = 1; + } else { + return Status(ErrorCodes::FailedToParse, "w has to be a number or a string"); + } + + wTimeout = obj["wtimeout"].numberInt(); + + return Status::OK(); +} + +Status WriteConcernOptions::parseSecondaryThrottle(const BSONObj& doc, + BSONObj* rawWriteConcernObj) { + string errMsg; + bool isSecondaryThrottle; + FieldParser::FieldState fieldState = + FieldParser::extract(doc, secondaryThrottleField, &isSecondaryThrottle, &errMsg); + if (fieldState == FieldParser::FIELD_INVALID) { + return Status(ErrorCodes::FailedToParse, errMsg); + } + + if (fieldState != FieldParser::FIELD_SET) { + fieldState = + FieldParser::extract(doc, mongosSecondaryThrottleField, &isSecondaryThrottle, &errMsg); - fieldState = FieldParser::extract(doc, - writeConcernField, - rawWriteConcernObj, - &errMsg); if (fieldState == FieldParser::FIELD_INVALID) { return Status(ErrorCodes::FailedToParse, errMsg); } + } - if (!isSecondaryThrottle) { - if (!rawWriteConcernObj->isEmpty()) { - return Status(ErrorCodes::UnsupportedFormat, - "Cannot have write concern when secondary throttle is false"); - } + BSONObj dummyBSON; + if (!rawWriteConcernObj) { + rawWriteConcernObj = &dummyBSON; + } - wNumNodes = 1; - return Status::OK(); - } + fieldState = FieldParser::extract(doc, writeConcernField, rawWriteConcernObj, &errMsg); + if (fieldState == FieldParser::FIELD_INVALID) { + return Status(ErrorCodes::FailedToParse, errMsg); + } - if (rawWriteConcernObj->isEmpty()) { - return Status(ErrorCodes::WriteConcernNotDefined, - "Secondary throttle is on, but write concern is not specified"); + if (!isSecondaryThrottle) { + if (!rawWriteConcernObj->isEmpty()) { + return Status(ErrorCodes::UnsupportedFormat, + "Cannot have write concern when secondary throttle is false"); } - return parse(*rawWriteConcernObj); + wNumNodes = 1; + return Status::OK(); } - BSONObj WriteConcernOptions::toBSON() const { - BSONObjBuilder builder; - - if (wMode.empty()) { - builder.append("w", wNumNodes); - } - else { - builder.append("w", wMode); - } + if (rawWriteConcernObj->isEmpty()) { + return Status(ErrorCodes::WriteConcernNotDefined, + "Secondary throttle is on, but write concern is not specified"); + } - if (syncMode == FSYNC) { - builder.append("fsync", true); - } - else if (syncMode == JOURNAL) { - builder.append("j", true); - } + return parse(*rawWriteConcernObj); +} - builder.append("wtimeout", wTimeout); +BSONObj WriteConcernOptions::toBSON() const { + BSONObjBuilder builder; - return builder.obj(); + if (wMode.empty()) { + builder.append("w", wNumNodes); + } else { + builder.append("w", wMode); } - bool WriteConcernOptions::shouldWaitForOtherNodes() const { - return !wMode.empty() || wNumNodes > 1; + if (syncMode == FSYNC) { + builder.append("fsync", true); + } else if (syncMode == JOURNAL) { + builder.append("j", true); } + + builder.append("wtimeout", wTimeout); + + return builder.obj(); +} + +bool WriteConcernOptions::shouldWaitForOtherNodes() const { + return !wMode.empty() || wNumNodes > 1; +} } |