Search in sources :

Example 46 with MetaDataException

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

the class FDBMetaDataStoreTest method updateSchemaOptions.

@Test
public void updateSchemaOptions() {
    int version;
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        version = metaDataStore.getRecordMetaData().getVersion();
        context.commit();
    }
    // Unset store record versions.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertTrue(metaDataStore.getRecordMetaData().isStoreRecordVersions());
        metaDataStore.updateStoreRecordVersions(false);
        context.commit();
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        assertFalse(metaDataStore.getRecordMetaData().isStoreRecordVersions());
    }
    // Set store record versions.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertFalse(metaDataStore.getRecordMetaData().isStoreRecordVersions());
        metaDataStore.updateStoreRecordVersions(true);
        context.commit();
        assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
        assertTrue(metaDataStore.getRecordMetaData().isStoreRecordVersions());
    }
    // Enable split long records with default validator. It should fail.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertFalse(metaDataStore.getRecordMetaData().isSplitLongRecords());
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.enableSplitLongRecords());
        assertEquals(e.getMessage(), "new meta-data splits long records");
        context.commit();
        assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
        assertTrue(metaDataStore.getRecordMetaData().isStoreRecordVersions());
    }
    // Enable split long records
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.setEvolutionValidator(MetaDataEvolutionValidator.newBuilder().setAllowUnsplitToSplit(true).build());
        assertFalse(metaDataStore.getRecordMetaData().isSplitLongRecords());
        metaDataStore.enableSplitLongRecords();
        context.commit();
        assertEquals(version + 3, metaDataStore.getRecordMetaData().getVersion());
        assertTrue(metaDataStore.getRecordMetaData().isStoreRecordVersions());
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertTrue(metaDataStore.getRecordMetaData().isStoreRecordVersions());
        assertEquals(version + 3, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with MetaDataException

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

the class FDBMetaDataStoreTest method indexes.

@ParameterizedTest(name = "indexes [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void indexes(final boolean indexCounterBasedSubspaceKey) {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaDataBuilder builder = RecordMetaData.newBuilder();
        if (indexCounterBasedSubspaceKey) {
            builder.enableCounterBasedSubspaceKeys();
        }
        metaDataStore.saveRecordMetaData(builder.setRecords(TestRecords1Proto.getDescriptor()).getRecordMetaData());
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        metaDataStore.addIndex("MySimpleRecord", "testIndex", "rec_no");
        assertNotNull(metaDataStore.getRecordMetaData().getIndex("testIndex"));
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getIndex("testIndex"));
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getIndex("testIndex"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.addIndex("MySimpleRecord", "testIndex", "rec_no"));
        assertEquals("Index testIndex already defined", e.getMessage());
        metaDataStore.dropIndex("testIndex");
        context.commit();
        e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getIndex("testIndex"));
        assertEquals("Index testIndex not defined", e.getMessage());
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getIndex("testIndex"));
        assertEquals("Index testIndex not defined", e.getMessage());
        e = assertThrows(MetaDataException.class, () -> metaDataStore.dropIndex("testIndex"));
        assertEquals("No index named testIndex defined", e.getMessage());
        context.commit();
    }
}
Also used : RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 48 with MetaDataException

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

the class OnlineIndexerBuildSumIndexTest method sumRebuild.

private void sumRebuild(@Nonnull List<TestRecords1Proto.MySimpleRecord> records, @Nullable List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding, int agents, boolean overlap) {
    Index index = new Index("newSumIndex", field("num_value_2").ungrouped(), IndexTypes.SUM);
    IndexAggregateFunction aggregateFunction = new IndexAggregateFunction(FunctionNames.SUM, index.getRootExpression(), index.getName());
    Runnable beforeBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                metaData.getIndex(index.getName());
            } catch (MetaDataException e) {
                assertEquals("Index newSumIndex not defined", e.getMessage());
            }
        }
    };
    Runnable afterBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            metaData.getIndex(index.getName());
            try (FDBRecordContext context = openContext()) {
                FDBRestrictedIndexQueryTest.assertThrowsAggregateFunctionNotSupported(() -> recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), aggregateFunction, TupleRange.ALL, IsolationLevel.SNAPSHOT), "newSumIndex.sum(Field { 'num_value_2' None} group 1)");
            } catch (Exception e) {
                fail();
            }
        }
    };
    List<TestRecords1Proto.MySimpleRecord> updatedRecords;
    if (recordsWhileBuilding == null || recordsWhileBuilding.size() == 0) {
        updatedRecords = records;
    } else {
        updatedRecords = updated(records, recordsWhileBuilding);
    }
    Runnable afterReadable = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                long sum = recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), aggregateFunction, TupleRange.ALL, IsolationLevel.SNAPSHOT).join().getLong(0);
                long expected = updatedRecords.stream().mapToInt(msg -> msg.hasNumValue2() ? msg.getNumValue2() : 0).sum();
                assertEquals(expected, sum);
            }
        }
    };
    singleRebuild(records, recordsWhileBuilding, agents, overlap, false, index, beforeBuild, afterBuild, afterReadable);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) FunctionNames(com.apple.foundationdb.record.FunctionNames) FDBRestrictedIndexQueryTest(com.apple.foundationdb.record.provider.foundationdb.query.FDBRestrictedIndexQueryTest) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) LongStream(java.util.stream.LongStream) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Tags(com.apple.test.Tags) Random(java.util.Random) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Index(com.apple.foundationdb.record.metadata.Index) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) Index(com.apple.foundationdb.record.metadata.Index) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException)

