use of com.apple.foundationdb.record.metadata.MetaDataException 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();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException 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"));
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException 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();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method updateRecordsWithLocalFileDescriptor.
@Test
public void updateRecordsWithLocalFileDescriptor() {
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"));
// Local file descriptor is not as evolved as the to-be updating records descriptor. It will fail.
metaDataStore.setLocalFileDescriptor(TestRecords1Proto.getDescriptor());
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor()));
assertEquals(e.getMessage(), "record type removed from union");
metaDataStore.setLocalFileDescriptor(TestRecords1EvolvedAgainProto.getDescriptor());
metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor());
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("AnotherRecord"));
e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("OneMoreRecord"));
assertEquals(e.getMessage(), "Unknown record type OneMoreRecord");
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("AnotherRecord"));
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("OneMoreRecord"));
assertEquals(e.getMessage(), "Unknown record type OneMoreRecord");
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreReplaceIndexTest method replacementIndexMissingInMetaDataFails.
@Test
public void replacementIndexMissingInMetaDataFails() {
try (FDBRecordContext context = openContext()) {
MetaDataException err = assertThrows(MetaDataException.class, () -> openSimpleRecordStore(context, metaDataBuilder -> {
final Index index = new Index("indexWithFakeReplacement", Key.Expressions.field("num_value_2"), IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "fakeIndex"));
metaDataBuilder.addIndex("MySimpleRecord", index);
}));
assertThat(err.getMessage(), containsString("Index indexWithFakeReplacement has replacement index fakeIndex that is not in the meta-data"));
}
}
Aggregations