Search in sources :

Example 81 with RecordMetaDataBuilder

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

the class FDBMetaDataStoreTest method withIncompatibleChange.

@Test
public void withIncompatibleChange() {
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.saveRecordMetaData(metaData1);
        context.commit();
    }
    RecordMetaData metaData2 = RecordMetaData.build(metaData1.toProto().toBuilder().setVersion(metaData1.getVersion() + 1).clearIndexes().build());
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.saveRecordMetaData(metaData2));
        assertThat(e.getMessage(), containsString("index missing in new meta-data"));
        context.commit();
    }
    // Using the builder, this change should be fine.
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    metaData1.getAllIndexes().forEach(index -> metaDataBuilder.removeIndex(index.getName()));
    RecordMetaData metaData3 = metaDataBuilder.getRecordMetaData();
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.saveRecordMetaData(metaData3);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
        MetaDataProtoTest.verifyEquals(metaData3, retrievedMetaData);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) 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 RecordMetaDataBuilder

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

the class FDBMetaDataStoreTest method nonDefaultUnionRecordTypes.

@Test
public void nonDefaultUnionRecordTypes() {
    int version;
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords3Proto.getDescriptor());
        metaDataBuilder.getOnlyRecordType().setPrimaryKey(Key.Expressions.concatenateFields("parent_path", "child_name"));
        RecordMetaData metaData = metaDataBuilder.getRecordMetaData();
        version = metaData.getVersion();
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
    }
    // Add a record type with the default union name to a record meta-data that has a non-default union name.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        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 second record type with non-default union name but union usage.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        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("SecondNonDefaultUnionRecord").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 with non-default name.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("UnionDescriptor"));
        assertEquals(e.getMessage(), "Cannot deprecate the union");
        // version must remain unchanged
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Add a record type to a meta-data that has non-default union name.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        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();
        addRecordType(newRecordType, Key.Expressions.field("rec_no"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getRecordType("MyNewRecord").getSinceVersion().intValue());
        context.commit();
    }
    // Deprecate a record type from a meta-data that has non-default union name.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        deprecateRecordType("MyHierarchicalRecord");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        assertTrue(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("UnionDescriptor").findFieldByName("_MyHierarchicalRecord").getOptions().getDeprecated());
        assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
    // do not commit
    }
    // Validate that deprecation can happen when the type is fully qualified
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        deprecateRecordType(".com.apple.foundationdb.record.test3.MyHierarchicalRecord");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
        assertTrue(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("UnionDescriptor").findFieldByName("_MyHierarchicalRecord").getOptions().getDeprecated());
        assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
}
Also used : 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 83 with RecordMetaDataBuilder

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

the class MetaDataEvolutionValidatorTest method swapRecordTypes.

@Test
public void swapRecordTypes() {
    FileDescriptor updatedFile = mutateFile(fileBuilder -> {
        // Update the field of the union descriptor.
        fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
            if (messageType.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
                messageType.getFieldBuilderList().forEach(field -> {
                    if (field.getName().equals("_MyOtherRecord")) {
                        field.setTypeName("MySimpleRecord");
                    }
                    if (field.getName().equals("_MySimpleRecord")) {
                        field.setTypeName("MyOtherRecord");
                    }
                });
            }
        });
    });
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    metaDataBuilder.addIndex("MyOtherRecord", "num_value_3_indexed");
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
    // Swap is noticed as the two records are not of compatible forms
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("", metaData1, metaData2);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.jupiter.api.Test)

Example 84 with RecordMetaDataBuilder

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

the class MetaDataEvolutionValidatorTest method defaultIndexRemovalPath.

/**
 * This test is supposed to validate that the "default" way of removing an index actually updates fields
 * in a safe way. As such, it is more to validate the methods on a RecordMetaDataBuilder that mutate version
 * information than the evolution validator.
 */
@Test
public void defaultIndexRemovalPath() {
    final String newIndexName = "MySimpleRecord$num_value_2";
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    metaDataBuilder.addIndex("MySimpleRecord", newIndexName, "num_value_2");
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
    assertNotNull(metaData1.getIndex(newIndexName));
    assertEquals(metaData1.getVersion(), metaData1.getIndex(newIndexName).getAddedVersion());
    assertEquals(metaData1.getVersion(), metaData1.getIndex(newIndexName).getLastModifiedVersion());
    metaDataBuilder.removeIndex(newIndexName);
    RecordMetaData metaData2 = metaDataBuilder.getRecordMetaData();
    assertEquals(1, metaData2.getFormerIndexes().size());
    final FormerIndex newFormerIndex = metaData2.getFormerIndexes().get(0);
    assertEquals(newIndexName, newFormerIndex.getFormerName());
    assertEquals(metaData1.getVersion(), newFormerIndex.getAddedVersion());
    assertEquals(metaData2.getVersion(), newFormerIndex.getRemovedVersion());
    validator.validate(metaData1, metaData2);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ByteString(com.google.protobuf.ByteString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 85 with RecordMetaDataBuilder

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

the class MetaDataEvolutionValidatorTest method nestedTypeChangesFieldType.

@Test
public void nestedTypeChangesFieldType() {
    FileDescriptor updatedFile = mutateField("HeaderRecord", "num", TestRecordsWithHeaderProto.getDescriptor(), field -> field.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED32));
    assertInvalid("field type changed", TestRecordsWithHeaderProto.getDescriptor(), updatedFile);
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsWithHeaderProto.getDescriptor());
    metaDataBuilder.getRecordType("MyRecord").setPrimaryKey(Key.Expressions.field("header").nest(Key.Expressions.concatenateFields("path", "rec_no")));
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("field type changed", metaData1, metaData2);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)150 Test (org.junit.jupiter.api.Test)91 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)69 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 Nonnull (javax.annotation.Nonnull)22 Index (com.apple.foundationdb.record.metadata.Index)21 Descriptors (com.google.protobuf.Descriptors)15 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)14 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)14 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)13 ByteString (com.google.protobuf.ByteString)13 Tuple (com.apple.foundationdb.tuple.Tuple)12 Collections (java.util.Collections)12 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)11 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)11 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)11 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)10 BooleanSource (com.apple.test.BooleanSource)10