From dcf3a10eeaef2ea22678c9ae9aaa1fa2e9a81fa2 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Thu, 3 Dec 2009 17:25:22 -0500 Subject: [PATCH] Start using Date_t. Can anyone think of a better name? --- client/examples/clientTest.cpp | 2 +- client/gridfs.h | 2 +- db/cloner.cpp | 2 +- db/jsobj.cpp | 1 + db/jsobj.h | 12 ++++++++---- db/json.cpp | 9 +++++---- db/matcher.cpp | 4 ++-- dbtests/jsobjtests.cpp | 18 ++++++++++++++---- dbtests/repltests.cpp | 4 ++-- s/chunk.cpp | 2 +- s/config.cpp | 2 +- s/d_logic.cpp | 2 +- s/shardkey.cpp | 4 ++-- scripting/engine.cpp | 2 +- scripting/engine_spidermonkey.cpp | 2 +- scripting/sm_db.cpp | 6 +++--- scripting/v8_wrapper.cpp | 2 +- util/background.cpp | 2 +- util/goodies.h | 10 +++++++++- util/optime.h | 3 +++ 20 files changed, 59 insertions(+), 32 deletions(-) diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp index 8f983757c63..ef1b760ab70 100644 --- a/client/examples/clientTest.cpp +++ b/client/examples/clientTest.cpp @@ -161,7 +161,7 @@ int main( int argc, const char **argv ) { } mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() ); - unsigned long long oldTime = out["ts"].timestampTime(); + Date_t oldTime = out["ts"].timestampTime(); unsigned int oldInc = out["ts"].timestampInc(); { diff --git a/client/gridfs.h b/client/gridfs.h index e209d9f8e28..3165d5f2988 100644 --- a/client/gridfs.h +++ b/client/gridfs.h @@ -158,7 +158,7 @@ namespace mongo { return _obj["contentType"].valuestr(); } - unsigned long long getUploadDate(){ + Date_t getUploadDate(){ return _obj["uploadDate"].date(); } diff --git a/db/cloner.cpp b/db/cloner.cpp index 11aa021fb3e..ea2f00396d1 100644 --- a/db/cloner.cpp +++ b/db/cloner.cpp @@ -549,7 +549,7 @@ namespace mongo { long long cursorId = 0; BSONElement cursorIdToken = fromToken.getField( "cursorId" ); if ( cursorIdToken.type() == Date ) { - cursorId = cursorIdToken.date(); + cursorId = cursorIdToken._numberLong(); } setClient( collection.c_str() ); diff --git a/db/jsobj.cpp b/db/jsobj.cpp index 17cdf4db533..3ff3f6bc51b 100644 --- a/db/jsobj.cpp +++ b/db/jsobj.cpp @@ -37,6 +37,7 @@ BOOST_STATIC_ASSERT( sizeof(int) == 4 ); BOOST_STATIC_ASSERT( sizeof(long long) == 8 ); BOOST_STATIC_ASSERT( sizeof(double) == 8 ); +BOOST_STATIC_ASSERT( sizeof(mongo::Date_t) == 8 ); BOOST_STATIC_ASSERT( sizeof(mongo::OID) == 12 ); namespace mongo { diff --git a/db/jsobj.h b/db/jsobj.h index 3a45e42d2dc..55a06da43c6 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -320,8 +320,8 @@ namespace mongo { /** Retrieve a java style date value from the element. Ensure element is of type Date before calling. */ - unsigned long long date() const { - return *reinterpret_cast< const unsigned long long* >( value() ); + Date_t date() const { + return *reinterpret_cast< const Date_t* >( value() ); } /** Convert the value to boolean, regardless of its type, in a javascript-like fashion @@ -584,7 +584,7 @@ namespace mongo { type() == CodeWScope; } - unsigned long long timestampTime() const{ + Date_t timestampTime() const{ unsigned long long t = ((unsigned int*)(value() + 4 ))[0]; return t * 1000; } @@ -1264,11 +1264,15 @@ namespace mongo { @param dt a Java-style 64 bit date value, that is the number of milliseconds since January 1, 1970, 00:00:00 GMT */ - void appendDate(const char *fieldName, unsigned long long dt) { + void appendDate(const char *fieldName, Date_t dt) { b.append((char) Date); b.append(fieldName); b.append(dt); } + void append(const char *fieldName, Date_t dt) { + appendDate(fieldName, dt); + } + /** Append a regular expression value @param regex the regular expression pattern @param regex options such as "i" or "g" diff --git a/db/json.cpp b/db/json.cpp index e0b7743518d..6879298a814 100644 --- a/db/json.cpp +++ b/db/json.cpp @@ -84,7 +84,7 @@ namespace mongo { BinDataType binDataType; string regex; string regexOptions; - unsigned long long date; + Date_t date; }; struct objectStart { @@ -369,7 +369,7 @@ namespace mongo { struct dateValue { dateValue( ObjectBuilder &_b ) : b( _b ) {} - void operator() ( unsigned long long v ) const { + void operator() ( Date_t v ) const { b.date = v; } ObjectBuilder &b; @@ -506,9 +506,10 @@ public: lexeme_d[ '"' >> ( *( range_p( 'A', 'Z' ) | range_p( 'a', 'z' ) | range_p( '0', '9' ) | ch_p( '+' ) | ch_p( '/' ) ) >> *ch_p( '=' ) )[ binDataBinary( self.b ) ] >> '"' ] >> ',' >> "\"$type\"" >> ':' >> lexeme_d[ '"' >> ( repeat_p( 2 )[ xdigit_p ] )[ binDataType( self.b ) ] >> '"' ] >> '}'; + // TODO: this will need to use a signed parser at some point date = dateS | dateT; - dateS = ch_p( '{' ) >> "\"$date\"" >> ':' >> uint_parser< unsigned long long >()[ dateValue( self.b ) ] >> '}'; - dateT = str_p( "Date" ) >> '(' >> uint_parser< unsigned long long >()[ dateValue( self.b ) ] >> ')'; + dateS = ch_p( '{' ) >> "\"$date\"" >> ':' >> uint_parser< Date_t >()[ dateValue( self.b ) ] >> '}'; + dateT = str_p( "Date" ) >> '(' >> uint_parser< Date_t >()[ dateValue( self.b ) ] >> ')'; regex = regexS | regexT; regexS = ch_p( '{' ) >> "\"$regex\"" >> ':' >> str[ regexValue( self.b ) ] >> ',' >> "\"$options\"" >> ':' >> lexeme_d[ '"' >> ( *( alpha_p ) )[ regexOptions( self.b ) ] >> '"' ] >> '}'; diff --git a/db/matcher.cpp b/db/matcher.cpp index c6649fb5830..c1f431a6108 100644 --- a/db/matcher.cpp +++ b/db/matcher.cpp @@ -460,8 +460,8 @@ namespace mongo { sprintf(buf, "%f", e.number()); } else if ( e.type() == Date ) { - unsigned long long d = e.date(); - time_t t = (d/1000); + Date_t d = e.date(); + time_t t = (d.millis/1000); time_t_to_String(t, buf); } else diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index 74f017380d8..4380b227795 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -730,12 +730,21 @@ namespace JsobjTests { } }; + class DateBuilder { + public: + void run() { + BSONObj o = BSON("" << Date_t(1234567890)); + ASSERT( o.firstElement().type() == Date ); + ASSERT( o.firstElement().date() == Date_t(1234567890) ); + } + }; + class DateNowBuilder { public: void run() { - unsigned long long before = jsTime(); + Date_t before = jsTime(); BSONObj o = BSON("now" << DATENOW); - unsigned long long after = jsTime(); + Date_t after = jsTime(); ASSERT( o.valid() ); @@ -749,9 +758,9 @@ namespace JsobjTests { class TimeTBuilder { public: void run() { - unsigned long long before = jsTime(); + Date_t before = jsTime(); time_t now = time(NULL); - unsigned long long after = jsTime(); + Date_t after = jsTime(); BSONObjBuilder b; b.appendTimeT("now", now); @@ -1251,6 +1260,7 @@ namespace JsobjTests { add< ValueStreamTests::Unallowed >(); add< ValueStreamTests::ElementAppend >(); add< SubObjectBuilder >(); + add< DateBuilder >(); add< DateNowBuilder >(); add< TimeTBuilder >(); add< MinMaxElementTest >(); diff --git a/dbtests/repltests.cpp b/dbtests/repltests.cpp index 86f3820d9d0..853779c1844 100644 --- a/dbtests/repltests.cpp +++ b/dbtests/repltests.cpp @@ -215,7 +215,7 @@ namespace ReplTests { deleteAll( ns() ); } private: - mutable unsigned long long date_; + mutable Date_t date_; }; class InsertAutoId : public Base { @@ -305,7 +305,7 @@ namespace ReplTests { insert( BSON( "_id" << 1 ) ); } private: - mutable unsigned long long date_; + mutable Date_t date_; }; class UpdateSameField : public Base { diff --git a/s/chunk.cpp b/s/chunk.cpp index 076d6dd268b..f405c577ce4 100644 --- a/s/chunk.cpp +++ b/s/chunk.cpp @@ -331,7 +331,7 @@ namespace mongo { _max = from.getObjectField( "max" ).getOwned(); _maxDotted = from.getObjectField( "maxDotted" ).getOwned(); _shard = from.getStringField( "shard" ); - _lastmod = from.hasField( "lastmod" ) ? from["lastmod"].date() : 0; + _lastmod = from.hasField( "lastmod" ) ? from["lastmod"]._numberLong() : 0; uassert( "Chunk needs a ns" , ! _ns.empty() ); uassert( "Chunk needs a server" , ! _ns.empty() ); diff --git a/s/config.cpp b/s/config.cpp index 661945d5a71..779ab602a25 100644 --- a/s/config.cpp +++ b/s/config.cpp @@ -344,7 +344,7 @@ namespace mongo { massert( "getoptime failed" , conn->simpleCommand( "admin" , &result , "getoptime" ) ); conn.done(); - return result["optime"].date(); + return result["optime"]._numberLong(); } /* --- ConfigServer ---- */ diff --git a/s/d_logic.cpp b/s/d_logic.cpp index ee260458969..a02d7d6298c 100644 --- a/s/d_logic.cpp +++ b/s/d_logic.cpp @@ -58,7 +58,7 @@ namespace mongo { return (unsigned long long)e.number(); if ( e.type() == Date || e.type() == Timestamp ) - return e.date(); + return e._numberLong(); errmsg = "version is not a numberic type"; diff --git a/s/shardkey.cpp b/s/shardkey.cpp index ad91c36cbc2..0aa97b997ee 100644 --- a/s/shardkey.cpp +++ b/s/shardkey.cpp @@ -207,8 +207,8 @@ namespace mongo { break; case Date: { - unsigned long long x = l.date(); - unsigned long long y = r.date(); + Date_t x = l.date(); + Date_t y = r.date(); assert( y >= x ); b.appendDate(fn, x + (y - x)/2); break; diff --git a/scripting/engine.cpp b/scripting/engine.cpp index a0c223804ca..a69f8db1f7b 100644 --- a/scripting/engine.cpp +++ b/scripting/engine.cpp @@ -71,7 +71,7 @@ namespace mongo { builder.appendNull( fieldName ); break; case Date: - builder.appendDate( fieldName , (unsigned long long) getNumber( scopeName ) ); + builder.appendDate( fieldName , Date_t(getNumber( scopeName )) ); break; default: stringstream temp; diff --git a/scripting/engine_spidermonkey.cpp b/scripting/engine_spidermonkey.cpp index 3ad76ee1b5c..be03ac4f86c 100644 --- a/scripting/engine_spidermonkey.cpp +++ b/scripting/engine_spidermonkey.cpp @@ -537,7 +537,7 @@ namespace mongo { return OBJECT_TO_JSVAL( JS_GetFunctionObject( func ) ); } case Date: - return OBJECT_TO_JSVAL( js_NewDateObjectMsec( _context , (jsdouble) e.date() ) ); + return OBJECT_TO_JSVAL( js_NewDateObjectMsec( _context , (jsdouble) e.date().millis ) ); case MinKey: return OBJECT_TO_JSVAL( JS_NewObject( _context , &minkey_class , 0 , 0 ) ); diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp index d132ddd16bc..81c5c0b2196 100644 --- a/scripting/sm_db.cpp +++ b/scripting/sm_db.cpp @@ -784,20 +784,20 @@ namespace mongo { { jsdouble d = js_DateGetMsecSinceEpoch( c->_context , o ); if ( d ){ - b.appendDate( name.c_str() , (unsigned long long)d ); + b.appendDate( name.c_str() , Date_t(d) ); return true; } } #elif defined( XULRUNNER ) if ( JS_InstanceOf( c->_context , o, globalSMEngine->_dateClass , 0 ) ){ jsdouble d = js_DateGetMsecSinceEpoch( c->_context , o ); - b.appendDate( name.c_str() , (unsigned long long)d ); + b.appendDate( name.c_str() , Date_t(d) ); return true; } #else if ( JS_InstanceOf( c->_context , o, &js_DateClass , 0 ) ){ jsdouble d = js_DateGetMsecSinceEpoch( c->_context , o ); - b.appendDate( name.c_str() , (unsigned long long)d ); + b.appendDate( name.c_str() , Date_t(d) ); return true; } #endif diff --git a/scripting/v8_wrapper.cpp b/scripting/v8_wrapper.cpp index cd111b13662..6a7b66f5e7a 100644 --- a/scripting/v8_wrapper.cpp +++ b/scripting/v8_wrapper.cpp @@ -268,7 +268,7 @@ namespace mongo { } if ( value->IsDate() ){ - b.appendDate( sname.c_str() , (unsigned long long )(v8::Date::Cast( *value )->NumberValue()) ); + b.appendDate( sname.c_str() , Date_t(v8::Date::Cast( *value )->NumberValue()) ); return; } diff --git a/util/background.cpp b/util/background.cpp index 4208373ad7c..ac3a48c8380 100644 --- a/util/background.cpp +++ b/util/background.cpp @@ -50,7 +50,7 @@ namespace mongo { bool BackgroundJob::wait(int msMax) { assert( state != NotStarted ); int ms = 1; - unsigned long long start = jsTime(); + Date_t start = jsTime(); while ( state != Done ) { sleepmillis(ms); if ( ms < 1000 ) diff --git a/util/goodies.h b/util/goodies.h index 3e169f7b195..7a2461cee68 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -219,7 +219,15 @@ namespace mongo { return (xt.sec & 0xfffff) * 1000 + t; } - inline unsigned long long jsTime() { + struct Date_t { + unsigned long long millis; + Date_t(): millis(0) {} + Date_t(unsigned long long m): millis(m) {} + operator unsigned long long&() { return millis; } + operator const unsigned long long&() const { return millis; } + }; + + inline Date_t jsTime() { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); unsigned long long t = xt.nsec / 1000000; diff --git a/util/optime.h b/util/optime.h index cfff86b1c78..b7d4f61ec3f 100644 --- a/util/optime.h +++ b/util/optime.h @@ -32,6 +32,9 @@ namespace mongo { unsigned getSecs() const { return secs; } + OpTime(Date_t date) { + reinterpret_cast(*this) = date.millis; + } OpTime(unsigned long long date) { reinterpret_cast(*this) = date; }