2323import org .elasticsearch .common .Strings ;
2424import org .elasticsearch .common .io .stream .StreamInput ;
2525import org .elasticsearch .common .io .stream .StreamOutput ;
26- import org .elasticsearch .common .io .stream .Streamable ;
26+ import org .elasticsearch .common .io .stream .Writeable ;
2727import org .elasticsearch .common .unit .TimeValue ;
2828import org .elasticsearch .common .xcontent .ToXContent ;
2929import org .elasticsearch .common .xcontent .ToXContentFragment ;
3030import org .elasticsearch .common .xcontent .XContentBuilder ;
3131
3232import java .io .IOException ;
33+ import java .util .Collections ;
3334import java .util .HashMap ;
3435import java .util .Map ;
3536
36- public class SearchStats implements Streamable , ToXContentFragment {
37+ public class SearchStats implements Writeable , ToXContentFragment {
3738
38- public static class Stats implements Streamable , ToXContentFragment {
39+ public static class Stats implements Writeable , ToXContentFragment {
3940
4041 private long queryCount ;
4142 private long queryTimeInMillis ;
@@ -53,8 +54,8 @@ public static class Stats implements Streamable, ToXContentFragment {
5354 private long suggestTimeInMillis ;
5455 private long suggestCurrent ;
5556
56- Stats () {
57-
57+ private Stats () {
58+ // for internal use, initializes all counts to 0
5859 }
5960
6061 public Stats (
@@ -78,16 +79,24 @@ public Stats(
7879 this .suggestCount = suggestCount ;
7980 this .suggestTimeInMillis = suggestTimeInMillis ;
8081 this .suggestCurrent = suggestCurrent ;
81-
8282 }
8383
84- public Stats (Stats stats ) {
85- this (
86- stats .queryCount , stats .queryTimeInMillis , stats .queryCurrent ,
87- stats .fetchCount , stats .fetchTimeInMillis , stats .fetchCurrent ,
88- stats .scrollCount , stats .scrollTimeInMillis , stats .scrollCurrent ,
89- stats .suggestCount , stats .suggestTimeInMillis , stats .suggestCurrent
90- );
84+ private Stats (StreamInput in ) throws IOException {
85+ queryCount = in .readVLong ();
86+ queryTimeInMillis = in .readVLong ();
87+ queryCurrent = in .readVLong ();
88+
89+ fetchCount = in .readVLong ();
90+ fetchTimeInMillis = in .readVLong ();
91+ fetchCurrent = in .readVLong ();
92+
93+ scrollCount = in .readVLong ();
94+ scrollTimeInMillis = in .readVLong ();
95+ scrollCurrent = in .readVLong ();
96+
97+ suggestCount = in .readVLong ();
98+ suggestTimeInMillis = in .readVLong ();
99+ suggestCurrent = in .readVLong ();
91100 }
92101
93102 public void add (Stats stats ) {
@@ -173,28 +182,7 @@ public long getSuggestCurrent() {
173182 }
174183
175184 public static Stats readStats (StreamInput in ) throws IOException {
176- Stats stats = new Stats ();
177- stats .readFrom (in );
178- return stats ;
179- }
180-
181- @ Override
182- public void readFrom (StreamInput in ) throws IOException {
183- queryCount = in .readVLong ();
184- queryTimeInMillis = in .readVLong ();
185- queryCurrent = in .readVLong ();
186-
187- fetchCount = in .readVLong ();
188- fetchTimeInMillis = in .readVLong ();
189- fetchCurrent = in .readVLong ();
190-
191- scrollCount = in .readVLong ();
192- scrollTimeInMillis = in .readVLong ();
193- scrollCurrent = in .readVLong ();
194-
195- suggestCount = in .readVLong ();
196- suggestTimeInMillis = in .readVLong ();
197- suggestCurrent = in .readVLong ();
185+ return new Stats (in );
198186 }
199187
200188 @ Override
@@ -238,11 +226,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
238226 }
239227 }
240228
241- Stats totalStats ;
242- long openContexts ;
229+ private final Stats totalStats ;
230+ private long openContexts ;
243231
244232 @ Nullable
245- Map <String , Stats > groupStats ;
233+ private Map <String , Stats > groupStats ;
246234
247235 public SearchStats () {
248236 totalStats = new Stats ();
@@ -254,27 +242,27 @@ public SearchStats(Stats totalStats, long openContexts, @Nullable Map<String, St
254242 this .groupStats = groupStats ;
255243 }
256244
257- public void add (SearchStats searchStats ) {
258- add (searchStats , true );
245+ public SearchStats (StreamInput in ) throws IOException {
246+ totalStats = Stats .readStats (in );
247+ openContexts = in .readVLong ();
248+ if (in .readBoolean ()) {
249+ groupStats = in .readMap (StreamInput ::readString , Stats ::readStats );
250+ }
259251 }
260252
261- public void add (SearchStats searchStats , boolean includeTypes ) {
253+ public void add (SearchStats searchStats ) {
262254 if (searchStats == null ) {
263255 return ;
264256 }
265257 addTotals (searchStats );
266258 openContexts += searchStats .openContexts ;
267- if (includeTypes && searchStats .groupStats != null && !searchStats .groupStats .isEmpty ()) {
259+ if (searchStats .groupStats != null && !searchStats .groupStats .isEmpty ()) {
268260 if (groupStats == null ) {
269261 groupStats = new HashMap <>(searchStats .groupStats .size ());
270262 }
271263 for (Map .Entry <String , Stats > entry : searchStats .groupStats .entrySet ()) {
272- Stats stats = groupStats .get (entry .getKey ());
273- if (stats == null ) {
274- groupStats .put (entry .getKey (), new Stats (entry .getValue ()));
275- } else {
276- stats .add (entry .getValue ());
277- }
264+ groupStats .putIfAbsent (entry .getKey (), new Stats ());
265+ groupStats .get (entry .getKey ()).add (entry .getValue ());
278266 }
279267 }
280268 }
@@ -296,7 +284,7 @@ public long getOpenContexts() {
296284
297285 @ Nullable
298286 public Map <String , Stats > getGroupStats () {
299- return this .groupStats ;
287+ return this .groupStats != null ? Collections . unmodifiableMap ( this . groupStats ) : null ;
300288 }
301289
302290 @ Override
@@ -344,15 +332,6 @@ static final class Fields {
344332 static final String SUGGEST_CURRENT = "suggest_current" ;
345333 }
346334
347- @ Override
348- public void readFrom (StreamInput in ) throws IOException {
349- totalStats = Stats .readStats (in );
350- openContexts = in .readVLong ();
351- if (in .readBoolean ()) {
352- groupStats = in .readMap (StreamInput ::readString , Stats ::readStats );
353- }
354- }
355-
356335 @ Override
357336 public void writeTo (StreamOutput out ) throws IOException {
358337 totalStats .writeTo (out );
0 commit comments