// Make sure we restore to a RecordId at or ahead of save point if same key on reverse cursor.
    void testSaveAndRestorePositionConsidersRecordId_Reverse(bool unique) {
        auto harnessHelper = newHarnessHelper();
        auto opCtx = harnessHelper->newOperationContext();
        auto sorted = harnessHelper->newSortedDataInterface(unique, {
            {key0, loc1},
            {key1, loc1},
            {key2, loc2},
        });

        auto cursor = sorted->newCursor(opCtx.get(), false);

        ASSERT_EQ(cursor->seek(key2, true), IndexKeyEntry(key2, loc2));

        cursor->savePositioned();
        removeFromIndex(opCtx, sorted, {{key2, loc2}});
        insertToIndex(opCtx, sorted, {{key2, loc1}});
        cursor->restore(opCtx.get());

        ASSERT_EQ(cursor->next(), IndexKeyEntry(key2, loc1));

        cursor->savePositioned();
        removeFromIndex(opCtx, sorted, {{key2, loc1}});
        insertToIndex(opCtx, sorted, {{key2, loc2}});
        cursor->restore(opCtx.get());

        ASSERT_EQ(cursor->next(), IndexKeyEntry(key1, loc1));

        cursor->savePositioned();
        removeFromIndex(opCtx, sorted, {{key1, loc1}});
        cursor->restore(opCtx.get());

        cursor->savePositioned();
        insertToIndex(opCtx, sorted, {{key1, loc1}});
        cursor->restore(opCtx.get()); // Lands at same point as initial save.

        // Advances from restore point since restore didn't move position.
        ASSERT_EQ(cursor->next(), IndexKeyEntry(key0, loc1));
    }
    // Ensure that repeated restores lands as close as possible to original position, even if data
    // inserted while saved and the current position removed in a way that temporarily makes the
    // cursor EOF.
    void testSaveAndRestorePositionSeesNewInsertsAfterEOF(bool forward, bool unique) {
        auto harnessHelper = newHarnessHelper();
        auto opCtx = harnessHelper->newOperationContext();
        auto sorted = harnessHelper->newSortedDataInterface(false, {
            {key1, loc1},
        });

        auto cursor = sorted->newCursor(opCtx.get(), forward);

        ASSERT_EQ(cursor->seek(key1, true), IndexKeyEntry(key1, loc1));
        // next() would return EOF now.

        cursor->savePositioned();
        removeFromIndex(opCtx, sorted, {{key1, loc1}});
        cursor->restore(opCtx.get());
        // The restore may have seeked to EOF.

        auto insertPoint = forward ? key2 : key0;
        cursor->savePositioned(); // Should still save key1 as "current position".
        insertToIndex(opCtx, sorted, {{insertPoint, loc1}});
        cursor->restore(opCtx.get());

        ASSERT_EQ(cursor->next(), IndexKeyEntry(insertPoint, loc1));
    }
    // Ensure that repeated restores lands as close as possible to original position, even if data
    // inserted while saved and the current position removed.
    void testSaveAndRestorePositionSeesNewInsertsAfterRemove(bool forward, bool unique) {
        auto harnessHelper = newHarnessHelper();
        auto opCtx = harnessHelper->newOperationContext();
        auto sorted = harnessHelper->newSortedDataInterface(unique, {
            {key1, loc1},
            {key3, loc1},
        });

        auto cursor = sorted->newCursor(opCtx.get(), forward);
        const auto seekPoint = forward ? key1 : key3;

        ASSERT_EQ(cursor->seek(seekPoint, true), IndexKeyEntry(seekPoint, loc1));

        cursor->savePositioned();
        removeFromIndex(opCtx, sorted, {{key1, loc1}});
        cursor->restore(opCtx.get());
        // The restore may have seeked since it can't return to the saved position.

        cursor->savePositioned(); // Should still save originally saved key as "current position".
        insertToIndex(opCtx, sorted, {{key2, loc1}});
        cursor->restore(opCtx.get());

        ASSERT_EQ(cursor->next(), IndexKeyEntry(key2, loc1));
    }
コード例 #4
0
// Insert a single record. Create a repair iterator pointing to that single record.
// Then invalidate the record and ensure that the repair iterator responds correctly.
// See SERVER-16300.
TEST(RecordStoreTestHarness, GetIteratorForRepairInvalidateSingleton) {
    unique_ptr<HarnessHelper> harnessHelper(newHarnessHelper());
    unique_ptr<RecordStore> rs(harnessHelper->newNonCappedRecordStore());

    {
        unique_ptr<OperationContext> opCtx(harnessHelper->newOperationContext());
        ASSERT_EQ(0, rs->numRecords(opCtx.get()));
    }

    // Insert one record.
    RecordId idToInvalidate;
    {
        unique_ptr<OperationContext> opCtx(harnessHelper->newOperationContext());
        WriteUnitOfWork uow(opCtx.get());
        StatusWith<RecordId> res = rs->insertRecord(opCtx.get(), "some data", 10, false);
        ASSERT_OK(res.getStatus());
        idToInvalidate = res.getValue();
        uow.commit();
    }

    // Double-check that the record store has one record in it now.
    {
        unique_ptr<OperationContext> opCtx(harnessHelper->newOperationContext());
        ASSERT_EQ(1, rs->numRecords(opCtx.get()));
    }

    {
        unique_ptr<OperationContext> opCtx(harnessHelper->newOperationContext());
        auto cursor = rs->getCursorForRepair(opCtx.get());
        // returns NULL if getCursorForRepair is not supported
        if (!cursor) {
            return;
        }

        // We should be pointing at the only record in the store.

        // Invalidate the record we're pointing at.
        cursor->savePositioned();
        cursor->invalidate(idToInvalidate);
        cursor->restore(opCtx.get());

        // Iterator should be EOF now because the only thing in the collection got deleted.
        ASSERT(!cursor->next());
    }
}
    // Ensure that restore lands as close as possible to original position, even if data inserted
    // while saved.
    void testSaveAndRestorePositionSeesNewInserts(bool forward, bool unique) {
        auto harnessHelper = newHarnessHelper();
        auto opCtx = harnessHelper->newOperationContext();
        auto sorted = harnessHelper->newSortedDataInterface(unique, {
            {key1, loc1},
            {key3, loc1},
        });

        auto cursor = sorted->newCursor(opCtx.get(), forward);
        const auto seekPoint = forward ? key1 : key3;

        ASSERT_EQ(cursor->seek(seekPoint, true), IndexKeyEntry(seekPoint, loc1));

        cursor->savePositioned();
        insertToIndex(opCtx, sorted, {{key2, loc1}});
        cursor->restore(opCtx.get());

        ASSERT_EQ(cursor->next(), IndexKeyEntry(key2, loc1));
    }
コード例 #6
0
 void saveUnpositioned() final {
     savePositioned();
     _lastReturnedId = RecordId();
 }