0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

bsonobj iter seems a littler faster

This commit is contained in:
dwight 2011-03-29 07:49:54 -04:00
parent ac6c7442c3
commit b689325c42
3 changed files with 75 additions and 3 deletions

View File

@ -425,6 +425,72 @@ namespace mongo {
return totalSize;
}
inline int BSONElement::size() const {
if ( totalSize >= 0 )
return totalSize;
int x = 0;
switch ( type() ) {
case EOO:
case Undefined:
case jstNULL:
case MaxKey:
case MinKey:
break;
case mongo::Bool:
x = 1;
break;
case NumberInt:
x = 4;
break;
case Timestamp:
case mongo::Date:
case NumberDouble:
case NumberLong:
x = 8;
break;
case jstOID:
x = 12;
break;
case Symbol:
case Code:
case mongo::String:
x = valuestrsize() + 4;
break;
case DBRef:
x = valuestrsize() + 4 + 12;
break;
case CodeWScope:
case Object:
case mongo::Array:
x = objsize();
break;
case BinData:
x = valuestrsize() + 4 + 1/*subtype*/;
break;
case RegEx:
{
const char *p = value();
size_t len1 = strlen(p);
p = p + len1 + 1;
size_t len2;
len2 = strlen( p );
x = (int) (len1 + 1 + len2 + 1);
}
break;
default:
{
StringBuilder ss;
ss << "BSONElement: bad type " << (int) type();
string msg = ss.str();
massert(10320 , msg.c_str(),false);
}
}
totalSize = x + fieldNameSize() + 1; // BSONType
return totalSize;
}
inline string BSONElement::toString( bool includeFieldName, bool full ) const {
StringBuilder s;
toString(s, includeFieldName, full);

View File

@ -120,7 +120,8 @@ namespace mongo {
/** Size of the element.
@param maxLen If maxLen is specified, don't scan more than maxLen bytes to calculate size.
*/
int size( int maxLen = -1 ) const;
int size( int maxLen ) const;
int size() const;
/** Wrap this element up as a singleton object. */
BSONObj wrap() const;

View File

@ -55,13 +55,18 @@ namespace mongo {
bool moreWithEOO() { return _pos < _theend; }
/** @return the next element in the object. For the final element, element.eoo() will be true. */
BSONElement next( bool checkEnd = false ) {
BSONElement next( bool checkEnd ) {
assert( _pos < _theend );
BSONElement e( _pos, checkEnd ? (int)(_theend - _pos) : -1 );
_pos += e.size( checkEnd ? (int)(_theend - _pos) : -1 );
return e;
}
BSONElement next() {
assert( _pos < _theend );
BSONElement e( _pos, -1 );
_pos += e.size( -1 );
return e;
}
void operator++() { next(); }
void operator++(int) { next(); }