Java 类com.google.appengine.api.datastore.Cursor 实例源码

项目:android-training-2017    文件:UserEndpoint.java   
/**
 * List all entities.
 *
 * @param cursor used for pagination to determine which page to return
 * @param limit  the maximum number of entries to return
 * @return a response that encapsulates the result list and the next page token/cursor
 */
@ApiMethod(
        name = "list",
        path = "user",
        httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<User> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Query<User> query = ofy().load().type(User.class).limit(limit);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<User> queryIterator = query.iterator();
    List<User> userList = new ArrayList<User>(limit);
    while (queryIterator.hasNext()) {
        userList.add(queryIterator.next());
    }
    return CollectionResponse.<User>builder().setItems(userList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
项目:Build-it-Bigger    文件:JokeEndpoint.java   
/**
 * List all entities.
 *
 * @param cursor used for pagination to determine which page to return
 * @param limit  the maximum number of entries to return
 * @return a response that encapsulates the result list and the next page token/cursor
 */
@ApiMethod(
        name = "list",
        path = "joke",
        httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Joke> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    Integer limitParam = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Query<Joke> query = ofy().load().type(Joke.class).limit(limitParam);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<Joke> queryIterator = query.iterator();
    List<Joke> jokeList = new ArrayList<>(limitParam);
    while (queryIterator.hasNext()) {
        jokeList.add(queryIterator.next());
    }
    return CollectionResponse.<Joke>builder().setItems(jokeList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
项目:Capstoneproject1    文件:OrdersEndpoint.java   
/**
 * List all entities.
 *
 * @param cursor used for pagination to determine which page to return
 * @param limit  the maximum number of entries to return
 * @return a response that encapsulates the result list and the next page token/cursor
 */
@ApiMethod(
        name = "list",
        path = "orders",
        httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Orders> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Query<Orders> query = ofy().load().type(Orders.class).limit(limit);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<Orders> queryIterator = query.iterator();
    List<Orders> ordersList = new ArrayList<Orders>(limit);
    while (queryIterator.hasNext()) {
        ordersList.add(queryIterator.next());
    }
    return CollectionResponse.<Orders>builder().setItems(ordersList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
项目:Capstoneproject1    文件:UsersEndpoint.java   
/**
 * List all entities.
 *
 * @param cursor used for pagination to determine which page to return
 * @param limit  the maximum number of entries to return
 * @return a response that encapsulates the result list and the next page token/cursor
 */
@ApiMethod(
        name = "list",
        path = "users",
        httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Users> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Query<Users> query = ofy().load().type(Users.class).limit(limit);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<Users> queryIterator = query.iterator();
    List<Users> usersList = new ArrayList<Users>(limit);
    while (queryIterator.hasNext()) {
        usersList.add(queryIterator.next());
    }
    return CollectionResponse.<Users>builder().setItems(usersList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      // Only for this user
      .setFilter(new Query.FilterPredicate(
          Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId))
      // a custom datastore index is required since you are filtering by one property
      // but ordering by another
      .addSort(Book.TITLE, SortDirection.ASCENDING);
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:getting-started-java    文件:DatastoreDao.java   
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      // Only for this user
      .setFilter(new Query.FilterPredicate(
          Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId))
      // a custom datastore index is required since you are filtering by one property
      // but ordering by another
      .addSort(Book.TITLE, SortDirection.ASCENDING);
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
项目:sc2gears    文件:ServerUtils.java   
/**
 * Counts the entities returned by the specified query.
 * @param ds    reference to the datastore service
 * @param query query whose results to count
 * @return the number of entities returned by the query
 */
public static int countEntities( final DatastoreService ds, final com.google.appengine.api.datastore.Query q ) {
    q.setKeysOnly();

    final int          batchSize    = 1000;
    final FetchOptions fetchOptions = FetchOptions.Builder.withLimit( batchSize );

    Cursor cursor = null;
    int    count  = 0;
    while ( true ) {
        if ( cursor != null )
            fetchOptions.startCursor( cursor );

        final QueryResultList< Entity > resultList = ds.prepare( q ).asQueryResultList( fetchOptions );

        count += resultList.size();

        if ( resultList.size() < batchSize )
            return count;

        cursor = resultList.getCursor();
    }
}
项目:appengine-tck    文件:QueryResultTest.java   
@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());
}
项目:appengine-tck    文件:CursorTest.java   
@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);
}
项目:appengine-tck    文件:CursorTest.java   
@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);
}
项目:appengine-tck    文件:CursorTest.java   
private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet,
                         String chkSt, String chkEnd) {
    FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit);
    if (stCursor != null) {
        fetchOption = fetchOption.startCursor(stCursor);
    }
    if (endCursor != null) {
        fetchOption = fetchOption.endCursor(endCursor);
    }
    QueryResultList<Entity> nextBatch = service.prepare(query)
        .asQueryResultList(fetchOption);
    assertEquals(exptRet, nextBatch.size());
    if (chkSt != null) {
        assertEquals(chkSt, nextBatch.get(0).getProperty("name"));
    }
    if (chkEnd != null) {
        assertEquals(chkEnd, nextBatch.get(nextBatch.size() - 1).getProperty("name"));
    }
    return nextBatch.getCursor();
}
项目:karma-exchange    文件:ListResponseMsg.java   
@Nullable
public static PagingInfo create(@Nullable Cursor afterCursor, int limit, boolean moreResults,
    UriInfo uriInfo) {
  String afterCursorStr = (afterCursor == null) ? "" : afterCursor.toWebSafeString();
  if (afterCursorStr.isEmpty()) {
    return null;
  } else {
    String nextUrl;
    if (moreResults) {
      Multimap<String, String> queryParams = toMultimap(uriInfo.getQueryParameters());
      queryParams.replaceValues(AFTER_CURSOR_PARAM, asList(afterCursorStr));
      queryParams.replaceValues(LIMIT_PARAM, asList(String.valueOf(limit)));
      nextUrl = URLUtil.buildURL(uriInfo.getAbsolutePath(), queryParams);
    } else {
      nextUrl = null;
    }
    return new PagingInfo(nextUrl, afterCursorStr);
  }
}
项目:karma-exchange    文件:PaginatedQuery.java   
private Builder(Class<T> resourceClass, @Nullable UriInfo uriInfo,
    @Nullable MultivaluedMap<String, String> queryParams, int defaultLimit) {
  if (uriInfo != null) {
    queryParams = uriInfo.getQueryParameters();
  }
  this.resourceClass = resourceClass;
  this.uriInfo = uriInfo;
  String afterCursorStr = queryParams.getFirst(PagingInfo.AFTER_CURSOR_PARAM);
  if (afterCursorStr != null) {
    afterCursor = Cursor.fromWebSafeString(afterCursorStr);
  } else {
    afterCursor = null;
  }
  limit = queryParams.containsKey(PagingInfo.LIMIT_PARAM) ?
      Integer.valueOf(queryParams.getFirst(PagingInfo.LIMIT_PARAM)) : defaultLimit;
  if (limit <= 0) {
    throw ErrorResponseMsg.createException("limit must be greater than zero",
      ErrorInfo.Type.BAD_REQUEST);
  }
}
项目:walkaround    文件:CheckedDatastore.java   
@Override public Cursor getCursor() throws PermanentFailure, RetryableFailure {
  return safeRun(new Evaluater<Cursor>() {
    @Override public Cursor run() {
      return iterator.getCursor();
    }
  });
}
项目:Capstoneproject1    文件:MyEndpoint.java   
@ApiMethod(name = "listUsers")
public CollectionResponse<Users> listQuote(@Nullable @com.google.api.server.spi.config.Named("cursor") String cursorString,
                                           @Nullable @com.google.api.server.spi.config.Named("count") Integer count) {

    Query<Users> query = ofy().load().type(Users.class);
    if (count != null) query.limit(count);
    if (cursorString != null && cursorString != "") {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }
    List<Users> records = new ArrayList<Users>();
    QueryResultIterator<Users> iterator = query.iterator();
    int num = 0;
    while (iterator.hasNext()) {
        records.add(iterator.next());
        if (count != null) {
            num++;
            if (num == count) break;
        }
    }
    //Find the next cursor
    if (cursorString != null && cursorString != "") {
        Cursor cursor = iterator.getCursor();
        if (cursor != null) {
            cursorString = cursor.toWebSafeString();
        }
    }
    return CollectionResponse.<Users>builder().setItems(records).setNextPageToken(cursorString).build();
}
项目:Capstoneproject1    文件:MyEndpoint.java   
@ApiMethod(
        name = "orderlist",
        httpMethod = ApiMethod.HttpMethod.GET)
