use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method updateRecordsWithRenamedAndNamedToOld.
@Test
public void updateRecordsWithRenamedAndNamedToOld() {
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MySimpleRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
assertSame(builder.getRecordType("MyOtherRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
builder.addIndex("MyOtherRecord", "num_value_3_indexed");
RecordMetaData metaData = builder.getRecordMetaData();
assertEquals(Collections.singletonList(metaData.getRecordType("MyOtherRecord")), metaData.recordTypesForIndex(metaData.getIndex("MyOtherRecord$num_value_3_indexed")));
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method invalidLocalMetaData.
@Test
public void invalidLocalMetaData() {
// Change the type of a field
Descriptors.FileDescriptor updatedFile = MetaDataEvolutionValidatorTest.mutateField("MySimpleRecord", "str_value_indexed", field -> field.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_BYTES));
RecordMetaDataProto.MetaData originalMetaData = RecordMetaData.build(TestRecords1Proto.getDescriptor()).toProto();
RecordMetaDataBuilder evolvedMetaDataBuilder = RecordMetaData.newBuilder().setLocalFileDescriptor(updatedFile);
MetaDataException e = assertThrows(MetaDataException.class, () -> evolvedMetaDataBuilder.setRecords(originalMetaData));
assertEquals("field type changed", e.getMessage());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method counterBasedSubspaceKeysBackwardCompatibility.
@Test
public void counterBasedSubspaceKeysBackwardCompatibility() {
// User had some old meta-data in which some indexes had implicit subspace keys.
RecordMetaDataProto.MetaData.Builder protoBuilder = RecordMetaDataProto.MetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor().toProto());
protoBuilder.addIndexesBuilder().setName("preUpgradeIndex").setType(IndexTypes.VALUE).addRecordType("MySimpleRecord").setRootExpression(Key.Expressions.field("num_value_2").toKeyExpression()).clearSubspaceKey();
RecordMetaDataBuilder preUpgradeBuilder = RecordMetaData.newBuilder().setRecords(protoBuilder.build(), true);
RecordMetaData preUpgradeMetaData = preUpgradeBuilder.build(true);
assertNotNull(preUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(preUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(preUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed"));
assertNotNull(preUpgradeMetaData.getIndex("preUpgradeIndex"));
assertEquals(preUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed").getName(), preUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(preUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique").getName(), preUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(preUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed").getName(), preUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
assertEquals(preUpgradeMetaData.getIndex("preUpgradeIndex").getName(), preUpgradeMetaData.getIndex("preUpgradeIndex").getSubspaceKey());
// User has chosen not to enable counter-based subspace keys after upgrade.
RecordMetaDataBuilder afterUpgradeBuilder = RecordMetaData.newBuilder().setRecords(preUpgradeMetaData.toProto());
afterUpgradeBuilder.addIndex("MySimpleRecord", "postUpgradeIndex", "num_value_2");
RecordMetaData afterUpgradeMetaData = afterUpgradeBuilder.build(true);
assertNotNull(afterUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed"));
assertNotNull(afterUpgradeMetaData.getIndex("preUpgradeIndex"));
assertNotNull(afterUpgradeMetaData.getIndex("postUpgradeIndex"));
assertEquals(afterUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed").getName(), afterUpgradeMetaData.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique").getName(), afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed").getName(), afterUpgradeMetaData.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
assertEquals(afterUpgradeMetaData.getIndex("preUpgradeIndex").getName(), afterUpgradeMetaData.getIndex("preUpgradeIndex").getSubspaceKey());
assertEquals(afterUpgradeMetaData.getIndex("postUpgradeIndex").getName(), afterUpgradeMetaData.getIndex("postUpgradeIndex").getSubspaceKey());
// But later user decides to enable counter-based subspace keys assignment. Previous indexes will use name, newer indexes will use the counter.
RecordMetaDataBuilder builderWithCounter = RecordMetaData.newBuilder().enableCounterBasedSubspaceKeys().setRecords(afterUpgradeMetaData.toProto());
builderWithCounter.addIndex("MySimpleRecord", "postUpgradeWithCounterIndex", "num_value_2");
RecordMetaData metaDataWithCounter = builderWithCounter.build(true);
assertNotNull(metaDataWithCounter.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(metaDataWithCounter.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(metaDataWithCounter.getIndex("MySimpleRecord$num_value_3_indexed"));
assertNotNull(metaDataWithCounter.getIndex("preUpgradeIndex"));
assertNotNull(metaDataWithCounter.getIndex("postUpgradeIndex"));
assertNotNull(metaDataWithCounter.getIndex("postUpgradeWithCounterIndex"));
assertEquals(metaDataWithCounter.getIndex("MySimpleRecord$str_value_indexed").getName(), metaDataWithCounter.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(metaDataWithCounter.getIndex("MySimpleRecord$num_value_unique").getName(), metaDataWithCounter.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(metaDataWithCounter.getIndex("MySimpleRecord$num_value_3_indexed").getName(), metaDataWithCounter.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
assertEquals(metaDataWithCounter.getIndex("preUpgradeIndex").getName(), metaDataWithCounter.getIndex("preUpgradeIndex").getSubspaceKey());
assertEquals(metaDataWithCounter.getIndex("postUpgradeIndex").getName(), metaDataWithCounter.getIndex("postUpgradeIndex").getSubspaceKey());
assertEquals(1L, metaDataWithCounter.getIndex("postUpgradeWithCounterIndex").getSubspaceKey());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method selfContainedMetaData.
@Test
public void selfContainedMetaData() {
RecordMetaData recordMetaData = RecordMetaData.build(TestRecordsChained1Proto.getDescriptor());
RecordMetaDataProto.MetaData metaData = recordMetaData.toProto();
// Rebuild from proto
RecordMetaData fromProto = RecordMetaData.build(metaData);
MetaDataProtoTest.verifyEquals(recordMetaData, fromProto);
// Basic setRecords
RecordMetaDataBuilder builder2 = RecordMetaData.newBuilder().setRecords(metaData);
MetaDataProtoTest.verifyEquals(recordMetaData, builder2.getRecordMetaData());
// Override a dependency
RecordMetaDataBuilder builder3 = RecordMetaData.newBuilder().addDependency(TestRecords1Proto.getDescriptor()).setRecords(builder2.getRecordMetaData().toProto());
MetaDataProtoTest.verifyEquals(recordMetaData, builder3.getRecordMetaData());
// Exclude dependencies
Descriptors.FileDescriptor[] dependencies = new Descriptors.FileDescriptor[] { TestRecords1Proto.getDescriptor() };
MetaDataException e = assertThrows(MetaDataException.class, () -> RecordMetaData.newBuilder().setRecords(recordMetaData.toProto(dependencies)));
assertEquals("Dependency test_records_1.proto not found", e.getMessage());
RecordMetaDataBuilder builder4 = RecordMetaData.newBuilder().addDependencies(dependencies).setRecords(recordMetaData.toProto(dependencies));
MetaDataProtoTest.verifyEquals(recordMetaData, builder4.getRecordMetaData());
Descriptors.FileDescriptor dep4 = builder4.getRecordMetaData().getRecordsDescriptor().getDependencies().get(1);
assertEquals(dep4.getName(), TestRecords1Proto.getDescriptor().getName());
assertSame(dep4, dependencies[0]);
Descriptors.FileDescriptor dep2 = builder2.getRecordMetaData().getRecordsDescriptor().getDependencies().get(1);
assertEquals(dep2.getName(), dep4.getName());
assertNotSame(dep2, dep4);
// Add and remove index
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecordsChained1Proto.getDescriptor());
int version = builder.getVersion();
builder.addIndex("MySimpleRecord", "MySimpleRecord$testIndex", "rec_no");
assertEquals(builder.getVersion(), version + 1);
assertNotNull(builder.getRecordMetaData().getIndex("MySimpleRecord$testIndex"));
builder.removeIndex("MySimpleRecord$testIndex");
assertEquals(builder.getVersion(), version + 2);
e = assertThrows(MetaDataException.class, () -> builder.getRecordMetaData().getIndex("MySimpleRecord$testIndex"));
assertEquals("Index MySimpleRecord$testIndex not defined", e.getMessage());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method indexOnNestedPrimaryKey.
@ParameterizedTest(name = "indexOnNestedPrimaryKey [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void indexOnNestedPrimaryKey(final boolean indexCounterBasedSubspaceKey) {
RecordMetaDataBuilder builder = createBuilder(TestRecordsWithHeaderProto.getDescriptor(), indexCounterBasedSubspaceKey);
builder.getRecordType("MyRecord").setPrimaryKey(field("header").nest("rec_no"));
builder.addIndex("MyRecord", new Index("MyRecord$PRIMARY", field("header").nest("rec_no"), IndexTypes.VALUE));
RecordMetaData metaData = builder.getRecordMetaData();
Index index = metaData.getIndex("MyRecord$PRIMARY");
assertNotNull(index);
assertNotNull(index.getPrimaryKeyComponentPositions());
assertArrayEquals(new int[] { 0 }, index.getPrimaryKeyComponentPositions());
if (!indexCounterBasedSubspaceKey) {
assertEquals(index.getName(), index.getSubspaceKey());
} else {
assertEquals(1L, index.getSubspaceKey());
}
}
Aggregations