use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method unionRecordTypes.
@Test
public void unionRecordTypes() {
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 a record type with default union name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).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(unionRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding UNION record type not allowed");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Add a record type with non-default union name but union usage.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.MessageOptions.Builder unionMessageOptions = DescriptorProtos.MessageOptions.newBuilder().setExtension(RecordMetaDataOptionsProto.record, RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder().setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.UNION).build());
DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("NonDefaultUnionRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).setOptions(unionMessageOptions).build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(unionRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding UNION record type not allowed");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Deprecate the record type union.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
assertEquals(e.getMessage(), "Cannot deprecate the union");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
}
use of com.apple.foundationdb.record.RecordMetaData 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.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method extensionRegistry.
@Test
public void extensionRegistry() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
// Default registry parses options
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
Descriptors.Descriptor mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
RecordMetaDataOptionsProto.FieldOptions recNoFieldOptions = mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().getExtension(RecordMetaDataOptionsProto.field);
assertNotNull(recNoFieldOptions);
assertThat(recNoFieldOptions.getPrimaryKey(), is(true));
// Empty registry does not
openMetaDataStore(context);
metaDataStore.setExtensionRegistry(ExtensionRegistry.getEmptyRegistry());
metaData = metaDataStore.getRecordMetaData();
mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
assertThat(mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().hasExtension(RecordMetaDataOptionsProto.field), is(false));
// Null registry behaves like the empty registry in proto2 and throws an exception in proto3
openMetaDataStore(context);
metaDataStore.setExtensionRegistry(null);
if (ProtoVersionSupplier.getProtoVersion() == 2) {
metaData = metaDataStore.getRecordMetaData();
mySimpleRecordDescriptor = metaData.getRecordType("MySimpleRecord").getDescriptor();
assertNotSame(mySimpleRecordDescriptor.getFile(), TestRecords1Proto.getDescriptor());
assertThat(mySimpleRecordDescriptor.findFieldByName("rec_no").getOptions().hasExtension(RecordMetaDataOptionsProto.field), is(false));
} else {
final FDBMetaDataStore finalMetaDataStore = metaDataStore;
assertThrows(NullPointerException.class, finalMetaDataStore::getRecordMetaData);
}
}
}
use of com.apple.foundationdb.record.RecordMetaData 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.RecordMetaData 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();
}
}
Aggregations