/** * @return a set of fetch options for the current limit, offset, and cursors, based on the default * fetch options. There will always be options even if default. */ private FetchOptions fetchOptions() { FetchOptions opts = FetchOptions.Builder.withDefaults(); if (this.startAt != null) opts = opts.startCursor(this.startAt); if (this.endAt != null) opts = opts.endCursor(this.endAt); if (this.limit != 0) opts = opts.limit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); if (this.chunk == null) opts = opts.chunkSize(DEFAULT_CHUNK_SIZE); else opts = opts.chunkSize(this.chunk); return opts; }
/** * @return a set of fetch options for the current limit and offset, or null if there is no limit * or offset. */ private FetchOptions fetchOptions() { // The FetchOptions builder interface is really, really awful if (this.cursor != null) { FetchOptions opts = FetchOptions.Builder.withCursor(this.cursor); if (this.limit != 0) opts = opts.limit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); return opts; } else if (this.limit != 0) { FetchOptions opts = FetchOptions.Builder.withLimit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); return opts; } else if (this.offset != 0) { FetchOptions opts = FetchOptions.Builder.withOffset(this.offset); return opts; } else { return null; } }
/** * Tests pagination of datastore entities. Verifies forward and backward paging returns entities * in the exact same order, even with duplicate values. * * @throws Exception */ public void test() throws Exception { // Initialize some test entities. final List<Entity> entities = initializeTestData(); // Add the entities to the datastore. DatastoreServiceFactory.getDatastoreService().put(entities); // Create a query to get the people, sorted by their initials. Query q = new Query(KIND); q.addSort(PROPERTY_NAME, SortDirection.ASCENDING); // This is to guarantee the order of duplicate initials. This is not // necessary if you are not concerned with the order of the duplicates. q.addSort(KEY, SortDirection.ASCENDING); // Fetch with a page size of 30 people. final FetchOptions options = FetchOptions.Builder.withDefaults(); options.limit(30); // Get first page. final Page page1 = getPage(q, options); System.out.println("Page 1"); System.out.println(page1); // Get the next page by setting the cursor to the "next" cursor // from the current page. options.startCursor(page1.next); final Page page2 = getPage(q, options); System.out.println("Page 2"); System.out.println(page2); // Make sure the next page starts after the previous page. assertTrue(page1.last().compareTo(page2.first()) < 0); // Get the next page by setting the cursor to the "next" cursor // from the current page. options.startCursor(page2.next); final Page page3 = getPage(q, options); System.out.println("Page 3"); System.out.println(page3); // Make sure the next page starts after the previous page. assertTrue(page1.last().compareTo(page2.first()) < 0); // For paging backward, create the same query in the reverse order. // Of course, each page will create a new query according to the // page direction desired. q = new Query(KIND); q.addSort(PROPERTY_NAME, SortDirection.DESCENDING); q.addSort(KEY, SortDirection.DESCENDING); // Get the previous page by setting the cursor to the "previous" // cursor from the current page. options.startCursor(page3.previous); final Page page2a = getPage(q, options); System.out.println("Page 2a"); // As the page will be returned in the reverse order because the // query is reversed, reverse the page for comparison with the // original. final Page reverse2a = page2a.reverse(); System.out.println(reverse2a); // Make sure this page 2 is exactly like the original. assertEquals(page2, reverse2a); // Get the previous page by setting the cursor to the "previous" // cursor from the current page. options.startCursor(page2a.next); final Page page1a = getPage(q, options); System.out.println("Page 1a"); // As the page will be returned in the reverse order because the // query is reversed, reverse the page for comparison with the // original. final Page reverse1a = page1a.reverse(); System.out.println(reverse1a); // Make sure this page 1 is exactly like the original. assertEquals(page1, reverse1a); }