Skip to content

Commit 7613265

Browse files
committed
Added BaseSortedDocValues#tryReadAHead(...) to improve testing of the look ahead logic
1 parent f74f9ec commit 7613265

File tree

2 files changed

+64
-33
lines changed

2 files changed

+64
-33
lines changed

server/src/main/java/org/elasticsearch/index/codec/tsdb/es819/ES819TSDBDocValuesProducer.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,11 @@ public long cost() {
385385
@Override
386386
public BlockLoader.Block tryRead(BlockLoader.BlockFactory factory, BlockLoader.Docs docs, int offset) throws IOException {
387387
if (ords instanceof BaseDenseNumericValues denseOrds) {
388-
if (valuesSorted || entry.termsDictEntry.termsDictSize == 1) {
389-
int firstDoc = docs.get(offset);
390-
denseOrds.advanceExact(firstDoc);
391-
long startValue = denseOrds.longValue();
392-
final int docCount = docs.count();
393-
int lastDoc = docs.get(docCount - 1);
394-
long lastValue = denseOrds.lookAheadValueAt(lastDoc);
395-
if (lastValue == startValue) {
396-
BytesRef b = lookupOrd(Math.toIntExact(startValue));
397-
return factory.constantBytes(BytesRef.deepCopyOf(b), docCount - offset);
398-
}
399-
// TODO: Since ordinals are sorted, start at 0 (offset by startValue), scan until lastValue,
400-
// then fill remaining positions with lastValue.
401-
// Falling back to tryRead(...) is safe here, given that current block index wasn't altered by looking ahead.
388+
var block = tryReadAHead(factory, docs, offset);
389+
if (block != null) {
390+
return block;
402391
}
392+
// Falling back to tryRead(...) is safe here, given that current block index wasn't altered by looking ahead.
403393
try (var builder = factory.singletonOrdinalsBuilder(this, docs.count() - offset, true)) {
404394
BlockLoader.SingletonLongBuilder delegate = new SingletonLongToSingletonOrdinalDelegate(builder);
405395
var result = denseOrds.tryRead(delegate, docs, offset);
@@ -410,6 +400,24 @@ public BlockLoader.Block tryRead(BlockLoader.BlockFactory factory, BlockLoader.D
410400
}
411401
return null;
412402
}
403+
404+
BlockLoader.Block tryReadAHead(BlockLoader.BlockFactory factory, BlockLoader.Docs docs, int offset) throws IOException {
405+
if (ords instanceof BaseDenseNumericValues denseOrds && (valuesSorted || entry.termsDictEntry.termsDictSize == 1)) {
406+
int firstDoc = docs.get(offset);
407+
denseOrds.advanceExact(firstDoc);
408+
long startValue = denseOrds.longValue();
409+
final int docCount = docs.count();
410+
int lastDoc = docs.get(docCount - 1);
411+
long lastValue = denseOrds.lookAheadValueAt(lastDoc);
412+
if (lastValue == startValue) {
413+
BytesRef b = lookupOrd(Math.toIntExact(startValue));
414+
return factory.constantBytes(BytesRef.deepCopyOf(b), docCount - offset);
415+
}
416+
// TODO: Since ordinals are sorted, start at 0 (offset by startValue), scan until lastValue,
417+
// then fill remaining positions with lastValue.
418+
}
419+
return null;
420+
}
413421
};
414422
}
415423

@@ -452,6 +460,10 @@ public TermsEnum termsEnum() throws IOException {
452460
public BlockLoader.Block tryRead(BlockLoader.BlockFactory factory, BlockLoader.Docs docs, int offset) throws IOException {
453461
return null;
454462
}
463+
464+
BlockLoader.Block tryReadAHead(BlockLoader.BlockFactory factory, BlockLoader.Docs docs, int offset) throws IOException {
465+
return null;
466+
}
455467
}
456468

457469
abstract static class BaseDenseNumericValues extends NumericDocValues implements BlockLoader.OptionalColumnAtATimeReader {

server/src/test/java/org/elasticsearch/index/codec/tsdb/es819/ES819TSDBDocValuesFormatTests.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,49 +1086,59 @@ public int get(int i) {
10861086
assertNotNull(idBlock);
10871087

10881088
{
1089-
var reader2 = ESTestCase.asInstanceOf(
1089+
var reader2 = (BaseSortedDocValues) ESTestCase.asInstanceOf(
10901090
OptionalColumnAtATimeReader.class,
10911091
leaf.reader().getSortedDocValues(secondField)
10921092
);
1093-
int randomOffset = 0;
1094-
ESTestCase.between(0, docs.count() - 1);
1095-
var block = (TestBlock) reader2.tryRead(factory, docs, randomOffset);
1093+
int randomOffset = ESTestCase.between(0, docs.count() - 1);
1094+
TestBlock block;
1095+
if (reader2.getValueCount() == 1) {
1096+
block = (TestBlock) reader2.tryReadAHead(factory, docs, randomOffset);
1097+
} else {
1098+
assertNull(reader2.tryReadAHead(factory, docs, randomOffset));
1099+
block = (TestBlock) reader2.tryRead(factory, docs, randomOffset);
1100+
}
10961101
assertNotNull(block);
10971102
assertThat(block.size(), equalTo(docs.count() - randomOffset));
1098-
for (int i = randomOffset; i < block.size(); i++) {
1103+
for (int i = 0; i < block.size(); i++) {
10991104
String actualHostName = BytesRefs.toString(block.get(i));
1100-
int id = ((Number) idBlock.get(i)).intValue();
1105+
int id = ((Number) idBlock.get(i + randomOffset)).intValue();
11011106
String expectedHostName = hostnames.get(id);
11021107
assertEquals(expectedHostName, actualHostName);
11031108
}
11041109
}
11051110
{
1106-
var reader3 = ESTestCase.asInstanceOf(
1111+
var reader3 = (BaseSortedDocValues) ESTestCase.asInstanceOf(
11071112
OptionalColumnAtATimeReader.class,
11081113
leaf.reader().getSortedDocValues(unsortedField)
11091114
);
1115+
int randomOffset = ESTestCase.between(0, docs.count() - 1);
1116+
TestBlock block;
1117+
if (reader3.getValueCount() == 1) {
1118+
block = (TestBlock) reader3.tryReadAHead(factory, docs, randomOffset);
1119+
} else {
1120+
assertNull(reader3.tryReadAHead(factory, docs, randomOffset));
1121+
block = (TestBlock) reader3.tryRead(factory, docs, randomOffset);
1122+
}
11101123
assertNotNull(reader3);
1111-
int randomOffset = 0;
1112-
ESTestCase.between(0, docs.count() - 1);
1113-
var block = (TestBlock) reader3.tryRead(factory, docs, randomOffset);
11141124
assertNotNull(block);
11151125
assertThat(block.size(), equalTo(docs.count() - randomOffset));
1116-
for (int i = randomOffset; i < block.size(); i++) {
1126+
for (int i = 0; i < block.size(); i++) {
11171127
String actualHostName = BytesRefs.toString(block.get(i));
1118-
int id = ((Number) idBlock.get(i)).intValue();
1128+
int id = ((Number) idBlock.get(i + randomOffset)).intValue();
11191129
String expectedHostName = hostnames.get(id);
11201130
assertEquals(expectedHostName, actualHostName);
11211131
}
11221132
}
11231133
for (int offset = 0; offset < idBlock.size(); offset += ESTestCase.between(1, numDocs)) {
11241134
int start = offset;
1125-
var reader1 = ESTestCase.asInstanceOf(
1135+
var reader1 = (BaseSortedDocValues) ESTestCase.asInstanceOf(
11261136
OptionalColumnAtATimeReader.class,
11271137
leaf.reader().getSortedDocValues(primaryField)
11281138
);
11291139
while (start < idBlock.size()) {
11301140
int end = start + random().nextInt(idBlock.size() - start);
1131-
TestBlock hostBlock = (TestBlock) reader1.tryRead(factory, new BlockLoader.Docs() {
1141+
TestBlock hostBlock = (TestBlock) reader1.tryReadAHead(factory, new BlockLoader.Docs() {
11321142
@Override
11331143
public int count() {
11341144
return end + 1;
@@ -1139,11 +1149,20 @@ public int get(int docId) {
11391149
return docId;
11401150
}
11411151
}, start);
1142-
assertNotNull(hostBlock);
1143-
assertThat(hostBlock.size(), equalTo(end - start + 1));
1144-
for (int i = 0; i < hostBlock.size(); i++) {
1145-
String actualHostName = BytesRefs.toString(hostBlock.get(i));
1146-
assertThat(actualHostName, equalTo(hostnames.get(((Number) idBlock.get(i + start)).intValue())));
1152+
Set<String> seenValues = new HashSet<>();
1153+
for (int p = start; p <= end; p++) {
1154+
String hostName = hostnames.get(((Number) idBlock.get(p)).intValue());
1155+
seenValues.add(hostName);
1156+
}
1157+
if (seenValues.size() == 1) {
1158+
assertNotNull(hostBlock);
1159+
assertThat(hostBlock.size(), equalTo(end - start + 1));
1160+
for (int i = 0; i < hostBlock.size(); i++) {
1161+
String actualHostName = BytesRefs.toString(hostBlock.get(i));
1162+
assertThat(actualHostName, equalTo(hostnames.get(((Number) idBlock.get(i + start)).intValue())));
1163+
}
1164+
} else {
1165+
assertNull(hostBlock);
11471166
}
11481167
if (start == idBlock.size() - 1) {
11491168
break;

0 commit comments

Comments
 (0)