diff options
author | matt dannenberg <matt.dannenberg@10gen.com> | 2015-08-07 06:36:44 -0400 |
---|---|---|
committer | matt dannenberg <matt.dannenberg@10gen.com> | 2015-08-12 04:22:28 -0400 |
commit | 86a3e6352eb27fd2e6115299bcec5103a830fe36 (patch) | |
tree | 7f7f42491808e8c487e47a710deb445448d18a58 /src/mongo/bson/util | |
parent | 7bb09c0377f5160857617c38ab07955f8f4b03f6 (diff) | |
download | mongo-86a3e6352eb27fd2e6115299bcec5103a830fe36.tar.gz |
SERVER-19554 merge ReplSetMetadata and ReplicationMetadata
Diffstat (limited to 'src/mongo/bson/util')
-rw-r--r-- | src/mongo/bson/util/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/bson/util/bson_extract.cpp | 26 | ||||
-rw-r--r-- | src/mongo/bson/util/bson_extract.h | 15 | ||||
-rw-r--r-- | src/mongo/bson/util/bson_extract_test.cpp | 28 |
4 files changed, 71 insertions, 0 deletions
diff --git a/src/mongo/bson/util/SConscript b/src/mongo/bson/util/SConscript index 6c8ce51e012..6aa6c1476c8 100644 --- a/src/mongo/bson/util/SConscript +++ b/src/mongo/bson/util/SConscript @@ -9,6 +9,7 @@ env.Library( ], LIBDEPS=[ '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/repl/optime', ], ) @@ -19,6 +20,7 @@ env.CppUnitTest( ], LIBDEPS=[ '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/repl/optime', 'bson_extract', ], ) diff --git a/src/mongo/bson/util/bson_extract.cpp b/src/mongo/bson/util/bson_extract.cpp index d5b5b83e6da..4906d0897a5 100644 --- a/src/mongo/bson/util/bson_extract.cpp +++ b/src/mongo/bson/util/bson_extract.cpp @@ -32,6 +32,13 @@ namespace mongo { +namespace { + +const char kTermFieldName[] = "term"; +const char kTimestampFieldName[] = "ts"; + +} // namespace + Status bsonExtractField(const BSONObj& object, StringData fieldName, BSONElement* outElement) { BSONElement element = object.getField(fieldName); if (element.eoo()) @@ -98,6 +105,25 @@ Status bsonExtractStringField(const BSONObj& object, StringData fieldName, std:: return Status::OK(); } +Status bsonExtractOpTimeField(const BSONObj& object, StringData fieldName, repl::OpTime* out) { + BSONElement element; + Status status = bsonExtractTypedField(object, fieldName, Object, &element); + if (!status.isOK()) + return status; + + BSONObj opTimeObj = element.Obj(); + Timestamp ts; + status = bsonExtractTimestampField(opTimeObj, kTimestampFieldName, &ts); + if (!status.isOK()) + return status; + long long term; + status = bsonExtractIntegerField(opTimeObj, kTermFieldName, &term); + if (!status.isOK()) + return status; + *out = repl::OpTime(ts, term); + return Status::OK(); +} + Status bsonExtractTimestampField(const BSONObj& object, StringData fieldName, Timestamp* out) { BSONElement element; Status status = bsonExtractTypedField(object, fieldName, bsonTimestamp, &element); diff --git a/src/mongo/bson/util/bson_extract.h b/src/mongo/bson/util/bson_extract.h index fa7e3bbc9ab..c5e34d3e3ba 100644 --- a/src/mongo/bson/util/bson_extract.h +++ b/src/mongo/bson/util/bson_extract.h @@ -33,6 +33,7 @@ #include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/bson/bsontypes.h" +#include "mongo/db/repl/optime.h" namespace mongo { @@ -94,6 +95,20 @@ Status bsonExtractIntegerField(const BSONObj& object, StringData fieldName, long */ Status bsonExtractStringField(const BSONObj& object, StringData fieldName, std::string* out); + +/** + * Finds an object-typed field named "fieldName" in "object" that represents an OpTime. + * + * The OpTime objects have two fields, a Timestamp ts and numeric term. + * + * Returns Status::OK() and sets *out to the found element's OpTime value on success. Returns + * ErrorCodes::NoSuchKey if there are no matches for "fieldName" or either subobject field is + * missing, and ErrorCodes::TypeMismatch if the type of the matching element is not Object, the ts + * subfield is not Timestamp, or the term subfield is not numeric. For return values other than + * Status::OK(), the resulting value of "*out" is undefined. + */ +Status bsonExtractOpTimeField(const BSONObj& object, StringData fieldName, repl::OpTime* out); + /** * Finds an Timestamp-typed element named "fieldName" in "object" and stores its value in "out". * diff --git a/src/mongo/bson/util/bson_extract_test.cpp b/src/mongo/bson/util/bson_extract_test.cpp index dc4b97f8a8f..1c7261d1301 100644 --- a/src/mongo/bson/util/bson_extract_test.cpp +++ b/src/mongo/bson/util/bson_extract_test.cpp @@ -30,6 +30,7 @@ #include "mongo/bson/util/bson_extract.h" #include "mongo/db/jsobj.h" +#include "mongo/db/repl/optime.h" #include "mongo/unittest/unittest.h" using namespace mongo; @@ -83,6 +84,33 @@ TEST(ExtractBSON, ExtractStringFieldWithDefault) { ASSERT_EQUALS(std::string("default"), s); } +TEST(ExtractBSON, ExtractOpTimeField) { + // Outer object cases. + BSONObj obj = BSON("a" << BSON("ts" << Timestamp(10, 0) << "term" << 2) << "b" + << "notAnObj"); + repl::OpTime opTime; + ASSERT_OK(bsonExtractOpTimeField(obj, "a", &opTime)); + ASSERT(repl::OpTime(Timestamp(10, 0), 2) == opTime); + ASSERT_EQUALS(ErrorCodes::TypeMismatch, bsonExtractOpTimeField(obj, "b", &opTime)); + ASSERT_EQUALS(ErrorCodes::NoSuchKey, bsonExtractOpTimeField(obj, "c", &opTime)); + + // Missing timestamp field. + obj = BSON("a" << BSON("ts" + << "notATimestamp" + << "term" << 2)); + ASSERT_EQUALS(ErrorCodes::TypeMismatch, bsonExtractOpTimeField(obj, "a", &opTime)); + // Wrong typed timestamp field. + obj = BSON("a" << BSON("term" << 2)); + ASSERT_EQUALS(ErrorCodes::NoSuchKey, bsonExtractOpTimeField(obj, "a", &opTime)); + // Missing term field. + obj = BSON("a" << BSON("ts" << Timestamp(10, 0) << "term" + << "notANumber")); + ASSERT_EQUALS(ErrorCodes::TypeMismatch, bsonExtractOpTimeField(obj, "a", &opTime)); + // Wrong typed term field. + obj = BSON("a" << BSON("ts" << Timestamp(10, 0))); + ASSERT_EQUALS(ErrorCodes::NoSuchKey, bsonExtractOpTimeField(obj, "a", &opTime)); +} + TEST(ExtractBSON, ExtractBooleanFieldWithDefault) { BSONObj obj1 = BSON("a" << 1 << "b" << "hello" |