summaryrefslogtreecommitdiff
path: root/src/mongo/bson/timestamp.h
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2015-05-07 16:45:29 -0400
committerAndy Schwerin <schwerin@mongodb.com>2015-05-12 09:57:33 -0400
commit9aac625685811873ffbc2d3e8d09531eff1ce10e (patch)
treeecd8dd9fc9d148b196c6ab2aa5e38e5f9e327c54 /src/mongo/bson/timestamp.h
parent21a16d229bdba571f1118a419898b666c666b869 (diff)
downloadmongo-9aac625685811873ffbc2d3e8d09531eff1ce10e.tar.gz
SERVER-13874 Make mongo::Milliseconds et al. aliases for equivalent stdx::chrono types.
Also introduces operators for adding stdx::chrono::duration to Date_t, subtracting two Date_ts to get Milliseconds, and remove the use of reinterpret_cast from the implementation of BSON Timestamp type.
Diffstat (limited to 'src/mongo/bson/timestamp.h')
-rw-r--r--src/mongo/bson/timestamp.h83
1 files changed, 45 insertions, 38 deletions
diff --git a/src/mongo/bson/timestamp.h b/src/mongo/bson/timestamp.h
index b223b5deb50..8565cc80847 100644
--- a/src/mongo/bson/timestamp.h
+++ b/src/mongo/bson/timestamp.h
@@ -30,51 +30,56 @@
#include "mongo/base/data_view.h"
#include "mongo/bson/util/builder.h"
#include "mongo/util/assert_util.h"
+#include "mongo/util/time_support.h"
namespace mongo {
- /* Timestamp: A combination of current second plus an ordinal value.
+ /**
+ * Timestamp: A combination of a count of seconds since the POSIX epoch plus an ordinal value.
*/
-#pragma pack(4)
class Timestamp {
- unsigned i; // ordinal comes first so we can do a single 64 bit compare on little endian
- unsigned secs;
public:
+ // Maximum Timestamp value.
+ static Timestamp max();
+
+ /**
+ * DEPRECATED Constructor that builds a Timestamp from a Date_t by using the
+ * high-order 4 bytes of "date" for the "secs" field and the low-order 4 bytes
+ * for the "i" field.
+ */
+ explicit Timestamp(Date_t date) : Timestamp(date.toULL()) {}
+
+ /**
+ * DEPRECATED Constructor that builds a Timestamp from a 64-bit unsigned integer by using
+ * the high-order 4 bytes of "v" for the "secs" field and the low-order 4 bytes for the "i"
+ * field."
+ */
+ explicit Timestamp(unsigned long long v) : Timestamp(v >> 32, v) {}
+
+ Timestamp(Seconds s, unsigned increment) : Timestamp(s.count(), increment) {}
+
+ Timestamp(unsigned a, unsigned b) : i(b), secs(a) {
+ dassert(secs <= std::numeric_limits<int>::max());
+ }
+
+ Timestamp() : i(0), secs(0) {}
+
unsigned getSecs() const {
return secs;
}
+
unsigned getInc() const {
return i;
}
- Timestamp(Date_t date) {
- reinterpret_cast<unsigned long long&>(*this) = date.millis;
- dassert( (int)secs >= 0 );
- }
-
- Timestamp(unsigned a, unsigned b) {
- secs = a;
- i = b;
- dassert( (int)secs >= 0 );
- }
- Timestamp( const Timestamp& other ) {
- secs = other.secs;
- i = other.i;
- dassert( (int)secs >= 0 );
- }
- Timestamp() {
- secs = 0;
- i = 0;
- }
-
- // Maximum Timestamp value.
- static Timestamp max();
-
unsigned long long asULL() const {
- return reinterpret_cast<const unsigned long long*>(&i)[0];
+ unsigned long long result = secs;
+ result <<= 32;
+ result |= i;
+ return result;
}
long long asLL() const {
- return reinterpret_cast<const long long*>(&i)[0];
+ return static_cast<long long>(asULL());
}
bool isNull() const { return secs == 0; }
@@ -86,31 +91,33 @@ namespace mongo {
std::string toString() const;
bool operator==(const Timestamp& r) const {
- return i == r.i && secs == r.secs;
+ return tie() == r.tie();
}
bool operator!=(const Timestamp& r) const {
- return !(*this == r);
+ return tie() != r.tie();
}
bool operator<(const Timestamp& r) const {
- if ( secs != r.secs )
- return secs < r.secs;
- return i < r.i;
+ return tie() < r.tie();
}
bool operator<=(const Timestamp& r) const {
- return *this < r || *this == r;
+ return tie() <= r.tie();
}
bool operator>(const Timestamp& r) const {
- return !(*this <= r);
+ return tie() > r.tie();
}
bool operator>=(const Timestamp& r) const {
- return !(*this < r);
+ return tie() >= r.tie();
}
// Append the BSON representation of this Timestamp to the given BufBuilder with the given
// name. This lives here because Timestamp manages its own serialization format.
void append(BufBuilder& builder, const StringData& fieldName) const;
+ private:
+ std::tuple<unsigned, unsigned> tie() const { return std::tie(secs, i); }
+
+ unsigned i; // ordinal comes first so we can do a single 64 bit compare on little endian
+ unsigned secs;
};
-#pragma pack()
} // namespace mongo