void run() { // Run the update. { Client::WriteContext ctx(&_txn, ns()); Client& c = cc(); CurOp& curOp = *c.curop(); OpDebug* opDebug = &curOp.debug(); UpdateDriver driver( (UpdateDriver::Options()) ); Database* db = ctx.ctx().db(); // Collection should be empty. ASSERT_EQUALS(0U, count(BSONObj())); UpdateRequest request(&_txn, nsString()); UpdateLifecycleImpl updateLifecycle(false, nsString()); request.setLifecycle(&updateLifecycle); // Update is the upsert {_id: 0, x: 1}, {$set: {y: 2}}. BSONObj query = fromjson("{_id: 0, x: 1}"); BSONObj updates = fromjson("{$set: {y: 2}}"); request.setUpsert(); request.setQuery(query); request.setUpdates(updates); ASSERT_OK(driver.parse(request.getUpdates(), request.isMulti())); // Setup update params. UpdateStageParams params(&request, &driver, opDebug); scoped_ptr<CanonicalQuery> cq(canonicalize(query)); params.canonicalQuery = cq.get(); scoped_ptr<WorkingSet> ws(new WorkingSet()); auto_ptr<EOFStage> eofStage(new EOFStage()); scoped_ptr<UpdateStage> updateStage( new UpdateStage(params, ws.get(), db, eofStage.release())); runUpdate(updateStage.get()); ctx.commit(); } // Verify the contents of the resulting collection. { Client::ReadContext ctx(&_txn, ns()); Collection* collection = ctx.ctx().db()->getCollection(&_txn, ns()); vector<BSONObj> objs; getCollContents(collection, &objs); // Expect a single document, {_id: 0, x: 1, y: 2}. ASSERT_EQUALS(1U, objs.size()); ASSERT_EQUALS(objs[0], fromjson("{_id: 0, x: 1, y: 2}")); } }
void SelectStageScene::prev(){ _curPage--; _curPage += _totalPages; _curPage = _curPage % _totalPages; updateStage(); }
void SelectStageScene::next(){ _curPage++; _curPage = _curPage % _totalPages; updateStage(); }
bool SelectStageScene::init(int level){ if(!CCScene::init()){ return false; } _selectLevel = level; CCSprite* bg = CCSprite::create(Images::game::game_bg_jpg); addChild(bg, -1); bg->setAnchorPoint(ccp(0, 0)); CCMenu* menu = CCMenu::create(); addChild(menu, 1); menu->setAnchorPoint(ccp(0, 0)); menu->setPosition(ccp(0, 0)); CCMenuItem* backItem = UIUtil::getSingleImageBtn( Images::common::back); menu->addChild(backItem); backItem->setTarget(this, menu_selector(SelectStageScene::onClickBtn)); backItem->setTag(-1); backItem->setAnchorPoint(ccp(1, 0)); LayoutUtil::layoutTo(backItem, 0, 1, bg, 0, 1, 4, -4); CCLabelTTF* title = CCLabelTTF::create("选择关卡", "arial-bold", 38); title->setColor(ccBLACK); addChild(title, 1); LayoutUtil::layoutTo(title, 0.5, 0, bg, 0.5, 1, 0, -110); CCLabelTTF* subTitleText = CCLabelTTF::create("难度:01/11", "arial-bold", 30); subTitleText->setColor(ccBLACK); addChild(subTitleText, 1); LayoutUtil::layoutTo(subTitleText, 0.5, 1, title, 0.5, 0, 0, -20); CCSize midSize = CCSize(400, 400); _midArea = CCNode::create(); _midArea->setContentSize(midSize); addChild(_midArea, 1); LayoutUtil::layoutToCenter(_midArea, bg, 0, -30); for (int i = 0; i < 16; i++) { int row = i / 4; int col = i % 4; _grids[i] = UIUtil::getSingleImageBtn(Images::selectLevel::unlock); _grids[i]->setTarget(this, menu_selector(SelectStageScene::onClickBtn)); menu->addChild(_grids[i]); LayoutUtil::layoutTo(_grids[i], 0.5, 0.5, _midArea, 0.5, 1, (col - 1.5f) * (midSize.width * 0.25f), (-row - 0.5) * (midSize.height * 0.25f)); _gridTexts[i] = CCLabelTTF::create("1", "arial", 32); _gridTexts[i]->setColor(ccBLACK); addChild(_gridTexts[i], 9); LayoutUtil::layoutToCenter(_gridTexts[i], _grids[i]); _gridLocks[i] = UIUtil::getSingleImageBtn(Images::selectLevel::lock); menu->addChild(_gridLocks[i]); LayoutUtil::layoutToCenter(_gridLocks[i], _grids[i]); _grids[i]->setVisible(false); } CCMenuItem* leftArrow = UIUtil::getSingleImageBtn(Images::selectLevel::icon_arrow); leftArrow->setTarget(this, menu_selector(SelectStageScene::prev)); menu->addChild(leftArrow); LayoutUtil::layoutTo(leftArrow, 1, 0.5, _midArea, 0, 0.5, 0); CCSprite* leftArrowBg = CCSprite::create(Images::selectLevel::icon_arrow); leftArrowBg->setScale(1.1); leftArrowBg->setColor(ccBLACK); addChild(leftArrowBg, 0); LayoutUtil::layoutToCenter(leftArrowBg, leftArrow); CCMenuItem* rightArrow = UIUtil::getSingleImageBtn(Images::selectLevel::icon_arrow); rightArrow->setTarget(this, menu_selector(SelectStageScene::next)); menu->addChild(rightArrow); rightArrow->setScaleX(-1); LayoutUtil::layoutTo(rightArrow, 0, 0.5, _midArea, 1, 0.5, 0); CCSprite* rightArrowBg = CCSprite::create(Images::selectLevel::icon_arrow); rightArrowBg->setScaleX(-1.1); rightArrowBg->setScaleY(1.1); rightArrowBg->setColor(ccBLACK); addChild(rightArrowBg, 0); LayoutUtil::layoutToCenter(rightArrowBg, rightArrow); cocos2d::extension::CCScale9Sprite* numBg = cocos2d::extension::CCScale9Sprite::create(CCRect(16, 16, 2, 2), Images::common::numBg); numBg->setContentSize(CCSize(110, numBg->getContentSize().height)); addChild(numBg, 1); LayoutUtil::layoutTo(numBg, 1, 0, _midArea, 1, 1, 60, 5); char temp[32]; sprintf(temp, "%d/%d", _curPage + 1, _totalPages); _indexText = CCLabelTTF::create("1/1", "arial", 26); _indexText->setAnchorPoint(ccp(1, 0)); addChild(_indexText, 1); LayoutUtil::layoutTo(_indexText, 1, 0.5, numBg, 0.7, 0.5, 0, 0); sprintf(temp, "难度:%02d/11", _selectLevel); subTitleText->setString(temp); MusicBtn* musicBtn = MusicBtn::create(); addChild(musicBtn, 1); LayoutUtil::layoutTo(musicBtn, 0, 0, bg, 0, 0, 5, 5); updateStage(); AdUtil::showAd(); return true; }
void run() { // Run the update. { Client::WriteContext ctx(&_txn, ns()); // Populate the collection. for (int i = 0; i < 10; ++i) { insert(BSON("_id" << i << "foo" << i)); } ASSERT_EQUALS(10U, count(BSONObj())); Client& c = cc(); CurOp& curOp = *c.curop(); OpDebug* opDebug = &curOp.debug(); UpdateDriver driver( (UpdateDriver::Options()) ); Database* db = ctx.ctx().db(); Collection* coll = db->getCollection(&_txn, ns()); // Get the DiskLocs that would be returned by an in-order scan. vector<DiskLoc> locs; getLocs(coll, CollectionScanParams::FORWARD, &locs); UpdateRequest request(&_txn, nsString()); UpdateLifecycleImpl updateLifecycle(false, nsString()); request.setLifecycle(&updateLifecycle); // Update is a multi-update that sets 'bar' to 3 in every document // where foo is less than 5. BSONObj query = fromjson("{foo: {$lt: 5}}"); BSONObj updates = fromjson("{$set: {bar: 3}}"); request.setMulti(); request.setQuery(query); request.setUpdates(updates); ASSERT_OK(driver.parse(request.getUpdates(), request.isMulti())); // Configure the scan. CollectionScanParams collScanParams; collScanParams.collection = coll; collScanParams.direction = CollectionScanParams::FORWARD; collScanParams.tailable = false; // Configure the update. UpdateStageParams updateParams(&request, &driver, opDebug); scoped_ptr<CanonicalQuery> cq(canonicalize(query)); updateParams.canonicalQuery = cq.get(); scoped_ptr<WorkingSet> ws(new WorkingSet()); auto_ptr<CollectionScan> cs( new CollectionScan(&_txn, collScanParams, ws.get(), cq->root())); scoped_ptr<UpdateStage> updateStage( new UpdateStage(updateParams, ws.get(), db, cs.release())); const UpdateStats* stats = static_cast<const UpdateStats*>(updateStage->getSpecificStats()); const size_t targetDocIndex = 3; while (stats->nModified < targetDocIndex) { WorkingSetID id = WorkingSet::INVALID_ID; PlanStage::StageState state = updateStage->work(&id); ASSERT_EQUALS(PlanStage::NEED_TIME, state); } // Remove locs[targetDocIndex]; updateStage->saveState(); updateStage->invalidate(locs[targetDocIndex], INVALIDATION_DELETION); BSONObj targetDoc = coll->docFor(&_txn, locs[targetDocIndex]); ASSERT(!targetDoc.isEmpty()); remove(targetDoc); updateStage->restoreState(&_txn); // Do the remaining updates. while (!updateStage->isEOF()) { WorkingSetID id = WorkingSet::INVALID_ID; PlanStage::StageState state = updateStage->work(&id); ASSERT(PlanStage::NEED_TIME == state || PlanStage::IS_EOF == state); } ctx.commit(); // 4 of the 5 matching documents should have been modified (one was deleted). ASSERT_EQUALS(4U, stats->nModified); ASSERT_EQUALS(4U, stats->nMatched); } // Check the contents of the collection. { Client::ReadContext ctx(&_txn, ns()); Collection* collection = ctx.ctx().db()->getCollection(&_txn, ns()); vector<BSONObj> objs; getCollContents(collection, &objs); // Verify that the collection now has 9 docs (one was deleted). ASSERT_EQUALS(9U, objs.size()); // Make sure that the collection has certain documents. assertHasDoc(objs, fromjson("{_id: 0, foo: 0, bar: 3}")); assertHasDoc(objs, fromjson("{_id: 1, foo: 1, bar: 3}")); assertHasDoc(objs, fromjson("{_id: 2, foo: 2, bar: 3}")); assertHasDoc(objs, fromjson("{_id: 4, foo: 4, bar: 3}")); assertHasDoc(objs, fromjson("{_id: 5, foo: 5}")); assertHasDoc(objs, fromjson("{_id: 6, foo: 6}")); } }