Example 49 with MetaDataException

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

the class RecordMetaDataBuilder method updateUnionFieldsAndRecordTypes.

private void updateUnionFieldsAndRecordTypes(@Nonnull Descriptors.Descriptor union, boolean processExtensionOptions) {
    final Map<String, RecordTypeBuilder> oldRecordTypes = ImmutableMap.copyOf(recordTypes);
    recordTypes.clear();
    unionFields.clear();
    for (Descriptors.FieldDescriptor unionField : union.getFields()) {
        Descriptors.Descriptor newDescriptor = unionField.getMessageType();
        Descriptors.Descriptor oldDescriptor = findOldDescriptor(unionField, union);
        if (unionFields.containsKey(newDescriptor)) {
            if (!recordTypes.containsKey(newDescriptor.getName())) {
                // Union field was seen before but the record type is unknown? This must not happen.
                throw new MetaDataException("Unknown record type for union field " + unionField.getName());
            }
            // For existing record types, the preferred field is the last one, except if there is one whose name matches.
            remapUnionField(newDescriptor, unionField);
        } else if (oldDescriptor == null) {
            // New field and record type.
            RecordTypeBuilder recordType = processRecordType(unionField, processExtensionOptions);
            if (recordType.getSinceVersion() != null && recordType.getSinceVersion() != version) {
                throw new MetaDataException(String.format("Record type version (%d) does not match meta-data version (%d)", recordType.getSinceVersion(), version));
            } else {
                recordType.setSinceVersion(version);
            }
            unionFields.put(newDescriptor, unionField);
        } else {
            updateRecordType(oldRecordTypes, oldDescriptor, newDescriptor);
            unionFields.put(newDescriptor, unionField);
        }
    }
}
Also used : Descriptors(com.google.protobuf.Descriptors) SyntheticRecordTypeBuilder(com.apple.foundationdb.record.metadata.SyntheticRecordTypeBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) JoinedRecordTypeBuilder(com.apple.foundationdb.record.metadata.JoinedRecordTypeBuilder) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException)

Example 50 with MetaDataException

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

the class RecordMetaDataBuilder method addJoinedRecordType.

/**
 * Add a new joined record type.
 * @param name the name of the new record type
 * @return a new uninitialized joined record type
 */
@Nonnull
@API(API.Status.EXPERIMENTAL)
public JoinedRecordTypeBuilder addJoinedRecordType(@Nonnull String name) {
    if (recordTypes.containsKey(name)) {
        throw new MetaDataException("There is already a record type named " + name);
    }
    if (syntheticRecordTypes.containsKey(name)) {
        throw new MetaDataException("There is already a synthetic record type named " + name);
    }
    JoinedRecordTypeBuilder recordType = new JoinedRecordTypeBuilder(name, getNextRecordTypeKey(), this);
    syntheticRecordTypes.put(name, recordType);
    return recordType;
}
Also used : JoinedRecordTypeBuilder(com.apple.foundationdb.record.metadata.JoinedRecordTypeBuilder) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Nonnull(javax.annotation.Nonnull) API(com.apple.foundationdb.annotation.API)

Aggregations

MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)61 Test (org.junit.jupiter.api.Test)33 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)25 Nonnull (javax.annotation.Nonnull)24 Index (com.apple.foundationdb.record.metadata.Index)22 Descriptors (com.google.protobuf.Descriptors)14 Tuple (com.apple.foundationdb.tuple.Tuple)13 List (java.util.List)12 ScanProperties (com.apple.foundationdb.record.ScanProperties)11 TupleRange (com.apple.foundationdb.record.TupleRange)11 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)11 IndexEntry (com.apple.foundationdb.record.IndexEntry)10 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)10 DescriptorProtos (com.google.protobuf.DescriptorProtos)10 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)9 Message (com.google.protobuf.Message)9 Collectors (java.util.stream.Collectors)9 IndexScanType (com.apple.foundationdb.record.IndexScanType)8