Search in sources :

Example 76 with RecordMetaData

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

Example 77 with RecordMetaData

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

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

the class FDBMetaDataStoreTest method extensionRegistry.

@Test
public void extensionRegistry() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        // Default registry parses options
        openMetaDataStore(context);
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        Descriptors.Descriptor mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
        assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
        RecordMetaDataOptionsProto.FieldOptions recNoFieldOptions = mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().getExtension(RecordMetaDataOptionsProto.field);
        assertNotNull(recNoFieldOptions);
        assertThat(recNoFieldOptions.getPrimaryKey(), is(true));
        // Empty registry does not
        openMetaDataStore(context);
        metaDataStore.setExtensionRegistry(ExtensionRegistry.getEmptyRegistry());
        metaData = metaDataStore.getRecordMetaData();
        mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
        assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
        assertThat(mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().hasExtension(RecordMetaDataOptionsProto.field), is(false));
        // Null registry behaves like the empty registry in proto2 and throws an exception in proto3
        openMetaDataStore(context);
        metaDataStore.setExtensionRegistry(null);
        if (ProtoVersionSupplier.getProtoVersion() == 2) {
            metaData = metaDataStore.getRecordMetaData();
            mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
            assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
            assertThat(mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().hasExtension(RecordMetaDataOptionsProto.field), is(false));
        } else {
            final FDBMetaDataStore finalMetaDataStore = metaDataStore;
            assertThrows(NullPointerException.class, finalMetaDataStore::getRecordMetaData);
        }
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 79 with RecordMetaData

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

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

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