本文整理汇总了Java中com.google.appengine.api.datastore.Cursor类的典型用法代码示例。如果您正苦于以下问题:Java Cursor类的具体用法?Java Cursor怎么用?Java Cursor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cursor类属于com.google.appengine.api.datastore包,在下文中一共展示了Cursor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: list
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
/**
* 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();
}
示例2: list
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
/**
* 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();
}
示例3: list
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
/**
* 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();
}
示例4: list
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
/**
* 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();
}
示例5: listBooks
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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);
}
}
示例6: listBooksByUser
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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);
}
}
示例7: countEntities
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
/**
* 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();
}
}
示例8: testCursor
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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());
}
示例9: testSort
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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);
}
示例10: testStartEndCursor
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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);
}
示例11: checkPage
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
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();
}
示例12: create
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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);
}
}
示例13: Builder
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
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);
}
}
示例14: getCursor
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@Override public Cursor getCursor() throws PermanentFailure, RetryableFailure {
return safeRun(new Evaluater<Cursor>() {
@Override public Cursor run() {
return iterator.getCursor();
}
});
}
示例15: listQuote
import com.google.appengine.api.datastore.Cursor; //导入依赖的package包/类
@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();
}