Skip to content

Commit 6499167

Browse files
committed
doubles
1 parent f30cc97 commit 6499167

File tree

4 files changed

+281
-4
lines changed

4 files changed

+281
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.index.codec.tsdb.es819;
1111

1212
import org.apache.lucene.index.NumericDocValues;
13+
import org.elasticsearch.index.mapper.BlockDocValuesReader;
1314
import org.elasticsearch.index.mapper.BlockLoader;
1415

1516
import java.io.IOException;
@@ -23,4 +24,8 @@ public abstract class BlockAwareNumericDocValues extends NumericDocValues {
2324
public abstract void loadBlock(BlockLoader.IntBuilder builder, BlockLoader.Docs docs) throws IOException;
2425

2526
public abstract void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOException;
27+
28+
public abstract void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble) throws IOException;
29+
30+
public abstract void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble) throws IOException;
2631
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.index.codec.tsdb.es819;
1111

1212
import org.apache.lucene.index.SortedNumericDocValues;
13+
import org.elasticsearch.index.mapper.BlockDocValuesReader.ToDouble;
1314
import org.elasticsearch.index.mapper.BlockLoader;
1415

1516
import java.io.IOException;
@@ -24,4 +25,8 @@ public abstract class BlockAwareSortedNumericDocValues extends SortedNumericDocV
2425

2526
public abstract void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOException;
2627

28+
public abstract void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, ToDouble toDouble) throws IOException;
29+
30+
public abstract void loadDoc(BlockLoader.DoubleBuilder builder, int docId, ToDouble toDouble) throws IOException;
31+
2732
}

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

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.lucene.util.packed.PackedInts;
4444
import org.elasticsearch.core.IOUtils;
4545
import org.elasticsearch.index.codec.tsdb.TSDBDocValuesEncoder;
46+
import org.elasticsearch.index.mapper.BlockDocValuesReader;
4647
import org.elasticsearch.index.mapper.BlockLoader;
4748

4849
import java.io.IOException;
@@ -1082,6 +1083,21 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
10821083
builder.appendInt(0);
10831084
}
10841085

1086+
@Override
1087+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1088+
throws IOException {
1089+
double value = toDouble.convert(0L);
1090+
for (int i = 0; i < docs.count(); i++) {
1091+
builder.appendDouble(value);
1092+
}
1093+
}
1094+
1095+
@Override
1096+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1097+
throws IOException {
1098+
builder.appendDouble(toDouble.convert(0L));
1099+
}
1100+
10851101
@Override
10861102
public int docID() {
10871103
return doc;
@@ -1191,6 +1207,29 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
11911207
builder.appendNull();
11921208
}
11931209
}
1210+
1211+
@Override
1212+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1213+
throws IOException {
1214+
double value = toDouble.convert(0L);
1215+
for (int i = 0; i < docs.count(); i++) {
1216+
if (disi.advanceExact(docs.get(i))) {
1217+
builder.appendDouble(value);
1218+
} else {
1219+
builder.appendNull();
1220+
}
1221+
}
1222+
}
1223+
1224+
@Override
1225+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1226+
throws IOException {
1227+
if (disi.advanceExact(docId)) {
1228+
builder.appendDouble(toDouble.convert(0L));
1229+
} else {
1230+
builder.appendNull();
1231+
}
1232+
}
11941233
};
11951234
}
11961235
}
@@ -1340,6 +1379,27 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
13401379
int value = Math.toIntExact(currentBlock[blockInIndex]);
13411380
builder.appendInt(value);
13421381
}
1382+
1383+
@Override
1384+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1385+
throws IOException {
1386+
for (int i = 0; i < docs.count(); i++) {
1387+
doc = docs.get(i);
1388+
builder.appendDouble(toDouble.convert(longValue()));
1389+
}
1390+
}
1391+
1392+
@Override
1393+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1394+
throws IOException {
1395+
doc = docId;
1396+
1397+
int blockIndex = docId >>> ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SHIFT;
1398+
int blockInIndex = docId & ES819TSDBDocValuesFormat.NUMERIC_BLOCK_MASK;
1399+
loadCurrentBlock(blockIndex);
1400+
double value = toDouble.convert(currentBlock[blockInIndex]);
1401+
builder.appendDouble(value);
1402+
}
13431403
};
13441404
} else {
13451405
final IndexedDISI disi = new IndexedDISI(
@@ -1449,6 +1509,33 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
14491509
builder.appendNull();
14501510
}
14511511
}
1512+
1513+
@Override
1514+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1515+
throws IOException {
1516+
// TODO: collect all doc ids for current block and then append values to builder?
1517+
1518+
for (int i = 0; i < docs.count(); i++) {
1519+
int docId = docs.get(i);
1520+
if (disi.advanceExact(docId)) {
1521+
double value = toDouble.convert(longValue());
1522+
builder.appendDouble(value);
1523+
} else {
1524+
builder.appendNull();
1525+
}
1526+
}
1527+
}
1528+
1529+
@Override
1530+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1531+
throws IOException {
1532+
if (disi.advanceExact(docId)) {
1533+
double value = toDouble.convert(longValue());
1534+
builder.appendDouble(value);
1535+
} else {
1536+
builder.appendNull();
1537+
}
1538+
}
14521539
};
14531540
}
14541541
}
@@ -1629,6 +1716,46 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
16291716
builder.endPositionEntry();
16301717
}
16311718
}
1719+
1720+
@Override
1721+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1722+
throws IOException {
1723+
for (int i = 0; i < docs.count(); i++) {
1724+
doc = docs.get(i);
1725+
start = addresses.get(doc);
1726+
end = addresses.get(doc + 1L);
1727+
count = (int) (end - start);
1728+
1729+
if (count == 1) {
1730+
builder.appendDouble(toDouble.convert(nextValue()));
1731+
} else {
1732+
builder.beginPositionEntry();
1733+
for (int v = 0; v < count; v++) {
1734+
builder.appendDouble(toDouble.convert(nextValue()));
1735+
}
1736+
builder.endPositionEntry();
1737+
}
1738+
}
1739+
}
1740+
1741+
@Override
1742+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1743+
throws IOException {
1744+
doc = docId;
1745+
start = addresses.get(docId);
1746+
end = addresses.get(docId + 1L);
1747+
count = (int) (end - start);
1748+
1749+
if (count == 1) {
1750+
builder.appendDouble(toDouble.convert(nextValue()));
1751+
} else {
1752+
builder.beginPositionEntry();
1753+
for (int v = 0; v < count; v++) {
1754+
builder.appendDouble(toDouble.convert(nextValue()));
1755+
}
1756+
builder.endPositionEntry();
1757+
}
1758+
}
16321759
};
16331760
} else {
16341761
// sparse
@@ -1760,6 +1887,46 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
17601887
}
17611888
}
17621889

