From fb9cd5a562a13dbf085ece4e4cb75ff17c3c849a Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 24 Oct 2013 14:01:43 +0200 Subject: [PATCH] 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. --- .../warmer/delete/DeleteWarmerRequest.java | 30 ++--------- .../warmer/delete/DeleteWarmerResponse.java | 28 ++--------- .../indices/warmer/put/PutWarmerRequest.java | 32 ++---------- .../indices/warmer/put/PutWarmerResponse.java | 31 ++---------- .../support/master/AcknowledgedRequest.java | 50 ++++++++++++++++--- .../support/master/AcknowledgedResponse.java | 36 +++++++++++-- 6 files changed, 89 insertions(+), 118 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/warmer/delete/DeleteWarmerRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/warmer/delete/DeleteWarmerRequest.java index 83be17e0404..ea5ecbb7006 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/warmer/delete/DeleteWarmerRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/warmer/delete/DeleteWarmerRequest.java @@ -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 - implements AcknowledgedRequest { +public class DeleteWarmerRequest extends AcknowledgedRequest { private String name; @@ -96,31 +93,12 @@ public class DeleteWarmerRequest extends MasterNodeOperationRequest - implements AcknowledgedRequest { +public class PutWarmerRequest extends AcknowledgedRequest { private String name; private SearchRequest searchRequest; - private TimeValue timeout = timeValueSeconds(10); PutWarmerRequest() { @@ -94,22 +88,6 @@ public class PutWarmerRequest extends MasterNodeOperationRequest> { +public abstract class AcknowledgedRequest extends MasterNodeOperationRequest { + 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 null. + */ + 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 null. + */ + protected void writeTimeout(StreamOutput out, Version minVersion) throws IOException { + if (minVersion == null || out.getVersion().onOrAfter(minVersion)) { + timeout.writeTo(out); + } + } + } diff --git a/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java b/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java index 90bf831a892..99b46fbda61 100644 --- a/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java +++ b/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java @@ -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); + } }