use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method newNameSuggestsNestedType.
/**
* Validate that a message type cannot be renamed to something with a "." in it.
*/
@Test
public void newNameSuggestsNestedType() {
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);
MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("OuterRecord", "OuterRecord.SomeNestedRecord"));
// We rely on underlying Protobuf validation for this case
assertEquals("Error converting from protobuf", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method multiTypeIndex.
@Test
public void multiTypeIndex() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsMultiProto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.addUniversalIndex(FDBRecordStoreTestBase.COUNT_INDEX);
metaDataStore.addMultiTypeIndex(Arrays.asList("MultiRecordOne", "MultiRecordTwo", "MultiRecordThree"), new Index("all$elements", Key.Expressions.field("element", KeyExpression.FanType.Concatenate), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
metaDataStore.addMultiTypeIndex(null, new Index("all$elements2", Key.Expressions.field("element", KeyExpression.FanType.Concatenate), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
metaDataStore.addMultiTypeIndex(Arrays.asList("MultiRecordTwo", "MultiRecordThree"), new Index("two&three$ego", Key.Expressions.field("ego"), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
metaDataStore.addMultiTypeIndex(Arrays.asList("MultiRecordOne"), new Index("one$name", Key.Expressions.field("name"), IndexTypes.VALUE));
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(5, metaData.getAllIndexes().size());
assertEquals(0, metaData.getFormerIndexes().size());
assertNotNull(metaData.getIndex("all$elements"));
assertEquals(3, metaData.recordTypesForIndex(metaData.getIndex("all$elements")).size());
assertNotNull(metaData.getIndex("all$elements2"));
assertEquals(3, metaData.recordTypesForIndex(metaData.getIndex("all$elements2")).size());
assertNotNull(metaData.getIndex("two&three$ego"));
assertEquals(2, metaData.recordTypesForIndex(metaData.getIndex("two&three$ego")).size());
assertNotNull(metaData.getIndex("one$name"));
assertEquals(1, metaData.recordTypesForIndex(metaData.getIndex("one$name")).size());
metaDataStore.dropIndex("one$name");
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getIndex("one$name"));
assertEquals("Index one$name not defined", e.getMessage());
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(4, metaData.getAllIndexes().size());
assertEquals(1, metaData.getFormerIndexes().size());
assertNotNull(metaDataStore.getRecordMetaData().getIndex("all$elements"));
assertNotNull(metaDataStore.getRecordMetaData().getIndex("all$elements2"));
assertNotNull(metaDataStore.getRecordMetaData().getIndex("two&three$ego"));
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getIndex("one$name"));
assertEquals("Index one$name not defined", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method withIncompatibleChange.
@Test
public void withIncompatibleChange() {
RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(metaData1);
context.commit();
}
RecordMetaData metaData2 = RecordMetaData.build(metaData1.toProto().toBuilder().setVersion(metaData1.getVersion() + 1).clearIndexes().build());
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.saveRecordMetaData(metaData2));
assertThat(e.getMessage(), containsString("index missing in new meta-data"));
context.commit();
}
// Using the builder, this change should be fine.
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaData1.getAllIndexes().forEach(index -> metaDataBuilder.removeIndex(index.getName()));
RecordMetaData metaData3 = metaDataBuilder.getRecordMetaData();
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(metaData3);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData retrievedMetaData = metaDataStore.getRecordMetaData();
MetaDataProtoTest.verifyEquals(metaData3, retrievedMetaData);
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method nonDefaultUnionRecordTypes.
@Test
public void nonDefaultUnionRecordTypes() {
int version;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords3Proto.getDescriptor());
metaDataBuilder.getOnlyRecordType().setPrimaryKey(Key.Expressions.concatenateFields("parent_path", "child_name"));
RecordMetaData metaData = metaDataBuilder.getRecordMetaData();
version = metaData.getVersion();
metaDataStore.saveRecordMetaData(metaData);
context.commit();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
}
// Add a record type with the default union name to a record meta-data that has a non-default union name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
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 second record type with non-default union name but union usage.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
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("SecondNonDefaultUnionRecord").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 with non-default name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("UnionDescriptor"));
assertEquals(e.getMessage(), "Cannot deprecate the union");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Add a record type to a meta-data that has non-default union name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
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("MyHierarchicalRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyNewRecord"));
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
assertEquals(version + 1, metaDataStore.getRecordMetaData().getRecordType("MyNewRecord").getSinceVersion().intValue());
context.commit();
}
// Deprecate a record type from a meta-data that has non-default union name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
deprecateRecordType("MyHierarchicalRecord");
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
assertTrue(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("UnionDescriptor").findFieldByName("_MyHierarchicalRecord").getOptions().getDeprecated());
assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
// do not commit
}
// Validate that deprecation can happen when the type is fully qualified
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
deprecateRecordType(".com.apple.foundationdb.record.test3.MyHierarchicalRecord");
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyHierarchicalRecord"));
assertTrue(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("UnionDescriptor").findFieldByName("_MyHierarchicalRecord").getOptions().getDeprecated());
assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreReplaceIndexTest method replacementMoreComplicatedGraphFails.
@Test
public void replacementMoreComplicatedGraphFails() {
try (FDBRecordContext context = openContext()) {
MetaDataException err = assertThrows(MetaDataException.class, () -> openSimpleRecordStore(context, metaDataBuilder -> {
final KeyExpression expr = Key.Expressions.field("num_value_2");
final Index indexA = new Index("indexA", expr, IndexTypes.VALUE, ImmutableMap.of(IndexOptions.REPLACED_BY_OPTION_PREFIX + "_0", "indexB", IndexOptions.REPLACED_BY_OPTION_PREFIX + "_1", "indexC"));
final Index indexB = new Index("indexB", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
final Index indexC = new Index("indexC", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
final Index indexD = new Index("indexD", expr);
metaDataBuilder.addIndex("MySimpleRecord", indexA);
metaDataBuilder.addIndex("MySimpleRecord", indexB);
metaDataBuilder.addIndex("MySimpleRecord", indexC);
metaDataBuilder.addIndex("MySimpleRecord", indexD);
}));
assertThat(err.getMessage(), containsString("has replacement indexes"));
}
}
Aggregations