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

The following examples show how to use com.google.appengine.api.datastore.QueryResultList. 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: 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 #2
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 #3
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 #4
Source File: QueryResultTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexListFromList() throws Exception {
    Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly");
    Key key = parent.getKey();

    Entity joe = createEntity("Person", key)
        .withProperty("name", "Joe")
        .withProperty("surname", "Moe")
        .store();

    Query query = new Query("Person")
        .setAncestor(key)
        .setKeysOnly();

    PreparedQuery preparedQuery = service.prepare(query);
    QueryResultList<Entity> list = preparedQuery.asQueryResultList(FetchOptions.Builder.withDefaults());
    List<Index> indexes = list.getIndexList();
    if (indexes != null) {
        // TODO -- something useful
        System.out.println("indexes = " + indexes);
    }
}
 
Example #5
Source File: ListPeopleServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private String getFirstCursor() {
  Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
  PreparedQuery pq = datastore.prepare(q);
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE);
  QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
  return results.getCursor().toWebSafeString();
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: PreparedQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsQueryResultList() throws Exception {
    QueryResultList<Entity> list = preparedQuery.asQueryResultList(withDefaults());
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(john, list.get(0));
}
 
Example #15
Source File: QueryFetchOptionsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private QueryResultList<Entity> executeQuery(FetchOptions fetchOptions) {
    Query query = new Query("Foo").addSort("bar");
    return service.prepare(query).asQueryResultList(fetchOptions);
}
 
Example #16
Source File: ListPeopleServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);

  // If this servlet is passed a cursor parameter, let's use it.
  String startCursor = req.getParameter("cursor");
  if (startCursor != null) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
  }

  Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
  PreparedQuery pq = datastore.prepare(q);

  QueryResultList<Entity> results;
  try {
    results = pq.asQueryResultList(fetchOptions);
  } catch (IllegalArgumentException e) {
    // IllegalArgumentException happens when an invalid cursor is used.
    // A user could have manually entered a bad cursor in the URL or there
    // may have been an internal implementation detail change in App Engine.
    // Redirect to the page without the cursor parameter to show something
    // rather than an error.
    resp.sendRedirect("/people");
    return;
  }

  resp.setContentType("text/html");
  resp.setCharacterEncoding("UTF-8");
  PrintWriter w = resp.getWriter();
  w.println("<!DOCTYPE html>");
  w.println("<meta charset=\"utf-8\">");
  w.println("<title>Cloud Datastore Cursor Sample</title>");
  w.println("<ul>");
  for (Entity entity : results) {
    w.println("<li>" + entity.getProperty("name") + "</li>");
  }
  w.println("</ul>");

  String cursorString = results.getCursor().toWebSafeString();

  // This servlet lives at '/people'.
  w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>");
}
 
Example #17
Source File: EntityBatchUpdater.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
/**
 * Processes the entities meeting the criteria defined by the query filter.
 * @return the string representation of the updated count
 */
public String processEntities() {
	if ( maxUpdates <= 0 )
		return getProcessedCountString();
	
	if ( batchSize == null )
		batchSize = 500;
	if ( maxUpdates < batchSize )
		batchSize = maxUpdates;
	
	final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
	
	final FetchOptions fetchOptions = FetchOptions.Builder.withLimit( batchSize );
	
	Cursor cursor = null;
	while ( true ) {
		if ( cursor != null )
			fetchOptions.startCursor( cursor );
		
		final QueryResultList< Entity > resultList = ds.prepare( query ).asQueryResultList( fetchOptions );
		
		for ( final Entity e : resultList ) {
			if ( entityProcessor != null )
				entityProcessor.processEntity( e );
			
			if ( makePropertiesUnindexed != null )
				for ( final String propertyName : makePropertiesUnindexed )
					e.setUnindexedProperty( propertyName, e.getProperty( propertyName ) );
			
			if ( newVersionToSet != null )
				e.setProperty( "v", newVersionToSet );
			
			if ( autoSave )
				ds.put( e );
			
			processedCount++;
		}
		
		if ( resultList.size() < batchSize || processedCount >= maxUpdates )
			return getProcessedCountString();
		
		cursor = resultList.getCursor();
	}
}