use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method doNotRenameSimilarNestedType.
/**
* If there are nested types with a similar name, do not rename the other nested types.
*/
@Test
public void doNotRenameSimilarNestedType() {
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);
metaDataStore.mutateMetaData(protoBuilder -> {
// Unqualify the OtterRecord.MiddleRecord references in a way where they can be distinguished from globally-scoped MiddleRecords
final DescriptorProtos.FileDescriptorProto.Builder fileBuilder = protoBuilder.getRecordsBuilder();
fileBuilder.getMessageTypeBuilderList().forEach(message -> {
if (!message.getName().equals("MiddleRecord")) {
message.getFieldBuilderList().forEach(field -> {
if (field.getTypeName().equals(".com.apple.foundationdb.record.test.doublenested.OuterRecord.MiddleRecord")) {
field.setTypeName("doublenested.OuterRecord.MiddleRecord");
}
});
}
});
// Verify that at least two fields are changed
assertThat(fileBuilder.getMessageTypeBuilderList().stream().flatMap(message -> message.getFieldBuilderList().stream()).filter(field -> field.getTypeName().equals("doublenested.OuterRecord.MiddleRecord")).count(), greaterThanOrEqualTo(1L));
MetaDataProtoEditor.renameRecordType(protoBuilder, "MiddleRecord", "MuddledRecord");
});
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertNotNull(metaData.getRecordsDescriptor().findMessageTypeByName("MuddledRecord"));
assertEquals("MiddleRecord", metaData.getRecordsDescriptor().findMessageTypeByName("MuddledRecord").findFieldByName("other_middle").getMessageType().getName());
assertEquals("MiddleRecord", metaData.getRecordType("OuterRecord").getDescriptor().findFieldByName("middle").getMessageType().getName());
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method withIndexesRequiringRebuild.
@Test
public void withIndexesRequiringRebuild() {
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
RecordMetaDataProto.MetaData.Builder protoBuilder = metaData.toProto().toBuilder().setVersion(metaData.getVersion() + 1);
protoBuilder.getIndexesBuilderList().forEach(index -> {
if (index.getName().equals("MySimpleRecord$str_value_indexed")) {
index.addOptions(RecordMetaDataProto.Index.Option.newBuilder().setKey(IndexOptions.UNIQUE_OPTION).setValue("true"));
index.setLastModifiedVersion(metaData.getVersion() + 1);
}
});
RecordMetaData metaData2 = RecordMetaData.build(protoBuilder.build());
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.saveRecordMetaData(metaData2));
assertThat(e.getMessage(), containsString("last modified version of index changed"));
MetaDataProtoTest.verifyEquals(metaData, metaDataStore.getRecordMetaData());
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
assertThat(retrievedMetaData.getIndex("MySimpleRecord$str_value_indexed").isUnique(), is(false));
MetaDataProtoTest.verifyEquals(metaData, retrievedMetaData);
}
MetaDataEvolutionValidator laxerValidator = MetaDataEvolutionValidator.newBuilder().setAllowIndexRebuilds(true).build();
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.setEvolutionValidator(laxerValidator);
metaDataStore.saveRecordMetaData(metaData2);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
assertThat(retrievedMetaData.getIndex("MySimpleRecord$str_value_indexed").isUnique(), is(true));
MetaDataProtoTest.verifyEquals(metaData2, metaDataStore.getRecordMetaData());
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method updateRecordsWithExtensionOption.
@Test
public void updateRecordsWithExtensionOption() throws Descriptors.DescriptorValidationException {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
// Add an extension option specifying that a field should have a new index
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = TestRecords1Proto.getDescriptor().toProto().toBuilder();
fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
if (messageType.getName().equals("MySimpleRecord")) {
messageType.getFieldBuilderList().forEach(field -> {
if (field.getName().equals("num_value_2")) {
RecordMetaDataOptionsProto.FieldOptions isIndexedOption = RecordMetaDataOptionsProto.FieldOptions.newBuilder().setIndex(RecordMetaDataOptionsProto.FieldOptions.IndexOption.newBuilder().setType(IndexTypes.VALUE).setUnique(true)).build();
field.getOptionsBuilder().setExtension(RecordMetaDataOptionsProto.field, isIndexedOption);
}
});
}
});
Descriptors.FileDescriptor newFileDescriptor = Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), new Descriptors.FileDescriptor[] { RecordMetaDataOptionsProto.getDescriptor() });
// Validate that new extension option will result in new index when built from file.
RecordMetaData metaDataFromFile = RecordMetaData.build(newFileDescriptor);
Index newIndex = metaDataFromFile.getIndex("MySimpleRecord$num_value_2");
assertEquals(Key.Expressions.field("num_value_2"), newIndex.getRootExpression());
assertThat("newIndex not marked as unique", newIndex.isUnique());
// Update records. Validate that created meta-data does not add index.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.updateRecords(newFileDescriptor);
// read from local cache
RecordMetaData metaData = metaDataStore.getRecordMetaData();
MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getIndex("MySimpleRecord$num_value_2"));
assertThat(e.getMessage(), containsString("Index MySimpleRecord$num_value_2 not defined"));
context.commit();
}
// Validate that reading the index back from database does not add the index.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.updateRecords(newFileDescriptor);
// read from the database
RecordMetaData metaData = metaDataStore.getRecordMetaData();
MetaDataException e = assertThrows(MetaDataException.class, () -> metaData.getIndex("MySimpleRecord$num_value_2"));
assertThat(e.getMessage(), containsString("Index MySimpleRecord$num_value_2 not defined"));
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method deprecateImportedRecordType.
@Test
public void deprecateImportedRecordType() {
int version;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsImportProto.getDescriptor());
version = metaData.getVersion();
metaDataStore.saveRecordMetaData(metaData);
context.commit();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
}
// Deprecate the record type
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(version, metaData.getVersion());
assertNotNull(metaData.getRecordType("MySimpleRecord"));
assertNotNull(metaData.getRecordType("MyLongRecord"));
MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("MySimpleRecord"));
assertEquals("Record type MySimpleRecord not found", e.getMessage());
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method withToProto.
@Test
public void withToProto() {
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsParentChildRelationshipProto.getDescriptor());
metaDataBuilder.addIndex("MyChildRecord", "MyChildRecord$str_value", Key.Expressions.field("str_value"));
metaDataBuilder.removeIndex("MyChildRecord$parent_rec_no");
metaDataBuilder.addIndex("MyChildRecord", new Index("MyChildRecord$parent&str", Key.Expressions.concatenateFields("parent_rec_no", "str_value"), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
metaDataBuilder.removeIndex("MyParentRecord$str_value_indexed");
metaDataBuilder.addIndex("MyParentRecord", "MyParentRecord$str&child", Key.Expressions.concat(Key.Expressions.field("str_value_indexed"), Key.Expressions.field("child_rec_nos", KeyExpression.FanType.FanOut)));
metaDataBuilder.addMultiTypeIndex(Arrays.asList(metaDataBuilder.getRecordType("MyChildRecord"), metaDataBuilder.getRecordType("MyParentRecord")), new Index("all$rec_nos", Key.Expressions.field("rec_no")));
RecordMetaData metaData = metaDataBuilder.getRecordMetaData();
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
MetaDataProtoTest.verifyEquals(metaData, retrievedMetaData);
}
}
Aggregations