99
1010package org .elasticsearch .sample ;
1111
12- import org .elasticsearch .action .ActionRequest ;
1312import org .elasticsearch .action .ActionRequestValidationException ;
14- import org .elasticsearch .action .ActionResponse ;
1513import org .elasticsearch .action .ActionType ;
14+ import org .elasticsearch .action .FailedNodeException ;
1615import org .elasticsearch .action .IndicesRequest ;
1716import org .elasticsearch .action .index .IndexRequest ;
1817import org .elasticsearch .action .support .IndicesOptions ;
18+ import org .elasticsearch .action .support .nodes .BaseNodeResponse ;
19+ import org .elasticsearch .action .support .nodes .BaseNodesRequest ;
20+ import org .elasticsearch .action .support .nodes .BaseNodesResponse ;
21+ import org .elasticsearch .cluster .ClusterName ;
22+ import org .elasticsearch .cluster .node .DiscoveryNode ;
1923import org .elasticsearch .common .collect .Iterators ;
2024import org .elasticsearch .common .io .stream .StreamInput ;
2125import org .elasticsearch .common .io .stream .StreamOutput ;
26+ import org .elasticsearch .common .io .stream .Writeable ;
2227import org .elasticsearch .common .xcontent .ChunkedToXContent ;
2328import org .elasticsearch .tasks .CancellableTask ;
2429import org .elasticsearch .tasks .Task ;
2530import org .elasticsearch .tasks .TaskId ;
31+ import org .elasticsearch .transport .AbstractTransportRequest ;
2632import org .elasticsearch .xcontent .ToXContent ;
2733
2834import java .io .IOException ;
35+ import java .util .Collection ;
2936import java .util .Iterator ;
3037import java .util .List ;
3138import java .util .Map ;
@@ -43,28 +50,35 @@ private GetSampleAction() {
4350 super (NAME );
4451 }
4552
46- public static class Response extends ActionResponse implements ChunkedToXContent {
53+ public static class Response extends BaseNodesResponse < NodeResponse > implements Writeable , ChunkedToXContent {
4754
48- private final List <IndexRequest > samples ;
55+ public Response (StreamInput in ) throws IOException {
56+ super (in );
57+ }
4958
50- public Response (final List <IndexRequest > samples ) {
51- this . samples = samples ;
59+ public Response (ClusterName clusterName , List <NodeResponse > nodes , List < FailedNodeException > failures ) {
60+ super ( clusterName , nodes , failures ) ;
5261 }
5362
5463 public List <IndexRequest > getSamples () {
55- return samples ;
64+ return getNodes (). stream (). map ( n -> n . samples ). filter ( Objects :: nonNull ). flatMap ( Collection :: stream ). toList () ;
5665 }
5766
5867 @ Override
59- public void writeTo (StreamOutput out ) throws IOException {
60- out .writeCollection (samples );
68+ protected List <NodeResponse > readNodesFrom (StreamInput in ) throws IOException {
69+ return in .readCollectionAsList (NodeResponse ::new );
70+ }
71+
72+ @ Override
73+ protected void writeNodesTo (StreamOutput out , List <NodeResponse > nodes ) throws IOException {
74+ out .writeCollection (nodes );
6175 }
6276
6377 @ Override
6478 public Iterator <? extends ToXContent > toXContentChunked (ToXContent .Params params ) {
6579 return Iterators .concat (
6680 chunk ((builder , p ) -> builder .startObject ().startArray ("samples" )),
67- Iterators .flatMap (samples .iterator (), sample -> single ((builder , params1 ) -> {
81+ Iterators .flatMap (getSamples () .iterator (), sample -> single ((builder , params1 ) -> {
6882 Map <String , Object > source = sample .sourceAsMap ();
6983 builder .value (source );
7084 return builder ;
@@ -75,40 +89,64 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
7589
7690 @ Override
7791 public boolean equals (Object o ) {
78- if (this == o ) {
79- return true ;
80- }
81- if (o == null || getClass () != o .getClass ()) {
82- return false ;
83- }
84- GetSampleAction .Response response = (GetSampleAction .Response ) o ;
85- return samples .equals (response .samples );
92+ if (this == o ) return true ;
93+ if (o == null || getClass () != o .getClass ()) return false ;
94+ Response that = (Response ) o ;
95+ return Objects .equals (getNodes (), that .getNodes ()) && Objects .equals (failures (), that .failures ());
8696 }
8797
8898 @ Override
8999 public int hashCode () {
90- return Objects .hash (samples );
100+ return Objects .hash (getNodes (), failures ());
101+ }
102+
103+ }
104+
105+ public static class NodeResponse extends BaseNodeResponse {
106+ private final List <IndexRequest > samples ;
107+
108+ protected NodeResponse (StreamInput in ) throws IOException {
109+ super (in );
110+ samples = in .readCollectionAsList (IndexRequest ::new );
111+ }
112+
113+ protected NodeResponse (DiscoveryNode node , List <IndexRequest > samples ) {
114+ super (node );
115+ this .samples = samples ;
116+ }
117+
118+ public List <IndexRequest > getSamples () {
119+ return samples ;
91120 }
92121
93122 @ Override
94- public String toString () {
95- return "Response{samples=" + samples + '}' ;
123+ public void writeTo (StreamOutput out ) throws IOException {
124+ super .writeTo (out );
125+ out .writeCollection (samples );
126+ }
127+
128+ @ Override
129+ public boolean equals (Object o ) {
130+ if (this == o ) return true ;
131+ if (o == null || getClass () != o .getClass ()) return false ;
132+ NodeResponse that = (NodeResponse ) o ;
133+ return samples .equals (that .samples );
134+ }
135+
136+ @ Override
137+ public int hashCode () {
138+ return Objects .hash (samples );
96139 }
97140 }
98141
99- public static class Request extends ActionRequest implements IndicesRequest .Replaceable {
142+ public static class Request extends BaseNodesRequest implements IndicesRequest .Replaceable {
100143 private String [] names ;
101144
102145 public Request (String [] names ) {
103- super ();
146+ super (( String []) null );
104147 this .names = names ;
105148 }
106149
107- public Request (StreamInput in ) throws IOException {
108- super (in );
109- this .names = in .readStringArray ();
110- }
111-
112150 @ Override
113151 public Task createTask (long id , String type , String action , TaskId parentTaskId , Map <String , String > headers ) {
114152 return new CancellableTask (id , type , action , "" , parentTaskId , headers );
@@ -138,4 +176,27 @@ public IndicesOptions indicesOptions() {
138176 return IndicesOptions .DEFAULT ;
139177 }
140178 }
179+
180+ public static class NodeRequest extends AbstractTransportRequest {
181+ private final String index ;
182+
183+ public NodeRequest (String index ) {
184+ this .index = index ;
185+ }
186+
187+ public NodeRequest (StreamInput in ) throws IOException {
188+ super (in );
189+ this .index = in .readString ();
190+ }
191+
192+ @ Override
193+ public void writeTo (StreamOutput out ) throws IOException {
194+ super .writeTo (out );
195+ out .writeString (index );
196+ }
197+
198+ public String getIndex () {
199+ return index ;
200+ }
201+ }
141202}
0 commit comments