com.facebook.presto.jdbc.internal.common.block.Int128ArrayBlockBuilder Maven / Gradle / Ivy
/*
* 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.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.jol.info.ClassLayout;
import com.facebook.presto.jdbc.internal.javax.annotation.Nullable;
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;
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.checkValidRegion;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.compactArray;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.getNum128Integers;
import static com.facebook.presto.jdbc.internal.common.block.BlockUtil.internalPositionInRange;
import static com.facebook.presto.jdbc.internal.common.block.Int128ArrayBlock.INT128_BYTES;
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.sizeOf;
import static java.lang.Integer.bitCount;
import static java.lang.Math.max;
import static java.lang.String.format;
public class Int128ArrayBlockBuilder
implements BlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(Int128ArrayBlockBuilder.class).instanceSize();
private static final Block NULL_VALUE_BLOCK = new Int128ArrayBlock(0, 1, new boolean[] {true}, new long[2]);
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private boolean initialized;
private final int initialEntryCount;
private int positionCount;
private boolean hasNullValue;
private boolean hasNonNullValue;
// it is assumed that these arrays are the same length
private boolean[] valueIsNull = new boolean[0];
private long[] values = new long[0];
private long retainedSizeInBytes;
private int entryPositionCount;
public Int128ArrayBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this.blockBuilderStatus = blockBuilderStatus;
this.initialEntryCount = max(expectedEntries, 1);
updateDataSize();
}
@Override
public BlockBuilder writeLong(long value)
{
if (valueIsNull.length <= positionCount) {
growCapacity();
}
values[(positionCount * 2) + entryPositionCount] = value;
entryPositionCount++;
hasNonNullValue = true;
return this;
}
@Override
public BlockBuilder closeEntry()
{
if (entryPositionCount != 2) {
throw new IllegalStateException("Expected entry size to be exactly " + INT128_BYTES + " bytes but was " + (entryPositionCount * SIZE_OF_LONG));
}
positionCount++;
entryPositionCount = 0;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + INT128_BYTES);
}
return this;
}
@Override
public BlockBuilder appendNull()
{
if (valueIsNull.length <= positionCount) {
growCapacity();
}
if (entryPositionCount != 0) {
throw new IllegalStateException("Current entry must be closed before a null can be written");
}
valueIsNull[positionCount] = true;
hasNullValue = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + INT128_BYTES);
}
return this;
}
@Override
public Block build()
{
if (!hasNonNullValue) {
return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, positionCount);
}
return new Int128ArrayBlock(0, positionCount, hasNullValue ? valueIsNull : null, values);
}
@Override
public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus)
{
return new Int128ArrayBlockBuilder(blockBuilderStatus, calculateBlockResetSize(positionCount));
}
@Override
public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
return new Int128ArrayBlockBuilder(blockBuilderStatus, max(calculateBlockResetSize(positionCount), expectedEntries));
}
private void growCapacity()
{
int newSize;
if (initialized) {
newSize = BlockUtil.calculateNewArraySize(valueIsNull.length);
}
else {
newSize = initialEntryCount;
initialized = true;
}
valueIsNull = Arrays.copyOf(valueIsNull, newSize);
values = Arrays.copyOf(values, newSize * 2);
updateDataSize();
}
private void updateDataSize()
{
retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);
if (blockBuilderStatus != null) {
retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE;
}
}
@Override
public long getSizeInBytes()
{
return Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION * (long) positionCount;
}
@Override
public OptionalInt fixedSizeInBytesPerPosition()
{
return OptionalInt.of(Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
@Override
public long getRegionSizeInBytes(int position, int length)
{
return Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION * (long) length;
}
@Override
public long getPositionsSizeInBytes(boolean[] usedPositions, int usedPositionCount)
{
return Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION * (long) usedPositionCount;
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public long getEstimatedDataSizeForStats(int position)
{
return isNull(position) ? 0 : INT128_BYTES;
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy