Use abstract classes as super typs for Acknowledge operations

Currently we have a marker interface for Acknowledged[Request|Response],
this makes not much sense since we duplicate the code in each subclass
or class that implements the interface. We can simply use abstract
classes and have it implemented only once.
This commit is contained in:
Simon Willnauer 2013-10-24 14:01:43 +02:00
parent 7867de4f5b
commit fb9cd5a562
6 changed files with 89 additions and 118 deletions

View File

@ -22,7 +22,6 @@ package org.elasticsearch.action.admin.indices.warmer.delete;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
@ -31,14 +30,12 @@ import org.elasticsearch.common.unit.TimeValue;
import java.io.IOException;
import static org.elasticsearch.common.unit.TimeValue.readTimeValue;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
/**
* A request to delete an index warmer.
*/
public class DeleteWarmerRequest extends MasterNodeOperationRequest<DeleteWarmerRequest>
implements AcknowledgedRequest<DeleteWarmerRequest> {
public class DeleteWarmerRequest extends AcknowledgedRequest<DeleteWarmerRequest> {
private String name;
@ -96,31 +93,12 @@ public class DeleteWarmerRequest extends MasterNodeOperationRequest<DeleteWarmer
return indices;
}
@Override
public DeleteWarmerRequest timeout(String timeout) {
this.timeout = TimeValue.parseTimeValue(timeout, this.timeout);
return this;
}
@Override
public DeleteWarmerRequest timeout(TimeValue timeout) {
this.timeout = timeout;
return this;
}
@Override
public TimeValue timeout() {
return timeout;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
name = in.readOptionalString();
indices = in.readStringArray();
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
timeout = readTimeValue(in);
}
readTimeout(in, Version.V_0_90_6);
}
@Override
@ -128,8 +106,6 @@ public class DeleteWarmerRequest extends MasterNodeOperationRequest<DeleteWarmer
super.writeTo(out);
out.writeOptionalString(name);
out.writeStringArrayNullable(indices);
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
timeout.writeTo(out);
}
writeTimeout(out, Version.V_0_90_6);
}
}

View File

@ -19,40 +19,18 @@
package org.elasticsearch.action.admin.indices.warmer.delete;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
* A response for a delete warmer.
*/
public class DeleteWarmerResponse extends ActionResponse implements AcknowledgedResponse {
private boolean acknowledged;
public class DeleteWarmerResponse extends AcknowledgedResponse {
DeleteWarmerResponse() {
super();
}
DeleteWarmerResponse(boolean acknowledged) {
this.acknowledged = acknowledged;
}
public boolean isAcknowledged() {
return acknowledged;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
acknowledged = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(acknowledged);
super(acknowledged);
}
}

View File

@ -24,29 +24,23 @@ import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import java.io.IOException;
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.unit.TimeValue.readTimeValue;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
/**
* A request to put a search warmer.
*/
public class PutWarmerRequest extends MasterNodeOperationRequest<PutWarmerRequest>
implements AcknowledgedRequest<PutWarmerRequest> {
public class PutWarmerRequest extends AcknowledgedRequest<PutWarmerRequest> {
private String name;
private SearchRequest searchRequest;
private TimeValue timeout = timeValueSeconds(10);
PutWarmerRequest() {
@ -94,22 +88,6 @@ public class PutWarmerRequest extends MasterNodeOperationRequest<PutWarmerReques
return this.searchRequest;
}
@Override
public PutWarmerRequest timeout(String timeout) {
return this;
}
@Override
public PutWarmerRequest timeout(TimeValue timeout) {
this.timeout = timeout;
return this;
}
@Override
public TimeValue timeout() {
return timeout;
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = searchRequest.validate();
@ -127,9 +105,7 @@ public class PutWarmerRequest extends MasterNodeOperationRequest<PutWarmerReques
searchRequest = new SearchRequest();
searchRequest.readFrom(in);
}
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
timeout = readTimeValue(in);
}
readTimeout(in, Version.V_0_90_6);
}
@Override
@ -142,8 +118,6 @@ public class PutWarmerRequest extends MasterNodeOperationRequest<PutWarmerReques
out.writeBoolean(true);
searchRequest.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
timeout.writeTo(out);
}
writeTimeout(out, Version.V_0_90_6);
}
}

View File

@ -19,44 +19,19 @@
package org.elasticsearch.action.admin.indices.warmer.put;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
* The response of put warmer operation.
*/
public class PutWarmerResponse extends ActionResponse implements AcknowledgedResponse {
private boolean acknowledged;
public class PutWarmerResponse extends AcknowledgedResponse {
PutWarmerResponse() {
super();
}
PutWarmerResponse(boolean acknowledged) {
this.acknowledged = acknowledged;
super(acknowledged);
}
/**
* Has the put warmer been ack'ed.
*/
public boolean isAcknowledged() {
return acknowledged;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
acknowledged = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(acknowledged);
}
}

View File

@ -18,32 +18,70 @@
package org.elasticsearch.action.support.master;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import java.io.IOException;
import static org.elasticsearch.common.unit.TimeValue.readTimeValue;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
/**
* Interface that allows to mark action requests that support acknowledgements.
* Abstract class that allows to mark action requests that support acknowledgements.
* Facilitates consistency across different api.
*/
public interface AcknowledgedRequest<T extends ActionRequest<T>> {
public abstract class AcknowledgedRequest<T extends MasterNodeOperationRequest> extends MasterNodeOperationRequest<T> {
protected TimeValue timeout = timeValueSeconds(10);
protected AcknowledgedRequest() {
}
/**
* Allows to set the timeout
* @param timeout timeout as a string (e.g. 1s)
* @return the request itself
*/
T timeout(String timeout);
public final T timeout(String timeout) {
this.timeout = TimeValue.parseTimeValue(timeout, this.timeout);
return (T)this;
}
/**
* Allows to set the timeout
* @param timeout timeout as a {@link TimeValue}
* @return the request itself
*/
T timeout(TimeValue timeout);
public final T timeout(TimeValue timeout) {
this.timeout = timeout;
return (T) this;
}
/**
* Returns the current timeout
* @return the current timeout as a {@link TimeValue}
*/
TimeValue timeout();
public final TimeValue timeout() {
return timeout;
}
/**
* Reads the timeout value if on or after the specified min version or if the version is <code>null</code>.
*/
protected void readTimeout(StreamInput in, Version minVersion) throws IOException {
if (minVersion == null || in.getVersion().onOrAfter(minVersion)) {
timeout = readTimeValue(in);
}
}
/**
* writes the timeout value if on or after the specified min version or if the version is <code>null</code>.
*/
protected void writeTimeout(StreamOutput out, Version minVersion) throws IOException {
if (minVersion == null || out.getVersion().onOrAfter(minVersion)) {
timeout.writeTo(out);
}
}
}

View File

@ -18,15 +18,45 @@
package org.elasticsearch.action.support.master;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
* Interface that allows to mark action responses that support acknowledgements.
* Abstract class that allows to mark action responses that support acknowledgements.
* Facilitates consistency across different api.
*/
public interface AcknowledgedResponse {
public abstract class AcknowledgedResponse extends ActionResponse {
private boolean acknowledged;
protected AcknowledgedResponse() {
}
protected AcknowledgedResponse(boolean acknowledged) {
this.acknowledged = acknowledged;
}
/**
* Returns whether the response is acknowledged or not
* @return true if the response is acknowledged, false otherwise
*/
boolean isAcknowledged();
public final boolean isAcknowledged() {
return acknowledged;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
acknowledged = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(acknowledged);
}
}