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.io.airlift.slice.DynamicSliceOutput;
import com.facebook.presto.jdbc.internal.io.airlift.slice.Slice;
import com.facebook.presto.jdbc.internal.io.airlift.slice.SliceInput;
import com.facebook.presto.jdbc.internal.io.airlift.slice.SliceOutput;
import com.facebook.presto.jdbc.internal.io.airlift.slice.Slices;
import com.facebook.presto.jdbc.internal.io.airlift.slice.UnsafeSlice;
import com.facebook.presto.jdbc.internal.jol.info.ClassLayout;
import com.facebook.presto.jdbc.internal.javax.annotation.Nullable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.MAX_ARRAY_SIZE;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.calculateBlockResetSize;
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.checkValidPositions;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.checkValidRegion;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.compactArray;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.compactOffsets;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.compactSlice;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.internalPositionInRange;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.SIZE_OF_BYTE;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.SIZE_OF_INT;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.SIZE_OF_LONG;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.SIZE_OF_SHORT;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.sizeOf;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.String.format;
public class VariableWidthBlockBuilder
extends AbstractVariableWidthBlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(VariableWidthBlockBuilder.class).instanceSize();
private BlockBuilderStatus blockBuilderStatus;
private boolean initialized;
private int initialEntryCount;
private int initialSliceOutputSize;
private SliceOutput sliceOutput = new DynamicSliceOutput(0);
private boolean hasNullValue;
// it is assumed that the offsets array is one position longer than the valueIsNull array
private boolean[] valueIsNull = new boolean[0];
private int[] offsets = new int[1];
private int positions;
private int currentEntrySize;
private long arraysRetainedSizeInBytes;
public VariableWidthBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytes)
{
this.blockBuilderStatus = blockBuilderStatus;
initialEntryCount = expectedEntries;
initialSliceOutputSize = min(expectedBytes, MAX_ARRAY_SIZE);
updateArraysDataSize();
}
@Override
public int getPositionOffset(int position)
{
checkValidPosition(position, positions);
return getOffset(position);
}
@Override
public int getSliceLength(int position)
{
checkValidPosition(position, positions);
return getOffset((position + 1)) - getOffset(position);
}
@Override
public Slice getRawSlice(int position)
{
return sliceOutput.getUnderlyingSlice();
}
@Override
public int getPositionCount()
{
return positions;
}
@Override
public long getSizeInBytes()
{
long arraysSizeInBytes = (Integer.BYTES + Byte.BYTES) * (long) positions;
return sliceOutput.size() + arraysSizeInBytes;
}
@Override
public OptionalInt fixedSizeInBytesPerPosition()
{
return OptionalInt.empty(); // size is variable based on the per element length
}
@Override
public long getRegionSizeInBytes(int positionOffset, int length)
{
int positionCount = getPositionCount();
checkValidRegion(positionCount, positionOffset, length);
long arraysSizeInBytes = (Integer.BYTES + Byte.BYTES) * (long) length;
return getOffset(positionOffset + length) - getOffset(positionOffset) + arraysSizeInBytes;
}
@Override
public long getPositionsSizeInBytes(boolean[] positions, int usedPositionCount)
{
int positionCount = getPositionCount();
checkValidPositions(positions, positionCount);
if (usedPositionCount == 0) {
return 0;
}
if (usedPositionCount == positionCount) {
return getSizeInBytes();
}
int sizeInBytes = 0;
for (int i = 0; i < positions.length; ++i) {
if (positions[i]) {
sizeInBytes += offsets[i + 1] - offsets[i];
}
}
return sizeInBytes + ((Integer.BYTES + Byte.BYTES) * (long) usedPositionCount);
}
@Override
public long getRetainedSizeInBytes()
{
long size = INSTANCE_SIZE + sliceOutput.getRetainedSize() + arraysRetainedSizeInBytes;
if (blockBuilderStatus != null) {
size += BlockBuilderStatus.INSTANCE_SIZE;
}
return size;
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer