【发布时间】:2011-07-29 01:31:01
【问题描述】:
我在 GAE 数据存储中有一个游戏列表,我想查询固定数量的游戏,从某个偏移量开始,即获取下一个 25 场游戏,从 id 为“75”的表单条目开始。
PersistenceManager pm = PMF.get().getPersistenceManager(); // from Google examples
Query query = pm.newQuery(Game.class); // objects of class Game are stored in datastore
query.setOrdering("creationDate asc");
/* querying for open games, not created by this player */
query.setFilter("state == Game.STATE_OPEN && serverPlayer.id != :playerId");
String playerId = "my-player-id";
List<Game> games = query.execute(playerId); // if there's lots of games, returned list has more entries, than user needs to see at a time
//...
现在我需要扩展该查询以仅获取 25 个游戏,并且仅获取 ID 为“75”的条目之后的游戏。因此用户可以浏览打开的游戏,一次只能获取 25 个。 我知道 GAE 数据存储有很多示例,但这些示例大多都使用 Python,包括用于设置查询限制的示例代码。 我正在寻找一个有效的 Java 代码示例,但目前找不到。
【问题讨论】:
-
使用不等查询过滤掉(我认为是)单个结果是一种资源浪费——SDK 必须执行两个查询来满足这一点。相反,从返回的结果集中过滤掉您不想要的单个结果。
-
最多可以有 5 款游戏属于相关玩家。但实际上在查询数百个游戏时,1 或 5 没有区别,所以你明白了,我将把“!=”逻辑移出查询。
标签: java google-app-engine google-cloud-datastore