Skip to content

Commit 65db2f5

Browse files
committed
refactor and remove assert
1 parent 32bb3a5 commit 65db2f5

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
192192
*/
193193
protected final LeafFactory leafFactory(SearchLookup searchLookup) {
194194
String include = name();
195-
var copy = searchLookup.maybeCopyWithSourceFilter(new SourceFilter(new String[] { include }, new String[0]));
195+
var copy = searchLookup.optimizedSourceProvider(new SourceFilter(new String[] { include }, new String[0]));
196196
return factory.apply(copy);
197197
}
198198

server/src/main/java/org/elasticsearch/search/lookup/ConcurrentSegmentSourceProvider.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,12 @@ class ConcurrentSegmentSourceProvider implements SourceProvider {
4444
}
4545

4646
private ConcurrentSegmentSourceProvider(ConcurrentSegmentSourceProvider source, SourceFilter filter) {
47+
assert source.isStoredSource == false;
4748
this.sourceLoaderProvider = source.sourceLoaderProvider;
4849
this.isStoredSource = source.isStoredSource;
49-
if (isStoredSource) {
50-
this.sourceLoader = source.sourceLoader;
51-
this.storedFieldLoader = source.storedFieldLoader;
52-
} else {
53-
this.sourceLoader = source.sourceLoaderProvider.apply(filter);
54-
// Also re-initialize stored field loader:
55-
this.storedFieldLoader = StoredFieldLoader.create(isStoredSource, sourceLoader.requiredStoredFields(), true);
56-
}
50+
this.sourceLoader = source.sourceLoaderProvider.apply(filter);
51+
// Also re-initialize stored field loader:
52+
this.storedFieldLoader = StoredFieldLoader.create(isStoredSource, sourceLoader.requiredStoredFields(), true);
5753
}
5854

5955
@Override
@@ -77,9 +73,13 @@ public Source getSource(LeafReaderContext ctx, int doc) throws IOException {
7773
}
7874

7975
@Override
80-
public SourceProvider maybeCopyWithSourceFilter(SourceFilter sourceFilter) {
76+
public SourceProvider optimizedSourceProvider(SourceFilter sourceFilter) {
8177
assert leaves.isEmpty() : "source provider must be unused when applying filter";
82-
return new ConcurrentSegmentSourceProvider(this, sourceFilter);
78+
if (isStoredSource) {
79+
return this;
80+
} else {
81+
return new ConcurrentSegmentSourceProvider(this, sourceFilter);
82+
}
8383
}
8484

8585
private static class Leaf implements SourceProvider {

server/src/main/java/org/elasticsearch/search/lookup/SearchLookup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public Source getSource(LeafReaderContext ctx, int doc) throws IOException {
153153
return sourceProvider.getSource(ctx, doc);
154154
}
155155

156-
public SearchLookup maybeCopyWithSourceFilter(SourceFilter sourceFilter) {
157-
SourceProvider copy = sourceProvider.maybeCopyWithSourceFilter(sourceFilter);
156+
public SearchLookup optimizedSourceProvider(SourceFilter sourceFilter) {
157+
SourceProvider copy = sourceProvider.optimizedSourceProvider(sourceFilter);
158158
return new SearchLookup(this, copy);
159159
}
160160
}

server/src/main/java/org/elasticsearch/search/lookup/SourceProvider.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ static SourceProvider fromLookup(MappingLookup lookup, SourceFilter filter, Sour
3737
return new ConcurrentSegmentSourceProvider(lookup, filter, metrics);
3838
}
3939

40-
default SourceProvider maybeCopyWithSourceFilter(SourceFilter sourceFilter) {
41-
assert false : "should not be invoked";
40+
/**
41+
* Optionally returns a new {@link SourceProvider} that is more optimized to load source with the provided source filter in mind.
42+
* <p>
43+
* Currently, if source mode is synthetic, and only a subset of fields are requested, then only loading source for requested fields
44+
* is much more efficient.
45+
*
46+
* @param sourceFilter The part of the source caller is actually interested in.
47+
* @return a new instance if source can be loaded in optimal a more optimal way, otherwise returns this instance.
48+
*/
49+
default SourceProvider optimizedSourceProvider(SourceFilter sourceFilter) {
4250
return this;
4351
}
4452
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plugin/ReinitializingSourceProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Source getSource(LeafReaderContext ctx, int doc) throws IOException {
5353
}
5454

5555
@Override
56-
public SourceProvider maybeCopyWithSourceFilter(SourceFilter sourceFilter) {
56+
public SourceProvider optimizedSourceProvider(SourceFilter sourceFilter) {
5757
assert perThreadProvider == null : "source provider must be unused when applying filter";
5858
return new ReinitializingSourceProvider(sourceFilter, sourceProviderFactory);
5959
}

0 commit comments

Comments
 (0)