2020import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
2121import org .elasticsearch .common .xcontent .XContentParser ;
2222import org .elasticsearch .common .xcontent .json .JsonXContent ;
23- import org .elasticsearch .core .Tuple ;
2423import org .elasticsearch .search .sort .SortOrder ;
2524import org .elasticsearch .snapshots .AbstractSnapshotIntegTestCase ;
2625import org .elasticsearch .snapshots .SnapshotInfo ;
@@ -106,31 +105,34 @@ private void doTestPagination(String repoName,
106105 GetSnapshotsRequest .SortBy sort ,
107106 SortOrder order ) throws IOException {
108107 final List <SnapshotInfo > allSnapshotsSorted = allSnapshotsSorted (names , repoName , sort , order );
109- final Tuple <String , List <SnapshotInfo >> batch1 = sortedWithLimit (repoName , sort , null , 2 , order );
110- assertEquals (allSnapshotsSorted .subList (0 , 2 ), batch1 .v2 ());
111- final Tuple <String , List <SnapshotInfo >> batch2 = sortedWithLimit (repoName , sort , batch1 .v1 (), 2 , order );
112- assertEquals (allSnapshotsSorted .subList (2 , 4 ), batch2 .v2 ());
113- final int lastBatch = names .size () - batch1 .v2 ().size () - batch2 .v2 ().size ();
114- final Tuple <String , List <SnapshotInfo >> batch3 = sortedWithLimit (repoName , sort , batch2 .v1 (), lastBatch , order );
115- assertEquals (batch3 .v2 (), allSnapshotsSorted .subList (batch1 .v2 ().size () + batch2 .v2 ().size (), names .size ()));
116- final Tuple <String , List <SnapshotInfo >> batch3NoLimit = sortedWithLimit (
108+ final GetSnapshotsResponse batch1 = sortedWithLimit (repoName , sort , null , 2 , order );
109+ assertEquals (allSnapshotsSorted .subList (0 , 2 ), batch1 .getSnapshots ());
110+ final GetSnapshotsResponse batch2 = sortedWithLimit (repoName , sort , batch1 .next (), 2 , order );
111+ assertEquals (allSnapshotsSorted .subList (2 , 4 ), batch2 .getSnapshots ());
112+ final int lastBatch = names .size () - batch1 .getSnapshots ().size () - batch2 .getSnapshots ().size ();
113+ final GetSnapshotsResponse batch3 = sortedWithLimit (repoName , sort , batch2 .next (), lastBatch , order );
114+ assertEquals (
115+ batch3 .getSnapshots (),
116+ allSnapshotsSorted .subList (batch1 .getSnapshots ().size () + batch2 .getSnapshots ().size (), names .size ())
117+ );
118+ final GetSnapshotsResponse batch3NoLimit = sortedWithLimit (
117119 repoName ,
118120 sort ,
119- batch2 .v1 (),
121+ batch2 .next (),
120122 GetSnapshotsRequest .NO_LIMIT ,
121123 order
122124 );
123- assertNull (batch3NoLimit .v1 ());
124- assertEquals (batch3 .v2 (), batch3NoLimit .v2 ());
125- final Tuple < String , List < SnapshotInfo >> batch3LargeLimit = sortedWithLimit (
125+ assertNull (batch3NoLimit .next ());
126+ assertEquals (batch3 .getSnapshots (), batch3NoLimit .getSnapshots ());
127+ final GetSnapshotsResponse batch3LargeLimit = sortedWithLimit (
126128 repoName ,
127129 sort ,
128- batch2 .v1 (),
130+ batch2 .next (),
129131 lastBatch + randomIntBetween (1 , 100 ),
130132 order
131133 );
132- assertEquals (batch3 .v2 (), batch3LargeLimit .v2 ());
133- assertNull (batch3LargeLimit .v1 ());
134+ assertEquals (batch3 .getSnapshots (), batch3LargeLimit .getSnapshots ());
135+ assertNull (batch3LargeLimit .next ());
134136 }
135137
136138 public void testSortAndPaginateWithInProgress () throws Exception {
@@ -180,16 +182,23 @@ private static void assertStablePagination(String repoName,
180182 final List <SnapshotInfo > allSorted = allSnapshotsSorted (allSnapshotNames , repoName , sort , order );
181183
182184 for (int i = 1 ; i <= allSnapshotNames .size (); i ++) {
183- final List <SnapshotInfo > subsetSorted = sortedWithLimit (repoName , sort , null , i , order ).v2 ();
185+ final List <SnapshotInfo > subsetSorted = sortedWithLimit (repoName , sort , null , i , order ).getSnapshots ();
184186 assertEquals (subsetSorted , allSorted .subList (0 , i ));
185187 }
186188
187189 for (int j = 0 ; j < allSnapshotNames .size (); j ++) {
188190 final SnapshotInfo after = allSorted .get (j );
189191 for (int i = 1 ; i < allSnapshotNames .size () - j ; i ++) {
190- final List <SnapshotInfo > subsetSorted = sortedWithLimit (
191- repoName , sort , GetSnapshotsRequest .After .from (after , sort ).asQueryParam (), i , order ).v2 ();
192+ final GetSnapshotsResponse getSnapshotsResponse =
193+ sortedWithLimit (repoName , sort , GetSnapshotsRequest .After .from (after , sort ).asQueryParam (), i , order );
194+ final GetSnapshotsResponse getSnapshotsResponseNumeric = sortedWithLimit (repoName , sort , j + 1 , i , order );
195+ final List <SnapshotInfo > subsetSorted = getSnapshotsResponse .getSnapshots ();
196+ assertEquals (subsetSorted , getSnapshotsResponseNumeric .getSnapshots ());
192197 assertEquals (subsetSorted , allSorted .subList (j + 1 , j + i + 1 ));
198+ assertEquals (allSnapshotNames .size (), getSnapshotsResponse .totalCount ());
199+ assertEquals (allSnapshotNames .size () - (j + i + 1 ), getSnapshotsResponse .remaining ());
200+ assertEquals (getSnapshotsResponseNumeric .totalCount (), getSnapshotsResponse .totalCount ());
201+ assertEquals (getSnapshotsResponseNumeric .remaining (), getSnapshotsResponse .remaining ());
193202 }
194203 }
195204 }
@@ -203,9 +212,11 @@ private static List<SnapshotInfo> allSnapshotsSorted(Collection<String> allSnaps
203212 if (order == SortOrder .DESC || randomBoolean ()) {
204213 request .addParameter ("order" , order .toString ());
205214 }
206- final Response response = getRestClient ().performRequest (request );
207- final List <SnapshotInfo > snapshotInfos = readSnapshotInfos ( response ). v2 ();
215+ final GetSnapshotsResponse getSnapshotsResponse = readSnapshotInfos ( getRestClient ().performRequest (request ) );
216+ final List <SnapshotInfo > snapshotInfos = getSnapshotsResponse . getSnapshots ();
208217 assertEquals (snapshotInfos .size (), allSnapshotNames .size ());
218+ assertEquals (getSnapshotsResponse .totalCount (), allSnapshotNames .size ());
219+ assertEquals (0 , getSnapshotsResponse .remaining ());
209220 for (SnapshotInfo snapshotInfo : snapshotInfos ) {
210221 assertThat (snapshotInfo .snapshotId ().getName (), is (in (allSnapshotNames )));
211222 }
@@ -216,20 +227,19 @@ private static Request baseGetSnapshotsRequest(String repoName) {
216227 return new Request (HttpGet .METHOD_NAME , "/_snapshot/" + repoName + "/*" );
217228 }
218229
219- private static Tuple < String , List < SnapshotInfo >> readSnapshotInfos (Response response ) throws IOException {
230+ private static GetSnapshotsResponse readSnapshotInfos (Response response ) throws IOException {
220231 try (InputStream input = response .getEntity ().getContent ();
221232 XContentParser parser = JsonXContent .jsonXContent .createParser (
222233 NamedXContentRegistry .EMPTY , DeprecationHandler .THROW_UNSUPPORTED_OPERATION , input )) {
223- final GetSnapshotsResponse getSnapshotsResponse = GetSnapshotsResponse .fromXContent (parser );
224- return Tuple .tuple (getSnapshotsResponse .next (), getSnapshotsResponse .getSnapshots ());
234+ return GetSnapshotsResponse .fromXContent (parser );
225235 }
226236 }
227237
228- private static Tuple < String , List < SnapshotInfo >> sortedWithLimit (String repoName ,
229- GetSnapshotsRequest .SortBy sortBy ,
230- String after ,
231- int size ,
232- SortOrder order ) throws IOException {
238+ private static GetSnapshotsResponse sortedWithLimit (String repoName ,
239+ GetSnapshotsRequest .SortBy sortBy ,
240+ String after ,
241+ int size ,
242+ SortOrder order ) throws IOException {
233243 final Request request = baseGetSnapshotsRequest (repoName );
234244 request .addParameter ("sort" , sortBy .toString ());
235245 if (size != GetSnapshotsRequest .NO_LIMIT || randomBoolean ()) {
@@ -244,4 +254,22 @@ private static Tuple<String, List<SnapshotInfo>> sortedWithLimit(String repoName
244254 final Response response = getRestClient ().performRequest (request );
245255 return readSnapshotInfos (response );
246256 }
257+
258+ private static GetSnapshotsResponse sortedWithLimit (String repoName ,
259+ GetSnapshotsRequest .SortBy sortBy ,
260+ int offset ,
261+ int size ,
262+ SortOrder order ) throws IOException {
263+ final Request request = baseGetSnapshotsRequest (repoName );
264+ request .addParameter ("sort" , sortBy .toString ());
265+ if (size != GetSnapshotsRequest .NO_LIMIT || randomBoolean ()) {
266+ request .addParameter ("size" , String .valueOf (size ));
267+ }
268+ request .addParameter ("offset" , String .valueOf (offset ));
269+ if (order == SortOrder .DESC || randomBoolean ()) {
270+ request .addParameter ("order" , order .toString ());
271+ }
272+ final Response response = getRestClient ().performRequest (request );
273+ return readSnapshotInfos (response );
274+ }
247275}
0 commit comments