Browse Source

Cut over MultiSearchResponse to Writeable (#41844)

Relates to #34389
Luca Cavanna 6 years ago
parent
commit
cfc12b4bea

+ 7 - 1
modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateAction.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.script.mustache;
 
 import org.elasticsearch.action.Action;
+import org.elasticsearch.common.io.stream.Writeable;
 
 public class MultiSearchTemplateAction extends Action<MultiSearchTemplateResponse> {
 
@@ -32,6 +33,11 @@ public class MultiSearchTemplateAction extends Action<MultiSearchTemplateRespons
 
     @Override
     public MultiSearchTemplateResponse newResponse() {
-        return new MultiSearchTemplateResponse();
+        throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
+    }
+
+    @Override
+    public Writeable.Reader<MultiSearchTemplateResponse> getResponseReader() {
+        return MultiSearchTemplateResponse::new;
     }
 }

+ 28 - 35
modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java

@@ -27,7 +27,7 @@ import org.elasticsearch.common.Nullable;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.io.stream.Streamable;
+import org.elasticsearch.common.io.stream.Writeable;
 import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.common.xcontent.ToXContent;
 import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -43,11 +43,19 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
     /**
      * A search template response item, holding the actual search template response, or an error message if it failed.
      */
-    public static class Item implements Streamable {
-        private SearchTemplateResponse response;
-        private Exception exception;
+    public static class Item implements Writeable {
+        private final SearchTemplateResponse response;
+        private final Exception exception;
 
-        Item() {
+        private Item(StreamInput in) throws IOException {
+            if (in.readBoolean()) {
+                this.response = new SearchTemplateResponse();
+                response.readFrom(in);
+                this.exception = null;
+            } else {
+                exception = in.readException();
+                this.response = null;
+            }
         }
 
         public Item(SearchTemplateResponse response, Exception exception) {
@@ -78,22 +86,6 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
             return this.response;
         }
 
-        public static Item readItem(StreamInput in) throws IOException {
-            Item item = new Item();
-            item.readFrom(in);
-            return item;
-        }
-
-        @Override
-        public void readFrom(StreamInput in) throws IOException {
-            if (in.readBoolean()) {
-                this.response = new SearchTemplateResponse();
-                response.readFrom(in);
-            } else {
-                exception = in.readException();
-            }
-        }
-
         @Override
         public void writeTo(StreamOutput out) throws IOException {
             if (response != null) {
@@ -113,17 +105,25 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
         public String toString() {
             return "Item [response=" + response + ", exception=" + exception + "]";
         }
-        
-        
     }
 
-    private Item[] items;
-    private long tookInMillis; 
+    private final Item[] items;
+    private final long tookInMillis;
     
-    MultiSearchTemplateResponse() {
+    MultiSearchTemplateResponse(StreamInput in) throws IOException {
+        super(in);
+        items = new Item[in.readVInt()];
+        for (int i = 0; i < items.length; i++) {
+            items[i] = new Item(in);
+        }
+        if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
+            tookInMillis = in.readVLong();
+        } else {
+            tookInMillis = -1L;
+        }
     }
 
-    public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
+    MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
         this.items = items;
         this.tookInMillis = tookInMillis;
     }    
@@ -149,14 +149,7 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
 
     @Override
     public void readFrom(StreamInput in) throws IOException {
-        super.readFrom(in);
-        items = new Item[in.readVInt()];
-        for (int i = 0; i < items.length; i++) {
-            items[i] = Item.readItem(in);
-        }
-        if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
-            tookInMillis = in.readVLong();
-        }
+        throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
     }
 
     @Override

+ 7 - 1
server/src/main/java/org/elasticsearch/action/search/MultiSearchAction.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.action.search;
 
 import org.elasticsearch.action.Action;
+import org.elasticsearch.common.io.stream.Writeable;
 
 public class MultiSearchAction extends Action<MultiSearchResponse> {
 
@@ -32,6 +33,11 @@ public class MultiSearchAction extends Action<MultiSearchResponse> {
 
     @Override
     public MultiSearchResponse newResponse() {
-        return new MultiSearchResponse();
+        throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
+    }
+
+    @Override
+    public Writeable.Reader<MultiSearchResponse> getResponseReader() {
+        return MultiSearchResponse::new;
     }
 }

+ 35 - 48
server/src/main/java/org/elasticsearch/action/search/MultiSearchResponse.java

@@ -27,7 +27,7 @@ import org.elasticsearch.common.ParseField;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.io.stream.Streamable;
+import org.elasticsearch.common.io.stream.Writeable;
 import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.common.xcontent.ConstructingObjectParser;
 import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -59,19 +59,37 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
     /**
      * A search response item, holding the actual search response, or an error message if it failed.
      */
-    public static class Item implements Streamable {
-        private SearchResponse response;
-        private Exception exception;
-
-        Item() {
-
-        }
+    public static class Item implements Writeable {
+        private final SearchResponse response;
+        private final Exception exception;
 
         public Item(SearchResponse response, Exception exception) {
             this.response = response;
             this.exception = exception;
         }
 
+        Item(StreamInput in) throws IOException{
+            if (in.readBoolean()) {
+                this.response = new SearchResponse();
+                this.response.readFrom(in);
+                this.exception = null;
+            } else {
+                this.exception = in.readException();
+                this.response = null;
+            }
+        }
+
+        @Override
+        public void writeTo(StreamOutput out) throws IOException {
+            if (response != null) {
+                out.writeBoolean(true);
+                response.writeTo(out);
+            } else {
+                out.writeBoolean(false);
+                out.writeException(exception);
+            }
+        }
+
         /**
          * Is it a failed search?
          */
@@ -95,47 +113,21 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
             return this.response;
         }
 
-        public static Item readItem(StreamInput in) throws IOException {
-            Item item = new Item();
-            item.readFrom(in);
-            return item;
-        }
-
-        @Override
-        public void readFrom(StreamInput in) throws IOException {
-            if (in.readBoolean()) {
-                this.response = new SearchResponse();
-                response.readFrom(in);
-            } else {
-                exception = in.readException();
-            }
-        }
-
-        @Override
-        public void writeTo(StreamOutput out) throws IOException {
-            if (response != null) {
-                out.writeBoolean(true);
-                response.writeTo(out);
-            } else {
-                out.writeBoolean(false);
-                out.writeException(exception);
-            }
-        }
-
         public Exception getFailure() {
             return exception;
         }
     }
 
-    private Item[] items;
-
-    private long tookInMillis;
-
-    MultiSearchResponse() {
-    }
+    private final Item[] items;
+    private final long tookInMillis;
 
     MultiSearchResponse(StreamInput in) throws IOException {
-        readFrom(in);
+        super(in);
+        items = new Item[in.readVInt()];
+        for (int i = 0; i < items.length; i++) {
+            items[i] = new Item(in);
+        }
+        tookInMillis = in.readVLong();
     }
 
     public MultiSearchResponse(Item[] items, long tookInMillis) {
@@ -164,12 +156,7 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
 
     @Override
     public void readFrom(StreamInput in) throws IOException {
-        super.readFrom(in);
-        items = new Item[in.readVInt()];
-        for (int i = 0; i < items.length; i++) {
-            items[i] = Item.readItem(in);
-        }
-        tookInMillis = in.readVLong();
+        throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
     }
 
     @Override