Skip to content

Commit 02a8739

Browse files
committed
Add fromXContent method to ClearScrollResponse (#24909)
ClearScrollResponse can print out its content into an XContentBuilder as it implements ToXContentObject. This PR add a fromXContent method to it so that we are able to recreate the response object when parsing the response back. This will be used in the high level REST client.
1 parent 52f75ea commit 02a8739

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,34 @@
2020
package org.elasticsearch.action.search;
2121

2222
import org.elasticsearch.action.ActionResponse;
23+
import org.elasticsearch.common.ParseField;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
27+
import org.elasticsearch.common.xcontent.ObjectParser;
2528
import org.elasticsearch.common.xcontent.StatusToXContentObject;
2629
import org.elasticsearch.common.xcontent.XContentBuilder;
30+
import org.elasticsearch.common.xcontent.XContentParser;
2731
import org.elasticsearch.rest.RestStatus;
2832

2933
import java.io.IOException;
3034

35+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3136
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
3237
import static org.elasticsearch.rest.RestStatus.OK;
3338

3439
public class ClearScrollResponse extends ActionResponse implements StatusToXContentObject {
3540

41+
private static final ParseField SUCCEEDED = new ParseField("succeeded");
42+
private static final ParseField NUMFREED = new ParseField("num_freed");
43+
44+
private static final ConstructingObjectParser<ClearScrollResponse, Void> PARSER = new ConstructingObjectParser<>("clear_scroll",
45+
true, a -> new ClearScrollResponse((boolean)a[0], (int)a[1]));
46+
static {
47+
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), SUCCEEDED, ObjectParser.ValueType.BOOLEAN);
48+
PARSER.declareField(constructorArg(), (parser, context) -> parser.intValue(), NUMFREED, ObjectParser.ValueType.INT);
49+
}
50+
3651
private boolean succeeded;
3752
private int numFreed;
3853

@@ -67,12 +82,19 @@ public RestStatus status() {
6782
@Override
6883
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
6984
builder.startObject();
70-
builder.field(Fields.SUCCEEDED, succeeded);
71-
builder.field(Fields.NUMFREED, numFreed);
85+
builder.field(SUCCEEDED.getPreferredName(), succeeded);
86+
builder.field(NUMFREED.getPreferredName(), numFreed);
7287
builder.endObject();
7388
return builder;
7489
}
7590

91+
/**
92+
* Parse the clear scroll response body into a new {@link ClearScrollResponse} object
93+
*/
94+
public static ClearScrollResponse fromXContent(XContentParser parser) throws IOException {
95+
return PARSER.apply(parser, null);
96+
}
97+
7698
@Override
7799
public void readFrom(StreamInput in) throws IOException {
78100
super.readFrom(in);
@@ -86,9 +108,4 @@ public void writeTo(StreamOutput out) throws IOException {
86108
out.writeBoolean(succeeded);
87109
out.writeVInt(numFreed);
88110
}
89-
90-
static final class Fields {
91-
static final String SUCCEEDED = "succeeded";
92-
static final String NUMFREED = "num_freed";
93-
}
94111
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search;
21+
22+
import org.elasticsearch.action.search.ClearScrollResponse;
23+
import org.elasticsearch.common.bytes.BytesReference;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
import org.elasticsearch.common.xcontent.XContentHelper;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
import org.elasticsearch.common.xcontent.XContentType;
29+
import org.elasticsearch.common.xcontent.json.JsonXContent;
30+
import org.elasticsearch.test.ESTestCase;
31+
32+
import java.io.IOException;
33+
34+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
35+
36+
public class ClearScrollResponseTests extends ESTestCase {
37+
38+
public void testToXContent() throws IOException {
39+
ClearScrollResponse clearScrollResponse = new ClearScrollResponse(true, 10);
40+
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
41+
clearScrollResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
42+
}
43+
assertEquals(true, clearScrollResponse.isSucceeded());
44+
assertEquals(10, clearScrollResponse.getNumFreed());
45+
}
46+
47+
public void testToAndFromXContent() throws IOException {
48+
XContentType xContentType = randomFrom(XContentType.values());
49+
ClearScrollResponse originalResponse = createTestItem();
50+
BytesReference originalBytes = toShuffledXContent(originalResponse, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
51+
ClearScrollResponse parsedResponse;
52+
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
53+
parsedResponse = ClearScrollResponse.fromXContent(parser);
54+
}
55+
assertEquals(originalResponse.isSucceeded(), parsedResponse.isSucceeded());
56+
assertEquals(originalResponse.getNumFreed(), parsedResponse.getNumFreed());
57+
BytesReference parsedBytes = XContentHelper.toXContent(parsedResponse, xContentType, randomBoolean());
58+
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
59+
}
60+
61+
private static ClearScrollResponse createTestItem() {
62+
return new ClearScrollResponse(randomBoolean(), randomIntBetween(0, Integer.MAX_VALUE));
63+
}
64+
}

0 commit comments

Comments
 (0)