mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
can iterate over a BSONObjBuilder without commiting
This commit is contained in:
parent
12dbcd8959
commit
64c2ab5a06
35
db/jsobj.h
35
db/jsobj.h
@ -44,6 +44,7 @@ namespace mongo {
|
|||||||
class BSONObjBuilder;
|
class BSONObjBuilder;
|
||||||
class BSONArrayBuilder;
|
class BSONArrayBuilder;
|
||||||
class BSONObjBuilderValueStream;
|
class BSONObjBuilderValueStream;
|
||||||
|
class BSONObjIterator;
|
||||||
|
|
||||||
#pragma pack(1)
|
#pragma pack(1)
|
||||||
|
|
||||||
@ -1628,6 +1629,8 @@ namespace mongo {
|
|||||||
return &b == &buf_;
|
return &b == &buf_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BSONObjIterator iterator() const ;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Append the provided arr object as an array.
|
// Append the provided arr object as an array.
|
||||||
void marshalArray( const char *fieldName, const BSONObj &arr ) {
|
void marshalArray( const char *fieldName, const BSONObj &arr ) {
|
||||||
@ -1751,29 +1754,35 @@ namespace mongo {
|
|||||||
BSONObjIterator(const BSONObj& jso) {
|
BSONObjIterator(const BSONObj& jso) {
|
||||||
int sz = jso.objsize();
|
int sz = jso.objsize();
|
||||||
if ( sz == 0 ) {
|
if ( sz == 0 ) {
|
||||||
pos = theend = 0;
|
_pos = _theend = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pos = jso.objdata() + 4;
|
_pos = jso.objdata() + 4;
|
||||||
theend = jso.objdata() + sz;
|
_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. */
|
/** @return true if more elements exist to be enumerated. */
|
||||||
bool moreWithEOO() {
|
bool moreWithEOO() {
|
||||||
return pos < theend;
|
return _pos < _theend;
|
||||||
}
|
}
|
||||||
bool more(){
|
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. */
|
/** @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 = false ) {
|
||||||
assert( pos < theend );
|
assert( _pos < _theend );
|
||||||
BSONElement e( pos, checkEnd ? (int)(theend - pos) : -1 );
|
BSONElement e( _pos, checkEnd ? (int)(_theend - _pos) : -1 );
|
||||||
pos += e.size( checkEnd ? (int)(theend - pos) : -1 );
|
_pos += e.size( checkEnd ? (int)(_theend - _pos) : -1 );
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
const char *pos;
|
const char* _pos;
|
||||||
const char *theend;
|
const char* _theend;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* iterator a BSONObj which is an array, in array order.
|
/* iterator a BSONObj which is an array, in array order.
|
||||||
@ -1989,6 +1998,12 @@ namespace mongo {
|
|||||||
return 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
|
/* WARNING: nested/dotted conversions are not 100% reversible
|
||||||
* nested2dotted(dotted2nested({a.b: {c:1}})) -> {a.b.c: 1}
|
* nested2dotted(dotted2nested({a.b: {c:1}})) -> {a.b.c: 1}
|
||||||
* also, dotted2nested ignores order
|
* also, dotted2nested ignores order
|
||||||
|
@ -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 {
|
class All : public Suite {
|
||||||
public:
|
public:
|
||||||
All() : Suite( "jsobj" ){
|
All() : Suite( "jsobj" ){
|
||||||
@ -1574,6 +1606,7 @@ namespace JsobjTests {
|
|||||||
add< InvalidIDFind >();
|
add< InvalidIDFind >();
|
||||||
add< ElementSetTest >();
|
add< ElementSetTest >();
|
||||||
add< EmbeddedNumbers >();
|
add< EmbeddedNumbers >();
|
||||||
|
add< BuilderPartialItearte >();
|
||||||
}
|
}
|
||||||
} myall;
|
} myall;
|
||||||
|
|
||||||
|
@ -102,10 +102,6 @@ namespace mongo {
|
|||||||
append( (void *)str.c_str(), str.length() + 1 );
|
append( (void *)str.c_str(), str.length() + 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
void append( int val , int padding ){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int len() const {
|
int len() const {
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user