Skip to content

Commit 962427d

Browse files
authored
Remove docID check from BlockDocValuesReader (#133101)
I have verified that our DocValues extensions track docId correctly; therefore, I think we can remove the extra docId tracking from BlockDocValuesReader.
1 parent c1b99f3 commit 962427d

File tree

1 file changed

+8
-64
lines changed

1 file changed

+8
-64
lines changed

server/src/main/java/org/elasticsearch/index/mapper/BlockDocValuesReader.java

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.io.IOException;
3939

4040
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.COSINE_MAGNITUDE_FIELD_SUFFIX;
41-
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.ElementType.BYTE;
4241

4342
/**
4443
* A reader that supports reading doc-values from a Lucene segment in Block fashion.
@@ -139,18 +138,13 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
139138
}
140139
}
141140
try (BlockLoader.LongBuilder builder = factory.longsFromDocValues(docs.count() - offset)) {
142-
int lastDoc = -1;
143141
for (int i = offset; i < docs.count(); i++) {
144142
int doc = docs.get(i);
145-
if (doc < lastDoc) {
146-
throw new IllegalStateException("docs within same block must be in order");
147-
}
148143
if (numericDocValues.advanceExact(doc)) {
149144
builder.appendLong(numericDocValues.longValue());
150145
} else {
151146
builder.appendNull();
152147
}
153-
lastDoc = doc;
154148
}
155149
return builder.build();
156150
}
@@ -179,7 +173,6 @@ public String toString() {
179173

180174
static class Longs extends BlockDocValuesReader {
181175
private final SortedNumericDocValues numericDocValues;
182-
private int docID = -1;
183176

184177
Longs(SortedNumericDocValues numericDocValues) {
185178
this.numericDocValues = numericDocValues;
@@ -190,9 +183,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
190183
try (BlockLoader.LongBuilder builder = factory.longsFromDocValues(docs.count() - offset)) {
191184
for (int i = offset; i < docs.count(); i++) {
192185
int doc = docs.get(i);
193-
if (doc < this.docID) {
194-
throw new IllegalStateException("docs within same block must be in order");
195-
}
196186
read(doc, builder);
197187
}
198188
return builder.build();
@@ -205,7 +195,6 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
205195
}
206196

207197
private void read(int doc, LongBuilder builder) throws IOException {
208-
this.docID = doc;
209198
if (false == numericDocValues.advanceExact(doc)) {
210199
builder.appendNull();
211200
return;
@@ -224,8 +213,7 @@ private void read(int doc, LongBuilder builder) throws IOException {
224213

225214
@Override
226215
public int docId() {
227-
// There is a .docID on the numericDocValues but it is often not implemented.
228-
return docID;
216+
return numericDocValues.docID();
229217
}
230218

231219
@Override
@@ -274,18 +262,13 @@ private static class SingletonInts extends BlockDocValuesReader {
274262
@Override
275263
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throws IOException {
276264
try (BlockLoader.IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
277-
int lastDoc = -1;
278265
for (int i = offset; i < docs.count(); i++) {
279266
int doc = docs.get(i);
280-
if (doc < lastDoc) {
281-
throw new IllegalStateException("docs within same block must be in order");
282-
}
283267
if (numericDocValues.advanceExact(doc)) {
284268
builder.appendInt(Math.toIntExact(numericDocValues.longValue()));
285269
} else {
286270
builder.appendNull();
287271
}
288-
lastDoc = doc;
289272
}
290273
return builder.build();
291274
}
@@ -314,7 +297,6 @@ public String toString() {
314297

315298
private static class Ints extends BlockDocValuesReader {
316299
private final SortedNumericDocValues numericDocValues;
317-
private int docID = -1;
318300

319301
Ints(SortedNumericDocValues numericDocValues) {
320302
this.numericDocValues = numericDocValues;
@@ -325,9 +307,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
325307
try (BlockLoader.IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
326308
for (int i = offset; i < docs.count(); i++) {
327309
int doc = docs.get(i);
328-
if (doc < this.docID) {
329-
throw new IllegalStateException("docs within same block must be in order");
330-
}
331310
read(doc, builder);
332311
}
333312
return builder.build();
@@ -340,7 +319,6 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
340319
}
341320

342321
private void read(int doc, IntBuilder builder) throws IOException {
343-
this.docID = doc;
344322
if (false == numericDocValues.advanceExact(doc)) {
345323
builder.appendNull();
346324
return;
@@ -359,8 +337,7 @@ private void read(int doc, IntBuilder builder) throws IOException {
359337

360338
@Override
361339
public int docId() {
362-
// There is a .docID on the numericDocValues but it is often not implemented.
363-
return docID;
340+
return numericDocValues.docID();
364341
}
365342

366343
@Override
@@ -413,7 +390,6 @@ public AllReader reader(LeafReaderContext context) throws IOException {
413390
private static class SingletonDoubles extends BlockDocValuesReader {
414391
private final NumericDocValues docValues;
415392
private final ToDouble toDouble;
416-
private int docID = -1;
417393

418394
SingletonDoubles(NumericDocValues docValues, ToDouble toDouble) {
419395
this.docValues = docValues;
@@ -423,29 +399,22 @@ private static class SingletonDoubles extends BlockDocValuesReader {
423399
@Override
424400
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throws IOException {
425401
try (BlockLoader.DoubleBuilder builder = factory.doublesFromDocValues(docs.count() - offset)) {
426-
int lastDoc = -1;
427402
for (int i = offset; i < docs.count(); i++) {
428403
int doc = docs.get(i);
429-
if (doc < lastDoc) {
430-
throw new IllegalStateException("docs within same block must be in order");
431-
}
432404
if (docValues.advanceExact(doc)) {
433405
builder.appendDouble(toDouble.convert(docValues.longValue()));
434406
} else {
435407
builder.appendNull();
436408
}
437-
lastDoc = doc;
438-
this.docID = doc;
439409
}
440410
return builder.build();
441411
}
442412
}
443413

444414
@Override
445415
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
446-
this.docID = docId;
447416
DoubleBuilder blockBuilder = (DoubleBuilder) builder;
448-
if (docValues.advanceExact(this.docID)) {
417+
if (docValues.advanceExact(docId)) {
449418
blockBuilder.appendDouble(toDouble.convert(docValues.longValue()));
450419
} else {
451420
blockBuilder.appendNull();
@@ -454,7 +423,7 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
454423

455424
@Override
456425
public int docId() {
457-
return docID;
426+
return docValues.docID();
458427
}
459428

460429
@Override
@@ -466,7 +435,6 @@ public String toString() {
466435
private static class Doubles extends BlockDocValuesReader {
467436
private final SortedNumericDocValues docValues;
468437
private final ToDouble toDouble;
469-
private int docID = -1;
470438

471439
Doubles(SortedNumericDocValues docValues, ToDouble toDouble) {
472440
this.docValues = docValues;
@@ -478,9 +446,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
478446
try (BlockLoader.DoubleBuilder builder = factory.doublesFromDocValues(docs.count() - offset)) {
479447
for (int i = offset; i < docs.count(); i++) {
480448
int doc = docs.get(i);
481-
if (doc < this.docID) {
482-
throw new IllegalStateException("docs within same block must be in order");
483-
}
484449
read(doc, builder);
485450
}
486451
return builder.build();
@@ -493,7 +458,6 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
493458
}
494459

495460
private void read(int doc, DoubleBuilder builder) throws IOException {
496-
this.docID = doc;
497461
if (false == docValues.advanceExact(doc)) {
498462
builder.appendNull();
499463
return;
@@ -512,7 +476,7 @@ private void read(int doc, DoubleBuilder builder) throws IOException {
512476

513477
@Override
514478
public int docId() {
515-
return docID;
479+
return docValues.docID();
516480
}
517481

518482
@Override
@@ -759,9 +723,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
759723
try (var builder = factory.singletonOrdinalsBuilder(ordinals, docs.count() - offset, false)) {
760724
for (int i = offset; i < docs.count(); i++) {
761725
int doc = docs.get(i);
762-
if (doc < ordinals.docID()) {
763-
throw new IllegalStateException("docs within same block must be in order");
764-
}
765726
if (ordinals.advanceExact(doc)) {
766727
builder.appendOrd(ordinals.ordValue());
767728
} else {
@@ -909,8 +870,6 @@ private static class BytesRefsFromBinary extends BlockDocValuesReader {
909870
private final ByteArrayStreamInput in = new ByteArrayStreamInput();
910871
private final BytesRef scratch = new BytesRef();
911872

912-
private int docID = -1;
913-
914873
BytesRefsFromBinary(BinaryDocValues docValues) {
915874
this.docValues = docValues;
916875
}
@@ -920,9 +879,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
920879
try (BlockLoader.BytesRefBuilder builder = factory.bytesRefs(docs.count() - offset)) {
921880
for (int i = offset; i < docs.count(); i++) {
922881
int doc = docs.get(i);
923-
if (doc < docID) {
924-
throw new IllegalStateException("docs within same block must be in order");
925-
}
926882
read(doc, builder);
927883
}
928884
return builder.build();
@@ -935,7 +891,6 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
935891
}
936892

937893
private void read(int doc, BytesRefBuilder builder) throws IOException {
938-
this.docID = doc;
939894
if (false == docValues.advanceExact(doc)) {
940895
builder.appendNull();
941896
return;
@@ -964,7 +919,7 @@ private void read(int doc, BytesRefBuilder builder) throws IOException {
964919

965920
@Override
966921
public int docId() {
967-
return docID;
922+
return docValues.docID();
968923
}
969924

970925
@Override
@@ -1014,7 +969,6 @@ private abstract static class AbstractDenseVectorFromBinary<T> extends BlockDocV
1014969
protected final IndexVersion indexVersion;
1015970
protected final int dimensions;
1016971
protected final T scratch;
1017-
protected int docID = -1;
1018972

1019973
AbstractDenseVectorFromBinary(BinaryDocValues docValues, int dims, IndexVersion indexVersion, T scratch) {
1020974
this.docValues = docValues;
@@ -1025,7 +979,7 @@ private abstract static class AbstractDenseVectorFromBinary<T> extends BlockDocV
1025979

1026980
@Override
1027981
public int docId() {
1028-
return docID;
982+
return docValues.docID();
1029983
}
1030984

1031985
@Override
@@ -1038,17 +992,13 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
1038992
try (BlockLoader.FloatBuilder builder = factory.denseVectors(docs.count() - offset, dimensions)) {
1039993
for (int i = offset; i < docs.count(); i++) {
1040994
int doc = docs.get(i);
1041-
if (doc < docID) {
1042-
throw new IllegalStateException("docs within same block must be in order");
1043-
}
1044995
read(doc, builder);
1045996
}
1046997
return builder.build();
1047998
}
1048999
}
10491000

10501001
private void read(int doc, BlockLoader.FloatBuilder builder) throws IOException {
1051-
this.docID = doc;
10521002
if (docValues.advanceExact(doc) == false) {
10531003
builder.appendNull();
10541004
return;
@@ -1191,7 +1141,6 @@ public String toString() {
11911141

11921142
private static class Booleans extends BlockDocValuesReader {
11931143
private final SortedNumericDocValues numericDocValues;
1194-
private int docID = -1;
11951144

11961145
Booleans(SortedNumericDocValues numericDocValues) {
11971146
this.numericDocValues = numericDocValues;
@@ -1202,9 +1151,6 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throw
12021151
try (BlockLoader.BooleanBuilder builder = factory.booleansFromDocValues(docs.count() - offset)) {
12031152
for (int i = offset; i < docs.count(); i++) {
12041153
int doc = docs.get(i);
1205-
if (doc < this.docID) {
1206-
throw new IllegalStateException("docs within same block must be in order");
1207-
}
12081154
read(doc, builder);
12091155
}
12101156
return builder.build();
@@ -1217,7 +1163,6 @@ public void read(int docId, BlockLoader.StoredFields storedFields, Builder build
12171163
}
12181164

12191165
private void read(int doc, BooleanBuilder builder) throws IOException {
1220-
this.docID = doc;
12211166
if (false == numericDocValues.advanceExact(doc)) {
12221167
builder.appendNull();
12231168
return;
@@ -1236,8 +1181,7 @@ private void read(int doc, BooleanBuilder builder) throws IOException {
12361181

12371182
@Override
12381183
public int docId() {
1239-
// There is a .docID on the numericDocValues but it is often not implemented.
1240-
return docID;
1184+
return numericDocValues.docID();
12411185
}
12421186

12431187
@Override

0 commit comments

Comments
 (0)