Skip to content

Commit b108530

Browse files
kkrik-eselasticsearchmachine
andauthored
Lazy initialization for SyntheticSourceSupport.loader() (elastic#120896) (elastic#120915)
* Lazy initialization for `SyntheticSourceSupport.loader()` * [CI] Auto commit changes from spotless * add missing --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 37fa39d commit b108530

File tree

29 files changed

+159
-153
lines changed

29 files changed

+159
-153
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,13 @@ public MatchOnlyTextFieldType fieldType() {
463463

464464
@Override
465465
protected SyntheticSourceSupport syntheticSourceSupport() {
466-
var loader = new StringStoredFieldFieldLoader(fieldType().storedFieldNameForSyntheticSource(), fieldType().name(), leafName()) {
467-
@Override
468-
protected void write(XContentBuilder b, Object value) throws IOException {
469-
b.value((String) value);
466+
return new SyntheticSourceSupport.Native(
467+
() -> new StringStoredFieldFieldLoader(fieldType().storedFieldNameForSyntheticSource(), fieldType().name(), leafName()) {
468+
@Override
469+
protected void write(XContentBuilder b, Object value) throws IOException {
470+
b.value((String) value);
471+
}
470472
}
471-
};
472-
473-
return new SyntheticSourceSupport.Native(loader);
473+
);
474474
}
475475
}

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,14 @@ public int docValueCount() {
707707
@Override
708708
protected SyntheticSourceSupport syntheticSourceSupport() {
709709
if (hasDocValues) {
710-
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed.value()) {
711-
@Override
712-
protected void writeValue(XContentBuilder b, long value) throws IOException {
713-
b.value(decodeForSyntheticSource(value, scalingFactor));
710+
return new SyntheticSourceSupport.Native(
711+
() -> new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed.value()) {
712+
@Override
713+
protected void writeValue(XContentBuilder b, long value) throws IOException {
714+
b.value(decodeForSyntheticSource(value, scalingFactor));
715+
}
714716
}
715-
};
716-
717-
return new SyntheticSourceSupport.Native(loader);
717+
);
718718
}
719719

720720
return super.syntheticSourceSupport();

plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,17 @@ public FieldMapper.Builder getMergeBuilder() {
563563
@Override
564564
protected SyntheticSourceSupport syntheticSourceSupport() {
565565
if (fieldType.stored()) {
566-
var loader = new StringStoredFieldFieldLoader(fullPath(), leafName()) {
566+
return new SyntheticSourceSupport.Native(() -> new StringStoredFieldFieldLoader(fullPath(), leafName()) {
567567
@Override
568568
protected void write(XContentBuilder b, Object value) throws IOException {
569569
b.value((String) value);
570570
}
571-
};
572-
573-
return new SyntheticSourceSupport.Native(loader);
571+
});
574572
}
575573

576574
var kwd = TextFieldMapper.SyntheticSourceHelper.getKeywordFieldMapperForSyntheticSource(this);
577575
if (kwd != null) {
578-
return new SyntheticSourceSupport.Native(kwd.syntheticFieldLoader(fullPath(), leafName()));
576+
return new SyntheticSourceSupport.Native(() -> kwd.syntheticFieldLoader(fullPath(), leafName()));
579577
}
580578

581579
return super.syntheticSourceSupport();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected String contentType() {
195195
@Override
196196
protected SyntheticSourceSupport syntheticSourceSupport() {
197197
if (hasDocValues) {
198-
var loader = new BinaryDocValuesSyntheticFieldLoader(fullPath()) {
198+
return new SyntheticSourceSupport.Native(() -> new BinaryDocValuesSyntheticFieldLoader(fullPath()) {
199199
@Override
200200
protected void writeValue(XContentBuilder b, BytesRef value) throws IOException {
201201
var in = new ByteArrayStreamInput();
@@ -221,9 +221,7 @@ protected void writeValue(XContentBuilder b, BytesRef value) throws IOException
221221
b.endArray();
222222
}
223223
}
224-
};
225-
226-
return new SyntheticSourceSupport.Native(loader);
224+
});
227225
}
228226

229227
return super.syntheticSourceSupport();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,14 @@ protected String contentType() {
552552
@Override
553553
protected SyntheticSourceSupport syntheticSourceSupport() {
554554
if (hasDocValues) {
555-
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed.value()) {
556-
@Override
557-
protected void writeValue(XContentBuilder b, long value) throws IOException {
558-
b.value(value == 1);
555+
return new SyntheticSourceSupport.Native(
556+
() -> new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed.value()) {
557+
@Override
558+
protected void writeValue(XContentBuilder b, long value) throws IOException {
559+
b.value(value == 1);
560+
}
559561
}
560-
};
561-
562-
return new SyntheticSourceSupport.Native(loader);
562+
);
563563
}
564564

565565
return super.syntheticSourceSupport();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,14 +1012,14 @@ public Long getNullValue() {
10121012
@Override
10131013
protected SyntheticSourceSupport syntheticSourceSupport() {
10141014
if (hasDocValues) {
1015-
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed) {
1016-
@Override
1017-
protected void writeValue(XContentBuilder b, long value) throws IOException {
1018-
b.value(fieldType().format(value, fieldType().dateTimeFormatter()));
1015+
return new SyntheticSourceSupport.Native(
1016+
() -> new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed) {
1017+
@Override
1018+
protected void writeValue(XContentBuilder b, long value) throws IOException {
1019+
b.value(fieldType().format(value, fieldType().dateTimeFormatter()));
1020+
}
10191021
}
1020-
};
1021-
1022-
return new SyntheticSourceSupport.Native(loader);
1022+
);
10231023
}
10241024

10251025
return super.syntheticSourceSupport();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static IndexableField field(int count) {
128128

129129
@Override
130130
protected SyntheticSourceSupport syntheticSourceSupport() {
131-
return new SyntheticSourceSupport.Native(new SyntheticFieldLoader());
131+
return new SyntheticSourceSupport.Native(SyntheticFieldLoader::new);
132132
}
133133

134134
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ protected String contentType() {
10271027
protected SyntheticSourceSupport syntheticSourceSupport() {
10281028
// Opt out of fallback synthetic source implementation
10291029
// since there is custom logic in #parseCreateField().
1030-
return new SyntheticSourceSupport.Native(SourceLoader.SyntheticFieldLoader.NOTHING);
1030+
return new SyntheticSourceSupport.Native(() -> SourceLoader.SyntheticFieldLoader.NOTHING);
10311031
}
10321032
};
10331033
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,27 @@ public SourceLoader.SyntheticFieldLoader loader() {
560560

561561
SyntheticSourceSupport FALLBACK = new Fallback();
562562

563-
record Native(SourceLoader.SyntheticFieldLoader loader) implements SyntheticSourceSupport {
563+
final class Native implements SyntheticSourceSupport {
564+
Supplier<SourceLoader.SyntheticFieldLoader> loaderSupplier;
565+
private SourceLoader.SyntheticFieldLoader loader;
566+
567+
@SuppressWarnings("checkstyle:RedundantModifier")
568+
public Native(Supplier<SourceLoader.SyntheticFieldLoader> loaderSupplier) {
569+
this.loaderSupplier = loaderSupplier;
570+
}
571+
564572
@Override
565573
public SyntheticSourceMode mode() {
566574
return SyntheticSourceMode.NATIVE;
567575
}
576+
577+
@Override
578+
public SourceLoader.SyntheticFieldLoader loader() {
579+
if (loader == null) {
580+
loader = loaderSupplier.get();
581+
}
582+
return loader;
583+
}
568584
}
569585

570586
SyntheticSourceMode mode();

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -622,17 +622,17 @@ protected void onMalformedValue(DocumentParserContext context, XContentBuilder m
622622
@Override
623623
protected SyntheticSourceSupport syntheticSourceSupport() {
624624
if (fieldType().hasDocValues()) {
625-
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed()) {
626-
final GeoPoint point = new GeoPoint();
627-
628-
@Override
629-
protected void writeValue(XContentBuilder b, long value) throws IOException {
630-
point.reset(GeoEncodingUtils.decodeLatitude((int) (value >>> 32)), GeoEncodingUtils.decodeLongitude((int) value));
631-
point.toXContent(b, ToXContent.EMPTY_PARAMS);
625+
return new SyntheticSourceSupport.Native(
626+
() -> new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), ignoreMalformed()) {
627+
final GeoPoint point = new GeoPoint();
628+
629+
@Override
630+
protected void writeValue(XContentBuilder b, long value) throws IOException {
631+
point.reset(GeoEncodingUtils.decodeLatitude((int) (value >>> 32)), GeoEncodingUtils.decodeLongitude((int) value));
632+
point.toXContent(b, ToXContent.EMPTY_PARAMS);
633+
}
632634
}
633-
};
634-
635-
return new SyntheticSourceSupport.Native(loader);
635+
);
636636
}
637637

638638
return super.syntheticSourceSupport();

0 commit comments

Comments
 (0)