com.facebook.presto.jdbc.internal.common.block.RowBlockBuilder Maven / Gradle / Ivy
The newest version!
/*
* 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.common.type.Type;
import com.facebook.presto.jdbc.internal.io.airlift.slice.SliceInput;
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.List;
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.RowBlock.createRowBlockInternal;
import static com.facebook.presto.jdbc.internal.io.airlift.slice.SizeOf.sizeOf;
import static java.lang.Math.max;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class RowBlockBuilder
extends AbstractRowBlock
implements BlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(RowBlockBuilder.class).instanceSize();
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private int positionCount;
private int[] fieldBlockOffsets;
private boolean[] rowIsNull;
private boolean hasNullRow;
private final BlockBuilder[] fieldBlockBuilders;
private boolean currentEntryOpened;
public RowBlockBuilder(List fieldTypes, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this(
blockBuilderStatus,
createFieldBlockBuilders(fieldTypes, blockBuilderStatus, expectedEntries),
new int[expectedEntries + 1],
new boolean[expectedEntries]);
}
private RowBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, BlockBuilder[] fieldBlockBuilders, int[] fieldBlockOffsets, boolean[] rowIsNull)
{
super(fieldBlockBuilders.length);
this.blockBuilderStatus = blockBuilderStatus;
this.positionCount = 0;
this.fieldBlockOffsets = requireNonNull(fieldBlockOffsets, "fieldBlockOffsets is null");
this.rowIsNull = requireNonNull(rowIsNull, "rowIsNull is null");
this.fieldBlockBuilders = requireNonNull(fieldBlockBuilders, "fieldBlockBuilders is null");
}
private static BlockBuilder[] createFieldBlockBuilders(List fieldTypes, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
// Stream API should not be used since constructor can be called in performance sensitive sections
BlockBuilder[] fieldBlockBuilders = new BlockBuilder[fieldTypes.size()];
for (int i = 0; i < fieldTypes.size(); i++) {
fieldBlockBuilders[i] = fieldTypes.get(i).createBlockBuilder(blockBuilderStatus, expectedEntries);
}
return fieldBlockBuilders;
}
@Override
protected Block[] getRawFieldBlocks()
{
return fieldBlockBuilders;
}
@Override
protected int[] getFieldBlockOffsets()
{
return fieldBlockOffsets;
}
@Override
public int getOffsetBase()
{
return 0;
}
@Nullable
@Override
protected boolean[] getRowIsNull()
{
return hasNullRow ? rowIsNull : null;
}
@Override
public boolean mayHaveNull()
{
return hasNullRow;
}
@Override
public boolean isNull(int position)
{
checkReadablePosition(position);
return hasNullRow && rowIsNull[position];
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
long sizeInBytes = (Integer.BYTES + Byte.BYTES) * (long) positionCount;
for (int i = 0; i < numFields; i++) {
sizeInBytes += fieldBlockBuilders[i].getSizeInBytes();
}
return sizeInBytes;
}
@Override
public long getRetainedSizeInBytes()
{
long size = INSTANCE_SIZE + sizeOf(fieldBlockOffsets) + sizeOf(rowIsNull);
for (int i = 0; i < numFields; i++) {
size += fieldBlockBuilders[i].getRetainedSizeInBytes();
}
if (blockBuilderStatus != null) {
size += BlockBuilderStatus.INSTANCE_SIZE;
}
return size;
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy