use of com.apple.foundationdb.record.RecordMetaData 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());
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method renameSimpleWithMultipleUnionAppearances.
/**
* Verify that if a record appears multiple times in the union that (1) all of the appearances are changed to the new
* type and that (2) only one type is renamed.
*/
@Test
public void renameSimpleWithMultipleUnionAppearances() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
metaDataStore.mutateMetaData(metaDataProtoBuilder -> metaDataProtoBuilder.getRecordsBuilder().getMessageTypeBuilderList().forEach(messageType -> {
if (messageType.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
// Rename the current _MySimpleRecord field
messageType.getFieldBuilderList().forEach(field -> {
if (field.getName().equals("_MySimpleRecord")) {
field.setName("_MySimpleRecord_v1");
}
});
messageType.addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE).setTypeName("." + TestRecords1Proto.MySimpleRecord.getDescriptor().getFullName()).setName("_MySimpleRecord").setNumber(messageType.getFieldBuilderList().stream().mapToInt(DescriptorProtos.FieldDescriptorProtoOrBuilder::getNumber).max().orElse(0) + 1).build());
}
}));
metaData = metaDataStore.getRecordMetaData();
assertEquals(TestRecords1Proto.RecordTypeUnion._MYSIMPLERECORD_FIELD_NUMBER, metaData.getUnionDescriptor().findFieldByName("_MySimpleRecord_v1").getNumber());
assertEquals(metaData.getUnionFieldForRecordType(metaData.getRecordType("MySimpleRecord")).getNumber(), metaData.getUnionDescriptor().findFieldByName("_MySimpleRecord").getNumber());
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
renameRecordType("MySimpleRecord", "MyNewSimpleRecord");
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(ImmutableSet.of("MyNewSimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
assertEquals(ImmutableSet.of("_MyNewSimpleRecord", "_MySimpleRecord_v1", "_MyOtherRecord"), metaData.getUnionDescriptor().getFields().stream().map(Descriptors.FieldDescriptor::getName).collect(Collectors.toSet()));
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(ImmutableSet.of("MyNewSimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
assertEquals(ImmutableSet.of("_MyNewSimpleRecord", "_MySimpleRecord_v1", "_MyOtherRecord"), metaData.getUnionDescriptor().getFields().stream().map(Descriptors.FieldDescriptor::getName).collect(Collectors.toSet()));
context.commit();
}
}
use of com.apple.foundationdb.record.RecordMetaData 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
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method simple.
@Test
public void simple() {
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"));
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
MetaDataProtoTest.verifyEquals(metaData, metaDataStore.getRecordMetaData());
}
}
use of com.apple.foundationdb.record.RecordMetaData 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());
}
}
Aggregations