/
BooleanBlock.java
234 lines (206 loc) · 8.91 KB
/
BooleanBlock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* 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.elasticsearch.TransportVersions;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.core.ReleasableIterator;
import org.elasticsearch.index.mapper.BlockLoader;
import java.io.IOException;
/**
* Block that stores boolean values.
* This class is generated. Do not edit it.
*/
public sealed interface BooleanBlock extends Block permits BooleanArrayBlock, BooleanVectorBlock, ConstantNullBlock, BooleanBigArrayBlock {
/**
* Retrieves the boolean value stored at the given value index.
*
* <p> Values for a given position are between getFirstValueIndex(position) (inclusive) and
* getFirstValueIndex(position) + getValueCount(position) (exclusive).
*
* @param valueIndex the value index
* @return the data value (as a boolean)
*/
boolean getBoolean(int valueIndex);
@Override
BooleanVector asVector();
@Override
BooleanBlock filter(int... positions);
@Override
ReleasableIterator<? extends BooleanBlock> lookup(IntBlock positions, ByteSizeValue targetBlockSize);
@Override
BooleanBlock expand();
@Override
default String getWriteableName() {
return "BooleanBlock";
}
NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Block.class, "BooleanBlock", BooleanBlock::readFrom);
private static BooleanBlock readFrom(StreamInput in) throws IOException {
return readFrom((BlockStreamInput) in);
}
static BooleanBlock readFrom(BlockStreamInput in) throws IOException {
final byte serializationType = in.readByte();
return switch (serializationType) {
case SERIALIZE_BLOCK_VALUES -> BooleanBlock.readValues(in);
case SERIALIZE_BLOCK_VECTOR -> BooleanVector.readFrom(in.blockFactory(), in).asBlock();
case SERIALIZE_BLOCK_ARRAY -> BooleanArrayBlock.readArrayBlock(in.blockFactory(), in);
case SERIALIZE_BLOCK_BIG_ARRAY -> BooleanBigArrayBlock.readArrayBlock(in.blockFactory(), in);
default -> {
assert false : "invalid block serialization type " + serializationType;
throw new IllegalStateException("invalid serialization type " + serializationType);
}
};
}
private static BooleanBlock readValues(BlockStreamInput in) throws IOException {
final int positions = in.readVInt();
try (BooleanBlock.Builder builder = in.blockFactory().newBooleanBlockBuilder(positions)) {
for (int i = 0; i < positions; i++) {
if (in.readBoolean()) {
builder.appendNull();
} else {
final int valueCount = in.readVInt();
builder.beginPositionEntry();
for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) {
builder.appendBoolean(in.readBoolean());
}
builder.endPositionEntry();
}
}
return builder.build();
}
}
@Override
default void writeTo(StreamOutput out) throws IOException {
BooleanVector vector = asVector();
final var version = out.getTransportVersion();
if (vector != null) {
out.writeByte(SERIALIZE_BLOCK_VECTOR);
vector.writeTo(out);
} else if (version.onOrAfter(TransportVersions.ESQL_SERIALIZE_ARRAY_BLOCK) && this instanceof BooleanArrayBlock b) {
out.writeByte(SERIALIZE_BLOCK_ARRAY);
b.writeArrayBlock(out);
} else if (version.onOrAfter(TransportVersions.ESQL_SERIALIZE_BIG_ARRAY) && this instanceof BooleanBigArrayBlock b) {
out.writeByte(SERIALIZE_BLOCK_BIG_ARRAY);
b.writeArrayBlock(out);
} else {
out.writeByte(SERIALIZE_BLOCK_VALUES);
BooleanBlock.writeValues(this, out);
}
}
private static void writeValues(BooleanBlock block, StreamOutput out) throws IOException {
final int positions = block.getPositionCount();
out.writeVInt(positions);
for (int pos = 0; pos < positions; pos++) {
if (block.isNull(pos)) {
out.writeBoolean(true);
} else {
out.writeBoolean(false);
final int valueCount = block.getValueCount(pos);
out.writeVInt(valueCount);
for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) {
out.writeBoolean(block.getBoolean(block.getFirstValueIndex(pos) + valueIndex));
}
}
}
}
/**
* Compares the given object with this block for equality. Returns {@code true} if and only if the
* given object is a BooleanBlock, and both blocks are {@link #equals(BooleanBlock, BooleanBlock) equal}.
*/
@Override
boolean equals(Object obj);
/** Returns the hash code of this block, as defined by {@link #hash(BooleanBlock)}. */
@Override
int hashCode();
/**
* Returns {@code true} if the given blocks are equal to each other, otherwise {@code false}.
* Two blocks are considered equal if they have the same position count, and contain the same
* values (including absent null values) in the same order. This definition ensures that the
* equals method works properly across different implementations of the BooleanBlock interface.
*/
static boolean equals(BooleanBlock block1, BooleanBlock block2) {
if (block1 == block2) {
return true;
}
final int positions = block1.getPositionCount();
if (positions != block2.getPositionCount()) {
return false;
}
for (int pos = 0; pos < positions; pos++) {
if (block1.isNull(pos) || block2.isNull(pos)) {
if (block1.isNull(pos) != block2.isNull(pos)) {
return false;
}
} else {
final int valueCount = block1.getValueCount(pos);
if (valueCount != block2.getValueCount(pos)) {
return false;
}
final int b1ValueIdx = block1.getFirstValueIndex(pos);
final int b2ValueIdx = block2.getFirstValueIndex(pos);
for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) {
if (block1.getBoolean(b1ValueIdx + valueIndex) != block2.getBoolean(b2ValueIdx + valueIndex)) {
return false;
}
}
}
}
return true;
}
/**
* Generates the hash code for the given block. The hash code is computed from the block's values.
* This ensures that {@code block1.equals(block2)} implies that {@code block1.hashCode()==block2.hashCode()}
* for any two blocks, {@code block1} and {@code block2}, as required by the general contract of
* {@link Object#hashCode}.
*/
static int hash(BooleanBlock block) {
final int positions = block.getPositionCount();
int result = 1;
for (int pos = 0; pos < positions; pos++) {
if (block.isNull(pos)) {
result = 31 * result - 1;
} else {
final int valueCount = block.getValueCount(pos);
result = 31 * result + valueCount;
final int firstValueIdx = block.getFirstValueIndex(pos);
for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) {
result = 31 * result + Boolean.hashCode(block.getBoolean(firstValueIdx + valueIndex));
}
}
}
return result;
}
/**
* Builder for {@link BooleanBlock}
*/
sealed interface Builder extends Block.Builder, BlockLoader.BooleanBuilder permits BooleanBlockBuilder {
/**
* Appends a boolean to the current entry.
*/
@Override
Builder appendBoolean(boolean value);
/**
* Copy the values in {@code block} from {@code beginInclusive} to
* {@code endExclusive} into this builder.
*/
Builder copyFrom(BooleanBlock block, int beginInclusive, int endExclusive);
@Override
Builder appendNull();
@Override
Builder beginPositionEntry();
@Override
Builder endPositionEntry();
@Override
Builder copyFrom(Block block, int beginInclusive, int endExclusive);
@Override
Builder mvOrdering(Block.MvOrdering mvOrdering);
@Override
BooleanBlock build();
}
}