public List<Orders> orderlist(@javax.annotation.Nullable @Named("cursor") String cursor){
    Query<Orders> query = ofy().load().type(Orders.class).limit(1000);
    if (cursor != null)
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    QueryResultIterator<Orders> queryIterator = query.iterator();
    List<Orders> ordersList = new ArrayList<Orders>(1000);
    while (queryIterator.hasNext()) {
        ordersList.add(queryIterator.next());
    }
    return ordersList;
}
项目:sc2gears    文件:ServerUtils.java   
/**
 * Sets a cursor to a query so the next execute() method will return results after the last query
 * @param query           query to set the cursor of
 * @param lastQueryResult reference to the last query result list
 */
public static void setQueryCursor( final Query query, final List< ? > lastQueryResult ) {
    final Cursor cursor = JDOCursorHelper.getCursor( lastQueryResult );

    final Map< String, Object > extensionMap = new HashMap< String, Object >( 2 ); // initial size of 2 because 1 with default load factor=0.75 would result in 0-size internal cache...
    extensionMap.put( JDOCursorHelper.CURSOR_EXTENSION, cursor );

    query.setExtensions( extensionMap );
}
项目:sc2gears    文件:ServerUtils.java   
/**
 * Sets the page info to a query.
 * @param query    query to set the page info of
 * @param pageInfo page info to be set
 */
