Skip to content

Commit 6bb2a4a

Browse files
committed
delegate
1 parent 560f8b9 commit 6bb2a4a

File tree

8 files changed

+73
-49
lines changed

8 files changed

+73
-49
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public BlockLoader blockLoader(BlockLoaderContext blContext) {
377377
return BlockLoader.CONSTANT_NULLS;
378378
}
379379
if (hasDocValues() && (blContext.fieldExtractPreference() != FieldExtractPreference.STORED || isSyntheticSource)) {
380-
return new BlockDocValuesReader.DoublesBlockLoader(name(), new BlockDocValuesReader.LongToScaledFloat(scalingFactor));
380+
return new BlockDocValuesReader.DoublesBlockLoader(name(), l -> l / scalingFactor);
381381
}
382382
// Multi fields don't have fallback synthetic source.
383383
if (isSyntheticSource && blContext.parentField(name()) == null) {

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ public BlockLoader.Block tryRead(
388388
BlockLoader.BlockFactory factory,
389389
BlockLoader.Docs docs,
390390
int offset,
391+
boolean nullsFiltered,
391392
BlockDocValuesReader.ToDouble toDouble
392393
) throws IOException {
393394
assert toDouble == null;
@@ -468,6 +469,7 @@ public BlockLoader.Block tryRead(
468469
BlockLoader.BlockFactory factory,
469470
BlockLoader.Docs docs,
470471
int offset,
472+
boolean nullsFiltered,
471473
BlockDocValuesReader.ToDouble toDouble
472474
) throws IOException {
473475
return null;
@@ -520,6 +522,7 @@ public BlockLoader.Block tryRead(
520522
BlockLoader.BlockFactory factory,
521523
BlockLoader.Docs docs,
522524
int offset,
525+
boolean nullsFiltered,
523526
BlockDocValuesReader.ToDouble toDouble
524527
) throws IOException {
525528
return null;
@@ -532,7 +535,7 @@ BlockLoader.Block tryRead(BlockLoader.SingletonLongBuilder builder, BlockLoader.
532535
}
533536
}
534537

535-
abstract static class BaseSparseNumericValues extends NumericDocValues implements BlockDocValuesReader.OptionalSingletonDoubles {
538+
abstract static class BaseSparseNumericValues extends NumericDocValues implements BlockLoader.OptionalColumnAtATimeReader {
536539
protected final IndexedDISI disi;
537540

538541
BaseSparseNumericValues(IndexedDISI disi) {
@@ -565,12 +568,12 @@ public final long cost() {
565568
}
566569

567570
@Override
568-
public BlockLoader.Block tryReadDoubles(
571+
public BlockLoader.Block tryRead(
569572
BlockLoader.BlockFactory factory,
570573
BlockLoader.Docs docs,
571574
int offset,
572-
BlockDocValuesReader.ToDouble toDouble,
573-
boolean nullsFiltered
575+
boolean nullsFiltered,
576+
BlockDocValuesReader.ToDouble toDouble
574577
) throws IOException {
575578
return null;
576579
}
@@ -1397,16 +1400,11 @@ public BlockLoader.Block tryRead(
13971400
BlockLoader.BlockFactory factory,
13981401
BlockLoader.Docs docs,
13991402
int offset,
1403+
boolean nullsFiltered,
14001404
BlockDocValuesReader.ToDouble toDouble
14011405
) throws IOException {
1402-
if (toDouble != null) {
1403-
try (BlockLoader.SingletonDoubleBuilder builder = factory.singletonDoubles(docs.count() - offset)) {
1404-
SingletonLongToDoubleDelegate delegate = new SingletonLongToDoubleDelegate(builder, toDouble);
1405-
return tryRead(delegate, docs, offset);
1406-
}
1407-
}
1408-
try (BlockLoader.SingletonLongBuilder builder = factory.singletonLongs(docs.count() - offset)) {
1409-
return tryRead(builder, docs, offset);
1406+
try (var longs = singletonLongs(factory, toDouble, docs.count() - offset)) {
1407+
return tryRead(longs, docs, offset);
14101408
}
14111409
}
14121410

@@ -1521,12 +1519,12 @@ public long longValue() throws IOException {
15211519
}
15221520

15231521
@Override
1524-
public BlockLoader.Block tryReadDoubles(
1522+
public BlockLoader.Block tryRead(
15251523
BlockLoader.BlockFactory factory,
15261524
BlockLoader.Docs docs,
15271525
int offset,
1528-
BlockDocValuesReader.ToDouble toDouble,
1529-
boolean nullsFiltered
1526+
boolean nullsFiltered,
1527+
BlockDocValuesReader.ToDouble toDouble
15301528
) throws IOException {
15311529
if (nullsFiltered == false) {
15321530
return null;
@@ -1557,11 +1555,11 @@ public BlockLoader.Block tryReadDoubles(
15571555
final int firstIndex = disi.index();
15581556
final int lastIndex = jumpDISI.index();
15591557
final int valueCount = lastIndex - firstIndex + 1;
1560-
// TODO: encode this via docCount
1561-
if (valueCount == docs.count()) {
1562-
final double[] values = new double[valueCount];
1563-
int i = 0;
1564-
while (i < valueCount) {
1558+
if (valueCount != docs.count()) {
1559+
return null;
1560+
}
1561+
try (var longs = singletonLongs(factory, toDouble, valueCount)) {
1562+
for (int i = 0; i < valueCount;) {
15651563
final int index = firstIndex + i;
15661564
final int blockIndex = index >>> ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SHIFT;
15671565
final int blockStartIndex = index & ES819TSDBDocValuesFormat.NUMERIC_BLOCK_MASK;
@@ -1573,14 +1571,12 @@ public BlockLoader.Block tryReadDoubles(
15731571
currentBlockIndex = blockIndex;
15741572
decoder.decode(valuesData, currentBlock);
15751573
}
1576-
// bulk convert from the
15771574
final int count = Math.min(ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE - blockStartIndex, valueCount - i);
1578-
toDouble.convert(currentBlock, blockStartIndex, values, i, count);
1575+
longs.appendLongs(currentBlock, blockStartIndex, count);
15791576
i += count;
15801577
}
1581-
return factory.doubles(values, docs.count());
1578+
return longs.build();
15821579
}
1583-
return null;
15841580
}
15851581
};
15861582
}
@@ -1877,6 +1873,18 @@ public BlockLoader.Builder endPositionEntry() {
18771873
public void close() {}
18781874
}
18791875

1876+
static BlockLoader.SingletonLongBuilder singletonLongs(
1877+
BlockLoader.BlockFactory factory,
1878+
BlockDocValuesReader.ToDouble toDouble,
1879+
int valueCount
1880+
) {
1881+
if (toDouble != null) {
1882+
return new SingletonLongToDoubleDelegate(factory.singletonDoubles(valueCount), toDouble);
1883+
} else {
1884+
return factory.singletonLongs(valueCount);
1885+
}
1886+
}
1887+
18801888
// Block builder that consumes long values and converts them to double using the provided converter function.
18811889
static final class SingletonLongToDoubleDelegate implements BlockLoader.SingletonLongBuilder {
18821890
private final BlockLoader.SingletonDoubleBuilder doubleBuilder;
@@ -1925,7 +1933,9 @@ public BlockLoader.Builder endPositionEntry() {
19251933
}
19261934

19271935
@Override
1928-
public void close() {}
1936+
public void close() {
1937+
doubleBuilder.close();
1938+
}
19291939
}
19301940

19311941
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static class SingletonLongs extends BlockDocValuesReader implements NumericDocVa
137137
@Override
138138
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
139139
if (numericDocValues instanceof BlockLoader.OptionalColumnAtATimeReader direct) {
140-
BlockLoader.Block result = direct.tryRead(factory, docs, offset, null);
140+
BlockLoader.Block result = direct.tryRead(factory, docs, offset, nullsFiltered, null);
141141
if (result != null) {
142142
return result;
143143
}
@@ -409,7 +409,7 @@ static class SingletonDoubles extends BlockDocValuesReader implements NumericDoc
409409
@Override
410410
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
411411
if (docValues instanceof BlockLoader.OptionalColumnAtATimeReader direct) {
412-
BlockLoader.Block result = direct.tryRead(factory, docs, offset, toDouble);
412+
BlockLoader.Block result = direct.tryRead(factory, docs, offset, nullsFiltered, toDouble);
413413
if (result != null) {
414414
return result;
415415
}
@@ -736,7 +736,7 @@ public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset, boole
736736
return readSingleDoc(factory, docs.get(offset));
737737
}
738738
if (ordinals instanceof BlockLoader.OptionalColumnAtATimeReader direct) {
739-
BlockLoader.Block block = direct.tryRead(factory, docs, offset, null);
739+
BlockLoader.Block block = direct.tryRead(factory, docs, offset, nullsFiltered, null);
740740
if (block != null) {
741741
return block;
742742
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ interface OptionalColumnAtATimeReader {
6666
* Attempts to read the values of all documents in {@code docs}
6767
* Returns {@code null} if unable to load the values.
6868
*
69+
* @param nullsFiltered if {@code true}, then target docs are guaranteed to have a value for the field.
70+
* see {@link ColumnAtATimeReader#read(BlockFactory, Docs, int, boolean)}
6971
* @param toDouble a function to convert long values to double, or null if no conversion is needed/supported
7072
*/
7173
@Nullable
72-
BlockLoader.Block tryRead(BlockFactory factory, Docs docs, int offset, BlockDocValuesReader.ToDouble toDouble) throws IOException;
74+
BlockLoader.Block tryRead(
75+
BlockFactory factory,
76+
Docs docs,
77+
int offset,
78+
boolean nullsFiltered,
79+
BlockDocValuesReader.ToDouble toDouble
80+
) throws IOException;
7381
}
7482

7583
interface RowStrideReader extends Reader {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ public void writeValue(XContentBuilder b, long value) throws IOException {
483483

484484
@Override
485485
BlockLoader blockLoaderFromDocValues(String fieldName) {
486-
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, BlockDocValuesReader.ToDouble.SORTABLE_SHORT_TO_HALF_FLOAT);
486+
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, l -> HalfFloatPoint.sortableShortToHalfFloat((short) l));
487487
}
488488

489489
@Override
@@ -677,7 +677,7 @@ public void writeValue(XContentBuilder b, long value) throws IOException {
677677

678678
@Override
679679
BlockLoader blockLoaderFromDocValues(String fieldName) {
680-
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, BlockDocValuesReader.ToDouble.SORTABLE_INT_TO_FLOAT);
680+
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, l -> NumericUtils.sortableIntToFloat((int) l));
681681
}
682682

683683
@Override
@@ -837,7 +837,7 @@ public void writeValue(XContentBuilder b, long value) throws IOException {
837837

838838
@Override
839839
BlockLoader blockLoaderFromDocValues(String fieldName) {
840-
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, BlockDocValuesReader.ToDouble.SORTABLE_LONG_TO_DOUBLE);
840+
return new BlockDocValuesReader.DoublesBlockLoader(fieldName, NumericUtils::sortableLongToDouble);
841841
}
842842

843843
@Override

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ public void testOptionalColumnAtATimeReader() throws Exception {
773773

774774
{
775775
// bulk loading timestamp:
776-
var block = (TestBlock) timestampDV.tryRead(factory, docs, 0, null);
776+
var block = (TestBlock) timestampDV.tryRead(factory, docs, 0, false, null);
777777
assertNotNull(block);
778778
assertEquals(size, block.size());
779779
for (int j = 0; j < block.size(); j++) {
@@ -785,10 +785,10 @@ public void testOptionalColumnAtATimeReader() throws Exception {
785785
}
786786
{
787787
// bulk loading counter field:
788-
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, null);
788+
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, false, null);
789789
assertNotNull(block);
790790
assertEquals(size, block.size());
791-
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, 0, null);
791+
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, 0, false, null);
792792
assertNotNull(stringBlock);
793793
assertEquals(size, stringBlock.size());
794794
for (int j = 0; j < block.size(); j++) {
@@ -805,7 +805,7 @@ public void testOptionalColumnAtATimeReader() throws Exception {
805805
}
806806
{
807807
// bulk loading gauge field:
808-
var block = (TestBlock) gaugeDV.tryRead(factory, docs, 0, null);
808+
var block = (TestBlock) gaugeDV.tryRead(factory, docs, 0, false, null);
809809
assertNotNull(block);
810810
assertEquals(size, block.size());
811811
for (int j = 0; j < block.size(); j++) {
@@ -843,7 +843,7 @@ public void testOptionalColumnAtATimeReader() throws Exception {
843843

844844
{
845845
// bulk loading timestamp:
846-
var block = (TestBlock) timestampDV.tryRead(blockFactory, docs, randomOffset, null);
846+
var block = (TestBlock) timestampDV.tryRead(blockFactory, docs, randomOffset, false, null);
847847
assertNotNull(block);
848848
assertEquals(size, block.size());
849849
for (int j = 0; j < block.size(); j++) {
@@ -855,11 +855,11 @@ public void testOptionalColumnAtATimeReader() throws Exception {
855855
}
856856
{
857857
// bulk loading counter field:
858-
var block = (TestBlock) counterDV.tryRead(factory, docs, randomOffset, null);
858+
var block = (TestBlock) counterDV.tryRead(factory, docs, randomOffset, false, null);
859859
assertNotNull(block);
860860
assertEquals(size, block.size());
861861

862-
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, randomOffset, null);
862+
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, randomOffset, false, null);
863863
assertNotNull(stringBlock);
864864
assertEquals(size, stringBlock.size());
865865

@@ -877,7 +877,7 @@ public void testOptionalColumnAtATimeReader() throws Exception {
877877
}
878878
{
879879
// bulk loading gauge field:
880-
var block = (TestBlock) gaugeDV.tryRead(factory, docs, randomOffset, null);
880+
var block = (TestBlock) gaugeDV.tryRead(factory, docs, randomOffset, false, null);
881881
assertNotNull(block);
882882
assertEquals(size, block.size());
883883
for (int j = 0; j < block.size(); j++) {
@@ -902,11 +902,11 @@ public void testOptionalColumnAtATimeReader() throws Exception {
902902
stringCounterDV = getBaseSortedDocValues(leafReader, counterFieldAsString);
903903
{
904904
// bulk loading counter field:
905-
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, null);
905+
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, false, null);
906906
assertNotNull(block);
907907
assertEquals(size, block.size());
908908

909-
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, 0, null);
909+
var stringBlock = (TestBlock) stringCounterDV.tryRead(factory, docs, 0, false, null);
910910
assertNotNull(stringBlock);
911911
assertEquals(size, stringBlock.size());
912912

@@ -1001,7 +1001,7 @@ public void testOptionalColumnAtATimeReaderWithSparseDocs() throws Exception {
10011001
var docs = TestBlock.docs(docIds);
10021002
{
10031003
timestampDV = getBaseDenseNumericValues(leafReader, timestampField);
1004-
var block = (TestBlock) timestampDV.tryRead(factory, docs, 0, null);
1004+
var block = (TestBlock) timestampDV.tryRead(factory, docs, 0, false, null);
10051005
assertNotNull(block);
10061006
assertEquals(numDocsPerQValue, block.size());
10071007
for (int j = 0; j < block.size(); j++) {
@@ -1012,7 +1012,7 @@ public void testOptionalColumnAtATimeReaderWithSparseDocs() throws Exception {
10121012
}
10131013
{
10141014
counterDV = getBaseDenseNumericValues(leafReader, counterField);
1015-
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, null);
1015+
var block = (TestBlock) counterDV.tryRead(factory, docs, 0, false, null);
10161016
assertNotNull(block);
10171017
assertEquals(numDocsPerQValue, block.size());
10181018
for (int j = 0; j < block.size(); j++) {
@@ -1023,7 +1023,7 @@ public void testOptionalColumnAtATimeReaderWithSparseDocs() throws Exception {
10231023
}
10241024
{
10251025
counterAsStringDV = getBaseSortedDocValues(leafReader, counterAsStringField);
1026-
var block = (TestBlock) counterAsStringDV.tryRead(factory, docs, 0, null);
1026+
var block = (TestBlock) counterAsStringDV.tryRead(factory, docs, 0, false, null);
10271027
assertNotNull(block);
10281028
assertEquals(numDocsPerQValue, block.size());
10291029
for (int j = 0; j < block.size(); j++) {
@@ -1086,7 +1086,7 @@ public int get(int i) {
10861086
}
10871087
};
10881088
var idReader = ESTestCase.asInstanceOf(OptionalColumnAtATimeReader.class, leaf.reader().getNumericDocValues("id"));
1089-
TestBlock idBlock = (TestBlock) idReader.tryRead(factory, docs, 0, null);
1089+
TestBlock idBlock = (TestBlock) idReader.tryRead(factory, docs, 0, false, null);
10901090
assertNotNull(idBlock);
10911091

10921092
{
@@ -1100,7 +1100,7 @@ public int get(int i) {
11001100
block = (TestBlock) reader2.tryReadAHead(factory, docs, randomOffset);
11011101
} else {
11021102
assertNull(reader2.tryReadAHead(factory, docs, randomOffset));
1103-
block = (TestBlock) reader2.tryRead(factory, docs, randomOffset, null);
1103+
block = (TestBlock) reader2.tryRead(factory, docs, randomOffset, false, null);
11041104
}
11051105
assertNotNull(block);
11061106
assertThat(block.size(), equalTo(docs.count() - randomOffset));
@@ -1122,7 +1122,7 @@ public int get(int i) {
11221122
block = (TestBlock) reader3.tryReadAHead(factory, docs, randomOffset);
11231123
} else {
11241124
assertNull(reader3.tryReadAHead(factory, docs, randomOffset));
1125-
block = (TestBlock) reader3.tryRead(factory, docs, randomOffset, null);
1125+
block = (TestBlock) reader3.tryRead(factory, docs, randomOffset, false, null);
11261126
}
11271127
assertNotNull(reader3);
11281128
assertNotNull(block);

0 commit comments

Comments
 (0)