From 60df5d09d3457eaf06ab87bf48e84769ce1b561d Mon Sep 17 00:00:00 2001 From: Arpit Bandejiya Date: Sat, 26 Nov 2022 20:22:23 +0530 Subject: [PATCH] made changes --- .../action/search/UpdatePitRequest.java | 93 +++++++++++++++---- .../action/search/UpdatePitRequestInfo.java | 19 +++- 2 files changed, 89 insertions(+), 23 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/search/UpdatePitRequest.java b/server/src/main/java/org/opensearch/action/search/UpdatePitRequest.java index 8c9ef54a0b79e..a8dfc8f7f831a 100644 --- a/server/src/main/java/org/opensearch/action/search/UpdatePitRequest.java +++ b/server/src/main/java/org/opensearch/action/search/UpdatePitRequest.java @@ -11,6 +11,7 @@ import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.common.io.stream.StreamInput; +import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.unit.TimeValue; import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.ToXContentObject; @@ -25,17 +26,9 @@ import static org.opensearch.action.ValidateActions.addValidationError; public class UpdatePitRequest extends ActionRequest implements ToXContentObject { -// TODO: update the pit reqyest to handle not just array +// TODO: update the pit request to handle not just array private final List updatePitRequests; - public UpdatePitRequest(StreamInput in) throws IOException { - super(in); - int size = in.readVInt(); - updatePitRequests = new ArrayList<>(); - for(int i=0;i getUpdatePitRequests() { return updatePitRequests; @@ -49,39 +42,103 @@ public UpdatePitRequest(List updatePitRequests){ this.updatePitRequests = updatePitRequests; } - public UpdatePitRequest() {} + public UpdatePitRequest() { + this.updatePitRequests = new ArrayList<>(); + } @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (keepAlive == null) { - validationException = addValidationError("keep alive not specified", validationException); + if (updatePitRequests == null || updatePitRequests.isEmpty()) { + validationException = addValidationError("No pit ids specified", validationException); } return validationException; } + public UpdatePitRequest(StreamInput in) throws IOException { + super(in); + int size = in.readVInt(); + updatePitRequests = new ArrayList<>(); + for(int i=0;i + // keep_alive: <> + // } + // ] + // } public void fromXContent(XContentParser parser) throws IOException { + updatePitRequests.clear(); if(parser.nextToken() != XContentParser.Token.START_OBJECT){ throw new IllegalArgumentException("Malformed content, must start with an object"); } else { XContentParser.Token token; String currentFieldName = null; + String currentFieldName1 = null; while((token = parser.nextToken()) != XContentParser.Token.END_OBJECT){ if (token == XContentParser.Token.FIELD_NAME){ currentFieldName = parser.currentName(); - } else if("keep_alive".equals(currentFieldName)){ - if(token.isValue() == false){ - throw new IllegalArgumentException("keep_alive should only contain a time value"); + } else if("pits".equals(currentFieldName)){ + if(token == XContentParser.Token.START_ARRAY){ + while(parser.nextToken() != XContentParser.Token.START_OBJECT) { + String pit_id =null; + String keep_alive = null; + if (parser.nextToken() == XContentParser.Token.FIELD_NAME){ + currentFieldName1 = parser.currentName(); + } + if("pit_id".equals(currentFieldName1)){ + pit_id = parser.text(); + } else{ + throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1); + } + + if (parser.nextToken() == XContentParser.Token.FIELD_NAME){ + currentFieldName1 = parser.currentName(); + } + if("keep_alive".equals(currentFieldName1)){ + keep_alive = parser.text(); + } else{ + throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1); + } + updatePitRequests.add(new UpdatePitRequestInfo(pit_id, keep_alive)); + + + if(parser.nextToken() !=XContentParser.Token.END_OBJECT){ + throw new IllegalArgumentException("pit_id array element should only contain pit_id " + currentFieldName1); + } + } + } else { + throw new IllegalArgumentException("pit_id array element should only contain pit_id"); + } - keepAlive = TimeValue.parseTimeValue(parser.text(),"keep_alive"); } } diff --git a/server/src/main/java/org/opensearch/action/search/UpdatePitRequestInfo.java b/server/src/main/java/org/opensearch/action/search/UpdatePitRequestInfo.java index f2a6cb7029979..81e6e479ab2c5 100644 --- a/server/src/main/java/org/opensearch/action/search/UpdatePitRequestInfo.java +++ b/server/src/main/java/org/opensearch/action/search/UpdatePitRequestInfo.java @@ -10,26 +10,35 @@ import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; -import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.xcontent.ToXContent; +import org.opensearch.common.xcontent.XContentBuilder; import java.io.IOException; public class UpdatePitRequestInfo { private final String pitId; - private final TimeValue keepAlive; + private final String keepAlive; - public UpdatePitRequestInfo(String pitId, TimeValue keepAlive){ + public UpdatePitRequestInfo(String pitId, String keepAlive){ this.pitId = pitId; this.keepAlive = keepAlive; } public UpdatePitRequestInfo(StreamInput in) throws IOException { pitId = in.readString(); - keepAlive = in.readTimeValue(); + keepAlive = in.readString(); } public void writeTo(StreamOutput out) throws IOException { out.writeString(pitId); - out.writeTimeValue(keepAlive); + out.writeString(keepAlive); + } + + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(); + builder.field("pit_id", pitId); + builder.field("keepAlive", keepAlive); + builder.endObject(); + return builder; } }