Skip to content

Commit 795935d

Browse files
committed
Ints
1 parent 4528fcc commit 795935d

File tree

14 files changed

+622
-150
lines changed

14 files changed

+622
-150
lines changed

server/src/main/java/org/elasticsearch/index/mapper/blockloader/docvalues/AbstractBytesRefsFromOrdsBlockLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
/**
2121
* Loads {@code keyword} style fields that are stored as a lookup table.
2222
*/
23-
public abstract class AbstractBytesRefsFromOrdsBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader {
23+
abstract class AbstractBytesRefsFromOrdsBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader {
2424
protected final String fieldName;
2525

26-
public AbstractBytesRefsFromOrdsBlockLoader(String fieldName) {
26+
AbstractBytesRefsFromOrdsBlockLoader(String fieldName) {
2727
this.fieldName = fieldName;
2828
}
2929

3030
@Override
31-
public BytesRefBuilder builder(BlockFactory factory, int expectedCount) {
31+
public final BytesRefBuilder builder(BlockFactory factory, int expectedCount) {
3232
return factory.bytesRefs(expectedCount);
3333
}
3434

3535
@Override
36-
public AllReader reader(LeafReaderContext context) throws IOException {
36+
public final AllReader reader(LeafReaderContext context) throws IOException {
3737
SortedSetDocValues docValues = context.reader().getSortedSetDocValues(fieldName);
3838
if (docValues != null) {
3939
SortedDocValues singleton = DocValues.unwrapSingleton(docValues);
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.mapper.blockloader.docvalues;
11+
12+
import org.apache.lucene.index.DocValues;
13+
import org.apache.lucene.index.LeafReaderContext;
14+
import org.apache.lucene.index.NumericDocValues;
15+
import org.apache.lucene.index.SortedNumericDocValues;
16+
17+
import java.io.IOException;
18+
19+
/**
20+
* Loads {@code int}s from doc values.
21+
*/
22+
public abstract class AbstractIntsFromDocValuesBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader {
23+
protected final String fieldName;
24+
25+
AbstractIntsFromDocValuesBlockLoader(String fieldName) {
26+
this.fieldName = fieldName;
27+
}
28+
29+
@Override
30+
public final Builder builder(BlockFactory factory, int expectedCount) {
31+
return factory.ints(expectedCount);
32+
}
33+
34+
@Override
35+
public final AllReader reader(LeafReaderContext context) throws IOException {
36+
SortedNumericDocValues docValues = context.reader().getSortedNumericDocValues(fieldName);
37+
if (docValues != null) {
38+
NumericDocValues singleton = DocValues.unwrapSingleton(docValues);
39+
if (singleton != null) {
40+
return singletonReader(singleton);
41+
}
42+
return sortedReader(docValues);
43+
}
44+
NumericDocValues singleton = context.reader().getNumericDocValues(fieldName);
45+
if (singleton != null) {
46+
return singletonReader(singleton);
47+
}
48+
return new ConstantNullsReader();
49+
}
50+
51+
protected abstract AllReader singletonReader(NumericDocValues docValues);
52+
53+
protected abstract AllReader sortedReader(SortedNumericDocValues docValues);
54+
55+
public static class Singleton extends BlockDocValuesReader implements BlockDocValuesReader.NumericDocValuesAccessor {
56+
private final NumericDocValues numericDocValues;
57+
58+
Singleton(NumericDocValues numericDocValues) {
59+
this.numericDocValues = numericDocValues;
60+
}
61+
62+
@Override
63+
public Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
64+
if (numericDocValues instanceof OptionalColumnAtATimeReader direct) {
65+
Block result = direct.tryRead(factory, docs, offset, nullsFiltered, null, true);
66+
if (result != null) {
67+
return result;
68+
}
69+
}
70+
try (IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
71+
for (int i = offset; i < docs.count(); i++) {
72+
int doc = docs.get(i);
73+
if (numericDocValues.advanceExact(doc)) {
74+
builder.appendInt(Math.toIntExact(numericDocValues.longValue()));
75+
} else {
76+
builder.appendNull();
77+
}
78+
}
79+
return builder.build();
80+
}
81+
}
82+
83+
@Override
84+
public void read(int docId, StoredFields storedFields, Builder builder) throws IOException {
85+
IntBuilder blockBuilder = (IntBuilder) builder;
86+
if (numericDocValues.advanceExact(docId)) {
87+
blockBuilder.appendInt(Math.toIntExact(numericDocValues.longValue()));
88+
} else {
89+
blockBuilder.appendNull();
90+
}
91+
}
92+
93+
@Override
94+
public int docId() {
95+
return numericDocValues.docID();
96+
}
97+
98+
@Override
99+
public String toString() {
100+
return "IntsFromDocValues.Singleton";
101+
}
102+
103+
@Override
104+
public NumericDocValues numericDocValues() {
105+
return numericDocValues;
106+
}
107+
}
108+
109+
public static class Sorted extends BlockDocValuesReader {
110+
private final SortedNumericDocValues numericDocValues;
111+
112+
Sorted(SortedNumericDocValues numericDocValues) {
113+
this.numericDocValues = numericDocValues;
114+
}
115+
116+
@Override
117+
public Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
118+
try (IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
119+
for (int i = offset; i < docs.count(); i++) {
120+
int doc = docs.get(i);
121+
read(doc, builder);
122+
}
123+
return builder.build();
124+
}
125+
}
126+
127+
@Override
128+
public void read(int docId, StoredFields storedFields, Builder builder) throws IOException {
129+
read(docId, (IntBuilder) builder);
130+
}
131+
132+
private void read(int doc, IntBuilder builder) throws IOException {
133+
if (false == numericDocValues.advanceExact(doc)) {
134+
builder.appendNull();
135+
return;
136+
}
137+
int count = numericDocValues.docValueCount();
138+
if (count == 1) {
139+
builder.appendInt(Math.toIntExact(numericDocValues.nextValue()));
140+
return;
141+
}
142+
builder.beginPositionEntry();
143+
for (int v = 0; v < count; v++) {
144+
builder.appendInt(Math.toIntExact(numericDocValues.nextValue()));
145+
}
146+
builder.endPositionEntry();
147+
}
148+
149+
@Override
150+
public int docId() {
151+
return numericDocValues.docID();
152+
}
153+
154+
@Override
155+
public String toString() {
156+
return "IntsFromDocValues.Sorted";
157+
}
158+
}
159+
}

server/src/main/java/org/elasticsearch/index/mapper/blockloader/docvalues/IntsBlockLoader.java

Lines changed: 12 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -9,145 +9,29 @@
99

1010
package org.elasticsearch.index.mapper.blockloader.docvalues;
1111

12-
import org.apache.lucene.index.DocValues;
13-
import org.apache.lucene.index.LeafReaderContext;
1412
import org.apache.lucene.index.NumericDocValues;
1513
import org.apache.lucene.index.SortedNumericDocValues;
16-
import org.elasticsearch.index.mapper.BlockLoader;
17-
18-
import java.io.IOException;
19-
20-
public class IntsBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader {
21-
private final String fieldName;
2214

15+
/**
16+
* Loads {@code int}s from doc values.
17+
*/
18+
public class IntsBlockLoader extends AbstractIntsFromDocValuesBlockLoader {
2319
public IntsBlockLoader(String fieldName) {
24-
this.fieldName = fieldName;
20+
super(fieldName);
2521
}
2622

2723
@Override
28-
public Builder builder(BlockFactory factory, int expectedCount) {
29-
return factory.ints(expectedCount);
24+
protected AllReader singletonReader(NumericDocValues docValues) {
25+
return new Singleton(docValues);
3026
}
3127

3228
@Override
33-
public AllReader reader(LeafReaderContext context) throws IOException {
34-
SortedNumericDocValues docValues = context.reader().getSortedNumericDocValues(fieldName);
35-
if (docValues != null) {
36-
NumericDocValues singleton = DocValues.unwrapSingleton(docValues);
37-
if (singleton != null) {
38-
return new SingletonInts(singleton);
39-
}
40-
return new Ints(docValues);
41-
}
42-
NumericDocValues singleton = context.reader().getNumericDocValues(fieldName);
43-
if (singleton != null) {
44-
return new SingletonInts(singleton);
45-
}
46-
return new ConstantNullsReader();
47-
}
48-
49-
public static class SingletonInts extends BlockDocValuesReader implements BlockDocValuesReader.NumericDocValuesAccessor {
50-
private final NumericDocValues numericDocValues;
51-
52-
SingletonInts(NumericDocValues numericDocValues) {
53-
this.numericDocValues = numericDocValues;
54-
}
55-
56-
@Override
57-
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
58-
if (numericDocValues instanceof BlockLoader.OptionalColumnAtATimeReader direct) {
59-
BlockLoader.Block result = direct.tryRead(factory, docs, offset, nullsFiltered, null, true);
60-
if (result != null) {
61-
return result;
62-
}
63-
}
64-
try (BlockLoader.IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
65-
for (int i = offset; i < docs.count(); i++) {
66-
int doc = docs.get(i);
67-
if (numericDocValues.advanceExact(doc)) {
68-
builder.appendInt(Math.toIntExact(numericDocValues.longValue()));
69-
} else {
70-
builder.appendNull();
71-
}
72-
}
73-
return builder.build();
74-
}
75-
}
76-
77-
@Override
78-
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
79-
IntBuilder blockBuilder = (IntBuilder) builder;
80-
if (numericDocValues.advanceExact(docId)) {
81-
blockBuilder.appendInt(Math.toIntExact(numericDocValues.longValue()));
82-
} else {
83-
blockBuilder.appendNull();
84-
}
85-
}
86-
87-
@Override
88-
public int docId() {
89-
return numericDocValues.docID();
90-
}
91-
92-
@Override
93-
public String toString() {
94-
return "BlockDocValuesReader.SingletonInts";
95-
}
96-
97-
@Override
98-
public NumericDocValues numericDocValues() {
99-
return numericDocValues;
100-
}
29+
protected AllReader sortedReader(SortedNumericDocValues docValues) {
30+
return new Sorted(docValues);
10131
}
10232

103-
public static class Ints extends BlockDocValuesReader {
104-
private final SortedNumericDocValues numericDocValues;
105-
106-
Ints(SortedNumericDocValues numericDocValues) {
107-
this.numericDocValues = numericDocValues;
108-
}
109-
110-
@Override
111-
public BlockLoader.Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException {
112-
try (BlockLoader.IntBuilder builder = factory.intsFromDocValues(docs.count() - offset)) {
113-
for (int i = offset; i < docs.count(); i++) {
114-
int doc = docs.get(i);
115-
read(doc, builder);
116-
}
117-
return builder.build();
118-
}
119-
}
120-
121-
@Override
122-
public void read(int docId, BlockLoader.StoredFields storedFields, Builder builder) throws IOException {
123-
read(docId, (IntBuilder) builder);
124-
}
125-
126-
private void read(int doc, IntBuilder builder) throws IOException {
127-
if (false == numericDocValues.advanceExact(doc)) {
128-
builder.appendNull();
129-
return;
130-
}
131-
int count = numericDocValues.docValueCount();
132-
if (count == 1) {
133-
builder.appendInt(Math.toIntExact(numericDocValues.nextValue()));
134-
return;
135-
}
136-
builder.beginPositionEntry();
137-
for (int v = 0; v < count; v++) {
138-
builder.appendInt(Math.toIntExact(numericDocValues.nextValue()));
139-
}
140-
builder.endPositionEntry();
141-
}
142-
143-
@Override
144-
public int docId() {
145-
return numericDocValues.docID();
146-
}
147-
148-
@Override
149-
public String toString() {
150-
return "BlockDocValuesReader.Ints";
151-
}
33+
@Override
34+
public String toString() {
35+
return "IntsFromDocValues[" + fieldName + "]";
15236
}
15337
}

server/src/main/java/org/elasticsearch/index/mapper/blockloader/docvalues/MvMaxBytesRefsFromOrdsBlockLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import java.io.IOException;
1717

1818
/**
19-
* Loads {@code MV_MAX} applied to {@code keyword} style fields that are stored as
20-
* a lookup table and ordinals.
19+
* Loads the MAX {@code keyword} in each doc.
2120
*/
2221
public class MvMaxBytesRefsFromOrdsBlockLoader extends AbstractBytesRefsFromOrdsBlockLoader {
2322
private final String fieldName;

0 commit comments

Comments
 (0)