Search in sources :

Example 81 with RecordMetaData

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

the class FDBMetaDataStoreTest method updateRecordsWithLocalFileDescriptor.

@Test
public void updateRecordsWithLocalFileDescriptor() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        // Local file descriptor is not as evolved as the to-be updating records descriptor. It will fail.
        metaDataStore.setLocalFileDescriptor(TestRecords1Proto.getDescriptor());
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor()));
        assertEquals(e.getMessage(), "record type removed from union");
        metaDataStore.setLocalFileDescriptor(TestRecords1EvolvedAgainProto.getDescriptor());
        metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("AnotherRecord"));
        e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("OneMoreRecord"));
        assertEquals(e.getMessage(), "Unknown record type OneMoreRecord");
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("AnotherRecord"));
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("OneMoreRecord"));
        assertEquals(e.getMessage(), "Unknown record type OneMoreRecord");
        context.commit();
    }
}
Also used : 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)

Example 82 with RecordMetaData

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

the class FDBMetaDataStoreTest method dontRenameRecordTypeInIndexesWhenClashingWithImported.

/**
 * Validate that if a {@code NESTED} record with the same name as an imported record type
 * has its name changed, then the indexes do not change their record type.
 */
@Test
public void dontRenameRecordTypeInIndexesWhenClashingWithImported() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecordsImportedAndNewProto.getDescriptor());
        assertEquals(ImmutableSet.of("MySimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
        assertSame(metaData.getRecordType("MySimpleRecord").getDescriptor(), TestRecords1Proto.MySimpleRecord.getDescriptor());
        assertNotSame(metaData.getRecordType("MySimpleRecord").getDescriptor(), TestRecordsImportedAndNewProto.MySimpleRecord.getDescriptor());
        assertSame(metaData.getRecordType("MyOtherRecord").getDescriptor(), TestRecordsImportedAndNewProto.MyOtherRecord.getDescriptor());
        assertNotSame(metaData.getRecordType("MyOtherRecord").getDescriptor(), TestRecords1Proto.MyOtherRecord.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
            MetaDataProtoEditor.renameRecordType(metaDataProtoBuilder, "MySimpleRecord", "MyNewSimpleRecord");
            assertThat(metaDataProtoBuilder.getRecordTypesList().stream().map(RecordMetaDataProto.RecordType::getName).collect(Collectors.toList()), hasItem("MySimpleRecord"));
            for (RecordMetaDataProto.Index index : metaDataProtoBuilder.getIndexesList()) {
                assertThat(index.getRecordTypeList(), not(hasItem("MyNewSimpleRecord")));
            }
        });
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        assertEquals(ImmutableSet.of("MySimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
        assertEquals("MyNewSimpleRecord", metaData.getRecordsDescriptor().findMessageTypeByName("MyOtherRecord").findFieldByName("simple").getMessageType().getName());
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        assertEquals(ImmutableSet.of("MySimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
        assertEquals("MyNewSimpleRecord", metaData.getRecordsDescriptor().findMessageTypeByName("MyOtherRecord").findFieldByName("simple").getMessageType().getName());
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordType(com.apple.foundationdb.record.metadata.RecordType) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 83 with RecordMetaData

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

the class MetaDataEvolutionValidatorTest method changeSplitLongRecords.

@Test
public void changeSplitLongRecords() {
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    assertThat(metaData1.isSplitLongRecords(), is(false));
    RecordMetaData metaData2 = RecordMetaData.build(metaData1.toProto().toBuilder().setVersion(metaData1.getVersion() + 1).setSplitLongRecords(true).build());
    assertInvalid("new meta-data splits long records", metaData1, metaData2);
    MetaDataEvolutionValidator laxerValidator = MetaDataEvolutionValidator.newBuilder().setAllowUnsplitToSplit(true).build();
    laxerValidator.validate(metaData1, metaData2);
    RecordMetaData metaData3 = RecordMetaData.build(metaData2.toProto().toBuilder().setVersion(metaData2.getVersion() + 1).setSplitLongRecords(false).build());
    assertInvalid("new meta-data no longer splits long records", metaData2, metaData3);
    assertInvalid("new meta-data no longer splits long records", laxerValidator, metaData2, metaData3);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test)

Example 84 with RecordMetaData

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

the class MetaDataEvolutionValidatorTest method formerIndexWithoutExistingIndex.

@Test
public void formerIndexWithoutExistingIndex() {
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = RecordMetaData.build(metaData1.toProto().toBuilder().setVersion(metaData1.getVersion() + 2).addFormerIndexes(RecordMetaDataProto.FormerIndex.newBuilder().setSubspaceKey(ByteString.copyFrom(Tuple.from("dummy_key").pack())).setRemovedVersion(metaData1.getVersion() + 2).setAddedVersion(metaData1.getVersion() + 1).build()).build());
    validator.validate(metaData1, metaData2);
    metaData2 = RecordMetaData.build(metaData2.toProto().toBuilder().setFormerIndexes(0, metaData2.getFormerIndexes().get(0).toProto().toBuilder().setAddedVersion(metaData1.getVersion()).build()).build());
    assertInvalid("former index without existing index has added version prior to old meta-data version", metaData1, metaData2);
    MetaDataEvolutionValidator laxerValidator = MetaDataEvolutionValidator.newBuilder().setAllowOlderFormerIndexAddedVerions(true).build();
    laxerValidator.validate(metaData1, metaData2);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test)

Example 85 with RecordMetaData

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

the class MetaDataEvolutionValidatorTest method dropField.

@Test
public void dropField() {
    FileDescriptor updatedFile = mutateFile(fileBuilder -> fileBuilder.getMessageTypeBuilderList().forEach(message -> {
        if (message.getName().equals("MySimpleRecord")) {
            int fieldNumValue2Index = 0;
            while (!message.getField(fieldNumValue2Index).getName().equals("num_value_2")) {
                fieldNumValue2Index++;
            }
            message.removeField(fieldNumValue2Index);
        }
    }));
    assertInvalid("field removed from message descriptor", TestRecords1Proto.getDescriptor(), updatedFile);
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("field removed from message descriptor", metaData1, metaData2);
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Descriptor(com.google.protobuf.Descriptors.Descriptor) Descriptors(com.google.protobuf.Descriptors) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PrefixTextTokenizer(com.apple.foundationdb.record.provider.common.text.PrefixTextTokenizer) UnaryOperator(java.util.function.UnaryOperator) TestSplitNestedTypesProto(com.apple.foundationdb.record.evolution.TestSplitNestedTypesProto) TestNewRecordTypeProto(com.apple.foundationdb.record.evolution.TestNewRecordTypeProto) TestSelfReferenceUnspooledProto(com.apple.foundationdb.record.evolution.TestSelfReferenceUnspooledProto) ArrayList(java.util.ArrayList) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) DefaultTextTokenizer(com.apple.foundationdb.record.provider.common.text.DefaultTextTokenizer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TestSelfReferenceProto(com.apple.foundationdb.record.evolution.TestSelfReferenceProto) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) TestUnmergedNestedTypesProto(com.apple.foundationdb.record.evolution.TestUnmergedNestedTypesProto) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) TextTokenizer(com.apple.foundationdb.record.provider.common.text.TextTokenizer) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TestHeaderAsGroupProto(com.apple.foundationdb.record.evolution.TestHeaderAsGroupProto) RankedSet(com.apple.foundationdb.async.RankedSet) TestRecordsEnumProto(com.apple.foundationdb.record.TestRecordsEnumProto) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) Matchers.is(org.hamcrest.Matchers.is) TestMergedNestedTypesProto(com.apple.foundationdb.record.evolution.TestMergedNestedTypesProto) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) AllSuffixesTextTokenizer(com.apple.foundationdb.record.provider.common.text.AllSuffixesTextTokenizer) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

RecordMetaData (com.apple.foundationdb.record.RecordMetaData)168 Test (org.junit.jupiter.api.Test)130 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)81 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 Nonnull (javax.annotation.Nonnull)47 Descriptors (com.google.protobuf.Descriptors)44 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)39 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)39 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)38 Tuple (com.apple.foundationdb.tuple.Tuple)36 List (java.util.List)33 ArrayList (java.util.ArrayList)31 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)30 Index (com.apple.foundationdb.record.metadata.Index)30 DescriptorProtos (com.google.protobuf.DescriptorProtos)30 Collectors (java.util.stream.Collectors)30 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)27 ByteString (com.google.protobuf.ByteString)27 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)27 RecordMetaDataOptionsProto (com.apple.foundationdb.record.RecordMetaDataOptionsProto)26