public static void setQueryPageInfo( final Query query, final PageInfo pageInfo ) {
    if ( pageInfo.getCursorString() == null )
        query.setRange( pageInfo.getOffset(), pageInfo.getOffset() + pageInfo.getLimit() );
    else {
        query.setRange( 0, pageInfo.getLimit() );

        final Map< String, Object > extensionMap = new HashMap< String, Object >( 2 ); // initial size of 2 because 1 with default load factor=0.75 would result in 0-size internal cache...
        extensionMap.put( JDOCursorHelper.CURSOR_EXTENSION, Cursor.fromWebSafeString( pageInfo.getCursorString() ) );

        query.setExtensions( extensionMap );
    }
}
项目:fullmetalgalaxy    文件:SynchroForum.java   
public SynchroForumCommand(Cursor p_cursor, int p_accountProcessed, int p_activeAccount,
    double p_maxLevel)
{
  m_cursor = p_cursor;
  m_accountProcessed = p_accountProcessed;
  m_activeAccount = p_activeAccount;
  m_maxLevel = p_maxLevel;
}
项目:dojo-ibl    文件:UserManager.java   
public static List<UserJDO> listAllUsers(PersistenceManager pm, String cursorString) {
    javax.jdo.Query query = pm.newQuery(UserJDO.class);
    if (cursorString != null) {
        Cursor cursor = Cursor.fromWebSafeString(cursorString);
        Map<String, Object> extensionMap = new HashMap<String, Object>();
        extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
        query.setExtensions(extensionMap);
    }
    query.setRange(0, LIMIT);
    return (List<UserJDO>) query.execute();
}
项目:dojo-ibl    文件:RunManager.java   
public static List<RunJDO> listAllRuns(PersistenceManager pm, String cursorString) {
    javax.jdo.Query query = pm.newQuery(RunJDO.class);
    if (cursorString != null) {
        Cursor cursor = Cursor.fromWebSafeString(cursorString);
        Map<String, Object> extensionMap = new HashMap<String, Object>();
        extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
        query.setExtensions(extensionMap);
    }
    query.setRange(0, LIMIT);
    return (List<RunJDO>) query.execute();
}
项目:appengine-pipelines    文件:AppEngineBackEnd.java   
@Override
public Pair<? extends Iterable<JobRecord>, String> queryRootPipelines(String classFilter,
    String cursor, final int limit) {
  Query query = new Query(JobRecord.DATA_STORE_KIND);
  Filter filter = classFilter == null || classFilter.isEmpty() ? new FilterPredicate(
      ROOT_JOB_DISPLAY_NAME, GREATER_THAN, null)
      : new FilterPredicate(ROOT_JOB_DISPLAY_NAME, EQUAL, classFilter);
  query.setFilter(filter);
  final PreparedQuery preparedQuery = dataStore.prepare(query);
  final FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
  if (limit > 0) {
    fetchOptions.limit(limit + 1);
  }
  if (cursor != null) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
  }
  return tryFiveTimes(
      new Operation<Pair<? extends Iterable<JobRecord>, String>>("queryRootPipelines") {
        @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());
        }
      });
}
项目:simple-datastore    文件:MassiveDownload.java   
public void setResult(final QueryResultList<Entity> result, final Cursor cursor, final boolean noMore) {
    final ArrayList<Entity> list = new ArrayList<>(result);

    if (bshFilter != null) {
        filterList(list);
    }

    resultBytes = SerializationHelper.getBytes(list);
    hasMore = !noMore;
    webCursor = cursor.toWebSafeString();
}
项目:io2014-codelabs    文件:DeviceSubscription.java   
/**
 * Deletes all device subscription entities continuously using task push queue.
 *
 * @param time Threshold time before which entities created will be deleted. If time is null,
 *             current time is used and set as Threshold time.
 * @param cursor Query cursor indicates last query result set position
 */
