com.facebook.presto.jdbc.internal.spi.block.MapBlockBuilder 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.jol.info.ClassLayout;
import javax.annotation.Nullable;
import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.function.BiConsumer;
import static com.facebook.presto.jdbc.internal.spi.block.BlockUtil.calculateBlockResetSize;
import static com.facebook.presto.jdbc.internal.airlift.slice.SizeOf.sizeOf;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class MapBlockBuilder
extends AbstractMapBlock
implements BlockBuilder
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(MapBlockBuilder.class).instanceSize();
private final MethodHandle keyBlockHashCode;
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private int positionCount;
private int[] offsets;
private boolean[] mapIsNull;
private final BlockBuilder keyBlockBuilder;
private final BlockBuilder valueBlockBuilder;
private int[] hashTables;
private boolean currentEntryOpened;
public MapBlockBuilder(
Type keyType,
Type valueType,
MethodHandle keyBlockNativeEquals,
MethodHandle keyNativeHashCode,
MethodHandle keyBlockHashCode,
BlockBuilderStatus blockBuilderStatus,
int expectedEntries)
{
this(
keyType,
keyBlockNativeEquals,
keyNativeHashCode,
keyBlockHashCode,
blockBuilderStatus,
keyType.createBlockBuilder(blockBuilderStatus, expectedEntries),
valueType.createBlockBuilder(blockBuilderStatus, expectedEntries),
new int[expectedEntries + 1],
new boolean[expectedEntries],
newNegativeOneFilledArray(expectedEntries * HASH_MULTIPLIER));
}
private MapBlockBuilder(
Type keyType,
MethodHandle keyBlockNativeEquals,
MethodHandle keyNativeHashCode,
MethodHandle keyBlockHashCode,
@Nullable BlockBuilderStatus blockBuilderStatus,
BlockBuilder keyBlockBuilder,
BlockBuilder valueBlockBuilder,
int[] offsets,
boolean[] mapIsNull,
int[] hashTables)
{
super(keyType, keyNativeHashCode, keyBlockNativeEquals);
requireNonNull(keyBlockHashCode, "keyBlockHashCode is null");
this.keyBlockHashCode = keyBlockHashCode;
this.blockBuilderStatus = blockBuilderStatus;
this.positionCount = 0;
this.offsets = requireNonNull(offsets, "offsets is null");
this.mapIsNull = requireNonNull(mapIsNull, "mapIsNull is null");
this.keyBlockBuilder = requireNonNull(keyBlockBuilder, "keyBlockBuilder is null");
this.valueBlockBuilder = requireNonNull(valueBlockBuilder, "valueBlockBuilder is null");
this.hashTables = requireNonNull(hashTables, "hashTables is null");
}
@Override
protected Block getKeys()
{
return keyBlockBuilder;
}
@Override
protected Block getValues()
{
return valueBlockBuilder;
}
@Override
protected int[] getHashTables()
{
return hashTables;
}
@Override
protected int[] getOffsets()
{
return offsets;
}
@Override
protected int getOffsetBase()
{
return 0;
}
@Override
protected boolean[] getMapIsNull()
{
return mapIsNull;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
return keyBlockBuilder.getSizeInBytes() + valueBlockBuilder.getSizeInBytes() +
(Integer.BYTES + Byte.BYTES) * (long) positionCount +
Integer.BYTES * HASH_MULTIPLIER * (long) keyBlockBuilder.getPositionCount();
}
@Override
public long getRetainedSizeInBytes()
{
long size = INSTANCE_SIZE + keyBlockBuilder.getRetainedSizeInBytes() + valueBlockBuilder.getRetainedSizeInBytes() + sizeOf(offsets) + sizeOf(mapIsNull) + sizeOf(hashTables);
if (blockBuilderStatus != null) {
size += BlockBuilderStatus.INSTANCE_SIZE;
}
return size;
}
@Override
public void retainedBytesForEachPart(BiConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy