Query q = new Query("Person"); QueryResultList<Entity> results; Cursor cursor = null; FetchOptions queryOptions = FetchOptions.Builder.withChunkSize(500); do { if (cursor != null) { queryOptions.startCursor(cursor); } results = datastore.prepare(q).asQueryResultList(queryOptions); for (Entity entity : results) { // do something } cursor = results.getCursor(); } while (results.size() == 500);
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
private QueryResultList<Entity> getNamespaces() { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); fetchOptions.limit(BATCH_SIZE); if (cursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)); } Query q = new Query(NAMESPACE_METADATA_KIND); q.setKeysOnly(); return datastore.prepare(q).asQueryResultList(fetchOptions); }
private FetchOptions configureFetchOptions(QueryBuilder<?> builder) { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if (builder.getLimit() != null) { fetchOptions.limit(builder.getLimit()); } if (builder.getCursor() != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(builder.getCursor())); } return fetchOptions; }
/** * @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.startCursor != null) opts = opts.startCursor(this.startCursor); if (this.endCursor != null) opts = opts.endCursor(this.endCursor); if (this.limit != 0) opts = opts.limit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); if (this.prefetchSize != null) opts = opts.prefetchSize(this.prefetchSize); if (this.chunkSize != null) opts = opts.chunkSize(this.chunkSize); return opts; }
fetchOptions.startCursor(Cursor.fromWebSafeString(marker));
fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
opts = withStartCursor(cursor); } else { opts.startCursor(cursor);
private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet, String chkSt, String chkEnd) { FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); if (stCursor != null) { fetchOption = fetchOption.startCursor(stCursor); } if (endCursor != null) { fetchOption = fetchOption.endCursor(endCursor); } QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); assertEquals(exptRet, nextBatch.size()); if (chkSt != null) { assertEquals(chkSt, nextBatch.get(0).getProperty("name")); } if (chkEnd != null) { assertEquals(chkEnd, nextBatch.get(nextBatch.size() - 1).getProperty("name")); } return nextBatch.getCursor(); } }
public static QueryResult queryEntityPrivate(LeanQuery leanQuery) throws LeanException { LeanAccount account = findCurrentAccount(); Query query = new Query(leanQuery.getKind()); query.addFilter("_account", Query.FilterOperator.EQUAL, account.id); for (QueryFilter queryFilter : leanQuery.getFilters()) { query.addFilter( queryFilter.getProperty(), queryFilter.getOperator().getFilterOperator(), queryFilter.getValue()); } for (QuerySort querySort : leanQuery.getSorts()) { query.addSort(querySort.getProperty(), querySort.getDirection().getSortDirection()); } FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if(leanQuery.getCursor() != null ) fetchOptions.startCursor(leanQuery.getCursor()); if(leanQuery.getOffset() != null) fetchOptions.offset(leanQuery.getOffset()); if(leanQuery.getLimit() != null) fetchOptions.limit(leanQuery.getLimit()); try { PreparedQuery pq = datastore.prepare(query); QueryResultList<Entity> result; result = pq.asQueryResultList(fetchOptions); return new QueryResult(result, result.getCursor()); } catch (DatastoreNeedIndexException dnie) { throw new LeanException(LeanException.Error.AppEngineMissingIndex); } }
@Test public void testStartEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); // fetch 1st page and get cursor1 QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // fetch 2nd page and get cursor2 nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1)); Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page. checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]); // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page. checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]); // cursor2 as start and cursor1 as end and 15 in limit -- should not return any. checkPage(query, cursor2, cursor1, limit, 0, null, null); }