protected void deleteAllContinuously(Date time, String cursor) {
  if (time == null) {
    time = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
  }

  Query.FilterPredicate timeFilter = new Query.FilterPredicate(PROPERTY_TIMESTAMP,
      FilterOperator.LESS_THAN_OR_EQUAL, time);
  QueryResultIterable<Entity> entities;
  List<Key> keys = new ArrayList<Key> ();
  List<String> subIds = new ArrayList<String> ();
  Query queryAll;

  queryAll = new Query(DeviceSubscription.SUBSCRIPTION_KIND).setFilter(timeFilter);
  FetchOptions options = FetchOptions.Builder.withLimit(BATCH_DELETE_SIZE);
  if (!StringUtility.isNullOrEmpty(cursor)) {
    options.startCursor(Cursor.fromWebSafeString(cursor));
  }

  entities = this.datastoreService.prepare(queryAll).asQueryResultIterable(options);
  if (entities != null && entities.iterator() != null) {
    for (Entity entity : entities) {
      keys.add(entity.getKey());
      String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
          String[].class);
      subIds.addAll(Arrays.asList(ids));
    }
  }

  if (keys.size() > 0) {
    deleteInBatch(keys);
    enqueueDeleteDeviceSubscription(time, entities.iterator().getCursor().toWebSafeString());
  }
  if (subIds.size() > 0) {
    deletePsiSubscriptions(subIds);
  }
}
项目:mturk-surveys    文件:MergeAnswersServlet.java   
private String merge(String cursorString) {
    Query<UserAnswer> query = ofy().load().type(UserAnswer.class).limit(1000);
    List<UserAnswer> toSaveList = new ArrayList<UserAnswer>(); 

    if (cursorString != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }

    boolean cont = false;
    QueryResultIterator<UserAnswer> iterator = query.iterator();

    while (iterator.hasNext()) {
        UserAnswer userAnswer = iterator.next();
        Map<String, String> answers = userAnswer.getAnswers();
        if(answers != null) {
            String answer = answers.get("householdIncome");
            if("$100,000-$149,999".equals(answer) || "$150,000-$199,999".equals(answer) ||
                    "$200,000-$249,999".equals(answer) || "$300,000 or more".equals(answer)) {
                answers.put("householdIncome", "$100,000 or more");
                toSaveList.add(userAnswer);
            }
        }
        cont = true;
    }

    if(toSaveList.size() > 0) {
        ofy().save().entities(toSaveList).now();
        logger.info(String.format("Merged %d answers", toSaveList.size()));
    }

    if(cont) {
        Cursor cursor = iterator.getCursor();
        return cursor.toWebSafeString();
    } else {
        return null;
    }
}
项目:mturk-surveys    文件:ApproveAssignmentsServlet.java   
private String approve(String cursorString) {
    Query<UserAnswer> query = ofy().load().type(UserAnswer.class).limit(30);

    if (cursorString != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }

    boolean cont = false;
    QueryResultIterator<UserAnswer> iterator = query.iterator();

    while (iterator.hasNext()) {
        UserAnswer userAnswer = iterator.next();
        try {
            List<Assignment> assignments = getAssignmentsForHITService.getAssignments(true, userAnswer.getHitId());
            for(Assignment assignment: assignments) {
                approveAssignmentService.approveAssignment(true, assignment.getAssignmentId());
            }
        } catch (MturkException e) {
            logger.log(Level.WARNING, e.getMessage());
        }
        cont = true;
    }

    if(cont) {
        Cursor cursor = iterator.getCursor();
        return cursor.toWebSafeString();
    } else {
        return null;
    }
}
项目:mturk-surveys    文件:DisposeHITsServlet.java   
private String dispose(String cursorString) {
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(new Date());
    endCal.set(Calendar.HOUR_OF_DAY, 0);
    endCal.set(Calendar.MINUTE, 0);
    endCal.set(Calendar.SECOND, 0);

    Calendar startCal = Calendar.getInstance();
    startCal.setTime(endCal.getTime());
    startCal.add(Calendar.DAY_OF_MONTH, -1);

    Query<UserAnswer> query = ofy().load().type(UserAnswer.class)
            .filter("date >=", startCal.getTime()).filter("date <", endCal.getTime()).limit(30);

    if (cursorString != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }

    boolean cont = false;
    QueryResultIterator<UserAnswer> iterator = query.iterator();

    while (iterator.hasNext()) {
        UserAnswer userAnswer = iterator.next();
        try {
            disposeHITService.disposeHIT(true, userAnswer.getHitId());
            logger.log(Level.INFO, String.format("Disposed HIT %s", userAnswer.getHitId()));
        } catch (MturkException e) {
            logger.log(Level.WARNING, e.getMessage());
        }
        cont = true;
    }

    if(cont) {
        Cursor cursor = iterator.getCursor();
        return cursor.toWebSafeString();
    } else {
        return null;
    }
}
项目:mturk-surveys    文件:UserAnswerService.java   
public CollectionResponse<UserAnswer> list(String cursorString, Integer limit) throws NoSuchAlgorithmException {
    List<UserAnswer> result = new ArrayList<UserAnswer>();
    Query<UserAnswer> query = ofy().load().type(UserAnswer.class)
            .filter("surveyId", DEMOGRAPHICS_SURVEY_ID).order("-date");

    if(cursorString != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }

    if(limit != null) {
        query = query.limit(limit);
    }

    boolean cont = false;
    QueryResultIterator<UserAnswer> iterator = query.iterator();

    while (iterator.hasNext()) {
        UserAnswer userAnswer = iterator.next();
        String workerId = userAnswer.getWorkerId();
        if(workerId != null) {
            userAnswer.setWorkerId(MD5.crypt(workerId));
        }
        userAnswer.setIp(null);
        result.add(userAnswer);
        cont = true;
    }

    if(cont) {
        Cursor cursor = iterator.getCursor();
        return CollectionResponse.<UserAnswer> builder().setItems(result).setNextPageToken(cursor.toWebSafeString()).build();
    } else {
        return CollectionResponse.<UserAnswer> builder().setItems(result).build();
    }
}
项目:gestionDepenseMobile    文件:CategoryEndpoint.java   
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listCategory")
public CollectionResponse<Category> listCategory(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Category> execute = null;

    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery("select from Category as Category");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<Category>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (Category obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<Category> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}
项目:gestionDepenseMobile    文件:ExpenseEndpoint.java   
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listExpense")
public CollectionResponse<Expense> listExpense(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Expense> execute = null;

    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery("select from Expense as Expense");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<Expense>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (Expense obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<Expense> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}
项目:gestionDepenseMobile    文件:IncomeEndpoint.java   
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listIncome")
public CollectionResponse<Income> listIncome(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Income> execute = null;

    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery("select from Income as Income");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<Income>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (Income obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<Income> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}
项目:solutions-mobile-backend-starter-java    文件:DeviceSubscription.java   
/**
 * Deletes all device subscription entities continuously using task push queue.
 *
 * @param time Threshold time before which entities created will be deleted. If time is null,
 *             current time is used and set as Threshold time.
 * @param cursor Query cursor indicates last query result set position
 */
protected void deleteAllContinuously(Date time, String cursor) {
  if (time == null) {
    time = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
  }

  Query.FilterPredicate timeFilter = new Query.FilterPredicate(PROPERTY_TIMESTAMP,
      FilterOperator.LESS_THAN_OR_EQUAL, time);
  QueryResultIterable<Entity> entities;
  List<Key> keys = new ArrayList<Key> ();
  List<String> subIds = new ArrayList<String> ();
  Query queryAll;

  queryAll = new Query(DeviceSubscription.SUBSCRIPTION_KIND).setFilter(timeFilter);
  FetchOptions options = FetchOptions.Builder.withLimit(BATCH_DELETE_SIZE);
  if (!StringUtility.isNullOrEmpty(cursor)) {
    options.startCursor(Cursor.fromWebSafeString(cursor));
  }

  entities = this.datastoreService.prepare(queryAll).asQueryResultIterable(options);
  if (entities != null && entities.iterator() != null) {
    for (Entity entity : entities) {
      keys.add(entity.getKey());
      String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
          String[].class);
      subIds.addAll(Arrays.asList(ids));
    }
  }

  if (keys.size() > 0) {
    deleteInBatch(keys);
    enqueueDeleteDeviceSubscription(time, entities.iterator().getCursor().toWebSafeString());
  }
  if (subIds.size() > 0) {
    deletePsiSubscriptions(subIds);
  }
}
项目:healthy-lifestyle    文件:DeviceSubscription.java   
/**
 * Deletes all device subscription entities continuously using task push queue.
 *
 * @param time Threshold time before which entities created will be deleted. If time is null,
 *             current time is used and set as Threshold time.
 * @param cursor Query cursor indicates last query result set position
 */
protected void deleteAllContinuously(Date time, String cursor) {
  if (time == null) {
    time = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
  }

  Query.FilterPredicate timeFilter = new Query.FilterPredicate(PROPERTY_TIMESTAMP,
      FilterOperator.LESS_THAN_OR_EQUAL, time);
  QueryResultIterable<Entity> entities;
  List<Key> keys = new ArrayList<Key> ();
  List<String> subIds = new ArrayList<String> ();
  Query queryAll;

  queryAll = new Query(DeviceSubscription.SUBSCRIPTION_KIND).setFilter(timeFilter);
  FetchOptions options = FetchOptions.Builder.withLimit(BATCH_DELETE_SIZE);
  if (!StringUtility.isNullOrEmpty(cursor)) {
    options.startCursor(Cursor.fromWebSafeString(cursor));
  }

  entities = this.datastoreService.prepare(queryAll).asQueryResultIterable(options);
  if (entities != null && entities.iterator() != null) {
    for (Entity entity : entities) {
      keys.add(entity.getKey());
      String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
          String[].class);
      subIds.addAll(Arrays.asList(ids));
    }
  }

  if (keys.size() > 0) {
    deleteInBatch(keys);
    enqueueDeleteDeviceSubscription(time, entities.iterator().getCursor().toWebSafeString());
  }
  if (subIds.size() > 0) {
    deletePsiSubscriptions(subIds);
  }
}
项目:appengine-tck    文件:QueryFetchOptionsTest.java   
@Test
public void testStartCursor() {
    QueryResultList<Entity> results = executeQuery(withLimit(3));
    Cursor cursor = results.getCursor();    // points to foo4

    results = executeQuery(withStartCursor(cursor));
    assertEquals(asList(foo4, foo5), results);
}
项目:appengine-tck    文件:QueryFetchOptionsTest.java   
@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);
}
项目:appengine-tck    文件:QueryFetchOptionsTest.java   
@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);
}