/** * @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; }
private FetchOptions getFetchOptionsWithoutOffsetAndLimit( FetchOptions opts, Integer chunkSize, Integer prefetchSize) { FetchOptions optsWithoutOffsetAndLimit = null; if (chunkSize != null) { optsWithoutOffsetAndLimit = FetchOptions.Builder.withChunkSize(opts.getChunkSize()); if (prefetchSize != null) { optsWithoutOffsetAndLimit.prefetchSize(opts.getPrefetchSize()); } } else if (prefetchSize != null) { optsWithoutOffsetAndLimit = FetchOptions.Builder.withPrefetchSize(prefetchSize); } return optsWithoutOffsetAndLimit; }
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); }
opts.limit(intExclNo - offset); opts = withStartCursor(cursor); } else { opts.startCursor(cursor); opts = withChunkSize(fetchSize); } else { opts.chunkSize(fetchSize);
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); } }
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);
@Test public void testOffsetAndLimit() { List<Entity> results = executeQuery(withOffset(1).limit(1)); assertEquals(asList(foo2), results); }
@Test public void testEndCursorAndOffsetAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).offset(1).limit(2)); assertEquals(asList(foo2, foo3), results); results = executeQuery(withEndCursor(cursor).offset(1).limit(5)); assertEquals(asList(foo2, foo3), results); }
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(); } }
@Test public void testEndCursorAndOffset() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).offset(1)); assertEquals(asList(foo2, foo3), results); }
.setAncestor(FormEntity.key(formClass).getRaw())); FetchOptions fetchOptions = FetchOptions.Builder.withChunkSize(500).prefetchSize(500); Iterator<Entity> it = preparedQuery.asIterator(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; }
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
@Test public void testEndCursorAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).limit(2)); assertEquals(asList(foo1, foo2), results); results = executeQuery(withEndCursor(cursor).limit(5)); // even if limit is past endCursor, endCursor will still apply assertEquals(asList(foo1, foo2, foo3), results); }
@Test @InSequence(8) public void testStep8FetchOption() { Query q = new Query(kindName, rootKey).addSort("count", Query.SortDirection.DESCENDING); Entity e = service.prepare(q).asIterator().next(); assertEquals(new Integer(bigCount - 1).longValue(), e.getProperty("count")); FetchOptions foTest = FetchOptions.Builder.withDefaults(); int ttl = service.prepare(q).countEntities(foTest.limit(500)); assertEquals(500, ttl); foTest = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.offset(150)); assertEquals((bigCount - 150), ttl); fo = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.offset(50).limit(150)); assertEquals(150, ttl); fo = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.limit(150).offset(offset)); int expect = (150 < (bigCount - offset)) ? 150 : (bigCount - offset); assertEquals(expect, ttl); }
@Test public void testStartCursorAndOffset() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withStartCursor(cursor).offset(1)); assertEquals(asList(foo5), results); }
final FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if (limit > 0) { fetchOptions.limit(limit + 1); fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
fetchOptions.startCursor(Cursor.fromWebSafeString(marker));
options.limit(limit);
@Test public void testEndCursorLessThanOffset() { QueryResultList<Entity> results = executeQuery(withLimit(1)); Cursor cursor = results.getCursor(); // points to foo2 results = executeQuery(withEndCursor(cursor).offset(3)); assertTrue(results.isEmpty()); }