Java Code Examples for com.google.appengine.api.datastore.QueryResultIterator#hasNext()

The following examples show how to use com.google.appengine.api.datastore.QueryResultIterator#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CursorIterator.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 2
Source File: DataMigrationEntitiesBaseScript.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void doOperation() {
    log("Running " + getClass().getSimpleName() + "...");
    log("Preview: " + isPreview());

    Cursor cursor = readPositionOfCursorFromFile().orElse(null);
    if (cursor == null) {
        log("Start from the beginning");
    } else {
        log("Start from cursor position: " + cursor.toWebSafeString());
    }

    boolean shouldContinue = true;
    while (shouldContinue) {
        shouldContinue = false;
        Query<T> filterQueryKeys = getFilterQuery().limit(BATCH_SIZE);
        if (cursor != null) {
            filterQueryKeys = filterQueryKeys.startAt(cursor);
        }
        QueryResultIterator<?> iterator;
        if (shouldUseTransaction()) {
            iterator = filterQueryKeys.keys().iterator();
        } else {
            iterator = filterQueryKeys.iterator();
        }

        while (iterator.hasNext()) {
            shouldContinue = true;

            // migrate
            if (shouldUseTransaction()) {
                migrateWithTrx((Key<T>) iterator.next());
            } else {
                migrateWithoutTrx((T) iterator.next());
            }

            numberOfScannedKey.incrementAndGet();
        }

        if (shouldContinue) {
            cursor = iterator.getCursor();
            flushEntitiesSavingBuffer();
            savePositionOfCursorToFile(cursor);
            log(String.format("Cursor Position: %s", cursor.toWebSafeString()));
            log(String.format("Number Of Entity Key Scanned: %d", numberOfScannedKey.get()));
            log(String.format("Number Of Entity affected: %d", numberOfAffectedEntities.get()));
            log(String.format("Number Of Entity updated: %d", numberOfUpdatedEntities.get()));
        }
    }

    deleteCursorPositionFile();
    log(isPreview() ? "Preview Completed!" : "Migration Completed!");
    log("Total number of entities: " + numberOfScannedKey.get());
    log("Number of affected entities: " + numberOfAffectedEntities.get());
    log("Number of updated entities: " + numberOfUpdatedEntities.get());
}
 
Example 3
Source File: LocalRawGcsService.java    From appengine-gcs-client with Apache License 2.0 4 votes vote down vote up
@Override
public ListItemBatch list(String bucket, String prefix, String delimiter,
    String marker, int maxResults, long timeoutMillis) throws IOException {
  ensureInitialized();
  Query query = makeQuery(bucket);
  int prefixLength;
  if (!Strings.isNullOrEmpty(prefix)) {
    Key keyPrefix = makeKey(bucket, prefix);
    query.setFilter(new FilterPredicate(KEY_RESERVED_PROPERTY, GREATER_THAN_OR_EQUAL, keyPrefix));
    prefixLength = prefix.length();
  } else {
    prefixLength = 0;
  }
  FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
  if (marker != null) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(marker));
  }
  List<ListItem> items = new ArrayList<>(maxResults);
  Set<String> prefixes = new HashSet<>();
  QueryResultIterator<Entity> dsResults =
      datastore.prepare(query).asQueryResultIterator(fetchOptions);
  while (items.size() < maxResults && dsResults.hasNext()) {
    Entity entity = dsResults.next();
    String name = entity.getKey().getName();
    if (prefixLength > 0 && !name.startsWith(prefix)) {
      break;
    }
    if (!Strings.isNullOrEmpty(delimiter)) {
      int delimiterIdx = name.indexOf(delimiter, prefixLength);
      if (delimiterIdx > 0) {
        name = name.substring(0, delimiterIdx + 1);
        if (prefixes.add(name)) {
          items.add(new ListItem.Builder().setName(name).setDirectory(true).build());
        }
        continue;
      }
    }
    GcsFilename filename = new GcsFilename(bucket, name);
    GcsFileMetadata metadata = createGcsFileMetadata(entity, filename);
    ListItem listItem = new ListItem.Builder()
        .setName(name)
        .setLength(metadata.getLength())
        .setLastModified(metadata.getLastModified())
        .build();
    items.add(listItem);
  }
  Cursor cursor = dsResults.getCursor();
  String nextMarker = null;
  if (items.size() == maxResults && cursor != null) {
    nextMarker = cursor.toWebSafeString();
  }
  return new ListItemBatch(items, nextMarker);
}