
io.trino.spi.block.Fixed12BlockBuilder 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.trino.spi.block;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import jakarta.annotation.Nullable;
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.trino.spi.block.BlockUtil.checkArrayRange;
import static io.trino.spi.block.BlockUtil.checkReadablePosition;
import static io.trino.spi.block.BlockUtil.checkValidRegion;
import static io.trino.spi.block.BlockUtil.compactArray;
import static io.trino.spi.block.Fixed12Block.FIXED12_BYTES;
import static io.trino.spi.block.Fixed12Block.decodeFixed12First;
import static io.trino.spi.block.Fixed12Block.encodeFixed12;
import static java.lang.Math.max;
public class Fixed12BlockBuilder
implements BlockBuilder
{
private static final int INSTANCE_SIZE = instanceSize(Fixed12BlockBuilder.class);
private static final Block NULL_VALUE_BLOCK = new Fixed12Block(0, 1, new boolean[] {true}, new int[3]);
@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 int[] values = new int[0];
private long retainedSizeInBytes;
public Fixed12BlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this.blockBuilderStatus = blockBuilderStatus;
this.initialEntryCount = max(expectedEntries, 1);
updateDataSize();
}
public void writeFixed12(long first, int second)
{
if (valueIsNull.length <= positionCount) {
growCapacity();
}
encodeFixed12(first, second, values, positionCount);
hasNonNullValue = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + FIXED12_BYTES);
}
}
@Override
public BlockBuilder appendNull()
{
if (valueIsNull.length <= positionCount) {
growCapacity();
}
valueIsNull[positionCount] = true;
hasNullValue = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Byte.BYTES + FIXED12_BYTES);
}
return this;
}
@Override
public Block build()
{
if (!hasNonNullValue) {
return RunLengthEncodedBlock.create(NULL_VALUE_BLOCK, positionCount);
}
return new Fixed12Block(0, positionCount, hasNullValue ? valueIsNull : null, values);
}
@Override
public BlockBuilder newBlockBuilderLike(int expectedEntries, BlockBuilderStatus blockBuilderStatus)
{
return new Fixed12BlockBuilder(blockBuilderStatus, 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 * 3);
updateDataSize();
}
private void updateDataSize()
{
retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);
if (blockBuilderStatus != null) {
retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE;
}
}
@Override
public OptionalInt fixedSizeInBytesPerPosition()
{
return OptionalInt.of(Fixed12Block.SIZE_IN_BYTES_PER_POSITION);
}
@Override
public long getSizeInBytes()
{
return Fixed12Block.SIZE_IN_BYTES_PER_POSITION * (long) positionCount;
}
@Override
public long getRegionSizeInBytes(int position, int length)
{
return Fixed12Block.SIZE_IN_BYTES_PER_POSITION * (long) length;
}
@Override
public long getPositionsSizeInBytes(boolean[] positions, int selectedPositionsCount)
{
return Fixed12Block.SIZE_IN_BYTES_PER_POSITION * (long) selectedPositionsCount;
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public long getEstimatedDataSizeForStats(int position)
{
return isNull(position) ? 0 : FIXED12_BYTES;
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy