com.facebook.presto.jdbc.internal.spi.block.ShortArrayBlockBuilder 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.spi.block;
import com.facebook.presto.jdbc.internal.jol.info.ClassLayout;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import static com.facebook.presto.jdbc.internal.spi.block.BlockUtil.calculateBlockResetSize;
import static com.facebook.presto.jdbc.internal.spi.block.BlockUtil.checkValidRegion;
import static com.facebook.presto.jdbc.internal.airlift.slice.SizeOf.sizeOf;
import static java.lang.Math.max;
public class ShortArrayBlockBuilder
implements BlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(ShortArrayBlockBuilder.class).instanceSize();
@Nullable
private BlockBuilderStatus blockBuilderStatus;
private boolean initialized;
private int initialEntryCount;
private int positionCount;
// it is assumed that these arrays are the same length
private boolean[] valueIsNull = new boolean[0];
private short[] values = new short[0];
private long retainedSizeInBytes;
public ShortArrayBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this.blockBuilderStatus = blockBuilderStatus;
this.initialEntryCount = max(expectedEntries, 1);
updateDataSize();
}
@Override
public BlockBuilder writeShort(int value)
{
if (values.length <= positionCount) {
growCapacity();
}
values[positionCount] = (short) value;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + Short.BYTES);
}
return this;
}
@Override
public BlockBuilder closeEntry()
{
return this;
}
@Override
public BlockBuilder appendNull()
{
if (values.length <= positionCount) {
growCapacity();
}
valueIsNull[positionCount] = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + Short.BYTES);
}
return this;
}
@Override
public Block build()
{
return new ShortArrayBlock(positionCount, valueIsNull, values);
}
@Override
public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus)
{
return new ShortArrayBlockBuilder(blockBuilderStatus, calculateBlockResetSize(positionCount));
}
private void growCapacity()
{
int newSize;
if (initialized) {
newSize = BlockUtil.calculateNewArraySize(values.length);
}
else {
newSize = initialEntryCount;
initialized = true;
}
valueIsNull = Arrays.copyOf(valueIsNull, newSize);
values = Arrays.copyOf(values, newSize);
updateDataSize();
}
private void updateDataSize()
{
retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);
if (blockBuilderStatus != null) {
retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE;
}
}
@Override
public long getSizeInBytes()
{
return (Short.BYTES + Byte.BYTES) * (long) positionCount;
}
@Override
public long getRegionSizeInBytes(int position, int length)
{
return (Short.BYTES + Byte.BYTES) * (long) length;
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public void retainedBytesForEachPart(BiConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy