Search in sources :

Example 1 with MetaDataEvolutionValidator

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

the class FDBMetaDataStoreTest method withIndexesRequiringRebuild.

@Test
public void withIndexesRequiringRebuild() {
    RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    RecordMetaDataProto.MetaData.Builder protoBuilder = metaData.toProto().toBuilder().setVersion(metaData.getVersion() + 1);
    protoBuilder.getIndexesBuilderList().forEach(index -> {
        if (index.getName().equals("MySimpleRecord$str_value_indexed")) {
            index.addOptions(RecordMetaDataProto.Index.Option.newBuilder().setKey(IndexOptions.UNIQUE_OPTION).setValue("true"));
            index.setLastModifiedVersion(metaData.getVersion() + 1);
        }
    });
    RecordMetaData metaData2 = RecordMetaData.build(protoBuilder.build());
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.saveRecordMetaData(metaData2));
        assertThat(e.getMessage(), containsString("last modified version of index changed"));
        MetaDataProtoTest.verifyEquals(metaData, metaDataStore.getRecordMetaData());
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
        assertThat(retrievedMetaData.getIndex("MySimpleRecord$str_value_indexed").isUnique(), is(false));
        MetaDataProtoTest.verifyEquals(metaData, retrievedMetaData);
    }
    MetaDataEvolutionValidator laxerValidator = MetaDataEvolutionValidator.newBuilder().setAllowIndexRebuilds(true).build();
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.setEvolutionValidator(laxerValidator);
        metaDataStore.saveRecordMetaData(metaData2);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
        assertThat(retrievedMetaData.getIndex("MySimpleRecord$str_value_indexed").isUnique(), is(true));
        MetaDataProtoTest.verifyEquals(metaData2, metaDataStore.getRecordMetaData());
    }
}
Also used : MetaDataEvolutionValidator(com.apple.foundationdb.record.metadata.MetaDataEvolutionValidator) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

RecordMetaData (com.apple.foundationdb.record.RecordMetaData)1 MetaDataEvolutionValidator (com.apple.foundationdb.record.metadata.MetaDataEvolutionValidator)1 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)1 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1