void run() { Client::WriteContext ctx(&_txn, ns()); Database* db = ctx.ctx().db(); Collection* coll = db->getCollection(&_txn, ns()); if (!coll) { WriteUnitOfWork wuow(&_txn); coll = db->createCollection(&_txn, ns()); wuow.commit(); } WorkingSet ws; std::set<WorkingSetID> expectedResultIds; std::set<WorkingSetID> resultIds; // Create a KeepMutationsStage with an EOF child, and flag 50 objects. We expect these // objects to be returned by the KeepMutationsStage. MatchExpression* nullFilter = NULL; std::auto_ptr<KeepMutationsStage> keep(new KeepMutationsStage(nullFilter, &ws, new EOFStage())); for (size_t i = 0; i < 50; ++i) { WorkingSetID id = ws.allocate(); WorkingSetMember* member = ws.get(id); member->state = WorkingSetMember::OWNED_OBJ; member->obj = BSON("x" << 1); ws.flagForReview(id); expectedResultIds.insert(id); } // Call work() on the KeepMutationsStage. The stage should start streaming the // already-flagged objects. WorkingSetID id = getNextResult(keep.get()); resultIds.insert(id); // Flag more objects, then call work() again on the KeepMutationsStage, and expect none // of the newly-flagged objects to be returned (the KeepMutationsStage does not // incorporate objects flagged since the streaming phase started). // // This condition triggers SERVER-15580 (the new flagging causes a rehash of the // unordered_set "WorkingSet::_flagged", which invalidates all iterators, which were // previously being dereferenced in KeepMutationsStage::work()). // Note that std::unordered_set<>::insert() triggers a rehash if the new number of // elements is greater than or equal to max_load_factor()*bucket_count(). size_t rehashSize = static_cast<size_t>(ws.getFlagged().max_load_factor() * ws.getFlagged().bucket_count()); while (ws.getFlagged().size() <= rehashSize) { WorkingSetID id = ws.allocate(); WorkingSetMember* member = ws.get(id); member->state = WorkingSetMember::OWNED_OBJ; member->obj = BSON("x" << 1); ws.flagForReview(id); } while ((id = getNextResult(keep.get())) != WorkingSet::INVALID_ID) { resultIds.insert(id); } // Assert that only the first 50 objects were returned. ASSERT(expectedResultIds == resultIds); }
void run() { OldClientWriteContext ctx(&_txn, ns()); Database* db = ctx.db(); Collection* coll = db->getCollection(ns()); if (!coll) { WriteUnitOfWork wuow(&_txn); coll = db->createCollection(&_txn, ns()); wuow.commit(); } WorkingSet ws; // Add 10 objects to the collection. for (size_t i = 0; i < 10; ++i) { insert(BSON("x" << 1)); } // Create 10 objects that are flagged. for (size_t i = 0; i < 10; ++i) { WorkingSetID id = ws.allocate(); WorkingSetMember* member = ws.get(id); member->state = WorkingSetMember::OWNED_OBJ; member->obj = Snapshotted<BSONObj>(SnapshotId(), BSON("x" << 2)); ws.flagForReview(id); } // Create a collscan to provide the 10 objects in the collection. CollectionScanParams params; params.collection = coll; params.direction = CollectionScanParams::FORWARD; params.tailable = false; params.start = RecordId(); CollectionScan* cs = new CollectionScan(&_txn, params, &ws, NULL); // Create a KeepMutations stage to merge in the 10 flagged objects. // Takes ownership of 'cs' MatchExpression* nullFilter = NULL; std::unique_ptr<KeepMutationsStage> keep(new KeepMutationsStage(nullFilter, &ws, cs)); for (size_t i = 0; i < 10; ++i) { WorkingSetID id = getNextResult(keep.get()); WorkingSetMember* member = ws.get(id); ASSERT_FALSE(ws.isFlagged(id)); ASSERT_EQUALS(member->obj.value()["x"].numberInt(), 1); } { WorkingSetID out; ASSERT_EQ(cs->work(&out), PlanStage::IS_EOF); } // Flagged results *must* be at the end. for (size_t i = 0; i < 10; ++i) { WorkingSetID id = getNextResult(keep.get()); WorkingSetMember* member = ws.get(id); ASSERT(ws.isFlagged(id)); ASSERT_EQUALS(member->obj.value()["x"].numberInt(), 2); } }
void run() { OperationContextImpl txn; Client::WriteContext ctx(&txn, ns()); Database* db = ctx.ctx().db(); Collection* coll = db->getCollection(&txn, ns()); if (!coll) { coll = db->createCollection(&txn, ns()); } WorkingSet ws; // Add 10 objects to the collection. for (size_t i = 0; i < 10; ++i) { insert(BSON("x" << 1)); } // Create 10 objects that are flagged. for (size_t i = 0; i < 10; ++i) { WorkingSetID id = ws.allocate(); WorkingSetMember* member = ws.get(id); member->state = WorkingSetMember::OWNED_OBJ; member->obj = BSON("x" << 2); ws.flagForReview(id); } // Create a collscan to provide the 10 objects in the collection. CollectionScanParams params; params.collection = coll; params.direction = CollectionScanParams::FORWARD; params.tailable = false; params.start = DiskLoc(); CollectionScan* cs = new CollectionScan(params, &ws, NULL); // Create a KeepMutations stage to merge in the 10 flagged objects. // Takes ownership of 'cs' KeepMutationsStage* keep = new KeepMutationsStage(NULL, &ws, cs); for (size_t i = 0; i < 10; ++i) { WorkingSetID id = getNextResult(keep); WorkingSetMember* member = ws.get(id); ASSERT_FALSE(ws.isFlagged(id)); ASSERT_EQUALS(member->obj["x"].numberInt(), 1); } ASSERT(cs->isEOF()); // Flagged results *must* be at the end. for (size_t i = 0; i < 10; ++i) { WorkingSetID id = getNextResult(keep); WorkingSetMember* member = ws.get(id); ASSERT(ws.isFlagged(id)); ASSERT_EQUALS(member->obj["x"].numberInt(), 2); } }