summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Seyster <justin.seyster@mongodb.com>2018-05-18 16:16:29 -0400
committerJustin Seyster <justin.seyster@mongodb.com>2018-05-22 18:29:41 -0400
commita9deb701a7533b566a2eb703aff70bf2f03b147c (patch)
treecc615998ab87302fe3d1b725ff8580d12834670b /src
parentadad00367e75e7c71a781e4610321f7be5d98aa6 (diff)
downloadmongo-a9deb701a7533b566a2eb703aff70bf2f03b147c.tar.gz
SERVER-34975 Treat an OID-embedded timestamp as unsigned.
Diffstat (limited to 'src')
-rw-r--r--src/mongo/bson/oid.cpp4
-rw-r--r--src/mongo/bson/oid.h7
-rw-r--r--src/mongo/bson/oid_test.cpp6
-rw-r--r--src/mongo/db/pipeline/expression_convert_test.cpp15
-rw-r--r--src/mongo/dbtests/jsobjtests.cpp2
5 files changed, 22 insertions, 12 deletions
diff --git a/src/mongo/bson/oid.cpp b/src/mongo/bson/oid.cpp
index 26d90d97bad..ca9c6e558a9 100644
--- a/src/mongo/bson/oid.cpp
+++ b/src/mongo/bson/oid.cpp
@@ -162,10 +162,6 @@ void OID::init(Date_t date, bool max) {
std::memcpy(_view().view(kInstanceUniqueOffset), &rest, kInstanceUniqueSize + kIncrementSize);
}
-time_t OID::asTimeT() const {
- return getTimestamp();
-}
-
std::string OID::toString() const {
return toHexLower(_data, kOIDSize);
}
diff --git a/src/mongo/bson/oid.h b/src/mongo/bson/oid.h
index 4474a12dbe2..7f4f31a4369 100644
--- a/src/mongo/bson/oid.h
+++ b/src/mongo/bson/oid.h
@@ -176,9 +176,8 @@ public:
*/
void initFromTermNumber(int64_t term);
- time_t asTimeT() const;
Date_t asDateT() const {
- return Date_t::fromMillisSinceEpoch(asTimeT() * 1000LL);
+ return Date_t::fromMillisSinceEpoch(getTimestamp() * 1000LL);
}
// True iff the OID is not empty
@@ -198,8 +197,8 @@ public:
static unsigned getMachineId(); // used by the 'features' command
static void regenMachineId();
- // Timestamp is 4 bytes so we just use int32_t
- typedef int32_t Timestamp;
+ // Timestamp is a 4-byte positive integer so we use uint32_t
+ typedef uint32_t Timestamp;
// Wrappers so we can return stuff by value.
struct InstanceUnique {
diff --git a/src/mongo/bson/oid_test.cpp b/src/mongo/bson/oid_test.cpp
index 8be156ae41d..6d34d6a5808 100644
--- a/src/mongo/bson/oid_test.cpp
+++ b/src/mongo/bson/oid_test.cpp
@@ -109,7 +109,7 @@ TEST(Basic, Deserialize) {
0xDEu,
0xADu,
0xBEu,
- 0xEFu, // timestamp is -559038737 (signed)
+ 0xEFu, // timestamp is 3735928559 (unsigned)
0x00u,
0x00u,
0x00u,
@@ -122,7 +122,7 @@ TEST(Basic, Deserialize) {
OID o1 = OID::from(OIDbytes);
- ASSERT_EQUALS(o1.getTimestamp(), -559038737);
+ ASSERT_EQUALS(o1.getTimestamp(), 3735928559);
OID::InstanceUnique u = o1.getInstanceUnique();
for (std::size_t i = 0; i < OID::kInstanceUniqueSize; ++i) {
ASSERT_EQUALS(u.bytes[i], 0x00u);
@@ -158,7 +158,7 @@ TEST(Basic, FromTerm) {
auto oidHead = oidStr.substr(0, 8);
auto oidTail = oidStr.substr(oidStr.length() - 1);
- ASSERT_EQUALS("7fffffff", oidHead);
+ ASSERT_EQUALS("ffffffff", oidHead);
ASSERT_EQUALS(term, std::stoi(oidTail));
}
}
diff --git a/src/mongo/db/pipeline/expression_convert_test.cpp b/src/mongo/db/pipeline/expression_convert_test.cpp
index 3cc94e92e8e..9db02ed058e 100644
--- a/src/mongo/db/pipeline/expression_convert_test.cpp
+++ b/src/mongo/db/pipeline/expression_convert_test.cpp
@@ -1947,6 +1947,21 @@ TEST_F(ExpressionConvertTest, ConvertObjectIdToDate) {
"2017-10-19T13:30:00.000Z");
}
+TEST_F(ExpressionConvertTest, ConvertObjectIdToDateFuture) {
+ auto expCtx = getExpCtx();
+
+ auto spec = BSON("$convert" << BSON("input"
+ << "$path1"
+ << "to"
+ << "date"));
+ auto convertExp = Expression::parseExpression(expCtx, spec, expCtx->variablesParseState);
+
+ Document oidInput{{"path1", OID("8071be82122d3312074f0300")}};
+
+ ASSERT_EQ(dateToISOStringUTC(convertExp->evaluate(oidInput).getDate()),
+ "2038-04-15T09:53:06.000Z");
+}
+
TEST_F(ExpressionConvertTest, ConvertStringToInt) {
auto expCtx = getExpCtx();
diff --git a/src/mongo/dbtests/jsobjtests.cpp b/src/mongo/dbtests/jsobjtests.cpp
index b670cab1dae..68ce6c52d63 100644
--- a/src/mongo/dbtests/jsobjtests.cpp
+++ b/src/mongo/dbtests/jsobjtests.cpp
@@ -1390,7 +1390,7 @@ public:
oid.init(base);
ASSERT_EQUALS(base.toMillisSinceEpoch() / 1000, oid.asDateT().toMillisSinceEpoch() / 1000);
- ASSERT_EQUALS(base.toTimeT(), oid.asTimeT());
+ ASSERT_EQUALS(base.toTimeT(), static_cast<time_t>(oid.getTimestamp()));
}
};