Search in sources :

Example 91 with RecordMetaData

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

the class MetaDataProtoTest method metadataProtoSimple.

@Test
public void metadataProtoSimple() throws KeyExpression.DeserializationException, KeyExpression.SerializationException {
    List<Descriptors.FileDescriptor> files = Arrays.asList(TestRecords1Proto.getDescriptor(), TestRecords2Proto.getDescriptor(), TestRecords4Proto.getDescriptor(), TestRecords5Proto.getDescriptor(), TestRecords6Proto.getDescriptor(), TestRecords7Proto.getDescriptor(), TestRecordsChained1Proto.getDescriptor(), TestRecordsChained2Proto.getDescriptor(), TestRecordsImportProto.getDescriptor(), TestRecordsImportFlatProto.getDescriptor(), TestRecordsMultiProto.getDescriptor(), TestRecordsParentChildRelationshipProto.getDescriptor(), TestRecordsWithUnionProto.getDescriptor(), TestRecordsIndexCompatProto.getDescriptor());
    for (int i = 0; i < files.size(); i++) {
        Descriptors.FileDescriptor file = files.get(i);
        RecordMetaData metaData = RecordMetaData.build(file);
        RecordMetaDataBuilder builder = RecordMetaData.newBuilder();
        builder.addDependencies(BASE_DEPENDENCIES);
        RecordMetaData metaDataRedone = builder.setRecords(metaData.toProto()).getRecordMetaData();
        verifyEquals(metaData, metaDataRedone);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test)

Example 92 with RecordMetaData

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

the class MetaDataProtoTest method versionstampIndexDeserialization.

@Test
public void versionstampIndexDeserialization() throws Exception {
    RecordMetaDataProto.MetaData.Builder protoBuilder = RecordMetaDataProto.MetaData.newBuilder().setRecords(TestRecordsWithHeaderProto.getDescriptor().toProto()).setStoreRecordVersions(true);
    protoBuilder.addIndexesBuilder().setName("VersionstampIndex").setType(IndexTypes.VERSION).addRecordType("MyRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setNesting(RecordMetaDataProto.Nesting.newBuilder().setParent(scalarField("header")).setChild(RecordMetaDataProto.KeyExpression.newBuilder().setThen(RecordMetaDataProto.Then.newBuilder().addChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("num"))).addChild(RecordMetaDataProto.KeyExpression.newBuilder().setVersion(RecordMetaDataProto.Version.getDefaultInstance()))))));
    protoBuilder.addRecordTypes(RecordMetaDataProto.RecordType.newBuilder().setName("MyRecord").setPrimaryKey(RecordMetaDataProto.KeyExpression.newBuilder().setNesting(RecordMetaDataProto.Nesting.newBuilder().setParent(scalarField("header")).setChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("rec_no"))))));
    RecordMetaData metaData = RecordMetaData.newBuilder().addDependencies(BASE_DEPENDENCIES).setRecords(protoBuilder.build()).getRecordMetaData();
    Index versionstampIndex = metaData.getIndex("VersionstampIndex");
    assertEquals(1, versionstampIndex.getRootExpression().versionColumns());
    assertEquals(IndexTypes.VERSION, versionstampIndex.getType());
    assertEquals(Key.Expressions.field("header").nest(Key.Expressions.concat(Key.Expressions.field("num"), VersionKeyExpression.VERSION)), versionstampIndex.getRootExpression());
    assertEquals(Collections.singletonList(metaData.getRecordType("MyRecord")), metaData.recordTypesForIndex(versionstampIndex));
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test)

Example 93 with RecordMetaData

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

the class MetaDataProtoTest method indexGroupingCompatibility.

