use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class MetaDataEvolutionValidatorTest method nestedTypeChangesName.
@Test
public void nestedTypeChangesName() {
FileDescriptor updatedFile = mutateFile(TestRecordsWithHeaderProto.getDescriptor(), fileBuilder -> fileBuilder.getMessageTypeBuilderList().forEach(message -> {
if (message.getName().equals("HeaderRecord")) {
message.setName("Header");
} else if (message.getName().equals("MyRecord")) {
message.getFieldBuilderList().forEach(field -> {
if (field.getName().equals("header")) {
field.setTypeName("." + fileBuilder.getPackage() + ".Header");
}
});
}
}));
assertThat(updatedFile.getMessageTypes().stream().map(Descriptor::getName).collect(Collectors.toList()), containsInAnyOrder("MyRecord", RecordMetaDataBuilder.DEFAULT_UNION_NAME, "Header"));
validator.validateUnion(TestRecordsWithHeaderProto.RecordTypeUnion.getDescriptor(), updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
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, updatedFile);
validator.validate(metaData1, metaData2);
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method updateRecordsWithRenamed.
@Test
public void updateRecordsWithRenamed() throws Descriptors.DescriptorValidationException {
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MySimpleRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MyOtherRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
builder.addIndex("MyOtherRecord", "num_value_3_indexed");
RecordMetaData metaData = builder.getRecordMetaData();
assertEquals(Collections.singletonList(metaData.getRecordType("MyOtherRecord")), metaData.recordTypesForIndex(metaData.getIndex("MyOtherRecord$num_value_3_indexed")));
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = TestRecords1Proto.getDescriptor().toProto().toBuilder();
fileBuilder.getMessageTypeBuilderList().forEach(message -> {
if (message.getName().equals("MyOtherRecord")) {
message.setName("MyOtherOtherRecord");
} else if (message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
message.getFieldBuilderList().forEach(field -> {
if (field.getTypeName().endsWith("MyOtherRecord")) {
field.setTypeName("MyOtherOtherRecord");
}
});
}
});
Descriptors.FileDescriptor updatedFileDescriptor = Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), new Descriptors.FileDescriptor[] { TestRecords1Proto.getDescriptor() });
builder.updateRecords(updatedFileDescriptor);
assertSame(builder.getRecordType("MySimpleRecord").getDescriptor().getFile(), updatedFileDescriptor);
assertThrows(MetaDataException.class, () -> builder.getRecordType("MyOtherRecord"));
assertSame(builder.getRecordType("MyOtherOtherRecord").getDescriptor().getFile(), updatedFileDescriptor);
metaData = builder.getRecordMetaData();
assertEquals(Collections.singletonList(metaData.getRecordType("MyOtherOtherRecord")), metaData.recordTypesForIndex(metaData.getIndex("MyOtherRecord$num_value_3_indexed")));
RecordMetaDataProto.MetaData metaDataProto = metaData.toProto();
assertThat(metaDataProto.getRecordTypesList().stream().map(RecordMetaDataProto.RecordType::getName).collect(Collectors.toList()), containsInAnyOrder("MySimpleRecord", "MyOtherOtherRecord"));
RecordMetaDataProto.Index indexProto = metaDataProto.getIndexesList().stream().filter(index -> index.getName().equals("MyOtherRecord$num_value_3_indexed")).findFirst().get();
assertEquals(Collections.singletonList("MyOtherOtherRecord"), indexProto.getRecordTypeList());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method localMetaData.
@Test
public void localMetaData() throws Descriptors.DescriptorValidationException {
// Record type moved from being imported to being present in the local descriptor
RecordMetaDataProto.MetaData previouslyImportedMetaData = RecordMetaData.build(TestRecordsImportProto.getDescriptor()).toProto(null);
RecordMetaDataBuilder nowFlatMetaData = RecordMetaData.newBuilder().setLocalFileDescriptor(TestRecordsImportFlatProto.getDescriptor()).setRecords(previouslyImportedMetaData);
assertNotNull(nowFlatMetaData.getRecordType("MySimpleRecord"));
assertSame(nowFlatMetaData.getRecordType("MySimpleRecord").getDescriptor(), TestRecordsImportFlatProto.MySimpleRecord.getDescriptor());
assertNotNull(nowFlatMetaData.getRecordType("MyLongRecord"));
assertSame(nowFlatMetaData.getRecordType("MyLongRecord").getDescriptor(), TestRecordsImportFlatProto.MyLongRecord.getDescriptor());
nowFlatMetaData.build(true);
// Record type moved from the descriptor to being in an imported file
RecordMetaDataProto.MetaData previouslyFlatMetaData = RecordMetaData.build(TestRecordsImportFlatProto.getDescriptor()).toProto(null);
RecordMetaDataBuilder nowImportedMetaData = RecordMetaData.newBuilder().setLocalFileDescriptor(TestRecordsImportProto.getDescriptor()).setRecords(previouslyFlatMetaData);
assertNotNull(nowImportedMetaData.getRecordType("MySimpleRecord"));
assertSame(nowImportedMetaData.getRecordType("MySimpleRecord").getDescriptor(), TestRecords1Proto.MySimpleRecord.getDescriptor());
assertNotNull(nowImportedMetaData.getRecordType("MyLongRecord"));
assertSame(nowImportedMetaData.getRecordType("MyLongRecord").getDescriptor(), TestRecords2Proto.MyLongRecord.getDescriptor());
nowImportedMetaData.build(true);
// The original meta-data
RecordMetaDataProto.MetaData originalMetaData = RecordMetaData.build(TestRecords1Proto.getDescriptor()).toProto(null);
// Evolve the local file descriptor by adding a record type to the union
RecordMetaDataBuilder evolvedMetaDataBuilder = RecordMetaData.newBuilder().setLocalFileDescriptor(TestRecords1EvolvedProto.getDescriptor()).setRecords(originalMetaData);
MetaDataException e = assertThrows(MetaDataException.class, () -> evolvedMetaDataBuilder.getRecordType("AnotherRecord"));
assertEquals("Unknown record type AnotherRecord", e.getMessage());
assertSame(evolvedMetaDataBuilder.build(true).getUnionDescriptor(), TestRecords1EvolvedProto.RecordTypeUnion.getDescriptor());
// Add an additional field to the record type union for _MySimpleRecord, and validate that field is *not* used when this is
// used with a local file descriptor, but that it is used when the new file is set with updateRecords
DescriptorProtos.FileDescriptorProto.Builder evolvedFileBuilder = TestRecords1EvolvedProto.getDescriptor().toProto().toBuilder();
evolvedFileBuilder.getMessageTypeBuilderList().forEach(message -> {
if (message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
message.getFieldBuilderList().forEach(field -> {
if (field.getName().equals("_MySimpleRecord")) {
field.setName("_MySimpleRecord_Old");
}
});
message.addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE).setTypeName("." + TestRecords1EvolvedProto.getDescriptor().getPackage() + ".MySimpleRecord").setName("_MySimpleRecord_New").setNumber(message.getFieldBuilderList().stream().mapToInt(DescriptorProtos.FieldDescriptorProto.Builder::getNumber).max().orElse(0) + 1));
}
});
Descriptors.FileDescriptor evolvedFile2 = Descriptors.FileDescriptor.buildFrom(evolvedFileBuilder.build(), TestRecords1EvolvedProto.getDescriptor().getDependencies().toArray(new Descriptors.FileDescriptor[0]));
RecordMetaData metaData2 = RecordMetaData.newBuilder().setLocalFileDescriptor(evolvedFile2).setRecords(originalMetaData).build();
assertSame(evolvedFile2.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MySimpleRecord_Old"), metaData2.getUnionFieldForRecordType(metaData2.getRecordType("MySimpleRecord")));
RecordMetaDataBuilder metaDataBuilder3 = RecordMetaData.newBuilder().setRecords(originalMetaData);
metaDataBuilder3.updateRecords(evolvedFile2);
RecordMetaData metaData3 = metaDataBuilder3.build();
assertSame(evolvedFile2.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MySimpleRecord_New"), metaData3.getUnionFieldForRecordType(metaData3.getRecordType("MySimpleRecord")));
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method primaryIndexDoesOverlapPrimaryKey.
@ParameterizedTest(name = "primaryIndexDoesOverlapPrimaryKey [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void primaryIndexDoesOverlapPrimaryKey(final boolean indexCounterBasedSubspaceKey) {
RecordMetaDataBuilder builder = createBuilder(TestRecords1Proto.getDescriptor(), indexCounterBasedSubspaceKey);
builder.addIndex("MySimpleRecord", "MySimpleRecord$PRIMARY", "rec_no");
RecordMetaData metaData = builder.getRecordMetaData();
Index index = metaData.getIndex("MySimpleRecord$PRIMARY");
assertNotNull(index);
assertNotNull(index.getPrimaryKeyComponentPositions());
assertArrayEquals(new int[] { 0 }, index.getPrimaryKeyComponentPositions());
if (!indexCounterBasedSubspaceKey) {
assertEquals(index.getName(), index.getSubspaceKey());
} else {
assertEquals(4L, index.getSubspaceKey());
}
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method updateRecordsWithNewUnionField.
@ParameterizedTest(name = "updateRecordsWithNewUnionField [reorderFields = {0}]")
@BooleanSource
public void updateRecordsWithNewUnionField(boolean reorderFields) {
final RecordMetaData oldMetaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
final RecordType oldSimpleRecord = oldMetaData.getRecordType("MySimpleRecord");
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MySimpleRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MyOtherRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
Descriptors.FileDescriptor updatedFileDescriptor;
if (reorderFields) {
updatedFileDescriptor = TestRecordsDuplicateUnionFieldsReordered.getDescriptor();
} else {
updatedFileDescriptor = TestRecordsDuplicateUnionFields.getDescriptor();
}
builder.updateRecords(updatedFileDescriptor);
RecordMetaData newMetaData = builder.getRecordMetaData();
assertEquals(TestRecords1EvolvedProto.RecordTypeUnion._MYSIMPLERECORD_FIELD_NUMBER, oldMetaData.getUnionFieldForRecordType(oldSimpleRecord).getNumber());
RecordType newSimpleRecord = newMetaData.getRecordType("MySimpleRecord");
assertSame(newMetaData.getUnionDescriptor().findFieldByName("_MySimpleRecord_new"), newMetaData.getUnionFieldForRecordType(newSimpleRecord));
assertThat(oldMetaData.getUnionFieldForRecordType(oldSimpleRecord).getNumber(), lessThan(newMetaData.getUnionFieldForRecordType(newSimpleRecord).getNumber()));
assertEquals(oldSimpleRecord.getSinceVersion(), newSimpleRecord.getSinceVersion());
MetaDataEvolutionValidator.getDefaultInstance().validate(oldMetaData, newMetaData);
}
Aggregations