From 602f0d384d426225f9d7766df2b71e15b72bf846 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 20 Jun 2022 21:17:57 -0400 Subject: [PATCH 1/6] Speed up synthetic source This speeds up synthetic source, especially when there are many fields in the index that are declared in the mapping but don't have values. This is fairly common with ECS, and the tsdb rally track uses that. And this improves fetch performance of that track: ``` | 50th percentile service time | default | 6.24029 | 4.85568 | ms | -22.19% | | 90th percentile service time | default | 7.89923 | 6.52069 | ms | -17.45% | | 99th percentile service time | default | 12.0306 | 16.435 | ms | +36.61% | | 100th percentile service time | default | 14.2873 | 17.1175 | ms | +19.81% | | 50th percentile service time | default_1k | 158.425 | 25.3236 | ms | -84.02% | | 90th percentile service time | default_1k | 165.46 | 30.8655 | ms | -81.35% | | 99th percentile service time | default_1k | 168.954 | 33.3342 | ms | -80.27% | | 100th percentile service time | default_1k | 174.341 | 34.8344 | ms | -80.02% | ``` There's a slight increase in the 99th and 100th percentile service time for fetching ten document which think is unlucky jitter. Hopefully. The average performance of fetching ten docs improves anyway so I think we're ok. Fetching a thousand documents improves 80% across the board which is lovely. This works by doing three things: 1. Teach the "leaf" layer of source loader to detect when the field is empty in that segment and remove it from the synthesis process entirely. This brings most of the speed up in tsdb. 2. Replace `hasValue` with a callback when writing the first value. `hasValue` was resulting in a 2^n-like number of calls that really showed up in the profiler. 3. Replace the `ArrayList` of leaf loaders with an array. Before fixing the other two issues the `ArrayList`'s iterator really showed up in the profiling. Probably much less worth it now, but it's small. All of this brings synthetic source much closer to the fetch performance of standard _source: ``` | 50th percentile service time | default_1k | 11.4016 | 25.3236 | ms | +122.11% | | 90th percentile service time | default_1k | 13.7212 | 30.8655 | ms | +124.95% | | 99th percentile service time | default_1k | 15.8785 | 33.3342 | ms | +109.93% | | 100th percentile service time | default_1k | 16.9715 | 34.8344 | ms | +105.25% | ``` One important thing, these perf numbers come from fetching *hot* blocks on disk. They mostly compare CPU overhead and not disk overhead. --- .../index/mapper/KeywordFieldMapper.java | 18 ++++-- .../index/mapper/NumberFieldMapper.java | 33 ++++++++-- .../index/mapper/ObjectMapper.java | 60 ++++++++++--------- .../index/mapper/SourceLoader.java | 49 ++++++++++----- 4 files changed, 106 insertions(+), 54 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index c1fe5ee41b87b..50115b54f0461 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -41,6 +41,7 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.search.AutomatonQueries; import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -1078,21 +1079,28 @@ public BytesSyntheticFieldLoader(String name, String simpleName) { @Override public Leaf leaf(LeafReader reader) throws IOException { SortedSetDocValues leaf = DocValues.getSortedSet(reader, name); + if (leaf.getValueCount() == 0) { + return SourceLoader.SyntheticFieldLoader.NOTHING.leaf(reader); + } return new SourceLoader.SyntheticFieldLoader.Leaf() { private boolean hasValue; @Override - public void advanceToDoc(int docId) throws IOException { - hasValue = leaf.advanceExact(docId); + public boolean empty() { + return false; } @Override - public boolean hasValue() { - return hasValue; + public void advanceToDoc(int docId) throws IOException { + hasValue = leaf.advanceExact(docId); } @Override - public void load(XContentBuilder b) throws IOException { + public void load(XContentBuilder b, CheckedRunnable before) throws IOException { + if (false == hasValue) { + return; + } + before.run(); long first = leaf.nextOrd(); long next = leaf.nextOrd(); if (next == SortedSetDocValues.NO_MORE_ORDS) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index c7199b1d20ba1..f5ca11478e74d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -17,6 +17,7 @@ import org.apache.lucene.index.DocValues; import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.sandbox.document.HalfFloatPoint; import org.apache.lucene.sandbox.search.IndexSortSortedNumericDocValuesRangeQuery; @@ -32,6 +33,7 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType; import org.elasticsearch.index.fielddata.plain.SortedDoublesIndexFieldData; @@ -1629,24 +1631,43 @@ protected NumericSyntheticFieldLoader(String name, String simpleName) { this.simpleName = simpleName; } + private SortedNumericDocValues dv(LeafReader reader) throws IOException { + SortedNumericDocValues dv = reader.getSortedNumericDocValues(name); + if (dv != null) { + return dv; + } + NumericDocValues single = reader.getNumericDocValues(name); + if (single != null) { + return DocValues.singleton(single); + } + return null; + } + @Override public Leaf leaf(LeafReader reader) throws IOException { - SortedNumericDocValues leaf = DocValues.getSortedNumeric(reader, name); + SortedNumericDocValues leaf = dv(reader); + if (leaf == null) { + return SourceLoader.SyntheticFieldLoader.NOTHING.leaf(reader); + } return new SourceLoader.SyntheticFieldLoader.Leaf() { private boolean hasValue; @Override - public void advanceToDoc(int docId) throws IOException { - hasValue = leaf.advanceExact(docId); + public boolean empty() { + return false; } @Override - public boolean hasValue() { - return hasValue; + public void advanceToDoc(int docId) throws IOException { + hasValue = leaf.advanceExact(docId); } @Override - public void load(XContentBuilder b) throws IOException { + public void load(XContentBuilder b, CheckedRunnable before) throws IOException { + if (false == hasValue) { + return; + } + before.run(); if (leaf.docValueCount() == 1) { b.field(simpleName); loadNextValue(b, leaf.nextValue()); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index 3e629b4a21119..2f752695b32ec 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -15,6 +15,7 @@ import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -554,51 +555,56 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep @Override public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() { - List fields = new ArrayList<>(); - mappers.values().stream().sorted(Comparator.comparing(Mapper::name)).forEach(sub -> { - SourceLoader.SyntheticFieldLoader subLoader = sub.syntheticFieldLoader(); - if (subLoader != null) { - fields.add(subLoader); - } - }); + List fields = mappers.values() + .stream() + .sorted(Comparator.comparing(Mapper::name)) + .map(Mapper::syntheticFieldLoader) + .filter(l -> l != null) + .toList(); return new SourceLoader.SyntheticFieldLoader() { @Override public Leaf leaf(LeafReader reader) throws IOException { - List leaves = new ArrayList<>(); + List l = new ArrayList<>(); for (SourceLoader.SyntheticFieldLoader field : fields) { - leaves.add(field.leaf(reader)); + Leaf leaf = field.leaf(reader); + if (false == leaf.empty()) { + l.add(leaf); + } } + SourceLoader.SyntheticFieldLoader.Leaf[] leaves = l.toArray(SourceLoader.SyntheticFieldLoader.Leaf[]::new); return new SourceLoader.SyntheticFieldLoader.Leaf() { @Override - public void advanceToDoc(int docId) throws IOException { - for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - leaf.advanceToDoc(docId); - } + public boolean empty() { + return leaves.length == 0; } @Override - public boolean hasValue() { + public void advanceToDoc(int docId) throws IOException { for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - if (leaf.hasValue()) { - return true; - } + leaf.advanceToDoc(docId); } - return false; } @Override - public void load(XContentBuilder b) throws IOException { - boolean started = false; - for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - if (leaf.hasValue()) { - if (false == started) { - started = true; - startSyntheticField(b); + public void load(XContentBuilder b, CheckedRunnable before) throws IOException { + class HasValue implements CheckedRunnable { + boolean hasValue; + + @Override + public void run() throws IOException { + if (hasValue) { + return; } - leaf.load(b); + hasValue = true; + before.run(); + startSyntheticField(b); } } - if (started) { + HasValue hasValue = new HasValue(); + for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { + leaf.load(b, hasValue); + } + if (hasValue.hasValue) { b.endObject(); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java index 89a4638d66a79..6604ffb35791d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java @@ -10,6 +10,7 @@ import org.apache.lucene.index.LeafReader; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.fieldvisitor.FieldsVisitor; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; @@ -82,15 +83,34 @@ public boolean reordersFieldValues() { @Override public Leaf leaf(LeafReader reader) throws IOException { SyntheticFieldLoader.Leaf leaf = loader.leaf(reader); + if (leaf.empty()) { + return new Leaf() { + @Override + public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { + // TODO accept a requested xcontent type + try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { + return BytesReference.bytes(b.startObject().endObject()); + } + } + }; + } return new Leaf() { @Override public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { + class HasValue implements CheckedRunnable { + boolean hasValue; + + @Override + public void run() throws IOException { + hasValue = true; + } + } + HasValue hasValue = new HasValue(); // TODO accept a requested xcontent type try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { leaf.advanceToDoc(docId); - if (leaf.hasValue()) { - leaf.load(b); - } else { + leaf.load(b, hasValue); + if (hasValue.hasValue == false) { b.startObject().endObject(); } return BytesReference.bytes(b); @@ -104,20 +124,17 @@ public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOEx * Load a field for {@link Synthetic}. */ interface SyntheticFieldLoader { - /** - * Load no values. - */ SyntheticFieldLoader NOTHING = r -> new Leaf() { @Override - public void advanceToDoc(int docId) throws IOException {} + public boolean empty() { + return true; + } @Override - public boolean hasValue() { - return false; - } + public void advanceToDoc(int docId) throws IOException {} @Override - public void load(XContentBuilder b) throws IOException {} + public void load(XContentBuilder b, CheckedRunnable before) throws IOException {} }; /** @@ -130,19 +147,19 @@ public void load(XContentBuilder b) throws IOException {} */ interface Leaf { /** - * Position the loader at a document. + * Is this entirely empty? */ - void advanceToDoc(int docId) throws IOException; + boolean empty(); /** - * Is there a value for this field in this document? + * Position the loader at a document. */ - boolean hasValue(); + void advanceToDoc(int docId) throws IOException; /** * Load values for this document. */ - void load(XContentBuilder b) throws IOException; + void load(XContentBuilder b, CheckedRunnable before) throws IOException; } } From cbf22e5fefb60402de740039e64329cbbb788476 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 21 Jun 2022 10:03:03 -0400 Subject: [PATCH 2/6] Big comment --- .../index/mapper/NumberFieldMapper.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index f5ca11478e74d..f447a500f15e4 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -1631,18 +1631,6 @@ protected NumericSyntheticFieldLoader(String name, String simpleName) { this.simpleName = simpleName; } - private SortedNumericDocValues dv(LeafReader reader) throws IOException { - SortedNumericDocValues dv = reader.getSortedNumericDocValues(name); - if (dv != null) { - return dv; - } - NumericDocValues single = reader.getNumericDocValues(name); - if (single != null) { - return DocValues.singleton(single); - } - return null; - } - @Override public Leaf leaf(LeafReader reader) throws IOException { SortedNumericDocValues leaf = dv(reader); @@ -1683,5 +1671,23 @@ public void load(XContentBuilder b, CheckedRunnable before) throws } protected abstract void loadNextValue(XContentBuilder b, long value) throws IOException; + + /** + * Returns a {@link SortedNumericDocValues} or null if it doesn't have any doc values. + * See {@link DocValues#getSortedNumeric} which is *nearly* the same, but it returns + * an "empty" implementation if there aren't any doc values. We need to be able to + * tell if there aren't any and return our empty leaf source loader. + */ + private SortedNumericDocValues dv(LeafReader reader) throws IOException { + SortedNumericDocValues dv = reader.getSortedNumericDocValues(name); + if (dv != null) { + return dv; + } + NumericDocValues single = reader.getNumericDocValues(name); + if (single != null) { + return DocValues.singleton(single); + } + return null; + } } } From ca099ecfb150a7c9edf85c430d94edab8398ef99 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 21 Jun 2022 12:17:18 -0400 Subject: [PATCH 3/6] Changes with Alan --- .../index/mapper/KeywordFieldMapper.java | 8 ++--- .../index/mapper/NumberFieldMapper.java | 8 ++--- .../index/mapper/ObjectMapper.java | 34 +++++++------------ .../index/mapper/SourceLoader.java | 26 +++++--------- 4 files changed, 28 insertions(+), 48 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index 50115b54f0461..70667faf01e49 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -41,7 +41,6 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.search.AutomatonQueries; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -1091,16 +1090,15 @@ public boolean empty() { } @Override - public void advanceToDoc(int docId) throws IOException { - hasValue = leaf.advanceExact(docId); + public boolean advanceToDoc(int docId) throws IOException { + return hasValue = leaf.advanceExact(docId); } @Override - public void load(XContentBuilder b, CheckedRunnable before) throws IOException { + public void load(XContentBuilder b) throws IOException { if (false == hasValue) { return; } - before.run(); long first = leaf.nextOrd(); long next = leaf.nextOrd(); if (next == SortedSetDocValues.NO_MORE_ORDS) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index f447a500f15e4..642727a325d9f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -33,7 +33,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType; import org.elasticsearch.index.fielddata.plain.SortedDoublesIndexFieldData; @@ -1646,16 +1645,15 @@ public boolean empty() { } @Override - public void advanceToDoc(int docId) throws IOException { - hasValue = leaf.advanceExact(docId); + public boolean advanceToDoc(int docId) throws IOException { + return hasValue = leaf.advanceExact(docId); } @Override - public void load(XContentBuilder b, CheckedRunnable before) throws IOException { + public void load(XContentBuilder b) throws IOException { if (false == hasValue) { return; } - before.run(); if (leaf.docValueCount() == 1) { b.field(simpleName); loadNextValue(b, leaf.nextValue()); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index 2f752695b32ec..afcd06331df1b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -15,7 +15,6 @@ import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.xcontent.support.XContentMapValues; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -573,40 +572,33 @@ public Leaf leaf(LeafReader reader) throws IOException { } SourceLoader.SyntheticFieldLoader.Leaf[] leaves = l.toArray(SourceLoader.SyntheticFieldLoader.Leaf[]::new); return new SourceLoader.SyntheticFieldLoader.Leaf() { + private boolean hasValue; + @Override public boolean empty() { return leaves.length == 0; } @Override - public void advanceToDoc(int docId) throws IOException { + public boolean advanceToDoc(int docId) throws IOException { + hasValue = false; for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - leaf.advanceToDoc(docId); + boolean leafHasValue = leaf.advanceToDoc(docId); + hasValue |= leafHasValue; } + return hasValue; } @Override - public void load(XContentBuilder b, CheckedRunnable before) throws IOException { - class HasValue implements CheckedRunnable { - boolean hasValue; - - @Override - public void run() throws IOException { - if (hasValue) { - return; - } - hasValue = true; - before.run(); - startSyntheticField(b); - } + public void load(XContentBuilder b) throws IOException { + if (hasValue == false) { + return; } - HasValue hasValue = new HasValue(); + startSyntheticField(b); for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - leaf.load(b, hasValue); - } - if (hasValue.hasValue) { - b.endObject(); + leaf.load(b); } + b.endObject(); } }; } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java index 6604ffb35791d..dac58a05fbe47 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java @@ -10,7 +10,6 @@ import org.apache.lucene.index.LeafReader; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.index.fieldvisitor.FieldsVisitor; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; @@ -97,20 +96,11 @@ public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOEx return new Leaf() { @Override public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { - class HasValue implements CheckedRunnable { - boolean hasValue; - - @Override - public void run() throws IOException { - hasValue = true; - } - } - HasValue hasValue = new HasValue(); // TODO accept a requested xcontent type try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { - leaf.advanceToDoc(docId); - leaf.load(b, hasValue); - if (hasValue.hasValue == false) { + if (leaf.advanceToDoc(docId)) { + leaf.load(b); + } else { b.startObject().endObject(); } return BytesReference.bytes(b); @@ -131,10 +121,12 @@ public boolean empty() { } @Override - public void advanceToDoc(int docId) throws IOException {} + public boolean advanceToDoc(int docId) throws IOException { + return false; + } @Override - public void load(XContentBuilder b, CheckedRunnable before) throws IOException {} + public void load(XContentBuilder b) throws IOException {} }; /** @@ -154,12 +146,12 @@ interface Leaf { /** * Position the loader at a document. */ - void advanceToDoc(int docId) throws IOException; + boolean advanceToDoc(int docId) throws IOException; /** * Load values for this document. */ - void load(XContentBuilder b, CheckedRunnable before) throws IOException; + void load(XContentBuilder b) throws IOException; } } From 9aebc5afe29ba48235893413fde6e5f2a910e90d Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 21 Jun 2022 13:54:10 -0400 Subject: [PATCH 4/6] Rename method --- .../elasticsearch/index/mapper/KeywordFieldMapper.java | 2 +- .../org/elasticsearch/index/mapper/NumberFieldMapper.java | 2 +- .../java/org/elasticsearch/index/mapper/ObjectMapper.java | 4 ++-- .../java/org/elasticsearch/index/mapper/SourceLoader.java | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index 70667faf01e49..1dfd44d8a3d0d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -1095,7 +1095,7 @@ public boolean advanceToDoc(int docId) throws IOException { } @Override - public void load(XContentBuilder b) throws IOException { + public void write(XContentBuilder b) throws IOException { if (false == hasValue) { return; } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index 642727a325d9f..5b47d43fca80b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -1650,7 +1650,7 @@ public boolean advanceToDoc(int docId) throws IOException { } @Override - public void load(XContentBuilder b) throws IOException { + public void write(XContentBuilder b) throws IOException { if (false == hasValue) { return; } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index afcd06331df1b..e98ccda06e352 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -590,13 +590,13 @@ public boolean advanceToDoc(int docId) throws IOException { } @Override - public void load(XContentBuilder b) throws IOException { + public void write(XContentBuilder b) throws IOException { if (hasValue == false) { return; } startSyntheticField(b); for (SourceLoader.SyntheticFieldLoader.Leaf leaf : leaves) { - leaf.load(b); + leaf.write(b); } b.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java index dac58a05fbe47..e6b468f275943 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java @@ -99,7 +99,7 @@ public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOEx // TODO accept a requested xcontent type try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { if (leaf.advanceToDoc(docId)) { - leaf.load(b); + leaf.write(b); } else { b.startObject().endObject(); } @@ -126,7 +126,7 @@ public boolean advanceToDoc(int docId) throws IOException { } @Override - public void load(XContentBuilder b) throws IOException {} + public void write(XContentBuilder b) throws IOException {} }; /** @@ -149,9 +149,9 @@ interface Leaf { boolean advanceToDoc(int docId) throws IOException; /** - * Load values for this document. + * Write values for this document. */ - void load(XContentBuilder b) throws IOException; + void write(XContentBuilder b) throws IOException; } } From e772fbf972df4a61fe659ef826ed2c427d8a10ee Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 22 Jun 2022 08:32:39 -0400 Subject: [PATCH 5/6] Fixup --- .../index/mapper/SourceLoader.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java index e6b468f275943..ab4dd5534f017 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java @@ -42,6 +42,15 @@ interface Leaf { * @param docId the doc to load */ BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException; + + Leaf EMPTY_OBJECT = new Leaf() { + @Override public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { + // TODO accept a requested xcontent type + try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { + return BytesReference.bytes(b.startObject().endObject()); + } + } + }; } /** @@ -83,15 +92,7 @@ public boolean reordersFieldValues() { public Leaf leaf(LeafReader reader) throws IOException { SyntheticFieldLoader.Leaf leaf = loader.leaf(reader); if (leaf.empty()) { - return new Leaf() { - @Override - public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { - // TODO accept a requested xcontent type - try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { - return BytesReference.bytes(b.startObject().endObject()); - } - } - }; + return Leaf.EMPTY_OBJECT; } return new Leaf() { @Override @@ -114,6 +115,9 @@ public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOEx * Load a field for {@link Synthetic}. */ interface SyntheticFieldLoader { + /** + * Load no values. + */ SyntheticFieldLoader NOTHING = r -> new Leaf() { @Override public boolean empty() { From 9762bd744ae575d4912310d453d8676f01ce7ad9 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 22 Jun 2022 08:58:07 -0400 Subject: [PATCH 6/6] Format --- .../main/java/org/elasticsearch/index/mapper/SourceLoader.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java index ab4dd5534f017..03e41a45b7a9f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceLoader.java @@ -44,7 +44,8 @@ interface Leaf { BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException; Leaf EMPTY_OBJECT = new Leaf() { - @Override public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { + @Override + public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException { // TODO accept a requested xcontent type try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) { return BytesReference.bytes(b.startObject().endObject());