Reputation: 741
I need to implement pagination in my result fetching so while looking around, I got a link to Query Cursors on the Google Developer site but the article explains the use of Query Cursors using the low-level API (I avoid this thing like the plague). Everything I saw on JDO uses setRange(start, end)
which (if I'm right) is not so efficient as you still pay for the overhead involved in fetching and discarding results preceding the range.
How can I use Query Cursors in JDO on top of the App Engine Datastore?
Upvotes: 2
Views: 1676
Reputation: 2520
From appengine docs:
Query q = pm.newQuery(Person.class);
q.setRange(0, 20);
List<Person> results = (List<Person>) q.execute();
// Use the first 20 results
Cursor cursor = JDOCursorHelper.getCursor(results);
https://developers.google.com/appengine/docs/java/datastore/jdo/queries
Then, to use the cursor (also in the documentation provided, which you should read):
String cursorString = cursor.toWebSafeString();
// Send cursor around as string
// Query q = the same query that produced the cursor;
Cursor cursor = Cursor.fromWebSafeString(cursorString);
Map<String, Object> extensionMap = new HashMap<String, Object>();
extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
q.setExtensions(extensionMap);
q.setRange(0, 20); //note, the same range;
//the query should cover everything you expect to load.
q.execute();
Upvotes: 5