Skip to content

Commit 18d4b5b

Browse files
committed
Merge some equivalent interfaces (#22816)
Remove `FromXContent` and use `CheckedFunction` instead. Remove `FromXContentWithContext` and use `ContentParser` instead.
1 parent 74dcd73 commit 18d4b5b

15 files changed

+56
-70
lines changed

core/src/main/java/org/elasticsearch/common/network/NetworkModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommand;
2727
import org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand;
2828
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;
29+
import org.elasticsearch.common.CheckedFunction;
2930
import org.elasticsearch.common.ParseField;
3031
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
3132
import org.elasticsearch.common.io.stream.Writeable;
@@ -34,7 +35,7 @@
3435
import org.elasticsearch.common.settings.Settings;
3536
import org.elasticsearch.common.util.BigArrays;
3637
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
37-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.FromXContent;
38+
import org.elasticsearch.common.xcontent.XContentParser;
3839
import org.elasticsearch.http.HttpServerTransport;
3940
import org.elasticsearch.indices.breaker.CircuitBreakerService;
4041
import org.elasticsearch.plugins.NetworkPlugin;
@@ -47,6 +48,7 @@
4748
import org.elasticsearch.transport.TransportRequestHandler;
4849
import org.elasticsearch.transport.local.LocalTransport;
4950

51+
import java.io.IOException;
5052
import java.util.ArrayList;
5153
import java.util.Arrays;
5254
import java.util.Collections;
@@ -168,7 +170,7 @@ private void registerHttpTransport(String key, Supplier<HttpServerTransport> fac
168170
* it is the name under which the command's reader is registered.
169171
*/
170172
private static <T extends AllocationCommand> void registerAllocationCommand(Writeable.Reader<T> reader,
171-
FromXContent<T> parser, ParseField commandName) {
173+
CheckedFunction<XContentParser, T, IOException> parser, ParseField commandName) {
172174
namedXContents.add(new NamedXContentRegistry.Entry(AllocationCommand.class, commandName, parser));
173175
namedWriteables.add(new NamedWriteableRegistry.Entry(AllocationCommand.class, commandName.getPreferredName(), reader));
174176
}

core/src/main/java/org/elasticsearch/common/xcontent/AbstractObjectParser.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22+
import org.elasticsearch.common.CheckedFunction;
2223
import org.elasticsearch.common.ParseField;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
@@ -36,26 +37,19 @@
3637
public abstract class AbstractObjectParser<Value, Context>
3738
implements BiFunction<XContentParser, Context, Value>, ContextParser<Context, Value> {
3839

39-
/**
40-
* Reads an object right from the parser without any context.
41-
*/
42-
@FunctionalInterface
43-
public interface NoContextParser<T> {
44-
T parse(XContentParser p) throws IOException;
45-
}
46-
4740
/**
4841
* Declare some field. Usually it is easier to use {@link #declareString(BiConsumer, ParseField)} or
4942
* {@link #declareObject(BiConsumer, ContextParser, ParseField)} rather than call this directly.
5043
*/
5144
public abstract <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Context, T> parser, ParseField parseField,
5245
ValueType type);
5346

54-
public <T> void declareField(BiConsumer<Value, T> consumer, NoContextParser<T> parser, ParseField parseField, ValueType type) {
47+
public <T> void declareField(BiConsumer<Value, T> consumer, CheckedFunction<XContentParser, T, IOException> parser,
48+
ParseField parseField, ValueType type) {
5549
if (parser == null) {
5650
throw new IllegalArgumentException("[parser] is required");
5751
}
58-
declareField(consumer, (p, c) -> parser.parse(p), parseField, type);
52+
declareField(consumer, (p, c) -> parser.apply(p), parseField, type);
5953
}
6054

6155
public <T> void declareObject(BiConsumer<Value, T> consumer, ContextParser<Context, T> objectParser, ParseField field) {
@@ -121,7 +115,7 @@ public void declareIntArray(BiConsumer<Value, List<Integer>> consumer, ParseFiel
121115
}
122116

123117
public void declareRawObject(BiConsumer<Value, BytesReference> consumer, ParseField field) {
124-
NoContextParser<BytesReference> bytesParser = p -> {
118+
CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> {
125119
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
126120
builder.prettyPrint();
127121
builder.copyCurrentStructure(p);

core/src/main/java/org/elasticsearch/common/xcontent/NamedXContentRegistry.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.common.xcontent;
2121

2222
import org.elasticsearch.ElasticsearchException;
23+
import org.elasticsearch.common.CheckedFunction;
2324
import org.elasticsearch.common.ParseField;
2425
import org.elasticsearch.common.ParsingException;
2526
import org.elasticsearch.common.io.stream.StreamInput;
@@ -47,25 +48,6 @@ public class NamedXContentRegistry {
4748
*/
4849
public static final NamedXContentRegistry EMPTY = new NamedXContentRegistry(emptyList());
4950

50-
/**
51-
* Parses an object with the type T from parser.
52-
*/
53-
public interface FromXContent<T> {
54-
/**
55-
* Parses an object with the type T from parser.
56-
*/
57-
T fromXContent(XContentParser parser) throws IOException;
58-
}
59-
60-
/**
61-
* Parses an object with the type T from parser.
62-
* @deprecated prefer {@link FromXContent} if possible
63-
*/
64-
@Deprecated
65-
public interface FromXContentWithContext<T> {
66-
T fromXContent(XContentParser parser, Object context) throws IOException;
67-
}
68-
6951
/**
7052
* An entry in the {@linkplain NamedXContentRegistry} containing the name of the object and the parser that can parse it.
7153
*/
@@ -77,20 +59,20 @@ public static class Entry {
7759
public final ParseField name;
7860

7961
/** A parser capability of parser the entry's class. */
80-
private final FromXContentWithContext<?> parser;
62+
private final ContextParser<Object, ?> parser;
8163

8264
/** Creates a new entry which can be stored by the registry. */
83-
public <T> Entry(Class<T> categoryClass, ParseField name, FromXContent<? extends T> parser) {
65+
public <T> Entry(Class<T> categoryClass, ParseField name, CheckedFunction<XContentParser, ? extends T, IOException> parser) {
8466
this.categoryClass = Objects.requireNonNull(categoryClass);
8567
this.name = Objects.requireNonNull(name);
86-
this.parser = Objects.requireNonNull((p, c) -> parser.fromXContent(p));
68+
this.parser = Objects.requireNonNull((p, c) -> parser.apply(p));
8769
}
8870
/**
8971
* Creates a new entry which can be stored by the registry.
90-
* @deprecated prefer {@link Entry#Entry(Class, ParseField, FromXContent)}. Contexts will be removed when possible
72+
* @deprecated prefer {@link Entry#Entry(Class, ParseField, CheckedFunction)}. Contexts will be removed when possible
9173
*/
9274
@Deprecated
93-
public <T> Entry(Class<T> categoryClass, ParseField name, FromXContentWithContext<? extends T> parser) {
75+
public <T> Entry(Class<T> categoryClass, ParseField name, ContextParser<Object, ? extends T> parser) {
9476
this.categoryClass = Objects.requireNonNull(categoryClass);
9577
this.name = Objects.requireNonNull(name);
9678
this.parser = Objects.requireNonNull(parser);
@@ -159,7 +141,7 @@ public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentPa
159141
throw new ParsingException(parser.getTokenLocation(),
160142
"Unknown " + categoryClass.getSimpleName() + " [" + name + "]: Parser didn't match");
161143
}
162-
return categoryClass.cast(entry.parser.fromXContent(parser, context));
144+
return categoryClass.cast(entry.parser.parse(parser, context));
163145
}
164146

165147
/**

core/src/main/java/org/elasticsearch/plugins/SearchPlugin.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import org.apache.lucene.search.Query;
2323
import org.elasticsearch.action.search.SearchRequest;
2424
import org.elasticsearch.action.search.SearchResponse;
25+
import org.elasticsearch.common.CheckedFunction;
2526
import org.elasticsearch.common.ParseField;
2627
import org.elasticsearch.common.io.stream.NamedWriteable;
2728
import org.elasticsearch.common.io.stream.StreamInput;
2829
import org.elasticsearch.common.io.stream.Writeable;
2930
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
30-
import org.elasticsearch.common.xcontent.AbstractObjectParser.NoContextParser;
3131
import org.elasticsearch.common.xcontent.XContent;
32+
import org.elasticsearch.common.xcontent.XContentParser;
3233
import org.elasticsearch.index.query.QueryBuilder;
3334
import org.elasticsearch.index.query.QueryParser;
3435
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
@@ -50,6 +51,7 @@
5051
import org.elasticsearch.search.suggest.Suggester;
5152
import org.elasticsearch.search.suggest.SuggestionBuilder;
5253

54+
import java.io.IOException;
5355
import java.util.List;
5456
import java.util.Map;
5557
import java.util.TreeMap;
@@ -150,7 +152,7 @@ public ScoreFunctionSpec(String name, Writeable.Reader<T> reader, ScoreFunctionP
150152
/**
151153
* Specification for a {@link Suggester}.
152154
*/
153-
class SuggesterSpec<T extends SuggestionBuilder<T>> extends SearchExtensionSpec<T, NoContextParser<T>> {
155+
class SuggesterSpec<T extends SuggestionBuilder<T>> extends SearchExtensionSpec<T, CheckedFunction<XContentParser, T, IOException>> {
154156
/**
155157
* Specification of custom {@link Suggester}.
156158
*
@@ -161,7 +163,7 @@ class SuggesterSpec<T extends SuggestionBuilder<T>> extends SearchExtensionSpec<
161163
* {@link StreamInput}
162164
* @param parser the parser the reads the query suggester from xcontent
163165
*/
164-
public SuggesterSpec(ParseField name, Writeable.Reader<T> reader, NoContextParser<T> parser) {
166+
public SuggesterSpec(ParseField name, Writeable.Reader<T> reader, CheckedFunction<XContentParser, T, IOException> parser) {
165167
super(name, reader, parser);
166168
}
167169

@@ -174,7 +176,7 @@ public SuggesterSpec(ParseField name, Writeable.Reader<T> reader, NoContextParse
174176
* {@link StreamInput}
175177
* @param parser the parser the reads the suggester builder from xcontent
176178
*/
177-
public SuggesterSpec(String name, Writeable.Reader<T> reader, NoContextParser<T> parser) {
179+
public SuggesterSpec(String name, Writeable.Reader<T> reader, CheckedFunction<XContentParser, T, IOException> parser) {
178180
super(name, reader, parser);
179181
}
180182
}
@@ -347,12 +349,13 @@ public Map<String, Writeable.Reader<? extends InternalAggregation>> getResultRea
347349
* Specification for a {@link SearchExtBuilder} which represents an additional section that can be
348350
* parsed in a search request (within the ext element).
349351
*/
350-
class SearchExtSpec<T extends SearchExtBuilder> extends SearchExtensionSpec<T, NoContextParser<T>> {
351-
public SearchExtSpec(ParseField name, Writeable.Reader<? extends T> reader, NoContextParser<T> parser) {
352+
class SearchExtSpec<T extends SearchExtBuilder> extends SearchExtensionSpec<T, CheckedFunction<XContentParser, T, IOException>> {
353+
public SearchExtSpec(ParseField name, Writeable.Reader<? extends T> reader,
354+
CheckedFunction<XContentParser, T, IOException> parser) {
352355
super(name, reader, parser);
353356
}
354357

355-
public SearchExtSpec(String name, Writeable.Reader<? extends T> reader, NoContextParser<T> parser) {
358+
public SearchExtSpec(String name, Writeable.Reader<? extends T> reader, CheckedFunction<XContentParser, T, IOException> parser) {
356359
super(name, reader, parser);
357360
}
358361
}

core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreFormat.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
package org.elasticsearch.repositories.blobstore;
2020

2121
import org.elasticsearch.cluster.metadata.MetaData;
22+
import org.elasticsearch.common.CheckedFunction;
2223
import org.elasticsearch.common.blobstore.BlobContainer;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
25-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.FromXContent;
2626
import org.elasticsearch.common.xcontent.ToXContent;
2727
import org.elasticsearch.common.xcontent.XContentHelper;
2828
import org.elasticsearch.common.xcontent.XContentParser;
@@ -40,7 +40,7 @@ public abstract class BlobStoreFormat<T extends ToXContent> {
4040

4141
protected final String blobNameFormat;
4242

43-
protected final FromXContent<T> reader;
43+
protected final CheckedFunction<XContentParser, T, IOException> reader;
4444

4545
protected final NamedXContentRegistry namedXContentRegistry;
4646

@@ -61,7 +61,8 @@ public abstract class BlobStoreFormat<T extends ToXContent> {
6161
* @param blobNameFormat format of the blobname in {@link String#format(Locale, String, Object...)} format
6262
* @param reader the prototype object that can deserialize objects with type T
6363
*/
64-
protected BlobStoreFormat(String blobNameFormat, FromXContent<T> reader, NamedXContentRegistry namedXContentRegistry) {
64+
protected BlobStoreFormat(String blobNameFormat, CheckedFunction<XContentParser, T, IOException> reader,
65+
NamedXContentRegistry namedXContentRegistry) {
6566
this.reader = reader;
6667
this.blobNameFormat = blobNameFormat;
6768
this.namedXContentRegistry = namedXContentRegistry;
@@ -109,7 +110,7 @@ protected String blobName(String name) {
109110

110111
protected T read(BytesReference bytes) throws IOException {
111112
try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, bytes)) {
112-
T obj = reader.fromXContent(parser);
113+
T obj = reader.apply(parser);
113114
return obj;
114115
}
115116
}

core/src/main/java/org/elasticsearch/repositories/blobstore/ChecksumBlobStoreFormat.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.index.IndexFormatTooNewException;
2424
import org.apache.lucene.index.IndexFormatTooOldException;
2525
import org.apache.lucene.store.OutputStreamIndexOutput;
26+
import org.elasticsearch.common.CheckedFunction;
2627
import org.elasticsearch.common.blobstore.BlobContainer;
2728
import org.elasticsearch.common.bytes.BytesArray;
2829
import org.elasticsearch.common.bytes.BytesReference;
@@ -33,10 +34,10 @@
3334
import org.elasticsearch.common.lucene.store.ByteArrayIndexInput;
3435
import org.elasticsearch.common.lucene.store.IndexOutputOutputStream;
3536
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
36-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.FromXContent;
3737
import org.elasticsearch.common.xcontent.ToXContent;
3838
import org.elasticsearch.common.xcontent.XContentBuilder;
3939
import org.elasticsearch.common.xcontent.XContentFactory;
40+
import org.elasticsearch.common.xcontent.XContentParser;
4041
import org.elasticsearch.common.xcontent.XContentType;
4142
import org.elasticsearch.gateway.CorruptStateException;
4243

@@ -73,7 +74,7 @@ public class ChecksumBlobStoreFormat<T extends ToXContent> extends BlobStoreForm
7374
* @param compress true if the content should be compressed
7475
* @param xContentType content type that should be used for write operations
7576
*/
76-
public ChecksumBlobStoreFormat(String codec, String blobNameFormat, FromXContent<T> reader,
77+
public ChecksumBlobStoreFormat(String codec, String blobNameFormat, CheckedFunction<XContentParser, T, IOException> reader,
7778
NamedXContentRegistry namedXContentRegistry, boolean compress, XContentType xContentType) {
7879
super(blobNameFormat, reader, namedXContentRegistry);
7980
this.xContentType = xContentType;
@@ -87,7 +88,7 @@ public ChecksumBlobStoreFormat(String codec, String blobNameFormat, FromXContent
8788
* @param reader prototype object that can deserialize T from XContent
8889
* @param compress true if the content should be compressed
8990
*/
90-
public ChecksumBlobStoreFormat(String codec, String blobNameFormat, FromXContent<T> reader,
91+
public ChecksumBlobStoreFormat(String codec, String blobNameFormat, CheckedFunction<XContentParser, T, IOException> reader,
9192
NamedXContentRegistry namedXContentRegistry, boolean compress) {
9293
this(codec, blobNameFormat, reader, namedXContentRegistry, compress, DEFAULT_X_CONTENT_TYPE);
9394
}

core/src/main/java/org/elasticsearch/repositories/blobstore/LegacyBlobStoreFormat.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
*/
1919
package org.elasticsearch.repositories.blobstore;
2020

21+
import org.elasticsearch.common.CheckedFunction;
2122
import org.elasticsearch.common.blobstore.BlobContainer;
2223
import org.elasticsearch.common.io.Streams;
2324
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2425
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2526
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.XContentParser;
2628

2729
import java.io.IOException;
2830
import java.io.InputStream;
@@ -36,7 +38,7 @@ public class LegacyBlobStoreFormat<T extends ToXContent> extends BlobStoreFormat
3638
* @param blobNameFormat format of the blobname in {@link String#format} format
3739
* @param reader the prototype object that can deserialize objects with type T
3840
*/
39-
public LegacyBlobStoreFormat(String blobNameFormat, NamedXContentRegistry.FromXContent<T> reader,
41+
public LegacyBlobStoreFormat(String blobNameFormat, CheckedFunction<XContentParser, T, IOException> reader,
4042
NamedXContentRegistry namedXContentRegistry) {
4143
super(blobNameFormat, reader, namedXContentRegistry);
4244
}

core/src/main/java/org/elasticsearch/search/SearchExtBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
package org.elasticsearch.search;
2121

22+
import org.elasticsearch.common.CheckedFunction;
2223
import org.elasticsearch.common.io.stream.NamedWriteable;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.io.stream.StreamOutput;
2526
import org.elasticsearch.common.io.stream.Writeable;
2627
import org.elasticsearch.common.xcontent.ToXContent;
27-
import org.elasticsearch.common.xcontent.AbstractObjectParser.NoContextParser;
2828
import org.elasticsearch.plugins.SearchPlugin;
2929
import org.elasticsearch.plugins.SearchPlugin.SearchExtSpec;
3030

@@ -35,7 +35,7 @@
3535
* read from the incoming stream, usually done adding a constructor that takes {@link StreamInput} as
3636
* an argument.
3737
*
38-
* Registration happens through {@link SearchPlugin#getSearchExts()}, which also needs a {@link NoContextParser} that's able to parse
38+
* Registration happens through {@link SearchPlugin#getSearchExts()}, which also needs a {@link CheckedFunction} that's able to parse
3939
* the incoming request from the REST layer into the proper {@link SearchExtBuilder} subclass.
4040
*
4141
* {@link #getWriteableName()} must return the same name as the one used for the registration

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,8 @@ private void registerSuggesters(List<SearchPlugin> plugins) {
594594
private void registerSuggester(SuggesterSpec<?> suggester) {
595595
namedWriteables.add(new NamedWriteableRegistry.Entry(
596596
SuggestionBuilder.class, suggester.getName().getPreferredName(), suggester.getReader()));
597-
// TODO Merge NoContextParser and FromXContent
598597
namedXContents.add(new NamedXContentRegistry.Entry(SuggestionBuilder.class, suggester.getName(),
599-
p -> suggester.getParser().parse(p)));
598+
suggester.getParser()));
600599
}
601600

602601
private Map<String, Highlighter> setupHighlighters(Settings settings, List<SearchPlugin> plugins) {
@@ -715,8 +714,7 @@ private void registerSearchExts(List<SearchPlugin> plugins) {
715714
}
716715

717716
private void registerSearchExt(SearchExtSpec<?> spec) {
718-
// TODO merge NoContextParser and ToXContent
719-
namedXContents.add(new NamedXContentRegistry.Entry(SearchExtBuilder.class, spec.getName(), p -> spec.getParser().parse(p)));
717+
namedXContents.add(new NamedXContentRegistry.Entry(SearchExtBuilder.class, spec.getName(), spec.getParser()));
720718
namedWriteables.add(new NamedWriteableRegistry.Entry(SearchExtBuilder.class, spec.getName().getPreferredName(), spec.getReader()));
721719
}
722720

0 commit comments

Comments
 (0)