Search in sources :

Example 41 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.

the class RecordMetaDataBuilderTest method updateRecords.

@Test
public void updateRecords() {
    RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    final int prevVersion = builder.getVersion();
    assertSame(builder.getRecordType("MySimpleRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
    assertSame(builder.getRecordType("MyOtherRecord").getDescriptor().getFile(), TestRecords1Proto.getDescriptor());
    builder.updateRecords(TestRecords1EvolvedProto.getDescriptor(), true);
    RecordMetaData recordMetaData = builder.build(true);
    assertSame(TestRecords1EvolvedProto.getDescriptor(), recordMetaData.getRecordType("MySimpleRecord").getDescriptor().getFile());
    assertSame(TestRecords1EvolvedProto.getDescriptor(), recordMetaData.getRecordType("MyOtherRecord").getDescriptor().getFile());
    assertSame(TestRecords1EvolvedProto.getDescriptor(), recordMetaData.getRecordType("AnotherRecord").getDescriptor().getFile());
    assertEquals(recordMetaData.getVersion(), recordMetaData.getRecordType("AnotherRecord").getSinceVersion().intValue());
    assertThat(recordMetaData.getRecordType("AnotherRecord").getSinceVersion(), greaterThan(prevVersion));
    MetaDataException e = assertThrows(MetaDataException.class, () -> RecordMetaData.newBuilder().setLocalFileDescriptor(TestRecords1EvolvedProto.getDescriptor()).setRecords(recordMetaData.toProto()).updateRecords(TestRecords1EvolvedProto.getDescriptor()));
    assertEquals("Updating the records descriptor is not allowed when the local file descriptor is set", e.getMessage());
    e = assertThrows(MetaDataException.class, () -> RecordMetaData.newBuilder().updateRecords(TestRecords1EvolvedProto.getDescriptor()));
    assertEquals("Records descriptor is not set yet", e.getMessage());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData 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")));
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 43 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData 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());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData 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());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with RecordMetaData

use of com.apple.foundationdb.record.RecordMetaData 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());
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Aggregations

RecordMetaData (com.apple.foundationdb.record.RecordMetaData)168 Test (org.junit.jupiter.api.Test)130 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)81 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 Nonnull (javax.annotation.Nonnull)47 Descriptors (com.google.protobuf.Descriptors)44 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)39 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)39 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)38 Tuple (com.apple.foundationdb.tuple.Tuple)36 List (java.util.List)33 ArrayList (java.util.ArrayList)31 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)30 Index (com.apple.foundationdb.record.metadata.Index)30 DescriptorProtos (com.google.protobuf.DescriptorProtos)30 Collectors (java.util.stream.Collectors)30 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)27 ByteString (com.google.protobuf.ByteString)27 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)27 RecordMetaDataOptionsProto (com.apple.foundationdb.record.RecordMetaDataOptionsProto)26