diff options
-rw-r--r-- | src/mongo/bson/bson_db.h | 15 | ||||
-rw-r--r-- | src/mongo/bson/bsonobjbuilder.h | 28 | ||||
-rw-r--r-- | src/mongo/bson/optime.cpp | 8 | ||||
-rw-r--r-- | src/mongo/bson/optime.h | 6 | ||||
-rw-r--r-- | src/mongo/db/hasher_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/json.cpp | 4 | ||||
-rw-r--r-- | src/mongo/dbtests/jsontests.cpp | 10 | ||||
-rw-r--r-- | src/mongo/dbtests/jstests.cpp | 4 |
8 files changed, 36 insertions, 41 deletions
diff --git a/src/mongo/bson/bson_db.h b/src/mongo/bson/bson_db.h index 3d9a37639d2..941aea1935c 100644 --- a/src/mongo/bson/bson_db.h +++ b/src/mongo/bson/bson_db.h @@ -44,15 +44,18 @@ namespace mongo { - inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( StringData fieldName , unsigned long long time , unsigned int inc ) { - OpTime t( (unsigned) (time / 1000) , inc ); - appendTimestamp( fieldName , t.asDate() ); + inline BSONObjBuilder& BSONObjBuilder::append(StringData fieldName, OpTime optime) { + optime.append(_b, fieldName); return *this; } - inline BSONObjBuilder& BSONObjBuilder::append(StringData fieldName, OpTime optime) { - appendTimestamp(fieldName, optime.asDate()); - return *this; + inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( StringData fieldName ) { + return append(fieldName, OpTime()); + } + + inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( StringData fieldName, + unsigned long long val ) { + return append(fieldName, OpTime(val)); } inline OpTime BSONElement::_opTime() const { diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h index 50ad4480669..a679140dae0 100644 --- a/src/mongo/bson/bsonobjbuilder.h +++ b/src/mongo/bson/bsonobjbuilder.h @@ -459,12 +459,9 @@ namespace mongo { } // Append a Timestamp field -- will be updated to next OpTime on db insert. - BSONObjBuilder& appendTimestamp( StringData fieldName ) { - _b.appendNum( (char) Timestamp ); - _b.appendStr( fieldName ); - _b.appendNum( (unsigned long long) 0 ); - return *this; - } + BSONObjBuilder& appendTimestamp( StringData fieldName ); + + BSONObjBuilder& appendTimestamp( StringData fieldName , unsigned long long val ); /** * To store an OpTime in BSON, use this function. @@ -472,20 +469,6 @@ namespace mongo { */ BSONObjBuilder& append(StringData fieldName, OpTime optime); - BSONObjBuilder& appendTimestamp( StringData fieldName , unsigned long long val ) { - _b.appendNum( (char) Timestamp ); - _b.appendStr( fieldName ); - _b.appendNum( val ); - return *this; - } - - /** - Timestamps are a special BSON datatype that is used internally for replication. - Append a timestamp element to the object being ebuilt. - @param time - in millis (but stored in seconds) - */ - BSONObjBuilder& appendTimestamp( StringData fieldName , unsigned long long time , unsigned int inc ); - /* Append an element of the deprecated DBRef type. @deprecated @@ -789,11 +772,6 @@ namespace mongo { BufBuilder &subobjStart() { return _b.subobjStart( num() ); } BufBuilder &subarrayStart() { return _b.subarrayStart( num() ); } - BSONArrayBuilder& appendTimestamp(unsigned int sec, unsigned int inc) { - _b.appendTimestamp(num(), sec, inc); - return *this; - } - BSONArrayBuilder& appendRegex(StringData regex, StringData options = "") { _b.appendRegex(num(), regex, options); return *this; diff --git a/src/mongo/bson/optime.cpp b/src/mongo/bson/optime.cpp index 94a49da7a75..ff044533a65 100644 --- a/src/mongo/bson/optime.cpp +++ b/src/mongo/bson/optime.cpp @@ -25,6 +25,7 @@ * then also delete it in the license file. */ +#include "mongo/bson/bsontypes.h" #include "mongo/bson/optime.h" #include <iostream> @@ -41,4 +42,11 @@ namespace mongo { return OpTime(t, i); } + void OpTime::append(BufBuilder& builder, const StringData& fieldName) const { + // No endian conversions needed, since we store in-memory representation + // in little endian format, regardless of target endian. + builder.appendNum( static_cast<char>(Timestamp) ); + builder.appendStr( fieldName ); + builder.appendNum( asDate() ); + } } diff --git a/src/mongo/bson/optime.h b/src/mongo/bson/optime.h index d090c57867e..bcd6ae3e5a8 100644 --- a/src/mongo/bson/optime.h +++ b/src/mongo/bson/optime.h @@ -30,6 +30,7 @@ #include <boost/thread/condition.hpp> #include <sstream> +#include "mongo/bson/util/builder.h" #include "mongo/util/assert_util.h" #include "mongo/util/concurrency/mutex.h" #include "mongo/util/time_support.h" @@ -125,6 +126,11 @@ namespace mongo { bool operator>=(const OpTime& r) const { return !(*this < r); } + + // Append the BSON representation of this OpTime to the given BufBuilder with the given + // name. This lives here because OpTime manages its own serialization format. + void append(BufBuilder& builder, const StringData& fieldName) const; + }; #pragma pack() diff --git a/src/mongo/db/hasher_test.cpp b/src/mongo/db/hasher_test.cpp index 808370922ef..1d52e8815b0 100644 --- a/src/mongo/db/hasher_test.cpp +++ b/src/mongo/db/hasher_test.cpp @@ -345,7 +345,7 @@ namespace { BSONObj o = BSON( "check" << Date_t( 0x5566778811223344LL ) ); ASSERT_EQUALS( hashIt( o ), 4476222765095560467LL ); - o = builder1.appendTimestamp( "check", 0x55667788LL * 1000LL, 0x11223344LL ).obj(); + o = builder1.append( "check", OpTime(0x55667788LL, 0x11223344LL) ).obj(); ASSERT_EQUALS( hashIt( o ), 4873046866288452390LL ); o = BSON( "check" << Date_t( 0 ) ); diff --git a/src/mongo/db/json.cpp b/src/mongo/db/json.cpp index f385630d833..6369ddfe50a 100644 --- a/src/mongo/db/json.cpp +++ b/src/mongo/db/json.cpp @@ -555,7 +555,7 @@ namespace mongo { if (!readToken(RBRACE)) { return parseError("Expecting '}'"); } - builder.appendTimestamp(fieldName, (static_cast<uint64_t>(seconds))*1000, count); + builder.append(fieldName, OpTime(seconds, count)); return Status::OK(); } @@ -815,7 +815,7 @@ namespace mongo { if (!readToken(RPAREN)) { return parseError("Expecting ')'"); } - builder.appendTimestamp(fieldName, (static_cast<uint64_t>(seconds))*1000, count); + builder.append(fieldName, OpTime(seconds, count)); return Status::OK(); } diff --git a/src/mongo/dbtests/jsontests.cpp b/src/mongo/dbtests/jsontests.cpp index 7d407d7ecd0..eadcec07af8 100644 --- a/src/mongo/dbtests/jsontests.cpp +++ b/src/mongo/dbtests/jsontests.cpp @@ -529,7 +529,7 @@ namespace JsonTests { public: void run() { BSONObjBuilder b; - b.appendTimestamp( "x" , 4000 , 10 ); + b.append( "x" , OpTime(4, 10) ); BSONObj o = b.obj(); ASSERT_EQUALS( "{ \"x\" : { \"$timestamp\" : { \"t\" : 4, \"i\" : 10 } } }", o.jsonString( Strict ) ); @@ -1775,7 +1775,7 @@ namespace JsonTests { class Timestamp : public Base { virtual BSONObj bson() const { BSONObjBuilder b; - b.appendTimestamp( "a", (unsigned long long) 20000, 5 ); + b.append( "a", OpTime(20, 5) ); return b.obj(); } virtual string json() const { @@ -1792,7 +1792,7 @@ namespace JsonTests { class TimestampZero : public Base { virtual BSONObj bson() const { BSONObjBuilder b; - b.appendTimestamp( "a", 0ULL, 0 ); + b.append( "a", OpTime() ); return b.obj(); } virtual string json() const { @@ -1839,7 +1839,7 @@ namespace JsonTests { class TimestampObject : public Base { virtual BSONObj bson() const { BSONObjBuilder b; - b.appendTimestamp( "a", (unsigned long long) 20000, 5 ); + b.append( "a", OpTime(20, 5) ); return b.obj(); } virtual string json() const { @@ -1880,7 +1880,7 @@ namespace JsonTests { class TimestampObjectZero : public Base { virtual BSONObj bson() const { BSONObjBuilder b; - b.appendTimestamp( "a", 0ULL, 0 ); + b.append( "a", OpTime() ); return b.obj(); } virtual string json() const { diff --git a/src/mongo/dbtests/jstests.cpp b/src/mongo/dbtests/jstests.cpp index 67299420841..58a1e2daeea 100644 --- a/src/mongo/dbtests/jstests.cpp +++ b/src/mongo/dbtests/jstests.cpp @@ -525,7 +525,7 @@ namespace JSTests { b.appendTimestamp( "a" , 123456789 ); b.appendMinKey( "b" ); b.appendMaxKey( "c" ); - b.appendTimestamp( "d" , 1234000 , 9876 ); + b.append( "d" , OpTime(1234, 9876) ); { @@ -1648,7 +1648,7 @@ namespace JSTests { class Timestamp : public TestRoundTrip { virtual BSONObj bson() const { BSONObjBuilder b; - b.appendTimestamp( "a", 20000ULL, 5 ); + b.append( "a", OpTime(20, 5) ); return b.obj(); } virtual string json() const { |