org.elasticsearch.action.admin.indices.rollover.RolloverResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.action.admin.indices.rollover;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
/**
* Response object for {@link RolloverRequest} API
*
* Note: there is a new class with the same name for the Java HLRC that uses a typeless format.
* Any changes done to this class should also go to that client class.
*/
public final class RolloverResponse extends ShardsAcknowledgedResponse implements ToXContentObject {
private static final ParseField NEW_INDEX = new ParseField("new_index");
private static final ParseField OLD_INDEX = new ParseField("old_index");
private static final ParseField DRY_RUN = new ParseField("dry_run");
private static final ParseField ROLLED_OVER = new ParseField("rolled_over");
private static final ParseField CONDITIONS = new ParseField("conditions");
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
"rollover",
true,
args -> new RolloverResponse(
(String) args[0],
(String) args[1],
(Map) args[2],
(Boolean) args[3],
(Boolean) args[4],
(Boolean) args[5],
(Boolean) args[6]
)
);
static {
PARSER.declareField(constructorArg(), (parser, context) -> parser.text(), OLD_INDEX, ObjectParser.ValueType.STRING);
PARSER.declareField(constructorArg(), (parser, context) -> parser.text(), NEW_INDEX, ObjectParser.ValueType.STRING);
PARSER.declareObject(constructorArg(), (parser, context) -> parser.map(), CONDITIONS);
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), DRY_RUN, ObjectParser.ValueType.BOOLEAN);
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ROLLED_OVER, ObjectParser.ValueType.BOOLEAN);
declareAcknowledgedAndShardsAcknowledgedFields(PARSER);
}
private final String oldIndex;
private final String newIndex;
private final Map conditionStatus;
private final boolean dryRun;
private final boolean rolledOver;
// Needs to be duplicated, because shardsAcknowledged gets (de)serailized as last field whereas
// in other subclasses of ShardsAcknowledgedResponse this field (de)serailized as first field.
private final boolean shardsAcknowledged;
RolloverResponse(StreamInput in) throws IOException {
super(in, false, in.getVersion().onOrAfter(Version.V_6_4_0));
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
oldIndex = in.readString();
newIndex = in.readString();
int conditionSize = in.readVInt();
conditionStatus = new HashMap<>(conditionSize);
for (int i = 0; i < conditionSize; i++) {
conditionStatus.put(in.readString(), in.readBoolean());
}
dryRun = in.readBoolean();
rolledOver = in.readBoolean();
shardsAcknowledged = in.readBoolean();
} else {
oldIndex = in.readString();
newIndex = in.readString();
int conditionSize = in.readVInt();
conditionStatus = new HashMap<>(conditionSize);
for (int i = 0; i < conditionSize; i++) {
conditionStatus.put(in.readString(), in.readBoolean());
}
dryRun = in.readBoolean();
rolledOver = in.readBoolean();
acknowledged = in.readBoolean();
shardsAcknowledged = in.readBoolean();
}
}
public RolloverResponse(
String oldIndex,
String newIndex,
Map conditionResults,
boolean dryRun,
boolean rolledOver,
boolean acknowledged,
boolean shardsAcknowledged
) {
super(acknowledged, shardsAcknowledged);
this.oldIndex = oldIndex;
this.newIndex = newIndex;
this.dryRun = dryRun;
this.rolledOver = rolledOver;
this.conditionStatus = conditionResults;
this.shardsAcknowledged = shardsAcknowledged;
}
/**
* Returns the name of the index that the request alias was pointing to
*/
public String getOldIndex() {
return oldIndex;
}
/**
* Returns the name of the index that the request alias currently points to
*/
public String getNewIndex() {
return newIndex;
}
/**
* Returns the statuses of all the request conditions
*/
public Map getConditionStatus() {
return conditionStatus;
}
/**
* Returns if the rollover execution was skipped even when conditions were met
*/
public boolean isDryRun() {
return dryRun;
}
/**
* Returns true if the rollover was not simulated and the conditions were met
*/
public boolean isRolledOver() {
return rolledOver;
}
@Override
public boolean isShardsAcknowledged() {
return shardsAcknowledged;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
super.writeTo(out);
out.writeString(oldIndex);
out.writeString(newIndex);
out.writeVInt(conditionStatus.size());
for (Map.Entry entry : conditionStatus.entrySet()) {
out.writeString(entry.getKey());
out.writeBoolean(entry.getValue());
}
out.writeBoolean(dryRun);
out.writeBoolean(rolledOver);
out.writeBoolean(shardsAcknowledged);
} else {
out.writeString(oldIndex);
out.writeString(newIndex);
out.writeVInt(conditionStatus.size());
for (Map.Entry entry : conditionStatus.entrySet()) {
out.writeString(entry.getKey());
out.writeBoolean(entry.getValue());
}
out.writeBoolean(dryRun);
out.writeBoolean(rolledOver);
out.writeBoolean(acknowledged);
writeShardsAcknowledged(out);
}
}
@Override
protected void addCustomFields(XContentBuilder builder, Params params) throws IOException {
super.addCustomFields(builder, params);
builder.field(OLD_INDEX.getPreferredName(), oldIndex);
builder.field(NEW_INDEX.getPreferredName(), newIndex);
builder.field(ROLLED_OVER.getPreferredName(), rolledOver);
builder.field(DRY_RUN.getPreferredName(), dryRun);
builder.startObject(CONDITIONS.getPreferredName());
for (Map.Entry entry : conditionStatus.entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
}
public static RolloverResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
@Override
public boolean equals(Object o) {
if (super.equals(o)) {
RolloverResponse that = (RolloverResponse) o;
return dryRun == that.dryRun
&& rolledOver == that.rolledOver
&& Objects.equals(oldIndex, that.oldIndex)
&& Objects.equals(newIndex, that.newIndex)
&& Objects.equals(conditionStatus, that.conditionStatus);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver);
}
}