use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method recordTypesWithOneOfUnion.
@Test
public void recordTypesWithOneOfUnion() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecordsOneOfProto.getDescriptor());
final KeyExpression pkey = Key.Expressions.field("rec_no");
metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
// Add a record type to oneOf. It should fail.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
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();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding record type to oneof is not allowed");
context.commit();
}
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class FunctionKeyRecordTest method openRecordStore.
private void openRecordStore(@Nonnull FDBRecordContext context, @Nonnull RecordMetaDataHook hook) {
RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecords8Proto.getDescriptor());
hook.apply(metaData);
createOrOpenRecordStore(context, metaData.getRecordMetaData());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class MetaDataEvolutionValidatorTest method fieldChangedFromMessageToGroup.
@Test
public void fieldChangedFromMessageToGroup() {
// The message and group types here have the same form, but messages and groups are serialized differently
assertInvalid("field type changed", TestRecordsWithHeaderProto.getDescriptor(), TestHeaderAsGroupProto.getDescriptor());
assertInvalid("field type changed", TestHeaderAsGroupProto.getDescriptor(), TestRecordsWithHeaderProto.getDescriptor());
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, TestHeaderAsGroupProto.getDescriptor());
assertInvalid("field type changed", metaData1, metaData2);
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class MetaDataEvolutionValidatorTest method addRecordTypeWithUniversalIndex.
@Test
public void addRecordTypeWithUniversalIndex() {
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaDataBuilder.addUniversalIndex(new Index("rec_no", "rec_no"));
RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
assertNotNull(metaData1.getUniversalIndex("rec_no"));
// Keep the index universal
RecordMetaData metaData2 = addNewRecordType(metaData1);
assertNotNull(metaData2.getUniversalIndex("rec_no"));
validator.validate(metaData1, metaData2);
// Make the index a multi-type index on the original record types
RecordMetaData metaData3 = replaceIndex(metaData2, "rec_no", indexProto -> indexProto.toBuilder().addRecordType("MySimpleRecord").addRecordType("MyOtherRecord").build());
MetaDataException e = assertThrows(MetaDataException.class, () -> metaData3.getUniversalIndex("rec_no"));
assertThat(e.getMessage(), containsString("Index rec_no not defined"));
assertInvalid("new index removes record type", metaData2, metaData3);
validator.validate(metaData1, metaData3);
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class MetaDataEvolutionValidatorTest method swapIsomorphicRecordTypesWithExplicitKeys.
@Test
public void swapIsomorphicRecordTypesWithExplicitKeys() {
FileDescriptor updatedFile = mutateFile(fileBuilder -> {
DescriptorProtos.DescriptorProto newMessageType = fileBuilder.getMessageTypeList().stream().filter(messageType -> messageType.getName().equals("MyOtherRecord")).findFirst().get().toBuilder().setName("MyOtherOtherRecord").build();
fileBuilder.addMessageType(newMessageType);
fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
if (messageType.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
messageType.addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE).setTypeName("MyOtherOtherRecord").setName("_MyOtherOtherRecord").setNumber(messageType.getFieldList().stream().mapToInt(DescriptorProtos.FieldDescriptorProto::getNumber).max().orElse(0) + 1));
}
});
});
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(updatedFile);
metaDataBuilder.getRecordType("MyOtherRecord").setRecordTypeKey("other");
metaDataBuilder.getRecordType("MyOtherOtherRecord").setRecordTypeKey("other_other");
RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
assertEquals("other", metaData1.getRecordType("MyOtherRecord").getRecordTypeKey());
assertEquals("other_other", metaData1.getRecordType("MyOtherOtherRecord").getRecordTypeKey());
// Swap the definitions in the union descriptor
FileDescriptor secondFile = mutateFile(updatedFile, fileBuilder -> fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
if (messageType.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
messageType.getFieldBuilderList().forEach(field -> {
if (field.getName().equals("_MyOtherRecord")) {
field.setTypeName("MyOtherOtherRecord");
} else if (field.getName().equals("_MyOtherOtherRecord")) {
field.setTypeName("MyOtherRecord");
}
});
}
}));
RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, secondFile);
assertEquals("other", metaData2.getRecordType("MyOtherRecord").getRecordTypeKey());
assertEquals("other_other", metaData2.getRecordType("MyOtherOtherRecord").getRecordTypeKey());
assertInvalid("record type key changed", metaData1, metaData2);
// Swap the definitions in the record type descriptor list
RecordMetaData metaData3 = replaceRecordsDescriptor(metaData1, secondFile, metaDataProtoBuilder -> metaDataProtoBuilder.getRecordTypesBuilderList().forEach(recordType -> {
if (recordType.getName().equals("MyOtherRecord")) {
recordType.setName("MyOtherOtherRecord");
} else if (recordType.getName().equals("MyOtherOtherRecord")) {
recordType.setName("MyOtherRecord");
}
}));
assertEquals("other", metaData3.getRecordType("MyOtherOtherRecord").getRecordTypeKey());
assertEquals("other_other", metaData3.getRecordType("MyOtherRecord").getRecordTypeKey());
validator.validate(metaData1, metaData3);
// Verify that using "update records" updates the record type keys
RecordMetaDataBuilder metaDataBuilder4 = RecordMetaData.newBuilder().setRecords(metaData1.toProto());
metaDataBuilder4.updateRecords(secondFile);
RecordMetaData metaData4 = metaDataBuilder4.getRecordMetaData();
assertEquals("other", metaData4.getRecordType("MyOtherOtherRecord").getRecordTypeKey());
assertEquals("other_other", metaData4.getRecordType("MyOtherRecord").getRecordTypeKey());
validator.validate(metaData1, metaData4);
}
Aggregations