com.facebook.presto.jdbc.internal.spi.block.ArrayBlock 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.jol.info.ClassLayout;
import java.util.function.BiConsumer;
import static com.facebook.presto.jdbc.internal.airlift.slice.SizeOf.sizeOf;
import static java.util.Objects.requireNonNull;
public class ArrayBlock
extends AbstractArrayBlock
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(ArrayBlock.class).instanceSize();
private final int arrayOffset;
private final int positionCount;
private final boolean[] valueIsNull;
private final Block values;
private final int[] offsets;
private long sizeInBytes;
private final long retainedSizeInBytes;
public ArrayBlock(int positionCount, boolean[] valueIsNull, int[] offsets, Block values)
{
this(0, positionCount, valueIsNull, offsets, values);
}
ArrayBlock(int arrayOffset, int positionCount, boolean[] valueIsNull, int[] offsets, Block values)
{
if (arrayOffset < 0) {
throw new IllegalArgumentException("arrayOffset is negative");
}
this.arrayOffset = arrayOffset;
if (positionCount < 0) {
throw new IllegalArgumentException("positionCount is negative");
}
this.positionCount = positionCount;
requireNonNull(valueIsNull, "valueIsNull is null");
if (valueIsNull.length - arrayOffset < positionCount) {
throw new IllegalArgumentException("isNull length is less than positionCount");
}
this.valueIsNull = valueIsNull;
requireNonNull(offsets, "offsets is null");
if (offsets.length - arrayOffset < positionCount + 1) {
throw new IllegalArgumentException("offsets length is less than positionCount");
}
this.offsets = offsets;
this.values = requireNonNull(values);
sizeInBytes = -1;
retainedSizeInBytes = INSTANCE_SIZE + values.getRetainedSizeInBytes() + sizeOf(offsets) + sizeOf(valueIsNull);
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
// this is racy but is safe because sizeInBytes is an int and the calculation is stable
if (sizeInBytes < 0) {
calculateSize();
}
return sizeInBytes;
}
private void calculateSize()
{
int valueStart = offsets[arrayOffset];
int valueEnd = offsets[arrayOffset + positionCount];
sizeInBytes = values.getRegionSizeInBytes(valueStart, valueEnd - valueStart) + ((Integer.BYTES + Byte.BYTES) * (long) this.positionCount);
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public void retainedBytesForEachPart(BiConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy