Skip to content

Commit 8d6832c

Browse files
author
Christoph Büscher
authored
Make SearchStats implement Writeable (#29258)
Moves another class over from Streamable to Writeable. By this, also some constructors can be removed or made private.
1 parent d2baf4b commit 8d6832c

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public CommonStats(StreamInput in) throws IOException {
233233
store = in.readOptionalStreamable(StoreStats::new);
234234
indexing = in.readOptionalStreamable(IndexingStats::new);
235235
get = in.readOptionalStreamable(GetStats::new);
236-
search = in.readOptionalStreamable(SearchStats::new);
236+
search = in.readOptionalWriteable(SearchStats::new);
237237
merge = in.readOptionalStreamable(MergeStats::new);
238238
refresh = in.readOptionalStreamable(RefreshStats::new);
239239
flush = in.readOptionalStreamable(FlushStats::new);
@@ -253,7 +253,7 @@ public void writeTo(StreamOutput out) throws IOException {
253253
out.writeOptionalStreamable(store);
254254
out.writeOptionalStreamable(indexing);
255255
out.writeOptionalStreamable(get);
256-
out.writeOptionalStreamable(search);
256+
out.writeOptionalWriteable(search);
257257
out.writeOptionalStreamable(merge);
258258
out.writeOptionalStreamable(refresh);
259259
out.writeOptionalStreamable(flush);

server/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
import org.elasticsearch.common.Strings;
2424
import org.elasticsearch.common.io.stream.StreamInput;
2525
import org.elasticsearch.common.io.stream.StreamOutput;
26-
import org.elasticsearch.common.io.stream.Streamable;
26+
import org.elasticsearch.common.io.stream.Writeable;
2727
import org.elasticsearch.common.unit.TimeValue;
2828
import org.elasticsearch.common.xcontent.ToXContent;
2929
import org.elasticsearch.common.xcontent.ToXContentFragment;
3030
import org.elasticsearch.common.xcontent.XContentBuilder;
3131

3232
import java.io.IOException;
33+
import java.util.Collections;
3334
import java.util.HashMap;
3435
import 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);

server/src/test/java/org/elasticsearch/search/stats/SearchStatsUnitTests.java renamed to server/src/test/java/org/elasticsearch/index/search/stats/SearchStatsTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.search.stats;
20+
package org.elasticsearch.index.search.stats;
2121

22-
import org.elasticsearch.index.search.stats.SearchStats;
2322
import org.elasticsearch.index.search.stats.SearchStats.Stats;
2423
import org.elasticsearch.test.ESTestCase;
2524

2625
import java.util.HashMap;
2726
import java.util.Map;
2827

29-
public class SearchStatsUnitTests extends ESTestCase {
28+
public class SearchStatsTests extends ESTestCase {
29+
3030
// https://github.com/elastic/elasticsearch/issues/7644
3131
public void testShardLevelSearchGroupStats() throws Exception {
3232
// let's create two dummy search stats with groups
@@ -52,7 +52,7 @@ public void testShardLevelSearchGroupStats() throws Exception {
5252
assertStats(groupStats1.get("group1"), 3);
5353
}
5454

55-
private void assertStats(Stats stats, long equalTo) {
55+
private static void assertStats(Stats stats, long equalTo) {
5656
assertEquals(equalTo, stats.getQueryCount());
5757
assertEquals(equalTo, stats.getQueryTimeInMillis());
5858
assertEquals(equalTo, stats.getQueryCurrent());
@@ -66,4 +66,5 @@ private void assertStats(Stats stats, long equalTo) {
6666
assertEquals(equalTo, stats.getSuggestTimeInMillis());
6767
assertEquals(equalTo, stats.getSuggestCurrent());
6868
}
69+
6970
}

0 commit comments

Comments
 (0)