Java Code Examples for com.google.appengine.api.datastore.QueryResultList#getCursor()

The following examples show how to use com.google.appengine.api.datastore.QueryResultList#getCursor() . 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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();
	}
}