Skip to content

Commit

Permalink
made changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpit-Bandejiya committed Nov 26, 2022
1 parent 44ee92f commit 60df5d0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UpdatePitRequestInfo> updatePitRequests;

public UpdatePitRequest(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
updatePitRequests = new ArrayList<>();
for(int i=0;i<size;i++){
updatePitRequests.add(new UpdatePitRequestInfo(in));
}
}

public List<UpdatePitRequestInfo> getUpdatePitRequests() {
return updatePitRequests;
Expand All @@ -49,39 +42,103 @@ public UpdatePitRequest(List<UpdatePitRequestInfo> 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<size;i++){
updatePitRequests.add(new UpdatePitRequestInfo(in));
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(updatePitRequests.size());
for(UpdatePitRequestInfo updatePitRequest: updatePitRequests) {
updatePitRequest.writeTo(out);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.field("keep_alive", keepAlive);
builder.field("pit_id", pit_id);
builder.startObject();
builder.startArray("pits");
for(UpdatePitRequestInfo requestInfo : updatePitRequests){
requestInfo.toXContent(builder,params);
}
builder.endArray();
builder.endObject();
return builder;
}

// Req body
// {
// pits: [
// {
// pit_id: <>
// 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");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 60df5d0

Please sign in to comment.