} else { println("Start from cursor position: " + cursorPosition); cursor = Cursor.fromWebSafeString(cursorPosition);
public static Cursor instantiate(SerializationStreamReader streamReader) throws SerializationException { return Cursor.fromWebSafeString(streamReader.readString()); }
public void query(long toVersion, Optional<String> startAt) { LOGGER.info("Starting initial sync query..."); Stopwatch stopwatch = Stopwatch.createStarted(); Query<FormRecordEntity> query = ofy().load().type(FormRecordEntity.class) .ancestor(FormEntity.key(formClass)) .chunk(500); if(startAt.isPresent()) { query = query.startAt(Cursor.fromWebSafeString(startAt.get())); } QueryResultIterator<FormRecordEntity> it = query.iterator(); while(it.hasNext()) { FormRecordEntity record = it.next(); if(visibilityPredicate.test(record.getRecordId())) { add(record); if(!sizeEstimator.timeAndSpaceRemaining()) { stop(it.getCursor().toWebSafeString()); break; } } } LOGGER.info("Initial sync query complete in " + stopwatch.elapsed(TimeUnit.SECONDS) + " with estimate size: " + sizeEstimator.getEstimatedSizeInBytes() + " bytes"); }
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; }
public void query(long localVersion, long toVersion, Optional<String> startAt) { LOGGER.info("Starting VersionRange query..."); Stopwatch stopwatch = Stopwatch.createStarted(); Query<FormRecordSnapshotEntity> query = ofy().load().type(FormRecordSnapshotEntity.class) .ancestor(FormEntity.key(formClass)) .filter("version >", localVersion) .chunk(500); if(startAt.isPresent()) { query = query.startAt(Cursor.fromWebSafeString(startAt.get())); } QueryResultIterator<FormRecordSnapshotEntity> it = query.iterator(); while(it.hasNext()) { FormRecordSnapshotEntity snapshot = it.next(); if(snapshot.getVersion() <= toVersion) { add(snapshot); if(!sizeEstimator.timeAndSpaceRemaining()) { stop(it.getCursor().toWebSafeString()); break; } } } LOGGER.info("VersionRange query complete in " + stopwatch.elapsed(TimeUnit.SECONDS) + " with estimate size: " + sizeEstimator.getEstimatedSizeInBytes() + " bytes"); }
fetchOptions.startCursor(Cursor.fromWebSafeString(marker));
fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
cursor = Cursor.fromWebSafeString((String) obj);
@Test public void testEndFetch() { int onePage = total - 30; Query query = new Query(kindName, rootKey); // fetch first page Cursor cursor = checkPage(query, null, null, onePage, onePage, null, null); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page, get remaining after 1st page. checkPage(query, decodedCursor, null, onePage, total - onePage, null, null); }
@Test public void testEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); // fetch 1st page Cursor cursor = checkPage(query, null, null, limit, limit, testDat[0], testDat[0]); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch 1st page again since using decodedCursor as end cursor checkPage(query, null, decodedCursor, limit, limit, testDat[0], testDat[0]); }
@Test public void testFilter() { int onePage = 5; String filterData = "ff"; Query query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("name", Query.FilterOperator.EQUAL, filterData)); // fetch first page Cursor cursor = checkPage(query, null, null, onePage, onePage, filterData, filterData); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page checkPage(query, decodedCursor, null, onePage, onePage, filterData, filterData); }
@Test public void testSort() { int onePage = 6; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); // fetch first page aa,aa,aa,aa,aa,aa Cursor cursor = checkPage(query, null, null, onePage, onePage, testDat[0], testDat[0]); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page aa,aa,aa,aa,bb,bb checkPage(query, decodedCursor, null, onePage, onePage, testDat[0], testDat[1]); // desc onePage = total / testDat.length; query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.DESCENDING); // fetch first page jj,jj,........,jj,jj String chkChar = testDat[testDat.length - 1]; cursor = checkPage(query, null, null, onePage, onePage, chkChar, chkChar); decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page ii,ii,........,ii,ii chkChar = testDat[testDat.length - 2]; checkPage(query, decodedCursor, null, onePage, onePage, chkChar, chkChar); }
query.cursor = Cursor.fromWebSafeString(cursorNode.getTextValue());
@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); }