Search in sources :

Example 26 with TestHelpers

use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreIndexTest method buildNewIndex.

/**
 * Verify that building an index works for a single-type index.
 */
@Test
public void buildNewIndex() throws Exception {
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context);
        recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_EXISTS).join();
        for (int i = 0; i < 10; i++) {
            TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
            recBuilder.setRecNo(i);
            recBuilder.setNumValue2(i % 10);
            recordStore.saveRecord(recBuilder.build());
        }
        commit(context);
    }
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_2").equalsValue(6)).build();
    assertEquals(Arrays.asList(6L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, this::uncheckedOpenSimpleRecordStore, context -> assertDiscardedAtLeast(9, context)));
    // Adds an index for that query.
    RecordMetaDataHook hook = metaData -> metaData.addIndex("MySimpleRecord", "new_index", "num_value_2");
    Opener openWithNewIndex = context -> uncheckedOpenSimpleRecordStore(context, hook);
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, hook);
        for (int i = 10; i < 20; i++) {
            TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
            recBuilder.setRecNo(i);
            recBuilder.setNumValue2(i % 10);
            recordStore.saveRecord(recBuilder.build());
        }
        commit(context);
    }
    // Only sees new record added after index.
    assertEquals(Arrays.asList(16L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, openWithNewIndex, TestHelpers::assertDiscardedNone));
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, hook);
        recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.NONE).join();
        assertEquals(1, timer.getCount(FDBStoreTimer.Events.REBUILD_INDEX), "should build new index");
        commit(context);
    }
    // Now see both records from indexed query.
    assertEquals(Arrays.asList(6L, 16L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, openWithNewIndex, TestHelpers::assertDiscardedNone));
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) Descriptors(com.google.protobuf.Descriptors) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Function(java.util.function.Function) Key(com.apple.foundationdb.record.metadata.Key) TestHelpers.assertDiscardedNone(com.apple.foundationdb.record.TestHelpers.assertDiscardedNone) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) TestHelpers(com.apple.foundationdb.record.TestHelpers) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) TestHelpers.assertDiscardedAtLeast(com.apple.foundationdb.record.TestHelpers.assertDiscardedAtLeast) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nullable(javax.annotation.Nullable) FDBRecordStoreBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) Query(com.apple.foundationdb.record.query.expressions.Query) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Tags(com.apple.test.Tags) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Index(com.apple.foundationdb.record.metadata.Index) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Aggregations

TestHelpers (com.apple.foundationdb.record.TestHelpers)26 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)26 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)24 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)23 EvaluationContext (com.apple.foundationdb.record.EvaluationContext)22 IndexScanType (com.apple.foundationdb.record.IndexScanType)22 Index (com.apple.foundationdb.record.metadata.Index)22 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)22 Key (com.apple.foundationdb.record.metadata.Key)22 Query (com.apple.foundationdb.record.query.expressions.Query)22 Tags (com.apple.test.Tags)22 Message (com.google.protobuf.Message)22 Arrays (java.util.Arrays)22 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)22 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)22 Tag (org.junit.jupiter.api.Tag)22 Test (org.junit.jupiter.api.Test)22 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)21 PlanHashable (com.apple.foundationdb.record.PlanHashable)20 RecordCursorResult (com.apple.foundationdb.record.RecordCursorResult)20