Search in sources :

Example 56 with MetaDataException

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

the class MetaDataProtoEditor method addDefaultUnionIfMissing.

/**
 * Add a default union to the given records descriptor if missing.
 *
 * <p>
 * This method is a no-op if the union is present. Otherwise, the method will add a union to the records
 * descriptor. The union descriptor will be filled in with all of the record types defined in the file except
 * {@code NESTED} record types.
 * </p>
 *
 * @param fileDescriptor the records descriptor of the record meta-data
 * @return the resulting records descriptor
 */
@Nonnull
public static Descriptors.FileDescriptor addDefaultUnionIfMissing(@Nonnull Descriptors.FileDescriptor fileDescriptor) {
    if (MetaDataProtoEditor.hasUnion(fileDescriptor)) {
        return fileDescriptor;
    }
    DescriptorProtos.FileDescriptorProto fileDescriptorProto = fileDescriptor.toProto();
    DescriptorProtos.FileDescriptorProto.Builder fileBuilder = fileDescriptorProto.toBuilder();
    fileBuilder.addMessageType(createDefaultUnion(fileBuilder));
    try {
        return Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), fileDescriptor.getDependencies().toArray(new Descriptors.FileDescriptor[0]));
    } catch (Descriptors.DescriptorValidationException e) {
        throw new MetaDataException("Failed to add a default union", e);
    }
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) Descriptors(com.google.protobuf.Descriptors) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Nonnull(javax.annotation.Nonnull)

Example 57 with MetaDataException

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

the class MetaDataProtoEditor method addDefaultUnionIfMissing.

/**
 * Creates a default union descriptor for the given file descriptor if missing.
 *
 * <p>
 * If the given file descriptor is missing a union message, this method will add one before updating the meta-data.
 * The generated union descriptor is constructed by adding any non-{@code NESTED} types in the file descriptor to the
 * union descriptor from the currently stored meta-data. A new field is not added if a field of the given type already
 * exists, and the order of any existing fields is preserved. Note that types are identified by name, so renaming
 * top-level message types may result in validation errors when trying to update the record descriptor.
 * </p>
 *
 * @param fileDescriptor the file descriptor to create a union for
 * @param baseUnionDescriptor the base union descriptor
 * @return the builder for the union
 */
@Nonnull
public static Descriptors.FileDescriptor addDefaultUnionIfMissing(@Nonnull Descriptors.FileDescriptor fileDescriptor, @Nonnull Descriptors.Descriptor baseUnionDescriptor) {
    if (MetaDataProtoEditor.hasUnion(fileDescriptor)) {
        return fileDescriptor;
    }
    DescriptorProtos.FileDescriptorProto fileDescriptorProto = fileDescriptor.toProto();
    DescriptorProtos.FileDescriptorProto.Builder fileBuilder = fileDescriptorProto.toBuilder();
    DescriptorProtos.DescriptorProto.Builder unionDescriptorBuilder = createSyntheticUnion(fileDescriptor, baseUnionDescriptor);
    for (DescriptorProtos.DescriptorProto.Builder messageType : fileBuilder.getMessageTypeBuilderList()) {
        RecordMetaDataOptionsProto.RecordTypeOptions.Usage messageTypeUsage = getMessageTypeUsage(messageType);
        if (messageTypeUsage != RecordMetaDataOptionsProto.RecordTypeOptions.Usage.NESTED && !hasField(fileBuilder, unionDescriptorBuilder, messageType)) {
            addFieldToUnion(unionDescriptorBuilder, fileBuilder, messageType);
        }
    }
    fileBuilder.addMessageType(unionDescriptorBuilder);
    try {
        return Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), fileDescriptor.getDependencies().toArray(new Descriptors.FileDescriptor[0]));
    } catch (Descriptors.DescriptorValidationException e) {
        throw new MetaDataException("Failed to add a default union", e);
    }
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) Descriptors(com.google.protobuf.Descriptors) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Nonnull(javax.annotation.Nonnull)

