/* * 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.spi.block; import org.openjdk.jol.info.ClassLayout; import java.util.Arrays; import java.util.List; import static com.facebook.presto.spi.block.BlockUtil.calculateBlockResetSize; import static com.facebook.presto.spi.block.BlockUtil.checkValidRegion; import static com.facebook.presto.spi.block.BlockUtil.intSaturatedCast; import static io.airlift.slice.SizeOf.sizeOf; import static java.lang.Math.max; import static java.lang.Math.toIntExact; import static java.util.Objects.requireNonNull; public class LongArrayBlockBuilder implements BlockBuilder { private static final int INSTANCE_SIZE = ClassLayout.parseClass(LongArrayBlockBuilder.class).instanceSize() + BlockBuilderStatus.INSTANCE_SIZE; private BlockBuilderStatus blockBuilderStatus; private boolean initialized; private int initialEntryCount; private int positionCount; // it is assumed that these arrays are the same length private boolean[] valueIsNull = new boolean[0]; private long[] values = new long[0]; private int retainedSizeInBytes; public LongArrayBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries) { this.blockBuilderStatus = requireNonNull(blockBuilderStatus, "blockBuilderStatus is null"); this.initialEntryCount = max(expectedEntries, 1); updateDataSize(); } @Override public BlockBuilder writeLong(long value) { if (values.length <= positionCount) { growCapacity(); } values[positionCount] = value; positionCount++; blockBuilderStatus.addBytes(Byte.BYTES + Long.BYTES); return this; } @Override public BlockBuilder closeEntry() { return this; } @Override public BlockBuilder appendNull() { if (values.length <= positionCount) { growCapacity(); } valueIsNull[positionCount] = true; positionCount++; blockBuilderStatus.addBytes(Byte.BYTES + Long.BYTES); return this; } @Override public Block build() { return new LongArrayBlock(positionCount, valueIsNull, values); } @Override public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus) { return new LongArrayBlockBuilder(blockBuilderStatus, calculateBlockResetSize(positionCount)); } private void growCapacity() { int newSize; if (initialized) { newSize = BlockUtil.calculateNewArraySize(values.length); } else { newSize = initialEntryCount; initialized = true; } valueIsNull = Arrays.copyOf(valueIsNull, newSize); values = Arrays.copyOf(values, newSize); updateDataSize(); } private void updateDataSize() { retainedSizeInBytes = intSaturatedCast(INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values)); } // Copied from LongArrayBlock @Override public int getSizeInBytes() { return intSaturatedCast((Long.BYTES + Byte.BYTES) * (long) positionCount); } @Override public int getRegionSizeInBytes(int position, int length) { return intSaturatedCast((Long.BYTES + Byte.BYTES) * (long) length); } @Override public int getRetainedSizeInBytes() { return retainedSizeInBytes; } @Override public int getPositionCount() { return positionCount; } @Override public long getLong(int position, int offset) { checkReadablePosition(position); if (offset != 0) { throw new IllegalArgumentException("offset must be zero"); } return values[position]; } @Override @Deprecated // TODO: Remove when we fix intermediate types on aggregations. public int getInt(int position, int offset) { checkReadablePosition(position); if (offset != 0) { throw new IllegalArgumentException("offset must be zero"); } return toIntExact(values[position]); } @Override @Deprecated // TODO: Remove when we fix intermediate types on aggregations. public short getShort(int position, int offset) { checkReadablePosition(position); if (offset != 0) { throw new IllegalArgumentException("offset must be zero"); } short value = (short) (values[position]); if (value != values[position]) { throw new ArithmeticException("short overflow"); } return value; } @Override @Deprecated // TODO: Remove when we fix intermediate types on aggregations. public byte getByte(int position, int offset) { checkReadablePosition(position); if (offset != 0) { throw new IllegalArgumentException("offset must be zero"); } byte value = (byte) (values[position]); if (value != values[position]) { throw new ArithmeticException("byte overflow"); } return value; } @Override public boolean isNull(int position) { checkReadablePosition(position); return valueIsNull[position]; } @Override public void writePositionTo(int position, BlockBuilder blockBuilder) { checkReadablePosition(position); blockBuilder.writeLong(values[position]); } @Override public Block getSingleValueBlock(int position) { checkReadablePosition(position); return new LongArrayBlock( 1, new boolean[] {valueIsNull[position]}, new long[] {values[position]}); } @Override public Block copyPositions(List<Integer> positions) { boolean[] newValueIsNull = new boolean[positions.size()]; long[] newValues = new long[positions.size()]; for (int i = 0; i < positions.size(); i++) { int position = positions.get(i); checkReadablePosition(position); newValueIsNull[i] = valueIsNull[position]; newValues[i] = values[position]; } return new LongArrayBlock(positions.size(), newValueIsNull, newValues); } @Override public Block getRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); return new LongArrayBlock(positionOffset, length, valueIsNull, values); } @Override public Block copyRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); boolean[] newValueIsNull = Arrays.copyOfRange(valueIsNull, positionOffset, positionOffset + length); long[] newValues = Arrays.copyOfRange(values, positionOffset, positionOffset + length); return new LongArrayBlock(length, newValueIsNull, newValues); } @Override public BlockEncoding getEncoding() { return new LongArrayBlockEncoding(); } @Override public String toString() { StringBuilder sb = new StringBuilder("LongArrayBlockBuilder{"); sb.append("positionCount=").append(getPositionCount()); sb.append('}'); return sb.toString(); } private void checkReadablePosition(int position) { if (position < 0 || position >= getPositionCount()) { throw new IllegalArgumentException("position is not valid"); } } }