JSBool internal_cursor_next(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ DBClientCursor *cursor = getCursor( cx, obj ); Convertor c(cx); BSONObj n = cursor->next(); *rval = c.toval( &n ); return JS_TRUE; }
/* * has_more = cursor:has_more(in_current_batch) * pass true to call moreInCurrentBatch (mongo >=1.5) */ static int cursor_has_more(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); bool in_current_batch = lua_toboolean(L, 2); if (in_current_batch) lua_pushboolean(L, cursor->moreInCurrentBatch()); else lua_pushboolean(L, cursor->more()); return 1; }
static int result_iterator(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, lua_upvalueindex(1)); if (cursor->more()) { bson_to_lua(L, cursor->next()); } else { lua_pushnil(L); } return 1; }
/* * res = cursor:next() */ static int cursor_next(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); if (cursor->more()) { bson_to_lua(L, cursor->next()); } else { lua_pushnil(L); } return 1; }
// "./db testclient" to invoke void testClient3() { out() << "testClient()" << endl; // DBClientConnection c(true); DBClientPaired c; string err; if ( !c.connect("10.211.55.2", "1.2.3.4") ) { // if( !c.connect("10.211.55.2", err) ) { out() << "testClient: connect() failed" << endl; } else { // temp: out() << "test query returns: " << c.findOne("foo.bar", fromjson("{}")).toString() << endl; } again: out() << "query foo.bar..." << endl; auto_ptr<DBClientCursor> cursor = c.query("foo.bar", BSONObj(), 0, 0, 0, Option_CursorTailable); DBClientCursor *cc = cursor.get(); if ( cc == 0 ) { out() << "query() returned 0, sleeping 10 secs" << endl; sleepsecs(10); goto again; } while ( 1 ) { bool m; try { m = cc->more(); } catch (AssertionException&) { out() << "more() asserted, sleeping 10 sec" << endl; goto again; } out() << "more: " << m << " dead:" << cc->isDead() << endl; if ( !m ) { if ( cc->isDead() ) out() << "cursor dead, stopping" << endl; else { out() << "Sleeping 10 seconds" << endl; sleepsecs(10); continue; } break; } out() << cc->next().toString() << endl; } }
/* * id = cursor:get_id() */ static int cursor_get_id(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); lua_pushnumber(L, cursor->getCursorId()); return 1; }
/* * has_result_flag = cursor:has_result_flag() */ static int cursor_has_result_flag(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); int flag = lua_tointeger(L, 2); lua_pushboolean(L, cursor->hasResultFlag(flag)); return 1; }
/* * is_tailable = cursor:is_tailable() */ static int cursor_is_tailable(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); lua_pushboolean(L, cursor->tailable()); return 1; }
/* * is_dead = cursor:is_dead() */ static int cursor_is_dead(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); lua_pushboolean(L, cursor->isDead()); return 1; }
/* * it_count = cursor:itcount() */ static int cursor_itcount(lua_State *L) { DBClientCursor *cursor = userdata_to_cursor(L, 1); lua_pushinteger(L, cursor->itcount()); return 1; }
JSBool internal_cursor_hasNext(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ DBClientCursor *cursor = getCursor( cx, obj ); *rval = cursor->more() ? JSVAL_TRUE : JSVAL_FALSE; return JS_TRUE; }