diff options
Diffstat (limited to 'util/goodies.h')
| -rw-r--r-- | util/goodies.h | 154 |
1 files changed, 18 insertions, 136 deletions
diff --git a/util/goodies.h b/util/goodies.h index 53a74c2..65bfbab 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -109,49 +109,12 @@ namespace mongo { // PRINTFL; prints file:line #define MONGO_PRINTFL cout << __FILE__ ":" << __LINE__ << endl #define PRINTFL MONGO_PRINTFL +#define MONGO_FLOG log() << __FILE__ ":" << __LINE__ << endl +#define FLOG MONGO_FLOG #undef assert #define assert MONGO_assert - struct WrappingInt { - WrappingInt() { - x = 0; - } - WrappingInt(unsigned z) : x(z) { } - unsigned x; - operator unsigned() const { - return x; - } - - - static int diff(unsigned a, unsigned b) { - return a-b; - } - bool operator<=(WrappingInt r) { - // platform dependent - int df = (r.x - x); - return df >= 0; - } - bool operator>(WrappingInt r) { - return !(r<=*this); - } - }; - - /* - - class DebugMutex : boost::noncopyable { - friend class lock; - mongo::mutex m; - int locked; - public: - DebugMutex() : locked(0); { } - bool isLocked() { return locked; } - }; - - */ - -//typedef scoped_lock lock; - inline bool startsWith(const char *str, const char *prefix) { size_t l = strlen(prefix); if ( strlen(str) < l ) return false; @@ -236,6 +199,7 @@ namespace mongo { _active = 0; } + // typically you do ProgressMeterHolder void reset( unsigned long long total , int secondsBetween = 3 , int checkInterval = 100 ) { _total = total; _secondsBetween = secondsBetween; @@ -257,6 +221,7 @@ namespace mongo { } /** + * @param n how far along we are relative to the total # we set in CurOp::setMessage * @return if row was printed */ bool hit( int n = 1 ) { @@ -282,13 +247,15 @@ namespace mongo { return true; } - unsigned long long done() { - return _done; + void setTotalWhileRunning( unsigned long long total ) { + _total = total; } - unsigned long long hits() { - return _hits; - } + unsigned long long done() const { return _done; } + + unsigned long long hits() const { return _hits; } + + unsigned long long total() const { return _total; } string toString() const { if ( ! _active ) @@ -314,6 +281,10 @@ namespace mongo { int _lastTime; }; + // e.g.: + // CurOp * op = cc().curop(); + // ProgressMeterHolder pm( op->setMessage( "index: (1/3) external sort" , d->stats.nrecords , 10 ) ); + // loop { pm.hit(); } class ProgressMeterHolder : boost::noncopyable { public: ProgressMeterHolder( ProgressMeter& pm ) @@ -417,7 +388,7 @@ namespace mongo { class ThreadSafeString { public: ThreadSafeString( size_t size=256 ) - : _size( 256 ) , _buf( new char[256] ) { + : _size( size ) , _buf( new char[size] ) { memset( _buf , 0 , _size ); } @@ -468,97 +439,6 @@ namespace mongo { ostream& operator<<( ostream &s, const ThreadSafeString &o ); - inline bool isNumber( char c ) { - return c >= '0' && c <= '9'; - } - - inline unsigned stringToNum(const char *str) { - unsigned x = 0; - const char *p = str; - while( 1 ) { - if( !isNumber(*p) ) { - if( *p == 0 && p != str ) - break; - throw 0; - } - x = x * 10 + *p++ - '0'; - } - return x; - } - - // for convenience, '{' is greater than anything and stops number parsing - inline int lexNumCmp( const char *s1, const char *s2 ) { - //cout << "START : " << s1 << "\t" << s2 << endl; - while( *s1 && *s2 ) { - - bool p1 = ( *s1 == (char)255 ); - bool p2 = ( *s2 == (char)255 ); - //cout << "\t\t " << p1 << "\t" << p2 << endl; - if ( p1 && !p2 ) - return 1; - if ( p2 && !p1 ) - return -1; - - bool n1 = isNumber( *s1 ); - bool n2 = isNumber( *s2 ); - - if ( n1 && n2 ) { - // get rid of leading 0s - while ( *s1 == '0' ) s1++; - while ( *s2 == '0' ) s2++; - - char * e1 = (char*)s1; - char * e2 = (char*)s2; - - // find length - // if end of string, will break immediately ('\0') - while ( isNumber (*e1) ) e1++; - while ( isNumber (*e2) ) e2++; - - int len1 = (int)(e1-s1); - int len2 = (int)(e2-s2); - - int result; - // if one is longer than the other, return - if ( len1 > len2 ) { - return 1; - } - else if ( len2 > len1 ) { - return -1; - } - // if the lengths are equal, just strcmp - else if ( (result = strncmp(s1, s2, len1)) != 0 ) { - return result; - } - - // otherwise, the numbers are equal - s1 = e1; - s2 = e2; - continue; - } - - if ( n1 ) - return 1; - - if ( n2 ) - return -1; - - if ( *s1 > *s2 ) - return 1; - - if ( *s2 > *s1 ) - return -1; - - s1++; s2++; - } - - if ( *s1 ) - return 1; - if ( *s2 ) - return -1; - return 0; - } - /** A generic pointer type for function arguments. * It will convert from any pointer type except auto_ptr. * Semantics are the same as passing the pointer returned from get() @@ -597,6 +477,8 @@ namespace mongo { T* _p; }; + + /** Hmmmm */ using namespace boost; |
