Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.jdbc.internal.common.block;
import com.facebook.presto.jdbc.internal.common.Utils;
import com.facebook.presto.jdbc.internal.common.type.Type;
import com.facebook.presto.jdbc.internal.io.airlift.slice.Slice;
import com.facebook.presto.jdbc.internal.io.airlift.slice.SliceOutput;
import com.facebook.presto.jdbc.internal.jol.info.ClassLayout;
import com.facebook.presto.jdbc.internal.javax.annotation.Nullable;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.checkArrayRange;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.checkValidPosition;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.checkValidRegion;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.internalPositionInRange;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class RunLengthEncodedBlock
implements Block
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(RunLengthEncodedBlock.class).instanceSize();
public static Block create(Type type, Object value, int positionCount)
{
Block block = Utils.nativeValueToBlock(type, value);
if (block instanceof RunLengthEncodedBlock) {
block = ((RunLengthEncodedBlock) block).getValue();
}
return new RunLengthEncodedBlock(block, positionCount);
}
private final Block value;
private final int positionCount;
public RunLengthEncodedBlock(Block value, int positionCount)
{
requireNonNull(value, "value is null");
if (value.getPositionCount() != 1) {
throw new IllegalArgumentException(format("Expected value to contain a single position but has %s positions", value.getPositionCount()));
}
if (value instanceof RunLengthEncodedBlock) {
this.value = ((RunLengthEncodedBlock) value).getValue();
}
else {
this.value = value;
}
if (positionCount < 0) {
throw new IllegalArgumentException("positionCount is negative");
}
this.positionCount = positionCount;
}
public Block getValue()
{
return value;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
return value.getSizeInBytes();
}
@Override
public OptionalInt fixedSizeInBytesPerPosition()
{
return OptionalInt.empty(); // size does not increase with each row
}
@Override
public long getLogicalSizeInBytes()
{
return positionCount * value.getLogicalSizeInBytes();
}
@Override
public long getRetainedSizeInBytes()
{
return INSTANCE_SIZE + value.getRetainedSizeInBytes();
}
@Override
public long getEstimatedDataSizeForStats(int position)
{
return value.getEstimatedDataSizeForStats(0);
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer