package crate.elasticsearch.action.export; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Required; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentHelper; import java.io.IOException; import java.util.Arrays; public class ExportRequest extends BroadcastOperationRequest<ExportRequest> { @Nullable protected String routing; @Nullable private String preference; private BytesReference source; private boolean querySourceUnsafe; private String[] types = Strings.EMPTY_ARRAY; ExportRequest() { } /** * Constructs a new export request against the provided indices. No indices provided means it will * run against all indices. */ public ExportRequest(String... indices) { super(indices); } @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = super.validate(); return validationException; } @Override protected void beforeStart() { if (querySourceUnsafe) { source = source.copyBytesArray(); querySourceUnsafe = false; } } /** * The query source to execute. */ public BytesReference source() { return source; } @Required public ExportRequest source(String source) { return this.source(new BytesArray(source), false); } @Required public ExportRequest source(BytesReference source, boolean unsafe) { this.source = source; this.querySourceUnsafe = unsafe; return this; } /** * The types of documents the query will run against. Defaults to all types. */ String[] types() { return this.types; } /** * The types of documents the query will run against. Defaults to all types. */ public ExportRequest types(String... types) { this.types = types; return this; } /** * A comma separated list of routing values to control the shards the search will be executed on. */ public String routing() { return this.routing; } /** * A comma separated list of routing values to control the shards the search will be executed on. */ public ExportRequest routing(String routing) { this.routing = routing; return this; } /** * The routing values to control the shards that the search will be executed on. */ public ExportRequest routing(String... routings) { this.routing = Strings.arrayToCommaDelimitedString(routings); return this; } public ExportRequest preference(String preference) { this.preference = preference; return this; } public String preference() { return this.preference; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); routing = in.readOptionalString(); preference = in.readOptionalString(); querySourceUnsafe = false; source = in.readBytesReference(); types = in.readStringArray(); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeOptionalString(routing); out.writeOptionalString(preference); out.writeBytesReference(source); out.writeStringArray(types); } @Override public String toString() { String sSource = "_na_"; try { sSource = XContentHelper.convertToJson(source, false); } catch (Exception e) { // ignore } return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + sSource + "]"; } }