14
14
import org .elasticsearch .common .io .stream .StreamOutput ;
15
15
import org .elasticsearch .common .io .stream .Writeable ;
16
16
import org .elasticsearch .common .lucene .Lucene ;
17
- import org .elasticsearch .common .xcontent .ObjectParser ;
17
+ import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
18
18
import org .elasticsearch .common .xcontent .ToXContentFragment ;
19
19
import org .elasticsearch .common .xcontent .ToXContentObject ;
20
20
import org .elasticsearch .common .xcontent .XContentBuilder ;
25
25
import java .io .IOException ;
26
26
import java .util .ArrayList ;
27
27
import java .util .Arrays ;
28
+ import java .util .Collections ;
28
29
import java .util .List ;
29
30
import java .util .Objects ;
30
- import java .util .function .Supplier ;
31
31
32
- import static org .elasticsearch .common .xcontent .ObjectParser .fromList ;
33
32
import static org .elasticsearch .common .xcontent .XContentParserUtils .ensureExpectedToken ;
34
33
35
34
79
78
*/
80
79
public class EqlSearchResponse extends ActionResponse implements ToXContentObject {
81
80
82
- private Hits hits ;
83
- private long tookInMillis ;
84
- private boolean isTimeout ;
81
+ private final Hits hits ;
82
+ private final long tookInMillis ;
83
+ private final boolean isTimeout ;
85
84
86
85
private static final class Fields {
87
86
static final String TOOK = "took" ;
@@ -93,25 +92,25 @@ private static final class Fields {
93
92
private static final ParseField TIMED_OUT = new ParseField (Fields .TIMED_OUT );
94
93
private static final ParseField HITS = new ParseField (Fields .HITS );
95
94
96
- private static final ObjectParser <EqlSearchResponse , Void > PARSER = objectParser ( EqlSearchResponse :: new );
97
-
98
- private static < R extends EqlSearchResponse > ObjectParser < R , Void > objectParser ( Supplier < R > supplier ) {
99
- ObjectParser < R , Void > parser = new ObjectParser <>( "eql/search_response" , false , supplier ) ;
100
- parser . declareLong ( EqlSearchResponse :: took , TOOK ) ;
101
- parser . declareBoolean ( EqlSearchResponse :: isTimeout , TIMED_OUT ) ;
102
- parser . declareObject ( EqlSearchResponse :: hits ,
103
- ( p , c ) -> Hits . fromXContent ( p ), HITS );
104
- return parser ;
105
- }
106
-
107
- // Constructor for parser from json
108
- protected EqlSearchResponse () {
109
- super ( );
95
+ private static final ConstructingObjectParser <EqlSearchResponse , Void > PARSER =
96
+ new ConstructingObjectParser <>( "eql/search_response" , true ,
97
+ args -> {
98
+ int i = 0 ;
99
+ Hits hits = ( Hits ) args [ i ++] ;
100
+ Long took = ( Long ) args [ i ++] ;
101
+ Boolean timeout = ( Boolean ) args [ i ];
102
+ return new EqlSearchResponse ( hits , took , timeout );
103
+ }) ;
104
+
105
+ static {
106
+ PARSER . declareObject ( ConstructingObjectParser . constructorArg (), ( p , c ) -> Hits . fromXContent ( p ), HITS );
107
+ PARSER . declareLong ( ConstructingObjectParser . constructorArg (), TOOK );
108
+ PARSER . declareBoolean ( ConstructingObjectParser . constructorArg (), TIMED_OUT );
110
109
}
111
110
112
111
public EqlSearchResponse (Hits hits , long tookInMillis , boolean isTimeout ) {
113
112
super ();
114
- this .hits ( hits ) ;
113
+ this .hits = hits == null ? Hits . EMPTY : hits ;
115
114
this .tookInMillis = tookInMillis ;
116
115
this .isTimeout = isTimeout ;
117
116
}
@@ -152,30 +151,14 @@ public long took() {
152
151
return tookInMillis ;
153
152
}
154
153
155
- public void took (long tookInMillis ) {
156
- this .tookInMillis = tookInMillis ;
157
- }
158
-
159
154
public boolean isTimeout () {
160
155
return isTimeout ;
161
156
}
162
157
163
- public void isTimeout (boolean isTimeout ) {
164
- this .isTimeout = isTimeout ;
165
- }
166
-
167
158
public Hits hits () {
168
159
return hits ;
169
160
}
170
161
171
- public void hits (Hits hits ) {
172
- if (hits == null ) {
173
- this .hits = new Hits ((Events )null , null );
174
- } else {
175
- this .hits = hits ;
176
- }
177
- }
178
-
179
162
@ Override
180
163
public boolean equals (Object o ) {
181
164
if (this == o ) {
@@ -210,60 +193,40 @@ private static final class Fields {
210
193
private static final ParseField JOIN_KEYS = new ParseField (Fields .JOIN_KEYS );
211
194
private static final ParseField EVENTS = new ParseField (Events .NAME );
212
195
213
- private static final ObjectParser <EqlSearchResponse .Sequence , Void > PARSER = objectParser (EqlSearchResponse .Sequence ::new );
196
+ private static final ConstructingObjectParser <EqlSearchResponse .Sequence , Void > PARSER =
197
+ new ConstructingObjectParser <>("eql/search_response_sequence" , true ,
198
+ args -> {
199
+ int i = 0 ;
200
+ @ SuppressWarnings ("unchecked" ) List <String > joinKeys = (List <String >) args [i ++];
201
+ @ SuppressWarnings ("unchecked" ) Events events = new Events (((List <SearchHit >) args [i ]).toArray (new SearchHit [0 ]));
202
+ return new EqlSearchResponse .Sequence (joinKeys , events );
203
+ });
214
204
215
- private static <R extends EqlSearchResponse .Sequence > ObjectParser <R , Void > objectParser (Supplier <R > supplier ) {
216
- ObjectParser <R , Void > parser = new ObjectParser <>("eql/search_response_sequence" , false , supplier );
217
- parser .declareStringArray (fromList (String .class , EqlSearchResponse .Sequence ::joinKeys ), JOIN_KEYS );
218
- parser .declareObjectArray (Sequence ::setEvents ,
219
- (p , c ) -> SearchHit .fromXContent (p ), EVENTS );
220
- return parser ;
205
+ static {
206
+ PARSER .declareStringArray (ConstructingObjectParser .optionalConstructorArg (), JOIN_KEYS );
207
+ PARSER .declareObjectArray (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> SearchHit .fromXContent (p ), EVENTS );
221
208
}
222
209
223
- private String [] joinKeys = null ;
224
- private Events events = null ;
210
+ private final List < String > joinKeys ;
211
+ private final Events events ;
225
212
226
- private Sequence (){
227
- this (null , null );
228
- }
229
-
230
- public Sequence (String [] joinKeys , Events events ) {
231
- this .joinKeys (joinKeys );
232
- if (events == null ) {
233
- this .events = new Events ((SearchHit [])(null ));
234
- } else {
235
- this .events = events ;
236
- }
213
+ public Sequence (List <String > joinKeys , Events events ) {
214
+ this .joinKeys = joinKeys == null ? Collections .emptyList () : joinKeys ;
215
+ this .events = events == null ? Events .EMPTY : events ;
237
216
}
238
217
239
218
public Sequence (StreamInput in ) throws IOException {
240
- this .joinKeys = in .readStringArray ();
219
+ this .joinKeys = in .readStringList ();
241
220
this .events = new Events (in );
242
221
}
243
222
244
- public void joinKeys (String [] joinKeys ) {
245
- if (joinKeys == null ) {
246
- this .joinKeys = new String [0 ];
247
- } else {
248
- this .joinKeys = joinKeys ;
249
- }
250
- }
251
-
252
- private void setEvents (List <SearchHit > hits ) {
253
- if (hits == null ) {
254
- this .events = new Events ((SearchHit [])(null ));
255
- } else {
256
- this .events = new Events (hits .toArray (new SearchHit [hits .size ()]));
257
- }
258
- }
259
-
260
223
public static Sequence fromXContent (XContentParser parser ) {
261
224
return PARSER .apply (parser , null );
262
225
}
263
226
264
227
@ Override
265
228
public void writeTo (StreamOutput out ) throws IOException {
266
- out .writeStringArray (joinKeys );
229
+ out .writeStringCollection (joinKeys );
267
230
out .writeVInt (events .entries ().length );
268
231
if (events .entries ().length > 0 ) {
269
232
for (SearchHit hit : events .entries ()) {
@@ -298,13 +261,13 @@ public boolean equals(Object o) {
298
261
return false ;
299
262
}
300
263
Sequence that = (Sequence ) o ;
301
- return Arrays .equals (joinKeys , that .joinKeys )
264
+ return Objects .equals (joinKeys , that .joinKeys )
302
265
&& Objects .equals (events , that .events );
303
266
}
304
267
305
268
@ Override
306
269
public int hashCode () {
307
- return Objects .hash (Arrays . hashCode ( joinKeys ) , events );
270
+ return Objects .hash (joinKeys , events );
308
271
}
309
272
}
310
273
@@ -316,70 +279,58 @@ private static final class Fields {
316
279
static final String PERCENT = "_percent" ;
317
280
}
318
281
319
- private int count ;
320
- private String [] keys ;
321
- private float percent ;
282
+ private final int count ;
283
+ private final List < String > keys ;
284
+ private final float percent ;
322
285
323
286
private static final ParseField COUNT = new ParseField (Fields .COUNT );
324
287
private static final ParseField KEYS = new ParseField (Fields .KEYS );
325
288
private static final ParseField PERCENT = new ParseField (Fields .PERCENT );
326
289
327
- private static final ObjectParser <EqlSearchResponse .Count , Void > PARSER = objectParser (EqlSearchResponse .Count ::new );
290
+ private static final ConstructingObjectParser <EqlSearchResponse .Count , Void > PARSER =
291
+ new ConstructingObjectParser <>("eql/search_response_count" , true ,
292
+ args -> {
293
+ int i = 0 ;
294
+ int count = (int ) args [i ++];
295
+ @ SuppressWarnings ("unchecked" ) List <String > joinKeys = (List <String >) args [i ++];
296
+ float percent = (float ) args [i ];
297
+ return new EqlSearchResponse .Count (count , joinKeys , percent );
298
+ });
328
299
329
- protected static <R extends EqlSearchResponse .Count > ObjectParser <R , Void > objectParser (Supplier <R > supplier ) {
330
- ObjectParser <R , Void > parser = new ObjectParser <>("eql/search_response_count" , false , supplier );
331
- parser .declareInt (EqlSearchResponse .Count ::count , COUNT );
332
- parser .declareStringArray (fromList (String .class , EqlSearchResponse .Count ::keys ), KEYS );
333
- parser .declareFloat (EqlSearchResponse .Count ::percent , PERCENT );
334
- return parser ;
300
+ static {
301
+ PARSER .declareInt (ConstructingObjectParser .constructorArg (), COUNT );
302
+ PARSER .declareStringArray (ConstructingObjectParser .constructorArg (), KEYS );
303
+ PARSER .declareFloat (ConstructingObjectParser .constructorArg (), PERCENT );
335
304
}
336
305
337
- private Count () {}
338
-
339
- public Count (int count , String [] keys , float percent ) {
306
+ public Count (int count , List <String > keys , float percent ) {
340
307
this .count = count ;
341
- this .keys ( keys ) ;
308
+ this .keys = keys == null ? Collections . emptyList () : keys ;
342
309
this .percent = percent ;
343
310
}
344
311
345
312
public Count (StreamInput in ) throws IOException {
346
313
count = in .readVInt ();
347
- keys = in .readStringArray ();
314
+ keys = in .readStringList ();
348
315
percent = in .readFloat ();
349
316
}
350
317
351
- public void count (int count ) {
352
- this .count = count ;
353
- }
354
-
355
- public void keys (String [] keys ) {
356
- if (keys == null ) {
357
- this .keys = new String [0 ];
358
- } else {
359
- this .keys = keys ;
360
- }
361
- }
362
-
363
- public void percent (float percent ) {
364
- this .percent = percent ;
365
- }
366
-
367
318
public static Count fromXContent (XContentParser parser ) {
368
319
return PARSER .apply (parser , null );
369
320
}
370
321
371
322
@ Override
372
323
public void writeTo (StreamOutput out ) throws IOException {
373
324
out .writeVInt (count );
374
- out .writeStringArray (keys );
325
+ out .writeStringCollection (keys );
375
326
out .writeFloat (percent );
376
327
}
377
328
378
329
@ Override
379
330
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
380
331
builder .startObject ();
381
332
builder .field (Fields .COUNT , count );
382
- builder .array (Fields .KEYS , keys );
333
+ builder .field (Fields .KEYS , keys );
383
334
builder .field (Fields .PERCENT , percent );
384
335
builder .endObject ();
385
336
return builder ;
@@ -395,13 +346,13 @@ public boolean equals(Object o) {
395
346
}
396
347
Count that = (Count ) o ;
397
348
return Objects .equals (count , that .count )
398
- && Arrays .equals (keys , that .keys )
349
+ && Objects .equals (keys , that .keys )
399
350
&& Objects .equals (percent , that .percent );
400
351
}
401
352
402
353
@ Override
403
354
public int hashCode () {
404
- return Objects .hash (count , Arrays . hashCode ( keys ) , percent );
355
+ return Objects .hash (count , keys , percent );
405
356
}
406
357
}
407
358
@@ -477,6 +428,7 @@ public int hashCode() {
477
428
478
429
// Events
479
430
public static class Events extends EqlSearchResponse .Entries <SearchHit > {
431
+ private static final Events EMPTY = new Events ((SearchHit []) null );
480
432
private static final String NAME = "events" ;
481
433
482
434
public Events (SearchHit [] entries ) {
@@ -545,6 +497,8 @@ protected final Count createEntry(StreamInput in) throws IOException {
545
497
546
498
// Hits
547
499
public static class Hits implements Writeable , ToXContentFragment {
500
+ public static final Hits EMPTY = new Hits ((Events )null , null );
501
+
548
502
private Events events = null ;
549
503
private Sequences sequences = null ;
550
504
private Counts counts = null ;
0 commit comments