
com.facebook.presto.jdbc.internal.spi.block.InterleavedBlockBuilder 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.spi.type.Type;
import com.facebook.presto.jdbc.internal.airlift.slice.Slice;
import com.facebook.presto.jdbc.internal.jol.info.ClassLayout;
import java.util.List;
import java.util.function.BiConsumer;
import static java.util.Objects.requireNonNull;
public class InterleavedBlockBuilder
extends AbstractInterleavedBlock
implements BlockBuilder
{
// TODO: This does not account for the size of the blockEncoding field
private static final int INSTANCE_SIZE = ClassLayout.parseClass(InterleavedBlockBuilder.class).instanceSize();
private final BlockBuilder[] blockBuilders;
private final InterleavedBlockEncoding blockEncoding;
private int positionCount;
private int currentBlockIndex;
private long sizeInBytes;
private long startSize;
private long retainedSizeInBytes;
private long startRetainedSize;
public InterleavedBlockBuilder(List types, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this(
types.stream()
.map(t -> t.createBlockBuilder(blockBuilderStatus, roundUpDivide(expectedEntries, types.size())))
.toArray(BlockBuilder[]::new)
);
}
public InterleavedBlockBuilder(List types, BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
this(
types.stream()
.map(t -> t.createBlockBuilder(blockBuilderStatus, roundUpDivide(expectedEntries, types.size()), expectedBytesPerEntry))
.toArray(BlockBuilder[]::new)
);
}
private static int roundUpDivide(int dividend, int divisor)
{
return (dividend + divisor - 1) / divisor;
}
/**
* Caller of this private constructor is responsible for making sure every element in `blockBuilders` is constructed with the same `blockBuilderStatus`
*/
private InterleavedBlockBuilder(BlockBuilder[] blockBuilders)
{
super(blockBuilders.length);
this.blockBuilders = requireNonNull(blockBuilders, "blockBuilders is null");
this.blockEncoding = computeBlockEncoding();
this.positionCount = 0;
this.sizeInBytes = 0;
this.retainedSizeInBytes = INSTANCE_SIZE;
for (BlockBuilder blockBuilder : blockBuilders) {
this.sizeInBytes += blockBuilder.getSizeInBytes();
this.retainedSizeInBytes += blockBuilder.getRetainedSizeInBytes();
}
this.startSize = -1;
this.startRetainedSize = -1;
}
@Override
protected Block getBlock(int blockIndex)
{
if (blockIndex < 0) {
throw new IllegalArgumentException("position is not valid");
}
return blockBuilders[blockIndex];
}
@Override
protected int toAbsolutePosition(int position)
{
return position;
}
@Override
public InterleavedBlockEncoding getEncoding()
{
return blockEncoding;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
return sizeInBytes;
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public void retainedBytesForEachPart(BiConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy