@Override public boolean hasNext() { return this.base.hasNext(); }
/** * Fetches entities in batches and puts them into the buffer. */ private void batchFetching() { Query<T> newQuery = this.query.limit(BUFFER_SIZE); if (this.cursor != null) { newQuery = newQuery.startAt(this.cursor); } QueryResultIterator<T> iterator = newQuery.iterator(); boolean shouldContinue = false; while (iterator.hasNext()) { shouldContinue = true; this.buffer.offer(iterator.next()); } if (shouldContinue) { this.cursor = iterator.getCursor(); } }
while (iterator.hasNext()) { shouldContinue = true; Key<T> keyOfEntityToMigrate = iterator.next();
@Override public Pair<? extends Iterable<JobRecord>, String> call() { QueryResultIterator<Entity> entities = preparedQuery.asQueryResultIterable(fetchOptions).iterator(); Cursor dsCursor = null; List<JobRecord> roots = new LinkedList<>(); while (entities.hasNext()) { if (limit > 0 && roots.size() >= limit) { dsCursor = entities.getCursor(); break; } JobRecord jobRecord = new JobRecord(entities.next()); roots.add(jobRecord); } return Pair.of(roots, dsCursor == null ? null : dsCursor.toWebSafeString()); } });
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"); }
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"); }
QueryResultIterator<Entity> dsResults = datastore.prepare(query).asQueryResultIterator(fetchOptions); while (items.size() < maxResults && dsResults.hasNext()) { Entity entity = dsResults.next(); String name = entity.getKey().getName();
@Test public void testAsQueryResultIterator() throws Exception { QueryResultIterator<Entity> iterator = preparedQuery.asQueryResultIterator(); assertNotNull(iterator); assertTrue(iterator.hasNext()); assertEquals(john, iterator.next()); }
@Test public void testAsQueryResultIteratorWithOptions() throws Exception { QueryResultIterator<Entity> iterator = preparedQuery.asQueryResultIterator(withDefaults()); assertNotNull(iterator); assertTrue(iterator.hasNext()); assertEquals(john, iterator.next()); }
@Test public void testAsQueryResultIterable() throws Exception { QueryResultIterable<Entity> iterable = preparedQuery.asQueryResultIterable(); assertNotNull(iterable); assertNotNull(iterable.iterator()); assertTrue(iterable.iterator().hasNext()); assertEquals(john, iterable.iterator().next()); }
@Test public void testAsQueryResultIterableWithOptions() throws Exception { QueryResultIterable<Entity> iterable = preparedQuery.asQueryResultIterable(withDefaults()); assertNotNull(iterable); assertNotNull(iterable.iterator()); assertTrue(iterable.iterator().hasNext()); assertEquals(john, iterable.iterator().next()); }
@Test public void testCursor() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly"); Key key = parent.getKey(); Entity john = createEntity("Person", key) .withProperty("name", "John") .withProperty("surname", "Doe") .store(); Query query = new Query("Person") .setAncestor(key) .setKeysOnly(); PreparedQuery preparedQuery = service.prepare(query); QueryResultIterator<Entity> iter = preparedQuery.asQueryResultIterator(); Assert.assertNotNull(iter.next()); Cursor cursor = iter.getCursor(); iter = service.prepare(query).asQueryResultIterator(FetchOptions.Builder.withStartCursor(cursor)); Assert.assertFalse(iter.hasNext()); }