Skip to content

Commit

Permalink
SERVER-1278 Expose cursor buffer remains to js testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Lerner committed Jul 22, 2010
1 parent b0d3fa0 commit f9c5b67
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions client/dbclientcursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace mongo {
if you want to exhaust whatever data has been fetched to the client already but
then perhaps stop.
*/
bool moreInCurrentBatch() { _assertIfNull(); return !_putBack.empty() || pos < nReturned; }
int objsLeftInBatch() const { _assertIfNull(); return _putBack.size() + nReturned - pos; }
bool moreInCurrentBatch() { return objsLeftInBatch() > 0; }

/** next
@return next object in the result cursor.
Expand Down Expand Up @@ -179,7 +180,7 @@ namespace mongo {
string _scopedHost;

// Don't call from a virtual function
void _assertIfNull() { uassert(13348, "connection died", this); }
void _assertIfNull() const { uassert(13348, "connection died", this); }
};

/** iterate over objects in current batch only - will not cause a network call
Expand Down
8 changes: 8 additions & 0 deletions dbtests/clienttests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,17 @@ namespace ClientTests {
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_EQUALS( 9 , c->objsLeftInBatch() );
ASSERT( c->moreInCurrentBatch() );

c->putBack( o );
ASSERT( c->more() );
ASSERT_EQUALS( 10, c->objsLeftInBatch() );
ASSERT( c->moreInCurrentBatch() );

o = c->next();
BSONObj o2 = c->next();
BSONObj o3 = c->next();
Expand All @@ -136,9 +141,12 @@ namespace ClientTests {
ASSERT_EQUALS( i, o[ "i" ].number() );
}
ASSERT( !c->more() );
ASSERT_EQUALS( 0, c->objsLeftInBatch() );
ASSERT( !c->moreInCurrentBatch() );

c->putBack( o );
ASSERT( c->more() );
ASSERT_EQUALS( 1, c->objsLeftInBatch() );
ASSERT( c->moreInCurrentBatch() );
ASSERT_EQUALS( 1, c->itcount() );
}
Expand Down
8 changes: 8 additions & 0 deletions scripting/sm_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ namespace mongo {
return JS_TRUE;
}

JSBool internal_cursor_objsLeftInBatch(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){
DBClientCursor *cursor = getCursor( cx, obj );
Convertor c(cx);
*rval = c.toval((double) cursor->objsLeftInBatch() );
return JS_TRUE;
}

JSBool internal_cursor_next(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){
DBClientCursor *cursor = getCursor( cx, obj );
if ( ! cursor->more() ){
Expand All @@ -114,6 +121,7 @@ namespace mongo {

JSFunctionSpec internal_cursor_functions[] = {
{ "hasNext" , internal_cursor_hasNext , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ "objsLeftInBatch" , internal_cursor_objsLeftInBatch , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ "next" , internal_cursor_next , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ 0 }
};
Expand Down
13 changes: 13 additions & 0 deletions scripting/v8_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace mongo {
ic->InstanceTemplate()->SetInternalFieldCount( 1 );
ic->PrototypeTemplate()->Set( v8::String::New("next") , FunctionTemplate::New( internalCursorNext ) );
ic->PrototypeTemplate()->Set( v8::String::New("hasNext") , FunctionTemplate::New( internalCursorHasNext ) );
ic->PrototypeTemplate()->Set( v8::String::New("objsLeftInBatch") , FunctionTemplate::New( internalCursorObjsLeftInBatch ) );
proto->Set( v8::String::New( "internalCursor" ) , ic );


Expand Down Expand Up @@ -409,6 +410,18 @@ namespace mongo {
return Boolean::New( ret );
}

v8::Handle<v8::Value> internalCursorObjsLeftInBatch(const v8::Arguments& args){
mongo::DBClientCursor * cursor = getCursor( args );
if ( ! cursor )
return V8::Number::New( (double) 0 );
int ret;
{
v8::Unlocker u;
ret = cursor->objsLeftInBatch();
}
return V8::Number::New( (double) ret );
}


// --- DB ----

Expand Down

0 comments on commit f9c5b67

Please sign in to comment.