Example 58 with MetaDataException

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

the class FDBRecordStore method addIndexStateReadConflict.

/**
 * Add a read conflict key so that the transaction will fail if the index state has changed.
 * @param indexName the index to conflict on, if it's state changes
 */
private void addIndexStateReadConflict(@Nonnull String indexName) {
    if (!getRecordMetaData().hasIndex(indexName)) {
        throw new MetaDataException("Index " + indexName + " does not exist in meta-data.");
    }
    if (indexStateReadConflicts.contains(indexName)) {
        return;
    } else {
        indexStateReadConflicts.add(indexName);
    }
    Transaction tr = ensureContextActive();
    byte[] indexStateKey = getSubspace().pack(Tuple.from(INDEX_STATE_SPACE_KEY, indexName));
    tr.addReadConflictKey(indexStateKey);
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException)

Example 59 with MetaDataException

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

the class PermutedMinMaxIndexMaintainerFactory method getIndexValidator.

@Override
@Nonnull
public IndexValidator getIndexValidator(Index index) {
    return new IndexValidator(index) {

        @Override
        public void validate(@Nonnull MetaDataValidator metaDataValidator) {
            super.validate(metaDataValidator);
            validateGrouping(1);
            int groupingCount = ((GroupingKeyExpression) index.getRootExpression()).getGroupingCount();
            validateNotVersion();
            int permutedSize = PermutedMinMaxIndexMaintainer.getPermutedSize(index);
            if (permutedSize < 0) {
                throw new MetaDataException("permuted size cannot be negative", LogMessageKeys.INDEX_NAME, index.getName());
            } else if (permutedSize == 0) {
                throw new MetaDataException("no permutation does not need a special index type for MIN and MAX", LogMessageKeys.INDEX_NAME, index.getName());
            } else if (permutedSize > groupingCount) {
                throw new MetaDataException("permuted size cannot be larger than grouping size", LogMessageKeys.INDEX_NAME, index.getName());
            }
        }

        @Override
        public void validateChangedOptions(@Nonnull Index oldIndex, @Nonnull Set<String> changedOptions) {
            if (changedOptions.contains(IndexOptions.PERMUTED_SIZE_OPTION)) {
                throw new MetaDataException("permuted size changed", LogMessageKeys.INDEX_NAME, index.getName());
            }
            super.validateChangedOptions(oldIndex, changedOptions);
        }
    };
}
Also used : IndexValidator(com.apple.foundationdb.record.metadata.IndexValidator) Set(java.util.Set) Nonnull(javax.annotation.Nonnull) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Index(com.apple.foundationdb.record.metadata.Index) MetaDataValidator(com.apple.foundationdb.record.metadata.MetaDataValidator) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Nonnull(javax.annotation.Nonnull)

Example 60 with MetaDataException

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

the class AtomicMutationIndexMaintainer method evaluateAggregateFunction.

@Override
@Nonnull
public CompletableFuture<Tuple> evaluateAggregateFunction(@Nonnull IndexAggregateFunction function, @Nonnull TupleRange range, @Nonnull IsolationLevel isolationveLevel) {
    if (!matchesAggregateFunction(function)) {
        throw new MetaDataException("this index does not support aggregate function: " + function);
    }
    final RecordCursor<IndexEntry> cursor = scan(IndexScanType.BY_GROUP, range, null, new ScanProperties(ExecuteProperties.newBuilder().setIsolationLevel(isolationveLevel).build()));
    final BiFunction<Tuple, Tuple, Tuple> aggregator = mutation.getAggregator();
    return cursor.reduce(mutation.getIdentity(), (accum, kv) -> aggregator.apply(accum, kv.getValue()));
}
Also used : ScanProperties(com.apple.foundationdb.record.ScanProperties) IndexEntry(com.apple.foundationdb.record.IndexEntry) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Tuple(com.apple.foundationdb.tuple.Tuple) Nonnull(javax.annotation.Nonnull)

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