/** * @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 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); }
@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); }
@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); }
@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 testEndCursorLessThanOffset() { QueryResultList<Entity> results = executeQuery(withLimit(1)); Cursor cursor = results.getCursor(); // points to foo2 results = executeQuery(withEndCursor(cursor).offset(3)); assertTrue(results.isEmpty()); }
@Test @InSequence(10) public void testStep10FilterInWithOption() { int filterNum = 100; Query q = new Query(kindName, rootKey); q.setFilter(new FilterPredicate("count", FilterOperator.IN, getFilterIn(filterNum))); int ttl = service.prepare(q).countEntities(fo.offset(filterNum / 2)); assertEquals((filterNum / 2), ttl); }
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); } }