Skip to content

Commit 8e35324

Browse files
authored
Deduplicate DocValueFormat objects from InternalAggregation when deserializing (#116640) (#116670)
1 parent 7936230 commit 8e35324

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

server/src/main/java/org/elasticsearch/search/DocValueFormat.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.document.InetAddressPoint;
1313
import org.apache.lucene.util.BytesRef;
1414
import org.elasticsearch.TransportVersions;
15+
import org.elasticsearch.common.io.stream.DelayableWriteable;
1516
import org.elasticsearch.common.io.stream.NamedWriteable;
1617
import org.elasticsearch.common.io.stream.StreamInput;
1718
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -260,7 +261,7 @@ private DateTime(DateFormatter formatter, ZoneId timeZone, DateFieldMapper.Resol
260261
this.formatSortValues = formatSortValues;
261262
}
262263

263-
public DateTime(StreamInput in) throws IOException {
264+
private DateTime(StreamInput in) throws IOException {
264265
String formatterPattern = in.readString();
265266
Locale locale = in.getTransportVersion().onOrAfter(TransportVersions.DATE_TIME_DOC_VALUES_LOCALES)
266267
? LocaleUtils.parse(in.readString())
@@ -285,6 +286,14 @@ public String getWriteableName() {
285286
return NAME;
286287
}
287288

289+
public static DateTime readFrom(StreamInput in) throws IOException {
290+
final DateTime dateTime = new DateTime(in);
291+
if (in instanceof DelayableWriteable.Deduplicator d) {
292+
return d.deduplicate(dateTime);
293+
}
294+
return dateTime;
295+
}
296+
288297
@Override
289298
public void writeTo(StreamOutput out) throws IOException {
290299
out.writeString(formatter.pattern());
@@ -528,7 +537,7 @@ public Decimal(String pattern) {
528537
this.format = new DecimalFormat(pattern, SYMBOLS);
529538
}
530539

531-
public Decimal(StreamInput in) throws IOException {
540+
private Decimal(StreamInput in) throws IOException {
532541
this(in.readString());
533542
}
534543

@@ -537,6 +546,14 @@ public String getWriteableName() {
537546
return NAME;
538547
}
539548

549+
public static Decimal readFrom(StreamInput in) throws IOException {
550+
final Decimal decimal = new Decimal(in);
551+
if (in instanceof DelayableWriteable.Deduplicator d) {
552+
return d.deduplicate(decimal);
553+
}
554+
return decimal;
555+
}
556+
540557
@Override
541558
public void writeTo(StreamOutput out) throws IOException {
542559
out.writeString(pattern);

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,8 @@ private void registerScoreFunction(ScoreFunctionSpec<?> scoreFunction) {
10131013

10141014
private void registerValueFormats() {
10151015
registerValueFormat(DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN);
1016-
registerValueFormat(DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new);
1017-
registerValueFormat(DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new);
1016+
registerValueFormat(DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::readFrom);
1017+
registerValueFormat(DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::readFrom);
10181018
registerValueFormat(DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH);
10191019
registerValueFormat(DocValueFormat.GEOTILE.getWriteableName(), in -> DocValueFormat.GEOTILE);
10201020
registerValueFormat(DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP);

server/src/test/java/org/elasticsearch/search/DocValueFormatTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public class DocValueFormatTests extends ESTestCase {
4343
public void testSerialization() throws Exception {
4444
List<Entry> entries = new ArrayList<>();
4545
entries.add(new Entry(DocValueFormat.class, DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN));
46-
entries.add(new Entry(DocValueFormat.class, DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new));
47-
entries.add(new Entry(DocValueFormat.class, DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new));
46+
entries.add(new Entry(DocValueFormat.class, DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::readFrom));
47+
entries.add(new Entry(DocValueFormat.class, DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::readFrom));
4848
entries.add(new Entry(DocValueFormat.class, DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH));
4949
entries.add(new Entry(DocValueFormat.class, DocValueFormat.GEOTILE.getWriteableName(), in -> DocValueFormat.GEOTILE));
5050
entries.add(new Entry(DocValueFormat.class, DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP));

0 commit comments

Comments
 (0)