2

我需要在我的结果获取中实现分页,所以在环顾四周时,我在 Google Developer 网站上获得了指向查询游标的链接,但文章解释了使用低级 API 使用查询游标(我避免像瘟疫这样的事情) . 我在 JDO 上看到的所有setRange(start, end)内容(如果我是对的)都不是那么有效,因为您仍然需要为获取和丢弃范围之前的结果所涉及的开销付出代价。如何在 App Engine 数据存储之上的 JDO 中使用查询游标?

4

1 回答 1

5

来自 appengine 文档:

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

然后,使用光标(也在提供的文档中,您应该阅读):

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();
于 2012-12-30T11:58:00.687 回答