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);
}
}
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);
}
}
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);
}
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);
}
};
}
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()));
}
Aggregations