Search in sources :

Example 61 with RecordMetaData

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

the class FDBMetaDataStoreTest method doNotRenameSimilarNestedType.

/**
 * If there are nested types with a similar name, do not rename the other nested types.
 */
@Test
public void doNotRenameSimilarNestedType() {
    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);
        metaDataStore.mutateMetaData(protoBuilder -> {
            // Unqualify the OtterRecord.MiddleRecord references in a way where they can be distinguished from globally-scoped MiddleRecords
            final DescriptorProtos.FileDescriptorProto.Builder fileBuilder = protoBuilder.getRecordsBuilder();
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (!message.getName().equals("MiddleRecord")) {
                    message.getFieldBuilderList().forEach(field -> {
                        if (field.getTypeName().equals(".com.apple.foundationdb.record.test.doublenested.OuterRecord.MiddleRecord")) {
                            field.setTypeName("doublenested.OuterRecord.MiddleRecord");
                        }
                    });
                }
            });
            // Verify that at least two fields are changed
            assertThat(fileBuilder.getMessageTypeBuilderList().stream().flatMap(message -> message.getFieldBuilderList().stream()).filter(field -> field.getTypeName().equals("doublenested.OuterRecord.MiddleRecord")).count(), greaterThanOrEqualTo(1L));
            MetaDataProtoEditor.renameRecordType(protoBuilder, "MiddleRecord", "MuddledRecord");
        });
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        assertNotNull(metaData.getRecordsDescriptor().findMessageTypeByName("MuddledRecord"));
        assertEquals("MiddleRecord", metaData.getRecordsDescriptor().findMessageTypeByName("MuddledRecord").findFieldByName("other_middle").getMessageType().getName());
        assertEquals("MiddleRecord", metaData.getRecordType("OuterRecord").getDescriptor().findFieldByName("middle").getMessageType().getName());
    }
}
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) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData 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)

Example 63 with RecordMetaData

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

the class FDBMetaDataStoreTest method updateRecordsWithExtensionOption.

@Test
public void updateRecordsWithExtensionOption() throws Descriptors.DescriptorValidationException {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    // Add an extension option specifying that a field should have a new index
    DescriptorProtos.FileDescriptorProto.Builder fileBuilder = TestRecords1Proto.getDescriptor().toProto().toBuilder();
    fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
        if (messageType.getName().equals("MySimpleRecord")) {
            messageType.getFieldBuilderList().forEach(field -> {
                if (field.getName().equals("num_value_2")) {
                    RecordMetaDataOptionsProto.FieldOptions isIndexedOption = RecordMetaDataOptionsProto.FieldOptions.newBuilder().setIndex(RecordMetaDataOptionsProto.FieldOptions.IndexOption.newBuilder().setType(IndexTypes.VALUE).setUnique(true)).build();
                    field.getOptionsBuilder().setExtension(RecordMetaDataOptionsProto.field, isIndexedOption);
                }
            });
        }
    });
    Descriptors.FileDescriptor newFileDescriptor = Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), new Descriptors.FileDescriptor[] { RecordMetaDataOptionsProto.getDescriptor() });
    // Validate that new extension option will result in new index when built from file.
    RecordMetaData metaDataFromFile = RecordMetaData.build(newFileDescriptor);
    Index newIndex = metaDataFromFile.getIndex("MySimpleRecord$num_value_2");
    assertEquals(Key.Expressions.field("num_value_2"), newIndex.getRootExpression());
    assertThat("newIndex not marked as unique", newIndex.isUnique());
    // Update records. Validate that created meta-data does not add index.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.updateRecords(newFileDescriptor);
        // read from local cache
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getIndex("MySimpleRecord$num_value_2"));
        assertThat(e.getMessage(), containsString("Index MySimpleRecord$num_value_2 not defined"));
        context.commit();
    }
    // Validate that reading the index back from database does not add the index.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.updateRecords(newFileDescriptor);
        // read from the database
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getIndex("MySimpleRecord$num_value_2"));
        assertThat(e.getMessage(), containsString("Index MySimpleRecord$num_value_2 not defined"));
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) Index(com.apple.foundationdb.record.metadata.Index) Descriptors(com.google.protobuf.Descriptors) 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 64 with RecordMetaData

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

the class FDBMetaDataStoreTest method deprecateImportedRecordType.

@Test
public void deprecateImportedRecordType() {
    int version;
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecordsImportProto.getDescriptor());
        version = metaData.getVersion();
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
    }
    // Deprecate the record type
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        assertEquals(version, metaData.getVersion());
        assertNotNull(metaData.getRecordType("MySimpleRecord"));
        assertNotNull(metaData.getRecordType("MyLongRecord"));
        MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("MySimpleRecord"));
        assertEquals("Record type MySimpleRecord not found", 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 65 with RecordMetaData

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

the class FDBMetaDataStoreTest method withToProto.

@Test
public void withToProto() {
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsParentChildRelationshipProto.getDescriptor());
    metaDataBuilder.addIndex("MyChildRecord", "MyChildRecord$str_value", Key.Expressions.field("str_value"));
    metaDataBuilder.removeIndex("MyChildRecord$parent_rec_no");
    metaDataBuilder.addIndex("MyChildRecord", new Index("MyChildRecord$parent&str", Key.Expressions.concatenateFields("parent_rec_no", "str_value"), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
    metaDataBuilder.removeIndex("MyParentRecord$str_value_indexed");
    metaDataBuilder.addIndex("MyParentRecord", "MyParentRecord$str&child", Key.Expressions.concat(Key.Expressions.field("str_value_indexed"), Key.Expressions.field("child_rec_nos", KeyExpression.FanType.FanOut)));
    metaDataBuilder.addMultiTypeIndex(Arrays.asList(metaDataBuilder.getRecordType("MyChildRecord"), metaDataBuilder.getRecordType("MyParentRecord")), new Index("all$rec_nos", Key.Expressions.field("rec_no")));
    RecordMetaData metaData = metaDataBuilder.getRecordMetaData();
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
        MetaDataProtoTest.verifyEquals(metaData, retrievedMetaData);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Index(com.apple.foundationdb.record.metadata.Index) 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)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