io.prestosql.spi.block.ArrayBlockBuilder 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 io.prestosql.spi.block;
import io.prestosql.spi.type.Type;
import org.openjdk.jol.info.ClassLayout;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.function.BiConsumer;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.prestosql.spi.block.ArrayBlock.createArrayBlockInternal;
import static io.prestosql.spi.block.BlockUtil.calculateBlockResetSize;
import static java.lang.Math.max;
import static java.util.Objects.requireNonNull;
public class ArrayBlockBuilder
extends AbstractArrayBlock
implements BlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(ArrayBlockBuilder.class).instanceSize();
private int positionCount;
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private boolean initialized;
private final int initialEntryCount;
private int[] offsets = new int[1];
private boolean[] valueIsNull = new boolean[0];
private boolean hasNullValue;
private final BlockBuilder values;
private boolean currentEntryOpened;
private long retainedSizeInBytes;
/**
* Caller of this constructor is responsible for making sure `valuesBlock` is constructed with the same `blockBuilderStatus` as the one in the argument
*/
public ArrayBlockBuilder(BlockBuilder valuesBlock, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this(
blockBuilderStatus,
valuesBlock,
expectedEntries);
}
public ArrayBlockBuilder(Type elementType, BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
this(
blockBuilderStatus,
elementType.createBlockBuilder(blockBuilderStatus, expectedEntries, expectedBytesPerEntry),
expectedEntries);
}
public ArrayBlockBuilder(Type elementType, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this(
blockBuilderStatus,
elementType.createBlockBuilder(blockBuilderStatus, expectedEntries),
expectedEntries);
}
/**
* Caller of this private constructor is responsible for making sure `values` is constructed with the same `blockBuilderStatus` as the one in the argument
*/
private ArrayBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, BlockBuilder values, int expectedEntries)
{
this.blockBuilderStatus = blockBuilderStatus;
this.values = requireNonNull(values, "values is null");
this.initialEntryCount = max(expectedEntries, 1);
updateDataSize();
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
return values.getSizeInBytes() + ((Integer.BYTES + Byte.BYTES) * (long) positionCount);
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes + values.getRetainedSizeInBytes();
}
@Override
public void retainedBytesForEachPart(BiConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy