Skip to content

Commit fb6b616

Browse files
Move SnapshotStats.fromXContent() to SnapshotStatsTest
1 parent 7d47a7b commit fb6b616

File tree

5 files changed

+121
-114
lines changed

5 files changed

+121
-114
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStats.java

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.io.stream.Writeable;
1616
import org.elasticsearch.common.unit.ByteSizeValue;
17-
import org.elasticsearch.common.xcontent.XContentParserUtils;
1817
import org.elasticsearch.core.TimeValue;
1918
import org.elasticsearch.xcontent.ToXContent;
2019
import org.elasticsearch.xcontent.ToXContentObject;
2120
import org.elasticsearch.xcontent.XContentBuilder;
22-
import org.elasticsearch.xcontent.XContentParser;
2321

2422
import java.io.IOException;
2523

@@ -192,114 +190,6 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
192190
return builder.endObject();
193191
}
194192

195-
public static SnapshotStats fromXContent(XContentParser parser) throws IOException {
196-
// Parse this old school style instead of using the ObjectParser since there's an impedance mismatch between how the
197-
// object has historically been written as JSON versus how it is structured in Java.
198-
XContentParser.Token token = parser.currentToken();
199-
if (token == null) {
200-
token = parser.nextToken();
201-
}
202-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
203-
long startTime = 0;
204-
long time = 0;
205-
int incrementalFileCount = 0;
206-
int totalFileCount = 0;
207-
int processedFileCount = Integer.MIN_VALUE;
208-
long incrementalSize = 0;
209-
long totalSize = 0;
210-
long processedSize = Long.MIN_VALUE;
211-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
212-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
213-
String currentName = parser.currentName();
214-
token = parser.nextToken();
215-
if (currentName.equals(Fields.INCREMENTAL)) {
216-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
217-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
218-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
219-
String innerName = parser.currentName();
220-
token = parser.nextToken();
221-
if (innerName.equals(Fields.FILE_COUNT)) {
222-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
223-
incrementalFileCount = parser.intValue();
224-
} else if (innerName.equals(Fields.SIZE_IN_BYTES)) {
225-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
226-
incrementalSize = parser.longValue();
227-
} else {
228-
// Unknown sub field, skip
229-
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
230-
parser.skipChildren();
231-
}
232-
}
233-
}
234-
} else if (currentName.equals(Fields.PROCESSED)) {
235-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
236-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
237-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
238-
String innerName = parser.currentName();
239-
token = parser.nextToken();
240-
if (innerName.equals(Fields.FILE_COUNT)) {
241-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
242-
processedFileCount = parser.intValue();
243-
} else if (innerName.equals(Fields.SIZE_IN_BYTES)) {
244-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
245-
processedSize = parser.longValue();
246-
} else {
247-
// Unknown sub field, skip
248-
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
249-
parser.skipChildren();
250-
}
251-
}
252-
}
253-
} else if (currentName.equals(Fields.TOTAL)) {
254-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
255-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
256-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
257-
String innerName = parser.currentName();
258-
token = parser.nextToken();
259-
if (innerName.equals(Fields.FILE_COUNT)) {
260-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
261-
totalFileCount = parser.intValue();
262-
} else if (innerName.equals(Fields.SIZE_IN_BYTES)) {
263-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
264-
totalSize = parser.longValue();
265-
} else {
266-
// Unknown sub field, skip
267-
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
268-
parser.skipChildren();
269-
}
270-
}
271-
}
272-
} else if (currentName.equals(Fields.START_TIME_IN_MILLIS)) {
273-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
274-
startTime = parser.longValue();
275-
} else if (currentName.equals(Fields.TIME_IN_MILLIS)) {
276-
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
277-
time = parser.longValue();
278-
} else {
279-
// Unknown field, skip
280-
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
281-
parser.skipChildren();
282-
}
283-
}
284-
}
285-
// Handle the case where the "processed" sub-object is omitted in toXContent() when processedFileCount == incrementalFileCount.
286-
if (processedFileCount == Integer.MIN_VALUE) {
287-
assert processedSize == Long.MIN_VALUE;
288-
processedFileCount = incrementalFileCount;
289-
processedSize = incrementalSize;
290-
}
291-
return new SnapshotStats(
292-
startTime,
293-
time,
294-
incrementalFileCount,
295-
totalFileCount,
296-
processedFileCount,
297-
incrementalSize,
298-
totalSize,
299-
processedSize
300-
);
301-
}
302-
303193
/**
304194
* Add stats instance to the total
305195
* @param stats Stats instance to add

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ protected boolean supportsUnknownFields() {
9595
innerParser.declareString(constructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.STAGE));
9696
innerParser.declareString(optionalConstructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.NODE));
9797
innerParser.declareString(optionalConstructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.REASON));
98-
innerParser.declareObject(constructorArg(), (p, c) -> SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS));
98+
innerParser.declareObject(
99+
constructorArg(),
100+
(p, c) -> SnapshotStatsTests.fromXContent(p),
101+
new ParseField(SnapshotStats.Fields.STATS)
102+
);
99103
PARSER = (p, indexId, shardName) -> {
100104
// Combine the index name in the context with the shard name passed in for the named object parser
101105
// into a ShardId to pass as context for the inner parser.

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexStatusTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public class SnapshotIndexStatusTests extends AbstractXContentTestCase<SnapshotI
5757
(p, c) -> SnapshotShardsStatsTests.PARSER.apply(p, null),
5858
new ParseField(SnapshotShardsStats.Fields.SHARDS_STATS)
5959
);
60-
innerParser.declareObject(constructorArg(), (p, c) -> SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS));
60+
innerParser.declareObject(
61+
constructorArg(),
62+
(p, c) -> SnapshotStatsTests.fromXContent(p),
63+
new ParseField(SnapshotStats.Fields.STATS)
64+
);
6165
innerParser.declareNamedObjects(
6266
constructorArg(),
6367
SnapshotIndexShardStatusTests.PARSER,

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatsTests.java

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.action.admin.cluster.snapshots.status;
1111

12+
import org.elasticsearch.common.xcontent.XContentParserUtils;
1213
import org.elasticsearch.test.AbstractXContentTestCase;
1314
import org.elasticsearch.xcontent.XContentParser;
1415

@@ -61,11 +62,119 @@ public void testXContentSerializationForEmptyStats() throws IOException {
6162

6263
@Override
6364
protected SnapshotStats doParseInstance(XContentParser parser) throws IOException {
64-
return SnapshotStats.fromXContent(parser);
65+
return fromXContent(parser);
6566
}
6667

6768
@Override
6869
protected boolean supportsUnknownFields() {
6970
return true;
7071
}
72+
73+
static SnapshotStats fromXContent(XContentParser parser) throws IOException {
74+
// Parse this old school style instead of using the ObjectParser since there's an impedance mismatch between how the
75+
// object has historically been written as JSON versus how it is structured in Java.
76+
XContentParser.Token token = parser.currentToken();
77+
if (token == null) {
78+
token = parser.nextToken();
79+
}
80+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
81+
long startTime = 0;
82+
long time = 0;
83+
int incrementalFileCount = 0;
84+
int totalFileCount = 0;
85+
int processedFileCount = Integer.MIN_VALUE;
86+
long incrementalSize = 0;
87+
long totalSize = 0;
88+
long processedSize = Long.MIN_VALUE;
89+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
90+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
91+
String currentName = parser.currentName();
92+
token = parser.nextToken();
93+
if (currentName.equals(SnapshotStats.Fields.INCREMENTAL)) {
94+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
95+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
96+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
97+
String innerName = parser.currentName();
98+
token = parser.nextToken();
99+
if (innerName.equals(SnapshotStats.Fields.FILE_COUNT)) {
100+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
101+
incrementalFileCount = parser.intValue();
102+
} else if (innerName.equals(SnapshotStats.Fields.SIZE_IN_BYTES)) {
103+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
104+
incrementalSize = parser.longValue();
105+
} else {
106+
// Unknown sub field, skip
107+
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
108+
parser.skipChildren();
109+
}
110+
}
111+
}
112+
} else if (currentName.equals(SnapshotStats.Fields.PROCESSED)) {
113+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
114+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
115+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
116+
String innerName = parser.currentName();
117+
token = parser.nextToken();
118+
if (innerName.equals(SnapshotStats.Fields.FILE_COUNT)) {
119+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
120+
processedFileCount = parser.intValue();
121+
} else if (innerName.equals(SnapshotStats.Fields.SIZE_IN_BYTES)) {
122+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
123+
processedSize = parser.longValue();
124+
} else {
125+
// Unknown sub field, skip
126+
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
127+
parser.skipChildren();
128+
}
129+
}
130+
}
131+
} else if (currentName.equals(SnapshotStats.Fields.TOTAL)) {
132+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
133+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
134+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
135+
String innerName = parser.currentName();
136+
token = parser.nextToken();
137+
if (innerName.equals(SnapshotStats.Fields.FILE_COUNT)) {
138+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
139+
totalFileCount = parser.intValue();
140+
} else if (innerName.equals(SnapshotStats.Fields.SIZE_IN_BYTES)) {
141+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
142+
totalSize = parser.longValue();
143+
} else {
144+
// Unknown sub field, skip
145+
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
146+
parser.skipChildren();
147+
}
148+
}
149+
}
150+
} else if (currentName.equals(SnapshotStats.Fields.START_TIME_IN_MILLIS)) {
151+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
152+
startTime = parser.longValue();
153+
} else if (currentName.equals(SnapshotStats.Fields.TIME_IN_MILLIS)) {
154+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser);
155+
time = parser.longValue();
156+
} else {
157+
// Unknown field, skip
158+
if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) {
159+
parser.skipChildren();
160+
}
161+
}
162+
}
163+
// Handle the case where the "processed" sub-object is omitted in toXContent() when processedFileCount == incrementalFileCount.
164+
if (processedFileCount == Integer.MIN_VALUE) {
165+
assert processedSize == Long.MIN_VALUE;
166+
processedFileCount = incrementalFileCount;
167+
processedSize = incrementalSize;
168+
}
169+
return new SnapshotStats(
170+
startTime,
171+
time,
172+
incrementalFileCount,
173+
totalFileCount,
174+
processedFileCount,
175+
incrementalSize,
176+
totalSize,
177+
processedSize
178+
);
179+
}
71180
}

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatusTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class SnapshotStatusTests extends AbstractChunkedSerializingTestCase<Snap
7777
PARSER.declareBoolean(optionalConstructorArg(), new ParseField(SnapshotStatus.INCLUDE_GLOBAL_STATE));
7878
PARSER.declareField(
7979
constructorArg(),
80-
SnapshotStats::fromXContent,
80+
SnapshotStatsTests::fromXContent,
8181
new ParseField(SnapshotStats.Fields.STATS),
8282
ObjectParser.ValueType.OBJECT
8383
);

0 commit comments

Comments
 (0)