Here are the examples of the java api class com.google.appengine.api.datastore.Cursor taken from open source projects.
1. QueryCursorTestsSmall#testCursorEndLowLevelBehavior()
Project: objectify
File: QueryCursorTestsSmall.java
File: QueryCursorTestsSmall.java
/** */ @Test public void testCursorEndLowLevelBehavior() throws Exception { com.google.appengine.api.datastore.Query query = new com.google.appengine.api.datastore.Query("Trivial"); PreparedQuery pq = ds().prepare(query); QueryResultIterator<Entity> it = pq.asQueryResultIterable().iterator(); it.next(); it.next(); assert !it.hasNext(); Cursor cursor = it.getCursor(); assert cursor != null; QueryResultIterator<Entity> it2 = pq.asQueryResultIterable(FetchOptions.Builder.withStartCursor(cursor)).iterator(); assert !it2.hasNext(); Cursor cursor2 = it2.getCursor(); assert cursor2 != null; QueryResultIterator<Entity> it3 = pq.asQueryResultIterable(FetchOptions.Builder.withStartCursor(cursor2)).iterator(); assert !it3.hasNext(); assert it3.getCursor() != null; }
2. QueryCursorTestsSmall#testCursorOneFetchToEnd()
Project: objectify
File: QueryCursorTestsSmall.java
File: QueryCursorTestsSmall.java
/** */ @Test public void testCursorOneFetchToEnd() throws Exception { Query<Trivial> q = ofy().load().type(Trivial.class); QueryResultIterator<Trivial> it = q.iterator(); it.next(); it.next(); assert !it.hasNext(); // We should be at end Cursor cursor = it.getCursor(); assert cursor != null; it = q.startAt(cursor).iterator(); assert !it.hasNext(); // Try that again just to be sure cursor = it.getCursor(); assert cursor != null; it = q.startAt(cursor).iterator(); assert !it.hasNext(); }
3. ChunkIterator#next()
Project: objectify
File: ChunkIterator.java
File: ChunkIterator.java
@Override public Chunk<T> next() { Cursor cursor = allKeys.getCursor(); Iterator<Key<T>> keys = chunks.next(); List<Result<T>> results = Lists.newArrayList(); while (keys.hasNext()) { results.add(engine.load(keys.next())); } engine.execute(); Iterable<T> materialized = Iterables.transform(results, ResultNowFunction.<T>instance()); return new Chunk<>(cursor, materialized); }
4. QueryCursorTestsSmall#cursorReverses()
Project: objectify
File: QueryCursorTestsSmall.java
File: QueryCursorTestsSmall.java
/** */ @Test public void cursorReverses() throws Exception { Query<Trivial> q = ofy().load().type(Trivial.class).order("__key__"); QueryResultIterator<Trivial> it = q.iterator(); @SuppressWarnings("unused") Cursor cursor0 = it.getCursor(); final Trivial item1 = it.next(); Cursor cursor1 = it.getCursor(); final Trivial item2 = it.next(); assert !it.hasNext(); Cursor cursor2 = it.getCursor(); Cursor cursor2Rev = it.getCursor().reverse(); it = q.reverse().startAt(cursor2Rev).iterator(); final Trivial item2Rev = it.next(); assert item2Rev.getSomeString().equals(item2.getSomeString()); assert it.hasNext(); final Trivial item1Rev = it.next(); //assert it.getCursor().equals(cursor1); assert item1Rev.getSomeString().equals(item1.getSomeString()); assert !it.hasNext(); }
5. RecommendationEndpoint#listRecommendation()
Project: solutions-mobile-shopping-assistant-backend-java
File: RecommendationEndpoint.java
File: RecommendationEndpoint.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 = "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(); // for lazy fetch. for (Recommendation obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<Recommendation>builder().setItems(execute).setNextPageToken(cursorString).build(); }
6. PlaceEndpoint#listPlace()
Project: solutions-mobile-shopping-assistant-backend-java
File: PlaceEndpoint.java
File: PlaceEndpoint.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 = "listPlace") public CollectionResponse<Place> listPlace(@Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { EntityManager mgr = null; Cursor cursor = null; List<Place> execute = null; try { mgr = getEntityManager(); Query query = mgr.createQuery("select from Place as Place"); 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<Place>) query.getResultList(); cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // for lazy fetch. for (Place obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<Place>builder().setItems(execute).setNextPageToken(cursorString).build(); }
7. OfferEndpoint#listOffer()
Project: solutions-mobile-shopping-assistant-backend-java
File: OfferEndpoint.java
File: OfferEndpoint.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 = "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(); // for lazy fetch. for (Offer obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<Offer>builder().setItems(execute).setNextPageToken(cursorString).build(); }
8. MessageEndpoint#listMessages()
Project: solutions-mobile-shopping-assistant-backend-java
File: MessageEndpoint.java
File: MessageEndpoint.java
/** * This function returns a list of messages starting with the newest message * first and in descending order from there * * @param cursorString * for paging, empty for the first request, subsequent requests can * use the returned information from an earlier request to fill this * parameter * @param limit * number of results returned for this query * @return * A collection of MessageData items */ @SuppressWarnings({ "unchecked", "unused" }) @ApiMethod(name = "listMessages") public CollectionResponse<MessageData> listMessages(@Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { EntityManager mgr = null; Cursor cursor = null; List<MessageData> execute = null; try { mgr = getEntityManager(); // query for messages, newest message first Query query = mgr.createQuery("select from MessageData as MessageData order by timestamp desc"); 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<MessageData>) query.getResultList(); cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // for lazy fetch. for (MessageData obj : execute) { ; } } finally { mgr.close(); } return CollectionResponse.<MessageData>builder().setItems(execute).setNextPageToken(cursorString).build(); }
9. DeviceInfoEndpoint#listDeviceInfo()
Project: solutions-mobile-shopping-assistant-backend-java
File: DeviceInfoEndpoint.java
File: DeviceInfoEndpoint.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 = "listDeviceInfo") public CollectionResponse<DeviceInfo> listDeviceInfo(@Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { EntityManager mgr = null; Cursor cursor = null; List<DeviceInfo> execute = null; try { mgr = getEntityManager(); Query query = mgr.createQuery("select from DeviceInfo as DeviceInfo"); 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<DeviceInfo>) query.getResultList(); cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // for lazy fetch. for (DeviceInfo obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<DeviceInfo>builder().setItems(execute).setNextPageToken(cursorString).build(); }
10. CheckInEndpoint#listCheckIn()
Project: solutions-mobile-shopping-assistant-backend-java
File: CheckInEndpoint.java
File: CheckInEndpoint.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 = "listCheckIn") public CollectionResponse<CheckIn> listCheckIn(@Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { EntityManager mgr = null; Cursor cursor = null; List<CheckIn> execute = null; try { mgr = getEntityManager(); Query query = mgr.createQuery("select from CheckIn as CheckIn"); 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<CheckIn>) query.getResultList(); cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // for lazy fetch. for (CheckIn obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<CheckIn>builder().setItems(execute).setNextPageToken(cursorString).build(); }
11. QueryCursorTestsSmall#testCursorEnd()
Project: objectify
File: QueryCursorTestsSmall.java
File: QueryCursorTestsSmall.java
/** */ @Test public void testCursorEnd() throws Exception { Query<Trivial> q = ofy().load().type(Trivial.class); QueryResultIterator<Trivial> it = q.limit(1).iterator(); assert it.hasNext(); Trivial t1 = it.next(); assert t1.getId().equals(triv1.getId()); assert !it.hasNext(); Cursor cursor = it.getCursor(); assert cursor != null; it = q.startAt(cursor).limit(1).iterator(); assert it.hasNext(); Trivial t2 = it.next(); assert t2.getId().equals(triv2.getId()); assert !it.hasNext(); // We should be at end cursor = it.getCursor(); assert cursor != null; it = q.startAt(cursor).iterator(); assert !it.hasNext(); // Try that again just to be sure cursor = it.getCursor(); assert cursor != null; it = q.startAt(cursor).iterator(); assert !it.hasNext(); }
12. QueryCursorTestsBig#testLimitAndCursorUsingIterator()
Project: objectify
File: QueryCursorTestsBig.java
File: QueryCursorTestsBig.java
/** */ @Test public void testLimitAndCursorUsingIterator() throws Exception { // create 30 objects with someString=foo, // then search for limit 20 (finding cursor at 15) // then search using that cursor // then use get() and see if we get the object at cursor List<Trivial> l1 = new ArrayList<>(); Cursor cursor = null; Query<Trivial> q1 = query().limit(20); QueryResultIterator<Trivial> i1 = q1.iterator(); int which = 0; while (i1.hasNext()) { which++; Trivial trivial = i1.next(); l1.add(trivial); if (which == 15) cursor = i1.getCursor(); } assert l1.size() == 20; List<Trivial> l2 = new ArrayList<>(); Query<Trivial> q2 = query().limit(20).startAt(cursor); QueryResultIterator<Trivial> i2 = q2.iterator(); while (i2.hasNext()) { Trivial trivial = i2.next(); l2.add(trivial); } assert l2.size() == 15; assert l2.get(0) == l1.get(15); assert l2.get(0) == q2.first().now(); }