Search in sources :

Example 26 with MetaDataException

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

the class FDBMetaDataStoreTest method renameSimpleRecordType.

/**
 * A basic test to verify that basic renaming works.
 */
@Test
public void renameSimpleRecordType() {
    List<Index> simpleRecordIndexes;
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        simpleRecordIndexes = metaData.getRecordType("MySimpleRecord").getIndexes();
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        renameRecordType("MySimpleRecord", "MyNewSimpleRecord");
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        Descriptors.FieldDescriptor simpleField = metaData.getUnionDescriptor().findFieldByNumber(TestRecords1Proto.RecordTypeUnion._MYSIMPLERECORD_FIELD_NUMBER);
        assertSame(metaData.getRecordType("MyNewSimpleRecord").getDescriptor(), simpleField.getMessageType());
        assertEquals("MyNewSimpleRecord", simpleField.getMessageType().getName());
        assertEquals("_MyNewSimpleRecord", simpleField.getName());
        assertEquals(ImmutableSet.of("MyNewSimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getRecordType("MySimpleRecord"));
        assertEquals("Unknown record type MySimpleRecord", e.getMessage());
        assertEquals(simpleRecordIndexes.stream().map(Index::getName).collect(Collectors.toSet()), metaData.getRecordType("MyNewSimpleRecord").getAllIndexes().stream().map(Index::getName).collect(Collectors.toSet()));
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        Descriptors.FieldDescriptor simpleField = metaData.getUnionDescriptor().findFieldByNumber(TestRecords1Proto.RecordTypeUnion._MYSIMPLERECORD_FIELD_NUMBER);
        assertSame(metaData.getRecordType("MyNewSimpleRecord").getDescriptor(), simpleField.getMessageType());
        assertEquals("MyNewSimpleRecord", simpleField.getMessageType().getName());
        assertEquals("_MyNewSimpleRecord", simpleField.getName());
        assertEquals(ImmutableSet.of("MyNewSimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getRecordType("MySimpleRecord"));
        assertEquals("Unknown record type MySimpleRecord", e.getMessage());
        assertEquals(simpleRecordIndexes.stream().map(Index::getName).collect(Collectors.toSet()), metaData.getRecordType("MyNewSimpleRecord").getAllIndexes().stream().map(Index::getName).collect(Collectors.toSet()));
        context.commit();
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) 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 27 with MetaDataException

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

the class FDBMetaDataStoreTest method renameFullyQualifiedSimpleRecordType.

/**
 * Test whether fully qualifying a record type name works.
 */
@Test
public void renameFullyQualifiedSimpleRecordType() {
    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);
        // In theory, fully qualifying the name could work, but it doesn't as implemented.
        MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType(".com.apple.foundationdb.record.test1.MySimpleRecord", "MyNewSimpleRecord"));
        assertThat(e.getMessage(), containsString("No record type found"));
    }
}
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 28 with MetaDataException

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

the class FDBMetaDataStoreTest method recordTypes.

@Test
public void recordTypes() {
    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 an existing record type.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MySimpleRecord").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(), "Record type MySimpleRecord already exists");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        // version should not change
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Add a record type.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        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("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getRecordType("MyNewRecord").getSinceVersion().intValue());
        context.commit();
    }
    // The old local file descriptor does not have the new record type. Using it should fail.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.setLocalFileDescriptor(TestRecords1Proto.getDescriptor());
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData());
        assertEquals("record type removed from union", e.getMessage());
        context.commit();
    }
    // Add a record type with index.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MyNewRecordWithIndex").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"), new Index("MyNewRecordWithIndex$index", "rec_no"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecordWithIndex"));
        assertNotNull(metaDataStore.getRecordMetaData().getIndex("MyNewRecordWithIndex$index"));
        assertEquals(version + 2, metaDataStore.getRecordMetaData().getRecordType("MyNewRecordWithIndex").getSinceVersion().intValue());
        // +1 because of the index.
        assertEquals(version + 3, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Deprecate the just-added record types.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version + 3, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecordWithIndex"));
        deprecateRecordType("MyNewRecord");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertDeprecated(metaDataStore.getRecordMetaData(), "MyNewRecord");
        assertEquals(version + 4, metaDataStore.getRecordMetaData().getVersion());
        deprecateRecordType("MyNewRecordWithIndex");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecordWithIndex"));
        assertDeprecated(metaDataStore.getRecordMetaData(), "MyNewRecordWithIndex");
        assertEquals(version + 5, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Deprecate a record type from the original proto.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version + 5, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        deprecateRecordType("MySimpleRecord");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertDeprecated(metaDataStore.getRecordMetaData(), "MySimpleRecord");
        assertEquals(version + 6, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
    // Deprecate a non-existent record type.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version + 6, metaDataStore.getRecordMetaData().getVersion());
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertDeprecated(metaDataStore.getRecordMetaData(), "MySimpleRecord");
        MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("MyNonExistentRecord"));
        assertEquals(e.getMessage(), "Record type MyNonExistentRecord not found");
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertEquals(version + 6, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) DescriptorProtos(com.google.protobuf.DescriptorProtos) Index(com.apple.foundationdb.record.metadata.Index) 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 29 with MetaDataException

use of com.apple.foundationdb.record.metadata.MetaDataException 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 30 with MetaDataException

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

the class FDBRecordStoreReplaceIndexTest method replacementIndexMissingInMetaDataFails.

@Test
public void replacementIndexMissingInMetaDataFails() {
    try (FDBRecordContext context = openContext()) {
        MetaDataException err = assertThrows(MetaDataException.class, () -> openSimpleRecordStore(context, metaDataBuilder -> {
            final Index index = new Index("indexWithFakeReplacement", Key.Expressions.field("num_value_2"), IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "fakeIndex"));
            metaDataBuilder.addIndex("MySimpleRecord", index);
        }));
        assertThat(err.getMessage(), containsString("Index indexWithFakeReplacement has replacement index fakeIndex that is not in the meta-data"));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Key(com.apple.foundationdb.record.metadata.Key) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) ScanProperties(com.apple.foundationdb.record.ScanProperties) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Matchers.empty(org.hamcrest.Matchers.empty) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) ImmutableMap(com.google.common.collect.ImmutableMap) Tags(com.apple.test.Tags) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Index(com.apple.foundationdb.record.metadata.Index) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Test(org.junit.jupiter.api.Test)

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