use of com.apple.foundationdb.record.KeyRange in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectory method getValueRange.
@Nonnull
private CompletableFuture<KeyRange> getValueRange(@Nonnull FDBRecordContext context, @Nullable ValueRange<?> valueRange, @Nonnull Subspace subspace) {
final byte[] startKey;
final byte[] stopKey;
final EndpointType startType;
final EndpointType stopType;
if (getValue() == KeySpaceDirectory.ANY_VALUE) {
if (valueRange != null && valueRange.getLowEndpoint() != EndpointType.TREE_START) {
if (KeyType.typeOf(valueRange.getLow()) != getKeyType()) {
throw invalidValueTypeException(KeyType.typeOf(valueRange.getLow()), getKeyType(), getName(), valueRange);
}
startKey = subspace.pack(valueRange.getLow());
startType = valueRange.getLowEndpoint();
if (startType != EndpointType.RANGE_INCLUSIVE && startType != EndpointType.RANGE_EXCLUSIVE) {
throw new RecordCoreArgumentException("Endpoint type not supported for directory list", LogMessageKeys.ENDPOINT_TYPE, startType);
}
} else {
final byte[] subspaceBytes = subspace.pack();
startKey = new byte[subspaceBytes.length + 1];
System.arraycopy(subspaceBytes, 0, startKey, 0, subspaceBytes.length);
startKey[subspaceBytes.length] = getKeyType().getTypeLowBounds();
startType = EndpointType.RANGE_INCLUSIVE;
}
if (valueRange != null && valueRange.getHighEndpoint() != EndpointType.TREE_END) {
if (KeyType.typeOf(valueRange.getHigh()) != getKeyType()) {
throw invalidValueTypeException(KeyType.typeOf(valueRange.getHigh()), getKeyType(), getName(), valueRange);
}
stopKey = subspace.pack(valueRange.getHigh());
stopType = valueRange.getHighEndpoint();
if (stopType != EndpointType.RANGE_INCLUSIVE && stopType != EndpointType.RANGE_EXCLUSIVE) {
throw new RecordCoreArgumentException("Endpoint type not supported for directory list", LogMessageKeys.ENDPOINT_TYPE, stopType);
}
} else {
final byte[] subspaceBytes = subspace.pack();
stopKey = new byte[subspaceBytes.length + 1];
System.arraycopy(subspaceBytes, 0, stopKey, 0, subspaceBytes.length);
stopKey[subspaceBytes.length] = getKeyType().getTypeHighBounds();
stopType = EndpointType.RANGE_EXCLUSIVE;
}
return CompletableFuture.completedFuture(new KeyRange(startKey, startType, stopKey, stopType));
} else {
if (valueRange != null) {
throw new RecordCoreArgumentException("range is not applicable when the subdirectory has a value.", LogMessageKeys.DIR_NAME, getName(), LogMessageKeys.RANGE, valueRange);
}
return toTupleValueAsync(context, this.value).thenApply(resolvedValue -> {
final byte[] key = subspace.pack(Tuple.from(resolvedValue.getResolvedValue()));
return new KeyRange(key, EndpointType.RANGE_INCLUSIVE, ByteArrayUtil.strinc(key), EndpointType.RANGE_EXCLUSIVE);
});
}
}
Aggregations