org.elasticsearch.compute.data.IntArrayBlock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of x-pack-esql-compute Show documentation
Show all versions of x-pack-esql-compute Show documentation
Elasticsearch subproject :x-pack:plugin:esql:compute
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.compute.data;
import org.apache.lucene.util.RamUsageEstimator;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.core.ReleasableIterator;
import org.elasticsearch.core.Releasables;
import java.io.IOException;
import java.util.BitSet;
/**
* Block implementation that stores values in a {@link IntArrayVector}.
* This class is generated. Do not edit it.
*/
final class IntArrayBlock extends AbstractArrayBlock implements IntBlock {
static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(IntArrayBlock.class);
private final IntArrayVector vector;
IntArrayBlock(
int[] values,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new IntArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering
);
}
private IntArrayBlock(
IntArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
: firstValueIndexes[getPositionCount()] == vector.getPositionCount();
}
static IntArrayBlock readArrayBlock(BlockFactory blockFactory, BlockStreamInput in) throws IOException {
final SubFields sub = new SubFields(blockFactory, in);
IntArrayVector vector = null;
boolean success = false;
try {
vector = IntArrayVector.readArrayVector(sub.vectorPositions(), in, blockFactory);
var block = new IntArrayBlock(vector, sub.positionCount, sub.firstValueIndexes, sub.nullsMask, sub.mvOrdering);
blockFactory.adjustBreaker(block.ramBytesUsed() - vector.ramBytesUsed() - sub.bytesReserved);
success = true;
return block;
} finally {
if (success == false) {
Releasables.close(vector);
blockFactory.adjustBreaker(-sub.bytesReserved);
}
}
}
void writeArrayBlock(StreamOutput out) throws IOException {
writeSubFields(out);
vector.writeArrayVector(vector.getPositionCount(), out);
}
@Override
public IntVector asVector() {
return null;
}
@Override
public int getInt(int valueIndex) {
return vector.getInt(valueIndex);
}
@Override
public IntBlock filter(int... positions) {
try (var builder = blockFactory().newIntBlockBuilder(positions.length)) {
for (int pos : positions) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int valueCount = getValueCount(pos);
int first = getFirstValueIndex(pos);
if (valueCount == 1) {
builder.appendInt(getInt(getFirstValueIndex(pos)));
} else {
builder.beginPositionEntry();
for (int c = 0; c < valueCount; c++) {
builder.appendInt(getInt(first + c));
}
builder.endPositionEntry();
}
}
return builder.mvOrdering(mvOrdering()).build();
}
}
@Override
public ReleasableIterator lookup(IntBlock positions, ByteSizeValue targetBlockSize) {
return new IntLookup(this, positions, targetBlockSize);
}
@Override
public ElementType elementType() {
return ElementType.INT;
}
@Override
public IntBlock expand() {
if (firstValueIndexes == null) {
incRef();
return this;
}
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// The following line is correct because positions with multi-values are never null.
int expandedPositionCount = vector.getPositionCount();
long bitSetRamUsedEstimate = Math.max(nullsMask.size(), BlockRamUsageEstimator.sizeOfBitSet(expandedPositionCount));
blockFactory().adjustBreaker(bitSetRamUsedEstimate);
IntArrayBlock expanded = new IntArrayBlock(
vector,
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
vector.incRef();
return expanded;
}
private long ramBytesUsedOnlyBlock() {
return BASE_RAM_BYTES_USED + BlockRamUsageEstimator.sizeOf(firstValueIndexes) + BlockRamUsageEstimator.sizeOfBitSet(nullsMask);
}
@Override
public long ramBytesUsed() {
return ramBytesUsedOnlyBlock() + vector.ramBytesUsed();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof IntBlock that) {
return IntBlock.equals(this, that);
}
return false;
}
@Override
public int hashCode() {
return IntBlock.hash(this);
}
@Override
public String toString() {
return getClass().getSimpleName()
+ "[positions="
+ getPositionCount()
+ ", mvOrdering="
+ mvOrdering()
+ ", vector="
+ vector
+ ']';
}
@Override
public void allowPassingToDifferentDriver() {
vector.allowPassingToDifferentDriver();
}
@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}
@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Releasables.closeExpectNoException(vector);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy