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; }
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); }
@Test public void testOffsetAndLimit() { List<Entity> results = executeQuery(withOffset(1).limit(1)); assertEquals(asList(foo2), results); }
/** * @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; }
@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 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); }
options.limit(limit);
final FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if (limit > 0) { fetchOptions.limit(limit + 1);
opts.limit(intExclNo - offset);
@Test public void testStartCursorAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withStartCursor(cursor).limit(1)); assertEquals(asList(foo4), 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 @InSequence(7) public void testStep7IterableWithOption() { FetchOptions foIterable = FetchOptions.Builder.withLimit(limit); Query q = new Query(kindName, rootKey); Iterator<Entity> eData = service.prepare(q).asIterator(fo.limit(limit)); assertEquals(limit, getSize(eData)); foIterable = FetchOptions.Builder.withOffset(offset); eData = service.prepare(q).asIterator(foIterable); assertEquals(bigCount - offset, getSize(eData)); }
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); } }