From 4640c03294bd2d20c1f1fd015040e44c70e67698 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 8 Jun 2022 08:58:18 -0400 Subject: [PATCH] Move the writeTo and read for get requests (#87467) This moves the writeTo methods next to the read requests for GET, MGET, and `_search` so they are easier to read. --- .../elasticsearch/action/get/GetRequest.java | 38 +++++++-------- .../action/get/MultiGetRequest.java | 18 +++---- .../action/get/MultiGetShardRequest.java | 46 +++++++++--------- .../search/internal/ShardSearchRequest.java | 48 +++++++++---------- 4 files changed, 75 insertions(+), 75 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/get/GetRequest.java b/server/src/main/java/org/elasticsearch/action/get/GetRequest.java index 9704777e9e8d8..23c412fe7795d 100644 --- a/server/src/main/java/org/elasticsearch/action/get/GetRequest.java +++ b/server/src/main/java/org/elasticsearch/action/get/GetRequest.java @@ -55,6 +55,8 @@ public class GetRequest extends SingleShardRequest implements Realti private VersionType versionType = VersionType.INTERNAL; private long version = Versions.MATCH_ANY; + public GetRequest() {} + GetRequest(StreamInput in) throws IOException { super(in); if (in.getVersion().before(Version.V_8_0_0)) { @@ -72,7 +74,23 @@ public class GetRequest extends SingleShardRequest implements Realti fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::readFrom); } - public GetRequest() {} + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + if (out.getVersion().before(Version.V_8_0_0)) { + out.writeString(MapperService.SINGLE_MAPPING_NAME); + } + out.writeString(id); + out.writeOptionalString(routing); + out.writeOptionalString(preference); + + out.writeBoolean(refresh); + out.writeOptionalStringArray(storedFields); + out.writeBoolean(realtime); + out.writeByte(versionType.getValue()); + out.writeLong(version); + out.writeOptionalWriteable(fetchSourceContext); + } /** * Constructs a new get request against the specified index. The {@link #id(String)} must also be set. @@ -224,24 +242,6 @@ public VersionType versionType() { return this.versionType; } - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - if (out.getVersion().before(Version.V_8_0_0)) { - out.writeString(MapperService.SINGLE_MAPPING_NAME); - } - out.writeString(id); - out.writeOptionalString(routing); - out.writeOptionalString(preference); - - out.writeBoolean(refresh); - out.writeOptionalStringArray(storedFields); - out.writeBoolean(realtime); - out.writeByte(versionType.getValue()); - out.writeLong(version); - out.writeOptionalWriteable(fetchSourceContext); - } - @Override public String toString() { return "get [" + index + "][" + id + "]: routing [" + routing + "]"; diff --git a/server/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java b/server/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java index 43a1fa9325bd5..2f0ed00c9760a 100644 --- a/server/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java +++ b/server/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java @@ -256,6 +256,15 @@ public MultiGetRequest(StreamInput in) throws IOException { items = in.readList(Item::new); } + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeOptionalString(preference); + out.writeBoolean(refresh); + out.writeBoolean(realtime); + out.writeList(items); + } + public List getItems() { return this.items; } @@ -541,15 +550,6 @@ public Iterator iterator() { return Collections.unmodifiableCollection(items).iterator(); } - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeOptionalString(preference); - out.writeBoolean(refresh); - out.writeBoolean(realtime); - out.writeList(items); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java b/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java index a1451ace11fba..60f38ce7b9883 100644 --- a/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java +++ b/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java @@ -27,6 +27,16 @@ public class MultiGetShardRequest extends SingleShardRequest locations; List items; + MultiGetShardRequest(MultiGetRequest multiGetRequest, String index, int shardId) { + super(index); + this.shardId = shardId; + locations = new ArrayList<>(); + items = new ArrayList<>(); + preference = multiGetRequest.preference; + realtime = multiGetRequest.realtime; + refresh = multiGetRequest.refresh; + } + MultiGetShardRequest(StreamInput in) throws IOException { super(in); int size = in.readVInt(); @@ -43,14 +53,19 @@ public class MultiGetShardRequest extends SingleShardRequest(); - items = new ArrayList<>(); - preference = multiGetRequest.preference; - realtime = multiGetRequest.realtime; - refresh = multiGetRequest.refresh; + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeVInt(locations.size()); + + for (int i = 0; i < locations.size(); i++) { + out.writeVInt(locations.get(i)); + items.get(i).writeTo(out); + } + + out.writeOptionalString(preference); + out.writeBoolean(refresh); + out.writeBoolean(realtime); } @Override @@ -107,19 +122,4 @@ public String[] indices() { } return indices; } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeVInt(locations.size()); - - for (int i = 0; i < locations.size(); i++) { - out.writeVInt(locations.get(i)); - items.get(i).writeTo(out); - } - - out.writeOptionalString(preference); - out.writeBoolean(refresh); - out.writeBoolean(realtime); - } } diff --git a/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java b/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java index 81cad60b68521..008e1638de9a4 100644 --- a/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java +++ b/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java @@ -242,6 +242,30 @@ public ShardSearchRequest( this.forceSyntheticSource = forceSyntheticSource; } + public ShardSearchRequest(ShardSearchRequest clone) { + this.shardId = clone.shardId; + this.shardRequestIndex = clone.shardRequestIndex; + this.searchType = clone.searchType; + this.numberOfShards = clone.numberOfShards; + this.scroll = clone.scroll; + this.source(clone.source); + this.aliasFilter = clone.aliasFilter; + this.indexBoost = clone.indexBoost; + this.nowInMillis = clone.nowInMillis; + this.requestCache = clone.requestCache; + this.clusterAlias = clone.clusterAlias; + this.allowPartialSearchResults = clone.allowPartialSearchResults; + this.canReturnNullResponseIfMatchNoDocs = clone.canReturnNullResponseIfMatchNoDocs; + this.bottomSortValues = clone.bottomSortValues; + this.originalIndices = clone.originalIndices; + this.readerId = clone.readerId; + this.keepAlive = clone.keepAlive; + this.channelVersion = clone.channelVersion; + this.waitForCheckpoint = clone.waitForCheckpoint; + this.waitForCheckpointsTimeout = clone.waitForCheckpointsTimeout; + this.forceSyntheticSource = clone.forceSyntheticSource; + } + public ShardSearchRequest(StreamInput in) throws IOException { super(in); shardId = new ShardId(in); @@ -302,30 +326,6 @@ public ShardSearchRequest(StreamInput in) throws IOException { originalIndices = OriginalIndices.readOriginalIndices(in); } - public ShardSearchRequest(ShardSearchRequest clone) { - this.shardId = clone.shardId; - this.shardRequestIndex = clone.shardRequestIndex; - this.searchType = clone.searchType; - this.numberOfShards = clone.numberOfShards; - this.scroll = clone.scroll; - this.source(clone.source); - this.aliasFilter = clone.aliasFilter; - this.indexBoost = clone.indexBoost; - this.nowInMillis = clone.nowInMillis; - this.requestCache = clone.requestCache; - this.clusterAlias = clone.clusterAlias; - this.allowPartialSearchResults = clone.allowPartialSearchResults; - this.canReturnNullResponseIfMatchNoDocs = clone.canReturnNullResponseIfMatchNoDocs; - this.bottomSortValues = clone.bottomSortValues; - this.originalIndices = clone.originalIndices; - this.readerId = clone.readerId; - this.keepAlive = clone.keepAlive; - this.channelVersion = clone.channelVersion; - this.waitForCheckpoint = clone.waitForCheckpoint; - this.waitForCheckpointsTimeout = clone.waitForCheckpointsTimeout; - this.forceSyntheticSource = clone.forceSyntheticSource; - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out);