Skip to content

Commit 7c80c39

Browse files
committed
BlockAwareSingletonOrdinals
1 parent af74e94 commit 7c80c39

File tree

4 files changed

+141
-6
lines changed

4 files changed

+141
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public abstract class BlockAwareNumericDocValues extends NumericDocValues {
2525

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

28+
public abstract void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset) throws IOException;
29+
2830
public abstract void loadBlock(
2931
BlockLoader.DoubleBuilder builder,
3032
BlockLoader.Docs docs,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index.codec.tsdb.es819;
11+
12+
import org.apache.lucene.index.SortedDocValues;
13+
import org.elasticsearch.index.mapper.BlockLoader;
14+
15+
import java.io.IOException;
16+
17+
public abstract class BlockAwareSortedDocValues extends SortedDocValues {
18+
19+
public abstract void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset) throws IOException;
20+
21+
}

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

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,14 @@ public SortedDocValues getSorted(FieldInfo field) throws IOException {
338338
}
339339

340340
private SortedDocValues getSorted(SortedEntry entry) throws IOException {
341-
final NumericDocValues ords = getNumeric(entry.ordsEntry, entry.termsDictEntry.termsDictSize);
341+
if (entry.ordsEntry.docsWithFieldOffset == -2) {
342+
return DocValues.emptySorted();
343+
}
344+
345+
final BlockAwareNumericDocValues ords = (BlockAwareNumericDocValues) getNumeric(
346+
entry.ordsEntry,
347+
entry.termsDictEntry.termsDictSize
348+
);
342349
return new BaseSortedDocValues(entry) {
343350

344351
@Override
@@ -370,10 +377,15 @@ public int advance(int target) throws IOException {
370377
public long cost() {
371378
return ords.cost();
372379
}
380+
381+
@Override
382+
public void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset) throws IOException {
383+
ords.loadBlock(builder, docs, offset);
384+
}
373385
};
374386
}
375387

376-
abstract class BaseSortedDocValues extends SortedDocValues {
388+
abstract class BaseSortedDocValues extends BlockAwareSortedDocValues {
377389

378390
final SortedEntry entry;
379391
final TermsEnum termsEnum;
@@ -1061,7 +1073,7 @@ public long longValue() {
10611073

10621074
@Override
10631075
public void loadBlock(BlockLoader.LongBuilder builder, BlockLoader.Docs docs, int offset) throws IOException {
1064-
for (int i = 0; i < docs.count(); i++) {
1076+
for (int i = offset; i < docs.count(); i++) {
10651077
builder.appendLong(0L);
10661078
}
10671079
}
@@ -1073,7 +1085,7 @@ public void loadDoc(BlockLoader.LongBuilder builder, int docId) {
10731085

10741086
@Override
10751087
public void loadBlock(BlockLoader.IntBuilder builder, BlockLoader.Docs docs, int offset) throws IOException {
1076-
for (int i = 0; i < docs.count(); i++) {
1088+
for (int i = offset; i < docs.count(); i++) {
10771089
builder.appendInt(0);
10781090
}
10791091
}
@@ -1083,6 +1095,14 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
10831095
builder.appendInt(0);
10841096
}
10851097

1098+
@Override
1099+
public void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset)
1100+
throws IOException {
1101+
for (int i = offset; i < docs.count(); i++) {
1102+
builder.appendOrd(0);
1103+
}
1104+
}
1105+
10861106
@Override
10871107
public void loadBlock(
10881108
BlockLoader.DoubleBuilder builder,
@@ -1212,6 +1232,18 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
12121232
}
12131233
}
12141234

1235+
@Override
1236+
public void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset)
1237+
throws IOException {
1238+
for (int i = offset; i < docs.count(); i++) {
1239+
if (disi.advanceExact(docs.get(i))) {
1240+
builder.appendOrd(0);
1241+
} else {
1242+
builder.appendNull();
1243+
}
1244+
}
1245+
}
1246+
12151247
@Override
12161248
public void loadBlock(
12171249
BlockLoader.DoubleBuilder builder,
@@ -1388,6 +1420,14 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
13881420
builder.appendInt(value);
13891421
}
13901422

1423+
@Override
1424+
public void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset) throws IOException {
1425+
for (int i = offset; i < docs.count(); i++) {
1426+
doc = docs.get(i);
1427+
builder.appendOrd(Math.toIntExact(longValue()));
1428+
}
1429+
}
1430+
13911431
@Override
13921432
public void loadBlock(
13931433
BlockLoader.DoubleBuilder builder,
@@ -1522,6 +1562,21 @@ public void loadDoc(BlockLoader.IntBuilder builder, int docId) throws IOExceptio
15221562
}
15231563
}
15241564

