summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/optime.cpp
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-08-12 13:24:35 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-08-12 17:19:31 -0400
commit2482d4387b6a0fcdf825e07d7f5260041a89f3f6 (patch)
tree165b54668d3d7589e71c702db330f40109e89227 /src/mongo/db/repl/optime.cpp
parent7e8fb8df8eb69c7817a8fd00b78ae2f617d9f10d (diff)
downloadmongo-2482d4387b6a0fcdf825e07d7f5260041a89f3f6.tar.gz
SERVER-19855 Parsing/serialization logic for OpTime
This change pulls the OpTime serialization/deserialization logic to be part of the class.
Diffstat (limited to 'src/mongo/db/repl/optime.cpp')
-rw-r--r--src/mongo/db/repl/optime.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/repl/optime.cpp b/src/mongo/db/repl/optime.cpp
index 87cf966c1ef..31d83d91e4f 100644
--- a/src/mongo/db/repl/optime.cpp
+++ b/src/mongo/db/repl/optime.cpp
@@ -31,10 +31,18 @@
#include <string>
#include <utility>
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/bson/util/bson_extract.h"
#include "mongo/db/repl/optime.h"
namespace mongo {
namespace repl {
+namespace {
+
+const char* kTimestampFieldName = "ts";
+const char* kTermFieldName = "t";
+
+} // namespace
OpTime::OpTime(Timestamp ts, long long term) : _timestamp(std::move(ts)), _term(term) {}
@@ -54,6 +62,30 @@ bool OpTime::isNull() const {
return _timestamp.isNull();
}
+void OpTime::append(BSONObjBuilder* builder) const {
+ builder->append(kTimestampFieldName, _timestamp);
+
+ // Don't add term in protocol version 0.
+ if (_term != kProtocolVersionV0Term) {
+ builder->append(kTermFieldName, _term);
+ }
+}
+
+StatusWith<OpTime> OpTime::parseFromBSON(const BSONObj& obj) {
+ Timestamp ts;
+ Status status = bsonExtractTimestampField(obj, kTimestampFieldName, &ts);
+ if (!status.isOK())
+ return status;
+
+ // Default to -1 if the term is absent.
+ long long term;
+ status = bsonExtractIntegerFieldWithDefault(obj, kTermFieldName, kProtocolVersionV0Term, &term);
+ if (!status.isOK())
+ return status;
+
+ return OpTime(ts, term);
+}
+
std::string OpTime::toString() const {
std::stringstream ss;
ss << "(term: " << _term << ", timestamp: " << _timestamp.toStringPretty() << ")";