Skip to content

Commit 309e4a1

Browse files
committed
Cut AnalyzeResponse over to Writeable (#41915)
This commit makes AnalyzeResponse and its various helper classes implement Writeable. The classes are also now immutable. Relates to #34389
1 parent 8e33a52 commit 309e4a1

File tree

6 files changed

+156
-173
lines changed

6 files changed

+156
-173
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.admin.indices.analyze;
2121

2222
import org.elasticsearch.action.Action;
23+
import org.elasticsearch.common.io.stream.Writeable;
2324

2425
public class AnalyzeAction extends Action<AnalyzeResponse> {
2526

@@ -30,8 +31,13 @@ private AnalyzeAction() {
3031
super(NAME);
3132
}
3233

34+
@Override
35+
public Writeable.Reader<AnalyzeResponse> getResponseReader() {
36+
return AnalyzeResponse::new;
37+
}
38+
3339
@Override
3440
public AnalyzeResponse newResponse() {
35-
return new AnalyzeResponse();
41+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
3642
}
3743
}

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeResponse.java

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.elasticsearch.common.Strings;
2424
import org.elasticsearch.common.io.stream.StreamInput;
2525
import org.elasticsearch.common.io.stream.StreamOutput;
26-
import org.elasticsearch.common.io.stream.Streamable;
26+
import org.elasticsearch.common.io.stream.Writeable;
2727
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2828
import org.elasticsearch.common.xcontent.ToXContentObject;
2929
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -43,17 +43,14 @@
4343

4444
public class AnalyzeResponse extends ActionResponse implements Iterable<AnalyzeResponse.AnalyzeToken>, ToXContentObject {
4545

46-
public static class AnalyzeToken implements Streamable, ToXContentObject {
47-
private String term;
48-
private int startOffset;
49-
private int endOffset;
50-
private int position;
51-
private int positionLength = 1;
52-
private Map<String, Object> attributes;
53-
private String type;
54-
55-
AnalyzeToken() {
56-
}
46+
public static class AnalyzeToken implements Writeable, ToXContentObject {
47+
private final String term;
48+
private final int startOffset;
49+
private final int endOffset;
50+
private final int position;
51+
private final int positionLength;
52+
private final Map<String, Object> attributes;
53+
private final String type;
5754

5855
@Override
5956
public boolean equals(Object o) {
@@ -85,6 +82,21 @@ public AnalyzeToken(String term, int position, int startOffset, int endOffset, i
8582
this.attributes = attributes;
8683
}
8784

85+
public AnalyzeToken(StreamInput in) throws IOException {
86+
term = in.readString();
87+
startOffset = in.readInt();
88+
endOffset = in.readInt();
89+
position = in.readVInt();
90+
Integer len = in.readOptionalVInt();
91+
if (len != null) {
92+
positionLength = len;
93+
} else {
94+
positionLength = 1;
95+
}
96+
type = in.readOptionalString();
97+
attributes = in.readMap();
98+
}
99+
88100
public String getTerm() {
89101
return this.term;
90102
}
@@ -134,12 +146,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
134146
return builder;
135147
}
136148

137-
public static AnalyzeToken readAnalyzeToken(StreamInput in) throws IOException {
138-
AnalyzeToken analyzeToken = new AnalyzeToken();
139-
analyzeToken.readFrom(in);
140-
return analyzeToken;
141-
}
142-
143149
public static AnalyzeToken fromXContent(XContentParser parser) throws IOException {
144150
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
145151
String field = null;
@@ -184,22 +190,6 @@ public static AnalyzeToken fromXContent(XContentParser parser) throws IOExceptio
184190
return new AnalyzeToken(term, position, startOffset, endOffset, positionLength, type, attributes);
185191
}
186192

187-
@Override
188-
public void readFrom(StreamInput in) throws IOException {
189-
term = in.readString();
190-
startOffset = in.readInt();
191-
endOffset = in.readInt();
192-
position = in.readVInt();
193-
Integer len = in.readOptionalVInt();
194-
if (len != null) {
195-
positionLength = len;
196-
} else {
197-
positionLength = 1;
198-
}
199-
type = in.readOptionalString();
200-
attributes = in.readMap();
201-
}
202-
203193
@Override
204194
public void writeTo(StreamOutput out) throws IOException {
205195
out.writeString(term);
@@ -212,18 +202,35 @@ public void writeTo(StreamOutput out) throws IOException {
212202
}
213203
}
214204

215-
private DetailAnalyzeResponse detail;
216-
217-
private List<AnalyzeToken> tokens;
205+
private final DetailAnalyzeResponse detail;
218206

219-
AnalyzeResponse() {
220-
}
207+
private final List<AnalyzeToken> tokens;
221208

222209
public AnalyzeResponse(List<AnalyzeToken> tokens, DetailAnalyzeResponse detail) {
223210
this.tokens = tokens;
224211
this.detail = detail;
225212
}
226213

214+
public AnalyzeResponse(StreamInput in) throws IOException {
215+
super.readFrom(in);
216+
int size = in.readVInt();
217+
if (size > 0) {
218+
tokens = new ArrayList<>(size);
219+
for (int i = 0; i < size; i++) {
220+
tokens.add(new AnalyzeToken(in));
221+
}
222+
}
223+
else {
224+
tokens = null;
225+
}
226+
detail = in.readOptionalWriteable(DetailAnalyzeResponse::new);
227+
}
228+
229+
@Override
230+
public void readFrom(StreamInput in) throws IOException {
231+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
232+
}
233+
227234
public List<AnalyzeToken> getTokens() {
228235
return this.tokens;
229236
}
@@ -268,20 +275,6 @@ public static AnalyzeResponse fromXContent(XContentParser parser) throws IOExcep
268275
return PARSER.parse(parser, null);
269276
}
270277

271-
@Override
272-
public void readFrom(StreamInput in) throws IOException {
273-
super.readFrom(in);
274-
int size = in.readVInt();
275-
tokens = new ArrayList<>(size);
276-
for (int i = 0; i < size; i++) {
277-
tokens.add(AnalyzeToken.readAnalyzeToken(in));
278-
}
279-
if (tokens.size() == 0) {
280-
tokens = null;
281-
}
282-
detail = in.readOptionalStreamable(DetailAnalyzeResponse::new);
283-
}
284-
285278
@Override
286279
public void writeTo(StreamOutput out) throws IOException {
287280
super.writeTo(out);
@@ -293,7 +286,7 @@ public void writeTo(StreamOutput out) throws IOException {
293286
} else {
294287
out.writeVInt(0);
295288
}
296-
out.writeOptionalStreamable(detail);
289+
out.writeOptionalWriteable(detail);
297290
}
298291

299292
@Override

0 commit comments

Comments
 (0)