1565+
@Override
1566+
public void loadBlock(BlockLoader.SingletonOrdinalsBuilder builder, BlockLoader.Docs docs, int offset) throws IOException {
1567+
// TODO: collect all doc ids for current block and then append values to builder?
1568+
1569+
for (int i = offset; i < docs.count(); i++) {
1570+
int docId = docs.get(i);
1571+
if (disi.advanceExact(docId)) {
1572+
int value = Math.toIntExact(longValue());
1573+
builder.appendOrd(value);
1574+
} else {
1575+
builder.appendNull();
1576+
}
1577+
}
1578+
}
1579+
15251580
@Override
15261581
public void loadBlock(
15271582
BlockLoader.DoubleBuilder builder,

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.io.stream.ByteArrayStreamInput;
2323
import org.elasticsearch.index.IndexVersion;
2424
import org.elasticsearch.index.codec.tsdb.es819.BlockAwareNumericDocValues;
25+
import org.elasticsearch.index.codec.tsdb.es819.BlockAwareSortedDocValues;
2526
import org.elasticsearch.index.codec.tsdb.es819.BlockAwareSortedNumericDocValues;
2627
import org.elasticsearch.index.mapper.BlockLoader.BlockFactory;
2728
import org.elasticsearch.index.mapper.BlockLoader.BooleanBuilder;
@@ -840,13 +841,21 @@ public AllReader reader(LeafReaderContext context) throws IOException {
840841
if (docValues != null) {
841842
SortedDocValues singleton = DocValues.unwrapSingleton(docValues);
842843
if (singleton != null) {
843-
return new SingletonOrdinals(singleton);
844+
if (singleton instanceof BlockAwareSortedDocValues blockAware) {
845+
return new BlockAwareSingletonOrdinals(blockAware);
846+
} else {
847+
return new SingletonOrdinals(singleton);
848+
}
844849
}
845850
return new Ordinals(docValues);
846851
}
847852
SortedDocValues singleton = context.reader().getSortedDocValues(fieldName);
848853
if (singleton != null) {
849-
return new SingletonOrdinals(singleton);
854+
if (singleton instanceof BlockAwareSortedDocValues blockAware) {
855+
return new BlockAwareSingletonOrdinals(blockAware);
856+
} else {
857+
return new SingletonOrdinals(singleton);
858+
}
850859
}
851860
return new ConstantNullsReader();
852861
}
@@ -925,6 +934,54 @@ public String toString() {
925934
}
926935
}
927936

937+
private static class BlockAwareSingletonOrdinals extends BlockDocValuesReader {
938+
private final BlockAwareSortedDocValues ordinals;
939+
940+
BlockAwareSingletonOrdinals(BlockAwareSortedDocValues ordinals) {
941+
this.ordinals = ordinals;
942+
}
943+
944+
private BlockLoader.Block readSingleDoc(BlockFactory factory, int docId) throws IOException {
945+
if (ordinals.advanceExact(docId)) {
946+
BytesRef v = ordinals.lookupOrd(ordinals.ordValue());
947+
// the returned BytesRef can be reused
948+
return factory.constantBytes(BytesRef.deepCopyOf(v), 1);
949+
} else {
950+
return factory.constantNulls(1);
951+
}
952+
}
953+
954+
@Override
955+
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset) throws IOException {
956+
if (docs.count() - offset == 1) {
957+
return readSingleDoc(factory, docs.get(offset));
958+
}
959+
try (var builder = factory.singletonOrdinalsBuilder(ordinals, docs.count() - offset)) {
960+
ordinals.loadBlock(builder, docs, offset);
961+
return builder.build();
962+
}
963+
}
964+
965+
@Override
966+
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
967+
if (ordinals.advanceExact(docId)) {
968+
((BytesRefBuilder) builder).appendBytesRef(ordinals.lookupOrd(ordinals.ordValue()));
969+
} else {
970+
builder.appendNull();
971+
}
972+
}
973+
974+
@Override
975+
public int docId() {
976+
return ordinals.docID();
977+
}
978+
979+
@Override
980+
public String toString() {
981+
return "BlockDocValuesReader.BlockAwareSingletonOrdinals";
982+
}
983+
}
984+
928985
private static class Ordinals extends BlockDocValuesReader {
929986
private final SortedSetDocValues ordinals;
930987

0 commit comments

Comments
 (0)