use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilder method processRecordType.
@Nonnull
private RecordTypeBuilder processRecordType(@Nonnull Descriptors.FieldDescriptor unionField, boolean processExtensionOptions) {
Descriptors.Descriptor descriptor = unionField.getMessageType();
RecordTypeBuilder recordType = new RecordTypeBuilder(descriptor);
if (recordTypes.putIfAbsent(recordType.getName(), recordType) != null) {
throw new MetaDataException("There is already a record type named " + recordType.getName());
}
if (processExtensionOptions) {
RecordMetaDataOptionsProto.RecordTypeOptions recordTypeOptions = descriptor.getOptions().getExtension(RecordMetaDataOptionsProto.record);
if (recordTypeOptions != null && recordTypeOptions.hasSinceVersion()) {
recordType.setSinceVersion(recordTypeOptions.getSinceVersion());
}
if (recordTypeOptions != null && recordTypeOptions.hasRecordTypeKey()) {
recordType.setRecordTypeKey(LiteralKeyExpression.fromProto(recordTypeOptions.getRecordTypeKey()).getValue());
}
protoFieldOptions(recordType);
}
return recordType;
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreOpeningTest method testUpdateRecords.
@Test
public void testUpdateRecords() {
KeySpacePath metaDataPath;
Subspace metaDataSubspace;
try (FDBRecordContext context = fdb.openContext()) {
metaDataPath = TestKeySpace.getKeyspacePath("record-test", "unit", "metadataStore");
metaDataSubspace = metaDataPath.toSubspace(context);
context.ensureActive().clear(Range.startsWith(metaDataSubspace.pack()));
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
RecordMetaData origMetaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
final int version = origMetaData.getVersion();
FDBMetaDataStore metaDataStore = createMetaDataStore(context, metaDataPath, metaDataSubspace, TestRecords1Proto.getDescriptor());
FDBRecordStore recordStore = storeBuilder(context, origMetaData).setMetaDataStore(metaDataStore).createOrOpen();
assertEquals(version, recordStore.getRecordMetaData().getVersion());
TestRecords1Proto.MySimpleRecord record = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).setStrValueIndexed("value").setNumValue3Indexed(1729).build();
recordStore.saveRecord(record);
// Update the records without a local descriptor. Storing an evolved record must fail.
final TestRecords1EvolvedProto.MySimpleRecord evolvedRecord = TestRecords1EvolvedProto.MySimpleRecord.newBuilder().setRecNo(1067L).setNumValue2(43).setStrValueIndexed("evolved value").setNumValue3Indexed(1730).build();
metaDataStore = createMetaDataStore(context, metaDataPath, metaDataSubspace, null);
// Bumps the version
metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor());
final FDBRecordStore recordStoreWithNoLocalFileDescriptor = storeBuilder(context, origMetaData).setMetaDataStore(metaDataStore).open();
assertEquals(version + 1, recordStoreWithNoLocalFileDescriptor.getRecordMetaData().getVersion());
MetaDataException e = assertThrows(MetaDataException.class, () -> recordStoreWithNoLocalFileDescriptor.saveRecord(evolvedRecord));
assertEquals(e.getMessage(), "descriptor did not match record type");
// Update the records with a local descriptor. Storing an evolved record must succeed this time.
metaDataStore = createMetaDataStore(context, metaDataPath, metaDataSubspace, TestRecords1EvolvedProto.getDescriptor());
// Bumps the version
metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor());
recordStore = storeBuilder(context, origMetaData).setMetaDataStore(metaDataStore).open();
assertEquals(version + 2, recordStore.getRecordMetaData().getVersion());
recordStore.saveRecord(evolvedRecord);
// Evolve the meta-data one more time and use it for local file descriptor. SaveRecord will succeed.
final TestRecords1EvolvedAgainProto.MySimpleRecord evolvedAgainRecord = TestRecords1EvolvedAgainProto.MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).setStrValueIndexed("value").setNumValue3Indexed(1729).build();
metaDataStore = createMetaDataStore(context, metaDataPath, metaDataSubspace, TestRecords1EvolvedAgainProto.getDescriptor());
// Bumps the version
metaDataStore.updateRecords(TestRecords1EvolvedProto.getDescriptor());
recordStore = storeBuilder(context, origMetaData).setMetaDataStore(metaDataStore).open();
assertEquals(version + 3, recordStore.getRecordMetaData().getVersion());
recordStore.saveRecord(evolvedAgainRecord);
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException 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.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method noUnionLocalFileDescriptor.
@Test
public void noUnionLocalFileDescriptor() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(TestNoUnionProto.getDescriptor());
context.commit();
}
// The type cannot become NESTED in the local file descriptor
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.setLocalFileDescriptor(TestNoUnionEvolvedIllegalProto.getDescriptor());
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData());
assertEquals("record type removed from union", e.getMessage());
context.commit();
}
// Deprecate a record type.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
deprecateRecordType(".com.apple.foundationdb.record.testnounion.MySimpleRecord");
context.commit();
}
// Pass a local file descriptor and make sure MySimpleRecord's deprecated.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.setLocalFileDescriptor(TestNoUnionProto.getDescriptor());
Descriptors.FieldDescriptor deprecatedField = metaDataStore.getRecordMetaData().getUnionDescriptor().getFields().get(0);
assertEquals("_MySimpleRecord", deprecatedField.getName());
assertTrue(deprecatedField.getOptions().getDeprecated());
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException 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"));
}
}
Aggregations