0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

SERVER-580 add putBack() to db client cursor

This commit is contained in:
Aaron 2010-03-01 10:07:01 -08:00
parent 45d3cfbfcf
commit 218036b2a4
3 changed files with 48 additions and 2 deletions

View File

@ -821,6 +821,9 @@ namespace mongo {
/** If true, safe to call next(). Requests more from server if necessary. */
bool DBClientCursor::more() {
if ( !_putBack.empty() )
return true;
if (haveLimit && pos >= nToReturn)
return false;
@ -836,6 +839,11 @@ namespace mongo {
BSONObj DBClientCursor::next() {
assert( more() );
if ( !_putBack.empty() ) {
BSONObj ret = _putBack.top();
_putBack.pop();
return ret;
}
pos++;
BSONObj o(data);
data += o.objsize();

View File

@ -210,7 +210,7 @@ namespace mongo {
if you want to exhaust whatever data has been fetched to the client already but
then perhaps stop.
*/
bool moreInCurrentBatch() { return pos < nReturned; }
bool moreInCurrentBatch() { return !_putBack.empty() || pos < nReturned; }
/** next
@return next object in the result cursor.
@ -219,6 +219,11 @@ namespace mongo {
if you do not want to handle that yourself, call nextSafe().
*/
BSONObj next();
/**
restore an object previously returned by next() to the cursor
*/
void putBack( const BSONObj &o ) { _putBack.push( o.getOwned() ); }
/** throws AssertionException if get back { $err : ... } */
BSONObj nextSafe() {
@ -317,6 +322,7 @@ namespace mongo {
int opts;
int batchSize;
auto_ptr<Message> m;
stack< BSONObj > _putBack;
int resultFlags;
long long cursorId;

View File

@ -111,7 +111,38 @@ namespace ClientTests {
ASSERT_EQUALS( 1111, c->itcount() );
}
};
class PushBack : public Base {
public:
PushBack() : Base( "PushBack" ) {}
void run() {
for( int i = 0; i < 10; ++i )
db.insert( ns(), BSON( "i" << i ) );
auto_ptr< DBClientCursor > c = db.query( ns(), Query().sort( BSON( "i" << 1 ) ) );
BSONObj o = c->next();
ASSERT( c->more() );
ASSERT( c->moreInCurrentBatch() );
c->putBack( o );
ASSERT( c->more() );
ASSERT( c->moreInCurrentBatch() );
o = c->next();
BSONObj o2 = c->next();
BSONObj o3 = c->next();
c->putBack( o3 );
c->putBack( o2 );
c->putBack( o );
for( int i = 0; i < 10; ++i ) {
o = c->next();
ASSERT_EQUALS( i, o[ "i" ].number() );
}
ASSERT( !c->more() );
ASSERT( !c->moreInCurrentBatch() );
c->putBack( o );
ASSERT( c->more() );
ASSERT( c->moreInCurrentBatch() );
ASSERT_EQUALS( 1, c->itcount() );
}
};
class All : public Suite {
public:
@ -123,6 +154,7 @@ namespace ClientTests {
add<ReIndex>();
add<ReIndex2>();
add<CS_10>();
add<PushBack>();
}
} all;