Search in sources :

Example 21 with MetaDataException

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

the class FDBMetaDataStoreTest method recordTypesWithOneOfUnion.

@Test
public void recordTypesWithOneOfUnion() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecordsOneOfProto.getDescriptor());
        final KeyExpression pkey = Key.Expressions.field("rec_no");
        metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
        metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    // Add a record type to oneOf. It should fail.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MyNewRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).build();
        MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
        assertEquals(e.getMessage(), "Adding record type to oneof is not allowed");
        context.commit();
    }
}
Also used : RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) DescriptorProtos(com.google.protobuf.DescriptorProtos) 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 22 with MetaDataException

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

the class FDBMetaDataStoreTest method dontRenameNonUnionToUnion.

/**
 * Verify that renaming a regular record type to the default union name throws an error.
 */
@Test
public void dontRenameNonUnionToUnion() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        // to avoid conflicts
        renameRecordType(RecordMetaDataBuilder.DEFAULT_UNION_NAME, "RecordOneUnion");
        MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("MySimpleRecord", RecordMetaDataBuilder.DEFAULT_UNION_NAME));
        assertEquals("Cannot rename record type to the default union name", e.getMessage());
    }
}
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 23 with MetaDataException

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

the class FDBMetaDataStoreTest method renameRecordTypeWithClashingNested.

/**
 * Verify that if there is a nested type defined in a message, then changing a top-level record type
 * to that name won't cause any fields in that type to start pointing to the new type.
 */
@Test
public void renameRecordTypeWithClashingNested() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecordsDoubleNestedProto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        renameRecordType("OtherRecord", "InnerRecord");
        validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
        // Unqualify the inner record's field type name
        metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
            DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
            DescriptorProtos.DescriptorProto.Builder outerBuilder = fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).findFirst().get();
            outerBuilder.getFieldBuilderList().stream().filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("MiddleRecord.InnerRecord"));
            outerBuilder.getNestedTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("MiddleRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
        });
        validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
    // do not commit
    }
    // isn't fully qualified.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
            DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
            DescriptorProtos.DescriptorProto.Builder outerBuilder = fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).findFirst().get();
            outerBuilder.getFieldBuilderList().stream().filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("MiddleRecord.InnerRecord"));
            outerBuilder.getNestedTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("MiddleRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
        });
        renameRecordType("OtherRecord", "InnerRecord");
        validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
    // do not commit
    }
    // would change the type that the Protobuf would not have built.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
            DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
            fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
        }));
        assertEquals("Error converting from protobuf", e.getMessage());
        assertNotNull(e.getCause());
        assertThat(e.getCause(), instanceOf(Descriptors.DescriptorValidationException.class));
        assertThat(e.getCause().getMessage(), containsString("\"InnerRecord\" is not defined."));
    // do not commit
    }
}
Also used : MetaDataEvolutionValidator(com.apple.foundationdb.record.metadata.MetaDataEvolutionValidator) DescriptorProtos(com.google.protobuf.DescriptorProtos) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) TestRecords3Proto(com.apple.foundationdb.record.TestRecords3Proto) Matchers.not(org.hamcrest.Matchers.not) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Transaction(com.apple.foundationdb.Transaction) TestRecordsMultiProto(com.apple.foundationdb.record.TestRecordsMultiProto) Tuple(com.apple.foundationdb.tuple.Tuple) TestRecordsDoubleNestedProto(com.apple.foundationdb.record.TestRecordsDoubleNestedProto) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TestRecordsImplicitUsageProto(com.apple.foundationdb.record.TestRecordsImplicitUsageProto) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) TestNoUnionEvolvedRenamedRecordTypeProto(com.apple.foundationdb.record.TestNoUnionEvolvedRenamedRecordTypeProto) Tag(org.junit.jupiter.api.Tag) ProtoVersionSupplier(com.apple.foundationdb.record.ProtoVersionSupplier) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ImmutableSet(com.google.common.collect.ImmutableSet) KeyValue(com.apple.foundationdb.KeyValue) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) TestRecordsNestedAsRecord(com.apple.foundationdb.record.TestRecordsNestedAsRecord) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) TestRecordsImportedAndNewProto(com.apple.foundationdb.record.TestRecordsImportedAndNewProto) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) TestNoUnionEvolvedProto(com.apple.foundationdb.record.TestNoUnionEvolvedProto) Matchers.containsString(org.hamcrest.Matchers.containsString) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestNoUnionProto(com.apple.foundationdb.record.TestNoUnionProto) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TestRecordsParentChildRelationshipProto(com.apple.foundationdb.record.TestRecordsParentChildRelationshipProto) TestRecords1EvolvedAgainProto(com.apple.foundationdb.record.TestRecords1EvolvedAgainProto) Descriptors(com.google.protobuf.Descriptors) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TestRecordsOneOfProto(com.apple.foundationdb.record.TestRecordsOneOfProto) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) TestRecordsImplicitUsageNoUnionProto(com.apple.foundationdb.record.TestRecordsImplicitUsageNoUnionProto) TestNoUnionEvolvedIllegalProto(com.apple.foundationdb.record.TestNoUnionEvolvedIllegalProto) Matchers.lessThan(org.hamcrest.Matchers.lessThan) BooleanSource(com.apple.test.BooleanSource) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) TestRecords1EvolvedProto(com.apple.foundationdb.record.TestRecords1EvolvedProto) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) RecordType(com.apple.foundationdb.record.metadata.RecordType) Matchers.hasItem(org.hamcrest.Matchers.hasItem) TestRecordsImportProto(com.apple.foundationdb.record.TestRecordsImportProto) Index(com.apple.foundationdb.record.metadata.Index) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) DescriptorProtos(com.google.protobuf.DescriptorProtos) 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 24 with MetaDataException

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

