Skip to content

Commit 60df5d0

Browse files
committed
made changes
1 parent 44ee92f commit 60df5d0

File tree

2 files changed

+89
-23
lines changed

2 files changed

+89
-23
lines changed

server/src/main/java/org/opensearch/action/search/UpdatePitRequest.java

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.opensearch.action.ActionRequest;
1212
import org.opensearch.action.ActionRequestValidationException;
1313
import org.opensearch.common.io.stream.StreamInput;
14+
import org.opensearch.common.io.stream.StreamOutput;
1415
import org.opensearch.common.unit.TimeValue;
1516
import org.opensearch.common.xcontent.ToXContent;
1617
import org.opensearch.common.xcontent.ToXContentObject;
@@ -25,17 +26,9 @@
2526
import static org.opensearch.action.ValidateActions.addValidationError;
2627

2728
public class UpdatePitRequest extends ActionRequest implements ToXContentObject {
28-
// TODO: update the pit reqyest to handle not just array
29+
// TODO: update the pit request to handle not just array
2930
private final List<UpdatePitRequestInfo> updatePitRequests;
3031

31-
public UpdatePitRequest(StreamInput in) throws IOException {
32-
super(in);
33-
int size = in.readVInt();
34-
updatePitRequests = new ArrayList<>();
35-
for(int i=0;i<size;i++){
36-
updatePitRequests.add(new UpdatePitRequestInfo(in));
37-
}
38-
}
3932

4033
public List<UpdatePitRequestInfo> getUpdatePitRequests() {
4134
return updatePitRequests;
@@ -49,39 +42,103 @@ public UpdatePitRequest(List<UpdatePitRequestInfo> updatePitRequests){
4942
this.updatePitRequests = updatePitRequests;
5043
}
5144

52-
public UpdatePitRequest() {}
45+
public UpdatePitRequest() {
46+
this.updatePitRequests = new ArrayList<>();
47+
}
5348

5449

5550
@Override
5651
public ActionRequestValidationException validate() {
5752
ActionRequestValidationException validationException = null;
58-
if (keepAlive == null) {
59-
validationException = addValidationError("keep alive not specified", validationException);
53+
if (updatePitRequests == null || updatePitRequests.isEmpty()) {
54+
validationException = addValidationError("No pit ids specified", validationException);
6055
}
6156
return validationException;
6257
}
6358

59+
public UpdatePitRequest(StreamInput in) throws IOException {
60+
super(in);
61+
int size = in.readVInt();
62+
updatePitRequests = new ArrayList<>();
63+
for(int i=0;i<size;i++){
64+
updatePitRequests.add(new UpdatePitRequestInfo(in));
65+
}
66+
}
67+
68+
@Override
69+
public void writeTo(StreamOutput out) throws IOException {
70+
super.writeTo(out);
71+
out.writeVInt(updatePitRequests.size());
72+
for(UpdatePitRequestInfo updatePitRequest: updatePitRequests) {
73+
updatePitRequest.writeTo(out);
74+
}
75+
}
76+
6477
@Override
6578
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
66-
builder.field("keep_alive", keepAlive);
67-
builder.field("pit_id", pit_id);
79+
builder.startObject();
80+
builder.startArray("pits");
81+
for(UpdatePitRequestInfo requestInfo : updatePitRequests){
82+
requestInfo.toXContent(builder,params);
83+
}
84+
builder.endArray();
85+
builder.endObject();
6886
return builder;
6987
}
7088

89+
// Req body
90+
// {
91+
// pits: [
92+
// {
93+
// pit_id: <>
94+
// keep_alive: <>
95+
// }
96+
// ]
97+
// }
7198
public void fromXContent(XContentParser parser) throws IOException {
99+
updatePitRequests.clear();
72100
if(parser.nextToken() != XContentParser.Token.START_OBJECT){
73101
throw new IllegalArgumentException("Malformed content, must start with an object");
74102
} else {
75103
XContentParser.Token token;
76104
String currentFieldName = null;
105+
String currentFieldName1 = null;
77106
while((token = parser.nextToken()) != XContentParser.Token.END_OBJECT){
78107
if (token == XContentParser.Token.FIELD_NAME){
79108
currentFieldName = parser.currentName();
80-
} else if("keep_alive".equals(currentFieldName)){
81-
if(token.isValue() == false){
82-
throw new IllegalArgumentException("keep_alive should only contain a time value");
109+
} else if("pits".equals(currentFieldName)){
110+
if(token == XContentParser.Token.START_ARRAY){
111+
while(parser.nextToken() != XContentParser.Token.START_OBJECT) {
112+
String pit_id =null;
113+
String keep_alive = null;
114+
if (parser.nextToken() == XContentParser.Token.FIELD_NAME){
115+
currentFieldName1 = parser.currentName();
116+
}
117+
if("pit_id".equals(currentFieldName1)){
118+
pit_id = parser.text();
119+
} else{
120+
throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1);
121+
}
122+
123+
if (parser.nextToken() == XContentParser.Token.FIELD_NAME){
124+
currentFieldName1 = parser.currentName();
125+
}
126+
if("keep_alive".equals(currentFieldName1)){
127+
keep_alive = parser.text();
128+
} else{
129+
throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1);
130+
}
131+
updatePitRequests.add(new UpdatePitRequestInfo(pit_id, keep_alive));
132+
133+
134+
if(parser.nextToken() !=XContentParser.Token.END_OBJECT){
135+
throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1);
136+
}
137+
}
138+
} else {
139+
throw new IllegalArgumentException("pit_id array element should only contain pit_id");
140+
83141
}
84-
keepAlive = TimeValue.parseTimeValue(parser.text(),"keep_alive");
85142
}
86143

87144
}

server/src/main/java/org/opensearch/action/search/UpdatePitRequestInfo.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,35 @@
1010

1111
import org.opensearch.common.io.stream.StreamInput;
1212
import org.opensearch.common.io.stream.StreamOutput;
13-
import org.opensearch.common.unit.TimeValue;
13+
import org.opensearch.common.xcontent.ToXContent;
14+
import org.opensearch.common.xcontent.XContentBuilder;
1415

1516
import java.io.IOException;
1617

1718
public class UpdatePitRequestInfo {
1819
private final String pitId;
19-
private final TimeValue keepAlive;
20+
private final String keepAlive;
2021

21-
public UpdatePitRequestInfo(String pitId, TimeValue keepAlive){
22+
public UpdatePitRequestInfo(String pitId, String keepAlive){
2223
this.pitId = pitId;
2324
this.keepAlive = keepAlive;
2425
}
2526

2627
public UpdatePitRequestInfo(StreamInput in) throws IOException {
2728
pitId = in.readString();
28-
keepAlive = in.readTimeValue();
29+
keepAlive = in.readString();
2930
}
3031

3132
public void writeTo(StreamOutput out) throws IOException {
3233
out.writeString(pitId);
33-
out.writeTimeValue(keepAlive);
34+
out.writeString(keepAlive);
35+
}
36+
37+
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
38+
builder.startObject();
39+
builder.field("pit_id", pitId);
40+
builder.field("keepAlive", keepAlive);
41+
builder.endObject();
42+
return builder;
3443
}
3544
}

0 commit comments

Comments
 (0)