diff options
author | dwight <dwight@10gen.com> | 2011-05-23 10:28:52 -0400 |
---|---|---|
committer | dwight <dwight@10gen.com> | 2011-05-23 20:03:11 -0400 |
commit | b99eaa3387cb7678d216a43338b8f897baa11638 (patch) | |
tree | f3e693b6c62f49bd52a7bd40d44b73a4a3c7eae5 /bson | |
parent | 3f9878191e9f44ec7cd48a1e439c2931d3ee1bc1 (diff) | |
download | mongo-b99eaa3387cb7678d216a43338b8f897baa11638.tar.gz |
bson getfields method
getfirstelementfieldname method - faster than firstElement().fieldName()
bson cleaning tweaking and comments
Diffstat (limited to 'bson')
-rw-r--r-- | bson/bson-inl.h | 33 | ||||
-rw-r--r-- | bson/bsonelement.h | 6 | ||||
-rw-r--r-- | bson/bsonobj.h | 42 |
3 files changed, 60 insertions, 21 deletions
diff --git a/bson/bson-inl.h b/bson/bson-inl.h index ee7010dc06f..8d2e34a5b74 100644 --- a/bson/bson-inl.h +++ b/bson/bson-inl.h @@ -89,16 +89,18 @@ namespace mongo { return b.obj(); } - inline bool BSONObj::hasElement(const char *name) const { - if ( !isEmpty() ) { - BSONObjIterator it(*this); - while ( it.moreWithEOO() ) { - BSONElement e = it.next(); - if ( strcmp(name, e.fieldName()) == 0 ) - return true; + inline void BSONObj::getFields(unsigned n, const char **fieldNames, BSONElement *fields) const { + BSONObjIterator i(*this); + while ( i.more() ) { + BSONElement e = i.next(); + const char *p = e.fieldName(); + for( unsigned i = 0; i < n; i++ ) { + if( strcmp(p, fieldNames[i]) == 0 ) { + fields[i] = e; + break; + } } } - return false; } inline BSONElement BSONObj::getField(const StringData& name) const { @@ -111,6 +113,21 @@ namespace mongo { return BSONElement(); } + inline int BSONObj::getIntField(const char *name) const { + BSONElement e = getField(name); + return e.isNumber() ? (int) e.number() : INT_MIN; + } + + inline bool BSONObj::getBoolField(const char *name) const { + BSONElement e = getField(name); + return e.type() == Bool ? e.boolean() : false; + } + + inline const char * BSONObj::getStringField(const char *name) const { + BSONElement e = getField(name); + return e.type() == String ? e.valuestr() : ""; + } + /* add all the fields from the object specified to this object */ inline BSONObjBuilder& BSONObjBuilder::appendElements(BSONObj x) { BSONObjIterator it(x); diff --git a/bson/bsonelement.h b/bson/bsonelement.h index 2b63a869c21..a6bf23e825b 100644 --- a/bson/bsonelement.h +++ b/bson/bsonelement.h @@ -153,9 +153,11 @@ namespace mongo { You must assure element is a boolean before calling. */ bool boolean() const { - return *value() ? true : false; + return *value(); // ? true : false; } + bool booleanSafe() const { return isBoolean() && boolean(); } + /** Retrieve a java style date value from the element. Ensure element is of type Date before calling. @see Bool(), trueValue() @@ -495,7 +497,7 @@ namespace mongo { return true; } - /** True if element is of a numeric type. */ + /** @return true if element is of a numeric type. */ inline bool BSONElement::isNumber() const { switch( type() ) { case NumberLong: diff --git a/bson/bsonobj.h b/bson/bsonobj.h index cf402bdbb33..548d7f07bbe 100644 --- a/bson/bsonobj.h +++ b/bson/bsonobj.h @@ -124,7 +124,9 @@ namespace mongo { */ bool isOwned() const { return _holder.get() != 0; } - /* make sure the data buffer is under the control of this BSONObj and not a remote buffer */ + /** assure the data buffer is under the control of this BSONObj and not a remote buffer + @see isOwned() + */ BSONObj getOwned() const; /** @return a new full (and owned) copy of the object. */ @@ -152,18 +154,20 @@ namespace mongo { /** adds the field names to the fields set. does NOT clear it (appends). */ int getFieldNames(set<string>& fields) const; - /** return has eoo() true if no match - supports "." notation to reach into embedded objects + /** @return the specified element. element.eoo() will be true if not found. + @param name field to find. supports dot (".") notation to reach into embedded objects. + for example "x.y" means "in the nested object in field x, retrieve field y" */ BSONElement getFieldDotted(const char *name) const; - /** return has eoo() true if no match - supports "." notation to reach into embedded objects + /** @return the specified element. element.eoo() will be true if not found. + @param name field to find. supports dot (".") notation to reach into embedded objects. + for example "x.y" means "in the nested object in field x, retrieve field y" */ BSONElement getFieldDotted(const string& name) const { return getFieldDotted( name.c_str() ); } - /** Like getFieldDotted(), but expands multikey arrays and returns all matching objects. + /** Like getFieldDotted(), but expands arrays and returns all matching objects. * Turning off expandLastArray allows you to retrieve nested array objects instead of * their contents. */ @@ -180,6 +184,14 @@ namespace mongo { */ BSONElement getField(const StringData& name) const; + /** Get several fields at once. This is faster than separate getField() calls as the size of + elements iterated can then be calculated only once each. + @param n number of fieldNames, and number of elements in the fields array + @param fields if a field is found its element is stored in its corresponding position in this array. + if not found the array element is unchanged. + */ + void getFields(unsigned n, const char **fieldNames, BSONElement *fields) const; + /** Get the field of the specified name. eoo() is true on the returned element if not found. */ @@ -199,7 +211,9 @@ namespace mongo { } /** @return true if field exists */ - bool hasField( const char * name ) const { return ! getField( name ).eoo(); } + bool hasField( const char * name ) const { return !getField(name).eoo(); } + /** @return true if field exists */ + bool hasElement(const char *name) const { return hasField(name); } /** @return "" if DNE or wrong type */ const char * getStringField(const char *name) const; @@ -210,7 +224,9 @@ namespace mongo { /** @return INT_MIN if not present - does some type conversions */ int getIntField(const char *name) const; - /** @return false if not present */ + /** @return false if not present + @see BSONElement::trueValue() + */ bool getBoolField(const char *name) const; /** @@ -270,7 +286,6 @@ namespace mongo { int woCompare(const BSONObj& r, const BSONObj &ordering = BSONObj(), bool considerFieldName=true) const; - bool operator<( const BSONObj& other ) const { return woCompare( other ) < 0; } bool operator<=( const BSONObj& other ) const { return woCompare( other ) <= 0; } bool operator>( const BSONObj& other ) const { return woCompare( other ) > 0; } @@ -295,8 +310,13 @@ namespace mongo { /** @return first field of the object */ BSONElement firstElement() const { return BSONElement(objdata() + 4); } - /** @return true if field exists in the object */ - bool hasElement(const char *name) const; + /** faster than firstElement().fieldName() - for the first element we can easily find the fieldname without + computing the element size. + */ + const char * firstElementFieldName() const { + const char *p = objdata() + 4; + return *p == EOO ? "" : p+1; + } /** Get the _id field from the object. For good performance drivers should assure that _id is the first element of the object; however, correct operation |