the class FDBMetaDataStoreTest method tryRenameNonExistentRecord.

/**
 * Verify that renaming a non-existent record doesn't work.
 */
@Test
public void tryRenameNonExistentRecord() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("MyNonExistentRecord", "SomethingElse"));
        assertEquals("No record type found with name MyNonExistentRecord", e.getMessage());
    }
}
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 25 with MetaDataException

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

the class FDBMetaDataStoreTest method unionRecordTypes.

@Test
public void unionRecordTypes() {
    int version;
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        version = metaData.getVersion();
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
    }
    // Add a record type with default union name.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).build();
        MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(unionRecordType, Key.Expressions.field("rec_no")));
        assertEquals(e.getMessage(), "Adding UNION record type not allowed");
        // version must remain unchanged
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Add a record type with non-default union name but union usage.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        DescriptorProtos.MessageOptions.Builder unionMessageOptions = DescriptorProtos.MessageOptions.newBuilder().setExtension(RecordMetaDataOptionsProto.record, RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder().setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.UNION).build());
        DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("NonDefaultUnionRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).setOptions(unionMessageOptions).build();
        MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(unionRecordType, Key.Expressions.field("rec_no")));
        assertEquals(e.getMessage(), "Adding UNION record type not allowed");
        // version must remain unchanged
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Deprecate the record type union.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
        assertEquals(e.getMessage(), "Cannot deprecate the union");
        // version must remain unchanged
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) DescriptorProtos(com.google.protobuf.DescriptorProtos) 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

MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)61 Test (org.junit.jupiter.api.Test)33 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)25 Nonnull (javax.annotation.Nonnull)24 Index (com.apple.foundationdb.record.metadata.Index)22 Descriptors (com.google.protobuf.Descriptors)14 Tuple (com.apple.foundationdb.tuple.Tuple)13 List (java.util.List)12 ScanProperties (com.apple.foundationdb.record.ScanProperties)11 TupleRange (com.apple.foundationdb.record.TupleRange)11 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)11 IndexEntry (com.apple.foundationdb.record.IndexEntry)10 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)10 DescriptorProtos (com.google.protobuf.DescriptorProtos)10 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)9 Message (com.google.protobuf.Message)9 Collectors (java.util.stream.Collectors)9 IndexScanType (com.apple.foundationdb.record.IndexScanType)8