com.google.appengine.api.datastore.Cursor Java Examples

The following examples show how to use com.google.appengine.api.datastore.Cursor. 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: QueryResultTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #2
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #4
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #5
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #6
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #7
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #8
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #9
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #10
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
	}
}
 
Example #11
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndCursor() {
    QueryResultList<Entity> results = executeQuery(withLimit(3));
    Cursor cursor = results.getCursor();    // points to foo4

    results = executeQuery(withEndCursor(cursor));
    assertEquals(asList(foo1, foo2, foo3), results);
}
 
Example #16
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #18
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndCursorAndLimit() {
    QueryResultList<Entity> results = executeQuery(withLimit(3));
    Cursor cursor = results.getCursor();    // points to foo4

    results = executeQuery(withEndCursor(cursor).limit(2));
    assertEquals(asList(foo1, foo2), results);

    results = executeQuery(withEndCursor(cursor).limit(5)); // even if limit is past endCursor, endCursor will still apply
    assertEquals(asList(foo1, foo2, foo3), results);
}
 
Example #19
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: DataMigrationEntitiesBaseScript.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Saves the cursor position to a file so it can be used in the next run.
 */
private void savePositionOfCursorToFile(Cursor cursor) {
    try {
        FileHelper.saveFile(
                BASE_LOG_URI + this.getClass().getSimpleName() + ".cursor", cursor.toWebSafeString());
    } catch (IOException e) {
        logError("Fail to save cursor position " + e.getMessage());
    }
}
 
Example #21
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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]);
}
 
Example #23
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testReverse() {
    Query query = new Query(kindName, rootKey);
    query.addSort(Entity.KEY_RESERVED_PROPERTY);
    QueryResultIterator<Entity> iter = service.prepare(query).asQueryResultIterator();
    Entity e1 = iter.next();
    Entity e2 = iter.next();
    Cursor cursor = iter.getCursor();
    //reverse
    query = query.reverse();
    cursor = cursor.reverse();
    iter = service.prepare(query).asQueryResultIterator(FetchOptions.Builder.withStartCursor(cursor));
    Assert.assertEquals(e2, iter.next());
    Assert.assertEquals(e1, iter.next());
}
 
Example #24
Source File: DeviceSubscription.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #25
Source File: DeviceSubscription.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #26
Source File: DataMigrationEntitiesBaseScript.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads the cursor position from the saved file.
 *
 * @return cursor if the file can be properly decoded.
 */
private Optional<Cursor> readPositionOfCursorFromFile() {
    try {
        String cursorPosition =
                FileHelper.readFile(BASE_LOG_URI + this.getClass().getSimpleName() + ".cursor");
        return Optional.of(Cursor.fromWebSafeString(cursorPosition));
    } catch (IOException | IllegalArgumentException e) {
        return Optional.empty();
    }
}
 
Example #27
Source File: OfferEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = "listOffer")
public CollectionResponse<Offer> listOffer(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

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

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from Offer as Offer");
    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<Offer>) 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 (Offer obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<Offer>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example #28
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@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());
        }
      });
}
 
Example #29
Source File: RecommendationEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = "listRecommendation")
public CollectionResponse<Recommendation> listRecommendation(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

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

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from Recommendation as Recommendation");
    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<Recommendation>) 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 (Recommendation obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<Recommendation>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example #30
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * 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 );
	}
}