1890+
@Override
1891+
public void loadBlock(BlockLoader.DoubleBuilder builder, BlockLoader.Docs docs, BlockDocValuesReader.ToDouble toDouble)
1892+
throws IOException {
1893+
for (int i = 0; i < docs.count(); i++) {
1894+
if (disi.advanceExact(docs.get(i))) {
1895+
set();
1896+
if (count == 1) {
1897+
builder.appendDouble(toDouble.convert(nextValue()));
1898+
} else {
1899+
builder.beginPositionEntry();
1900+
for (int v = 0; v < count; v++) {
1901+
builder.appendDouble(toDouble.convert(nextValue()));
1902+
}
1903+
builder.endPositionEntry();
1904+
}
1905+
} else {
1906+
builder.appendNull();
1907+
}
1908+
}
1909+
}
1910+
1911+
@Override
1912+
public void loadDoc(BlockLoader.DoubleBuilder builder, int docId, BlockDocValuesReader.ToDouble toDouble)
1913+
throws IOException {
1914+
if (disi.advanceExact(docId)) {
1915+
set();
1916+
if (count == 1) {
1917+
builder.appendDouble(toDouble.convert(nextValue()));
1918+
} else {
1919+
builder.beginPositionEntry();
1920+
for (int v = 0; v < count; v++) {
1921+
builder.appendDouble(toDouble.convert(nextValue()));
1922+
}
1923+
builder.endPositionEntry();
1924+
}
1925+
} else {
1926+
builder.appendNull();
1927+
}
1928+
}
1929+
17631930
@Override
17641931
public int docValueCount() {
17651932
set();

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

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public int docId() {
506506

507507
@Override
508508
public String toString() {
509-
return "BlockDocValuesReader.Ints";
509+
return "BlockDocValuesReader.BlockAwareInts";
510510
}
511511
}
512512

@@ -539,13 +539,25 @@ public AllReader reader(LeafReaderContext context) throws IOException {
539539
if (docValues != null) {
540540
NumericDocValues singleton = DocValues.unwrapSingleton(docValues);
541541
if (singleton != null) {
542-
return new SingletonDoubles(singleton, toDouble);
542+
if (singleton instanceof BlockAwareNumericDocValues blockAware) {
543+
return new BlockAwareSingletonDoubles(blockAware, toDouble);
544+
} else {
545+
return new SingletonDoubles(singleton, toDouble);
546+
}
547+
}
548+
if (docValues instanceof BlockAwareSortedNumericDocValues blockAware) {
549+
return new BlockAwareDoubles(blockAware, toDouble);
550+
} else {
551+
return new Doubles(docValues, toDouble);
543552
}
544-
return new Doubles(docValues, toDouble);
545553
}
546554
NumericDocValues singleton = context.reader().getNumericDocValues(fieldName);
547555
if (singleton != null) {
548-
return new SingletonDoubles(singleton, toDouble);
556+
if (singleton instanceof BlockAwareNumericDocValues blockAware) {
557+
return new BlockAwareSingletonDoubles(blockAware, toDouble);
558+
} else {
559+
return new SingletonDoubles(singleton, toDouble);
560+
}
549561
}
550562
return new ConstantNullsReader();
551563
}
@@ -604,6 +616,59 @@ public String toString() {
604616
}
605617
}
606618

