0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-29 16:47:28 +01:00

can iterate over a BSONObjBuilder without commiting

This commit is contained in:
Eliot Horowitz 2010-04-21 15:02:37 -04:00
parent 12dbcd8959
commit 64c2ab5a06
3 changed files with 58 additions and 14 deletions

View File

@ -44,6 +44,7 @@ namespace mongo {
class BSONObjBuilder;
class BSONArrayBuilder;
class BSONObjBuilderValueStream;
class BSONObjIterator;
#pragma pack(1)
@ -1627,6 +1628,8 @@ namespace mongo {
bool owned() const {
return &b == &buf_;
}
BSONObjIterator iterator() const ;
private:
// Append the provided arr object as an array.
@ -1751,29 +1754,35 @@ namespace mongo {
BSONObjIterator(const BSONObj& jso) {
int sz = jso.objsize();
if ( sz == 0 ) {
pos = theend = 0;
_pos = _theend = 0;
return;
}
pos = jso.objdata() + 4;
theend = jso.objdata() + sz;
_pos = jso.objdata() + 4;
_theend = jso.objdata() + sz;
}
BSONObjIterator( const char * start , const char * end ){
_pos = start + 4;
_theend = end;
}
/** @return true if more elements exist to be enumerated. */
bool moreWithEOO() {
return pos < theend;
return _pos < _theend;
}
bool more(){
return pos < theend && pos[0];
return _pos < _theend && _pos[0];
}
/** @return the next element in the object. For the final element, element.eoo() will be true. */
BSONElement next( bool checkEnd = false ) {
assert( pos < theend );
BSONElement e( pos, checkEnd ? (int)(theend - pos) : -1 );
pos += e.size( checkEnd ? (int)(theend - pos) : -1 );
assert( _pos < _theend );
BSONElement e( _pos, checkEnd ? (int)(_theend - _pos) : -1 );
_pos += e.size( checkEnd ? (int)(_theend - _pos) : -1 );
return e;
}
private:
const char *pos;
const char *theend;
const char* _pos;
const char* _theend;
};
/* iterator a BSONObj which is an array, in array order.
@ -1988,6 +1997,12 @@ namespace mongo {
dotted2nested(b, obj);
return b.obj();
}
inline BSONObjIterator BSONObjBuilder::iterator() const {
const char * s = b.buf() + offset_;
const char * e = b.buf() + b.len();
return BSONObjIterator( s , e );
}
/* WARNING: nested/dotted conversions are not 100% reversible
* nested2dotted(dotted2nested({a.b: {c:1}})) -> {a.b.c: 1}

View File

@ -1480,6 +1480,38 @@ namespace JsobjTests {
}
};
class BuilderPartialItearte {
public:
void run(){
{
BSONObjBuilder b;
b.append( "x" , 1 );
b.append( "y" , 2 );
BSONObjIterator i = b.iterator();
ASSERT( i.more() );
ASSERT_EQUALS( 1 , i.next().numberInt() );
ASSERT( i.more() );
ASSERT_EQUALS( 2 , i.next().numberInt() );
ASSERT( ! i.more() );
b.append( "z" , 3 );
i = b.iterator();
ASSERT( i.more() );
ASSERT_EQUALS( 1 , i.next().numberInt() );
ASSERT( i.more() );
ASSERT_EQUALS( 2 , i.next().numberInt() );
ASSERT( i.more() );
ASSERT_EQUALS( 3 , i.next().numberInt() );
ASSERT( ! i.more() );
ASSERT_EQUALS( BSON( "x" << 1 << "y" << 2 << "z" << 3 ) , b.obj() );
}
}
};
class All : public Suite {
public:
All() : Suite( "jsobj" ){
@ -1574,6 +1606,7 @@ namespace JsobjTests {
add< InvalidIDFind >();
add< ElementSetTest >();
add< EmbeddedNumbers >();
add< BuilderPartialItearte >();
}
} myall;

View File

@ -102,10 +102,6 @@ namespace mongo {
append( (void *)str.c_str(), str.length() + 1 );
}
void append( int val , int padding ){
}
int len() const {
return l;
}