Skip to content

Commit cfc12b4

Browse files
authored
Cut over MultiSearchResponse to Writeable (elastic#41844)
Relates to elastic#34389
1 parent c9e8beb commit cfc12b4

File tree

4 files changed

+77
-85
lines changed

4 files changed

+77
-85
lines changed

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateAction.java

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

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

2425
public class MultiSearchTemplateAction extends Action<MultiSearchTemplateResponse> {
2526

@@ -32,6 +33,11 @@ private MultiSearchTemplateAction() {
3233

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

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.elasticsearch.common.Strings;
2828
import org.elasticsearch.common.io.stream.StreamInput;
2929
import org.elasticsearch.common.io.stream.StreamOutput;
30-
import org.elasticsearch.common.io.stream.Streamable;
30+
import org.elasticsearch.common.io.stream.Writeable;
3131
import org.elasticsearch.common.unit.TimeValue;
3232
import org.elasticsearch.common.xcontent.ToXContent;
3333
import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -43,11 +43,19 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
4343
/**
4444
* A search template response item, holding the actual search template response, or an error message if it failed.
4545
*/
46-
public static class Item implements Streamable {
47-
private SearchTemplateResponse response;
48-
private Exception exception;
46+
public static class Item implements Writeable {
47+
private final SearchTemplateResponse response;
48+
private final Exception exception;
4949

50-
Item() {
50+
private Item(StreamInput in) throws IOException {
51+
if (in.readBoolean()) {
52+
this.response = new SearchTemplateResponse();
53+
response.readFrom(in);
54+
this.exception = null;
55+
} else {
56+
exception = in.readException();
57+
this.response = null;
58+
}
5159
}
5260

5361
public Item(SearchTemplateResponse response, Exception exception) {
@@ -78,22 +86,6 @@ public SearchTemplateResponse getResponse() {
7886
return this.response;
7987
}
8088

81-
public static Item readItem(StreamInput in) throws IOException {
82-
Item item = new Item();
83-
item.readFrom(in);
84-
return item;
85-
}
86-
87-
@Override
88-
public void readFrom(StreamInput in) throws IOException {
89-
if (in.readBoolean()) {
90-
this.response = new SearchTemplateResponse();
91-
response.readFrom(in);
92-
} else {
93-
exception = in.readException();
94-
}
95-
}
96-
9789
@Override
9890
public void writeTo(StreamOutput out) throws IOException {
9991
if (response != null) {
@@ -113,17 +105,25 @@ public Exception getFailure() {
113105
public String toString() {
114106
return "Item [response=" + response + ", exception=" + exception + "]";
115107
}
116-
117-
118108
}
119109

120-
private Item[] items;
121-
private long tookInMillis;
110+
private final Item[] items;
111+
private final long tookInMillis;
122112

123-
MultiSearchTemplateResponse() {
113+
MultiSearchTemplateResponse(StreamInput in) throws IOException {
114+
super(in);
115+
items = new Item[in.readVInt()];
116+
for (int i = 0; i < items.length; i++) {
117+
items[i] = new Item(in);
118+
}
119+
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
120+
tookInMillis = in.readVLong();
121+
} else {
122+
tookInMillis = -1L;
123+
}
124124
}
125125

126-
public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
126+
MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
127127
this.items = items;
128128
this.tookInMillis = tookInMillis;
129129
}
@@ -149,14 +149,7 @@ public TimeValue getTook() {
149149

150150
@Override
151151
public void readFrom(StreamInput in) throws IOException {
152-
super.readFrom(in);
153-
items = new Item[in.readVInt()];
154-
for (int i = 0; i < items.length; i++) {
155-
items[i] = Item.readItem(in);
156-
}
157-
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
158-
tookInMillis = in.readVLong();
159-
}
152+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
160153
}
161154

162155
@Override

server/src/main/java/org/elasticsearch/action/search/MultiSearchAction.java

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

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

2425
public class MultiSearchAction extends Action<MultiSearchResponse> {
2526

@@ -32,6 +33,11 @@ private MultiSearchAction() {
3233

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

server/src/main/java/org/elasticsearch/action/search/MultiSearchResponse.java

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.elasticsearch.common.Strings;
2828
import org.elasticsearch.common.io.stream.StreamInput;
2929
import org.elasticsearch.common.io.stream.StreamOutput;
30-
import org.elasticsearch.common.io.stream.Streamable;
30+
import org.elasticsearch.common.io.stream.Writeable;
3131
import org.elasticsearch.common.unit.TimeValue;
3232
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
3333
import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -59,19 +59,37 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
5959
/**
6060
* A search response item, holding the actual search response, or an error message if it failed.
6161
*/
62-
public static class Item implements Streamable {
63-
private SearchResponse response;
64-
private Exception exception;
65-
66-
Item() {
67-
68-
}
62+
public static class Item implements Writeable {
63+
private final SearchResponse response;
64+
private final Exception exception;
6965

7066
public Item(SearchResponse response, Exception exception) {
7167
this.response = response;
7268
this.exception = exception;
7369
}
7470

71+
Item(StreamInput in) throws IOException{
72+
if (in.readBoolean()) {
73+
this.response = new SearchResponse();
74+
this.response.readFrom(in);
75+
this.exception = null;
76+
} else {
77+
this.exception = in.readException();
78+
this.response = null;
79+
}
80+
}
81+
82+
@Override
83+
public void writeTo(StreamOutput out) throws IOException {
84+
if (response != null) {
85+
out.writeBoolean(true);
86+
response.writeTo(out);
87+
} else {
88+
out.writeBoolean(false);
89+
out.writeException(exception);
90+
}
91+
}
92+
7593
/**
7694
* Is it a failed search?
7795
*/
@@ -95,47 +113,21 @@ public SearchResponse getResponse() {
95113
return this.response;
96114
}
97115

98-
public static Item readItem(StreamInput in) throws IOException {
99-
Item item = new Item();
100-
item.readFrom(in);
101-
return item;
102-
}
103-
104-
@Override
105-
public void readFrom(StreamInput in) throws IOException {
106-
if (in.readBoolean()) {
107-
this.response = new SearchResponse();
108-
response.readFrom(in);
109-
} else {
110-
exception = in.readException();
111-
}
112-
}
113-
114-
@Override
115-
public void writeTo(StreamOutput out) throws IOException {
116-
if (response != null) {
117-
out.writeBoolean(true);
118-
response.writeTo(out);
119-
} else {
120-
out.writeBoolean(false);
121-
out.writeException(exception);
122-
}
123-
}
124-
125116
public Exception getFailure() {
126117
return exception;
127118
}
128119
}
129120

130-
private Item[] items;
131-
132-
private long tookInMillis;
133-
134-
MultiSearchResponse() {
135-
}
121+
private final Item[] items;
122+
private final long tookInMillis;
136123

137124
MultiSearchResponse(StreamInput in) throws IOException {
138-
readFrom(in);
125+
super(in);
126+
items = new Item[in.readVInt()];
127+
for (int i = 0; i < items.length; i++) {
128+
items[i] = new Item(in);
129+
}
130+
tookInMillis = in.readVLong();
139131
}
140132

141133
public MultiSearchResponse(Item[] items, long tookInMillis) {
@@ -164,12 +156,7 @@ public TimeValue getTook() {
164156

165157
@Override
166158
public void readFrom(StreamInput in) throws IOException {
167-
super.readFrom(in);
168-
items = new Item[in.readVInt()];
169-
for (int i = 0; i < items.length; i++) {
170-
items[i] = Item.readItem(in);
171-
}
172-
tookInMillis = in.readVLong();
159+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
173160
}
174161

175162
@Override

0 commit comments

Comments
 (0)