Use Supplier for StreamInput#readOptionalStreamable

This commit changes the signature of StreamInput#readOptionalStreamable
to accept a Supplier to create new streamables rather than requiring
callers to construct new instances. This has the advantage of avoiding
an allocation in cases when the stream indicates the resulting
streamable is null
This commit is contained in:
Jason Tedor 2015-11-17 11:25:35 -06:00
parent 4975422a35
commit 1cd4a29b6f
14 changed files with 21 additions and 19 deletions

View File

@ -223,8 +223,8 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
http = HttpStats.readHttpStats(in); http = HttpStats.readHttpStats(in);
} }
breaker = AllCircuitBreakerStats.readOptionalAllCircuitBreakerStats(in); breaker = AllCircuitBreakerStats.readOptionalAllCircuitBreakerStats(in);
scriptStats = in.readOptionalStreamable(new ScriptStats()); scriptStats = in.readOptionalStreamable(ScriptStats::new);
discoveryStats = in.readOptionalStreamable(new DiscoveryStats(null)); discoveryStats = in.readOptionalStreamable(() -> new DiscoveryStats(null));
} }

View File

@ -553,10 +553,10 @@ public class CommonStats implements Streamable, ToXContent {
if (in.readBoolean()) { if (in.readBoolean()) {
segments = SegmentsStats.readSegmentsStats(in); segments = SegmentsStats.readSegmentsStats(in);
} }
translog = in.readOptionalStreamable(new TranslogStats()); translog = in.readOptionalStreamable(TranslogStats::new);
suggest = in.readOptionalStreamable(new SuggestStats()); suggest = in.readOptionalStreamable(SuggestStats::new);
requestCache = in.readOptionalStreamable(new RequestCacheStats()); requestCache = in.readOptionalStreamable(RequestCacheStats::new);
recoveryStats = in.readOptionalStreamable(new RecoveryStats()); recoveryStats = in.readOptionalStreamable(RecoveryStats::new);
} }
@Override @Override

View File

@ -346,7 +346,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
indicesOptions = IndicesOptions.readIndicesOptions(in); indicesOptions = IndicesOptions.readIndicesOptions(in);
requestCache = in.readOptionalBoolean(); requestCache = in.readOptionalBoolean();
template = in.readOptionalStreamable(new Template()); template = in.readOptionalStreamable(Template::new);
} }
@Override @Override

View File

@ -68,7 +68,7 @@ public class RestoreSource implements Streamable, ToXContent {
} }
public static RestoreSource readOptionalRestoreSource(StreamInput in) throws IOException { public static RestoreSource readOptionalRestoreSource(StreamInput in) throws IOException {
return in.readOptionalStreamable(new RestoreSource()); return in.readOptionalStreamable(RestoreSource::new);
} }
@Override @Override

View File