619+
private static class BlockAwareSingletonDoubles extends BlockDocValuesReader {
620+
private final BlockAwareNumericDocValues docValues;
621+
private final ToDouble toDouble;
622+
private int docID = -1;
623+
624+
BlockAwareSingletonDoubles(BlockAwareNumericDocValues docValues, ToDouble toDouble) {
625+
this.docValues = docValues;
626+
this.toDouble = toDouble;
627+
}
628+
629+
@Override
630+
public BlockLoader.Block read(BlockFactory factory, Docs docs) throws IOException {
631+
try (BlockLoader.DoubleBuilder builder = factory.doublesFromDocValues(docs.count())) {
632+
int lastDoc = -1;
633+
for (int i = 0; i < docs.count(); i++) {
634+
int doc = docs.get(i);
635+
if (doc < lastDoc) {
636+
throw new IllegalStateException("docs within same block must be in order");
637+
}
638+
if (docValues.advanceExact(doc)) {
639+
builder.appendDouble(toDouble.convert(docValues.longValue()));
640+
} else {
641+
builder.appendNull();
642+
}
643+
lastDoc = doc;
644+
this.docID = doc;
645+
}
646+
return builder.build();
647+
}
648+
}
649+
650+
@Override
651+
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
652+
this.docID = docId;
653+
DoubleBuilder blockBuilder = (DoubleBuilder) builder;
654+
if (docValues.advanceExact(this.docID)) {
655+
blockBuilder.appendDouble(toDouble.convert(docValues.longValue()));
656+
} else {
657+
blockBuilder.appendNull();
658+
}
659+
}
660+
661+
@Override
662+
public int docId() {
663+
return docID;
664+
}
665+
666+
@Override
667+
public String toString() {
668+
return "BlockDocValuesReader.SingletonDoubles";
669+
}
670+
}
671+
607672
private static class Doubles extends BlockDocValuesReader {
608673
private final SortedNumericDocValues docValues;
609674
private final ToDouble toDouble;
@@ -662,6 +727,41 @@ public String toString() {
662727
}
663728
}
664729

730+
private static class BlockAwareDoubles extends BlockDocValuesReader {
731+
private final BlockAwareSortedNumericDocValues blockAware;
732+
private final ToDouble toDouble;
733+
734+
BlockAwareDoubles(BlockAwareSortedNumericDocValues blockAware, ToDouble toDouble) {
735+
this.blockAware = blockAware;
736+
this.toDouble = toDouble;
737+
}
738+
739+
@Override
740+
public BlockLoader.Block read(BlockFactory factory, Docs docs) throws IOException {
741+
try (BlockLoader.DoubleBuilder builder = factory.doublesFromDocValues(docs.count())) {
742+
blockAware.loadBlock(builder, docs, toDouble);
743+
return builder.build();
744+
}
745+
}
746+
747+
@Override
748+
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
749+
DoubleBuilder blockBuilder = (DoubleBuilder) builder;
750+
blockAware.loadDoc(blockBuilder, docId, toDouble);
751+
}
752+
753+
@Override
754+
public int docId() {
755+
return blockAware.docID();
756+
}
757+
758+
@Override
759+
public String toString() {
760+
return "BlockDocValuesReader.BlockAwareDoubles";
761+
}
762+
763+
}
764+
665765
public static class DenseVectorBlockLoader extends DocValuesBlockLoader {
666766
private final String fieldName;
667767
private final int dimensions;

0 commit comments

Comments
 (0)