@Test
// Older (deprecated) types had special grouping behavior
@SuppressWarnings("deprecation")
public void indexGroupingCompatibility() throws Exception {
    RecordMetaDataProto.MetaData.Builder protoBuilder = RecordMetaDataProto.MetaData.newBuilder();
    protoBuilder.setRecords(TestRecordsIndexCompatProto.getDescriptor().toProto());
    protoBuilder.addIndexesBuilder().setName("RecordCount").setType(IndexTypes.COUNT).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setEmpty(RecordMetaDataProto.Empty.getDefaultInstance()));
    protoBuilder.addIndexesBuilder().setName("MaxRecNo").setType(IndexTypes.MAX_EVER).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("rec_no")));
    protoBuilder.addIndexesBuilder().setName("MaxRecNoGrouped").setType(IndexTypes.MAX_EVER).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setThen(RecordMetaDataProto.Then.newBuilder().addChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("index"))).addChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("rec_no")))));
    RecordMetaData metaData = RecordMetaData.newBuilder().addDependencies(BASE_DEPENDENCIES).setRecords(protoBuilder.build(), true).getRecordMetaData();
    Index regularIndex = metaData.getIndex("MyCompatRecord$index");
    assertFalse(regularIndex.getRootExpression() instanceof GroupingKeyExpression, "should not have Grouping");
    Index compatRank = metaData.getIndex("MyCompatRecord$rank");
    assertTrue(compatRank.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
    assertEquals(1, ((GroupingKeyExpression) compatRank.getRootExpression()).getGroupedCount());
    Index modernRank = metaData.getIndex("MyModernRecord$rank");
    assertTrue(modernRank.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
    assertEquals(1, ((GroupingKeyExpression) modernRank.getRootExpression()).getGroupedCount());
    Index recordCount = metaData.getIndex("RecordCount");
    assertTrue(recordCount.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
    assertEquals(0, ((GroupingKeyExpression) recordCount.getRootExpression()).getGroupedCount());
    Index maxRecNo = metaData.getIndex("MaxRecNo");
    assertTrue(maxRecNo.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
    assertEquals(1, ((GroupingKeyExpression) maxRecNo.getRootExpression()).getGroupedCount());
    Index maxRecNoGrouped = metaData.getIndex("MaxRecNoGrouped");
    assertTrue(maxRecNoGrouped.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
    assertEquals(1, ((GroupingKeyExpression) maxRecNoGrouped.getRootExpression()).getGroupedCount());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Test(org.junit.jupiter.api.Test)

Example 94 with RecordMetaData

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

the class MetaDataEvolutionValidatorTestV3 method onlyFileProto2ToProto3.

/**
 * Validate that it is legal to change the records descriptor from proto2 to proto3 as long as all of the records
 * contained within that file are still the same syntax.
 */
@Test
public void onlyFileProto2ToProto3() throws InvalidProtocolBufferException {
    assertNotEquals(TestRecords1Proto.getDescriptor().getSyntax(), TestRecords1ImportedProto.getDescriptor().getSyntax());
    MetaDataEvolutionValidator.getDefaultInstance().validateUnion(TestRecords1Proto.RecordTypeUnion.getDescriptor(), TestRecords1ImportedProto.RecordTypeUnion.getDescriptor());
    MetaDataEvolutionValidator.getDefaultInstance().validateUnion(TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(), TestRecords1Proto.RecordTypeUnion.getDescriptor());
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, TestRecords1ImportedProto.getDescriptor());
    MetaDataEvolutionValidator.getDefaultInstance().validate(metaData1, metaData2);
    // Validate that the nested proto2 records in the proto3 file have proper nullability semantics
    TestRecords1Proto.RecordTypeUnion unionRecordProto2 = TestRecords1Proto.RecordTypeUnion.newBuilder().setMySimpleRecord(TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(0).setStrValueIndexed("")).build();
    assertThat(unionRecordProto2.getMySimpleRecord().hasNumValue2(), is(false));
    assertThat(unionRecordProto2.getMySimpleRecord().hasStrValueIndexed(), is(true));
    TestRecords1ImportedProto.RecordTypeUnion unionRecordProto3 = TestRecords1ImportedProto.RecordTypeUnion.parseFrom(unionRecordProto2.toByteString());
    assertThat(unionRecordProto3.getMySimpleRecord().hasNumValue2(), is(false));
    assertThat(unionRecordProto3.getMySimpleRecord().hasStrValueIndexed(), is(true));
    final FieldDescriptor unionField = TestRecords1ImportedProto.RecordTypeUnion.getDescriptor().findFieldByName("_MySimpleRecord");
    final FieldDescriptor numValue2Field = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("num_value_2");
    final FieldDescriptor strValueIndexedField = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("str_value_indexed");
    DynamicMessage dynamicUnionRecordProto3 = DynamicMessage.parseFrom(TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(), unionRecordProto2.toByteString());
    Message dynamicSimpleRecord = (Message) dynamicUnionRecordProto3.getField(unionField);
    assertThat(dynamicSimpleRecord.hasField(numValue2Field), is(false));
    assertThat(dynamicSimpleRecord.hasField(strValueIndexedField), is(true));
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) TestRecords1ImportedProto(com.apple.foundationdb.record.evolution.TestRecords1ImportedProto) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) DynamicMessage(com.google.protobuf.DynamicMessage) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.jupiter.api.Test)

Example 95 with RecordMetaData

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

the class MetaDataEvolutionValidatorTestV3 method nestedProto2ToProto3.

/**
 * Validate that if a nested field changes syntax, then the meta-data evolution is invalidated.
 */
@Test
public void nestedProto2ToProto3() {
    assertInvalid("message descriptor proto syntax changed", TestRecordsNestedProto2.getDescriptor(), TestRecordsNestedProto3.getDescriptor());
    assertInvalid("message descriptor proto syntax changed", TestRecordsNestedProto3.getDescriptor(), TestRecordsNestedProto2.getDescriptor());
    RecordMetaData metaData1 = RecordMetaData.build(TestRecordsNestedProto2.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, TestRecordsNestedProto3.getDescriptor(), protoBuilder -> protoBuilder.clearDependencies().addDependencies(TestNestedProto3.getDescriptor().toProto()));
    assertInvalid("message descriptor proto syntax changed", metaData1, metaData2);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test)

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