@ -39,7 +39,7 @@ public class RoutingValidationException extends RoutingException {
public RoutingValidationException(StreamInput in) throws IOException { public RoutingValidationException(StreamInput in) throws IOException {
super(in); super(in);
validation = in.readOptionalStreamable(new RoutingTableValidation()); validation = in.readOptionalStreamable(RoutingTableValidation::new);
} }
@Override @Override

View File

@ -52,6 +52,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
import static org.elasticsearch.ElasticsearchException.readException; import static org.elasticsearch.ElasticsearchException.readException;
import static org.elasticsearch.ElasticsearchException.readStackTrace; import static org.elasticsearch.ElasticsearchException.readStackTrace;
@ -517,8 +518,9 @@ public abstract class StreamInput extends InputStream {
/** /**
* Serializes a potential null value. * Serializes a potential null value.
*/ */
public <T extends Streamable> T readOptionalStreamable(T streamable) throws IOException { public <T extends Streamable> T readOptionalStreamable(Supplier<T> supplier) throws IOException {
if (readBoolean()) { if (readBoolean()) {
T streamable = supplier.get();
streamable.readFrom(this); streamable.readFrom(this);
return streamable; return streamable;
} else { } else {

View File

@ -62,7 +62,7 @@ public final class CommitStats implements Streamable, ToXContent {
} }
public static CommitStats readOptionalCommitStatsFrom(StreamInput in) throws IOException { public static CommitStats readOptionalCommitStatsFrom(StreamInput in) throws IOException {
return in.readOptionalStreamable(new CommitStats()); return in.readOptionalStreamable(CommitStats::new);
} }

View File

@ -57,7 +57,7 @@ public class AllCircuitBreakerStats implements Streamable, ToXContent {
} }
public static AllCircuitBreakerStats readOptionalAllCircuitBreakerStats(StreamInput in) throws IOException { public static AllCircuitBreakerStats readOptionalAllCircuitBreakerStats(StreamInput in) throws IOException {
AllCircuitBreakerStats stats = in.readOptionalStreamable(new AllCircuitBreakerStats()); AllCircuitBreakerStats stats = in.readOptionalStreamable(AllCircuitBreakerStats::new);
return stats; return stats;
} }

View File

@ -74,7 +74,7 @@ public class CircuitBreakerStats implements Streamable, ToXContent {
} }
public static CircuitBreakerStats readOptionalCircuitBreakerStats(StreamInput in) throws IOException { public static CircuitBreakerStats readOptionalCircuitBreakerStats(StreamInput in) throws IOException {
CircuitBreakerStats stats = in.readOptionalStreamable(new CircuitBreakerStats()); CircuitBreakerStats stats = in.readOptionalStreamable(CircuitBreakerStats::new);
return stats; return stats;
} }

View File

@ -194,7 +194,7 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl
} }
public static InternalAggregations readOptionalAggregations(StreamInput in) throws IOException { public static InternalAggregations readOptionalAggregations(StreamInput in) throws IOException {
return in.readOptionalStreamable(new InternalAggregations()); return in.readOptionalStreamable(InternalAggregations::new);
} }
@Override @Override

View File

@ -559,7 +559,7 @@ public class InternalSearchHit implements SearchHit {
score = in.readFloat(); score = in.readFloat();
id = in.readText(); id = in.readText();
type = in.readText(); type = in.readText();
nestedIdentity = in.readOptionalStreamable(new InternalNestedIdentity()); nestedIdentity = in.readOptionalStreamable(InternalNestedIdentity::new);
version = in.readLong(); version = in.readLong();
source = in.readBytesReference(); source = in.readBytesReference();
if (source.length() == 0) { if (source.length() == 0) {
@ -810,7 +810,7 @@ public class InternalSearchHit implements SearchHit {
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
field = in.readOptionalText(); field = in.readOptionalText();
offset = in.readInt(); offset = in.readInt();
child = in.readOptionalStreamable(new InternalNestedIdentity()); child = in.readOptionalStreamable(InternalNestedIdentity::new);
} }
@Override @Override

View File

@ -180,7 +180,7 @@ public class ShardSearchLocalRequest extends ContextAndHeaderHolder implements S
types = in.readStringArray(); types = in.readStringArray();
filteringAliases = in.readStringArray(); filteringAliases = in.readStringArray();
nowInMillis = in.readVLong(); nowInMillis = in.readVLong();
template = in.readOptionalStreamable(new Template()); template = in.readOptionalStreamable(Template::new);
requestCache = in.readOptionalBoolean(); requestCache = in.readOptionalBoolean();
} }

View File

@ -190,7 +190,7 @@ public class RestoreInfo implements ToXContent, Streamable {
* @return restore info * @return restore info
*/ */
public static RestoreInfo readOptionalRestoreInfo(StreamInput in) throws IOException { public static RestoreInfo readOptionalRestoreInfo(StreamInput in) throws IOException {
return in.readOptionalStreamable(new RestoreInfo()); return in.readOptionalStreamable(RestoreInfo::new);
} }
} }

View File

@ -324,7 +324,7 @@ public class SnapshotInfo implements ToXContent, Streamable {
* @return deserialized snapshot info or null * @return deserialized snapshot info or null
*/ */
public static SnapshotInfo readOptionalSnapshotInfo(StreamInput in) throws IOException { public static SnapshotInfo readOptionalSnapshotInfo(StreamInput in) throws IOException {
return in.readOptionalStreamable(new SnapshotInfo()); return in.readOptionalStreamable(SnapshotInfo::new);
} }
} }