使用游标对象化分页

发布于 2024-11-29 05:42:32 字数 638 浏览 1 评论 0 原文

我的 RPC 服务中有这个方法:

@Override
public Entrata[] getEntrate(int from, int to) {
    List<Entrata> data = entrateDao.list();
    return data.toArray(new Entrata[0]);
}

如您所见,我没有使用这两个参数,在 SQL 世界中,我将使用这两个参数作为 LIMIT 和 OFFSET。

目前还不完全清楚我现在必须做什么,我开始阅读以下内容: http://code.google.com/p/objectify-appengine/ wiki/IntroductionToObjectify#Cursors

我想我必须执行 query.startCursor()

然后迭代“TO”次,页面大小。

好的?你能帮我一些片段吗? :)

I have this method in my RPC service:

@Override
public Entrata[] getEntrate(int from, int to) {
    List<Entrata> data = entrateDao.list();
    return data.toArray(new Entrata[0]);
}

As you can see, I am not using the two parameters, which, in a SQL world, I would use as LIMIT and OFFSET.

It's not completely clear what I have to do now, I started reading this:
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Cursors

I think I have to do a query.startCursor(<my_"from"_parameter>)

Then iterate for "TO" times, the page size.

All right? Can you help me with some snippets? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-12-06 05:42:32

来自文档:游标让您在查询结果集中获取“检查点”,将检查点存储在其他地方,然后从上次停止的位置恢复

由于您只需要限制/偏移,因此必须使用Objectify Query 的 limit()offset() 方法。就像:

ob.query(Entrata.class).limit(to - from).offset(from)

或者,当你有光标时:

String cursor = // get it from request
Query<Entrata> query = ob.query(Entrata.class);
Query q = query.startCursor(Cursor.fromWebSafeString(cursor));
q.limit(x);
QueryResultIterator<Entrate> iterator = query.iterator()
List<Entrate> data = // fetch data
String newCursor = iterrator.getStartCursor().toWebSafeString()
return new EntrataListWithCursor(data, cursor);

From docs: Cursors let you take a "checkpoint" in a query result set, store the checkpoint elsewhere, and then resume from where you left off late

As you need just limit/offset, you have to use limit() and offset() method of Objectify Query. Like:

ob.query(Entrata.class).limit(to - from).offset(from)

Or, when you have cursor:

String cursor = // get it from request
Query<Entrata> query = ob.query(Entrata.class);
Query q = query.startCursor(Cursor.fromWebSafeString(cursor));
q.limit(x);
QueryResultIterator<Entrate> iterator = query.iterator()
List<Entrate> data = // fetch data
String newCursor = iterrator.getStartCursor().toWebSafeString()
return new EntrataListWithCursor(data, cursor);
千年*琉璃梦 2024-12-06 05:42:32

我只是想确保您的代码中没有任何错误,因为您可以复制并粘贴 Igor Artamonov 代码。
这是来自 Objectify Wiki 的更清晰的代码,错误较少​​,并有一些文档:

// create the query and set the limit to 1000
Query<Car> query = ofy().load().type(Car.class).limit(1000);

// Here you get the cursor (if exists) from the request
// For the first request, i-e the first page, this parameter(cursor) will be null
String cursorStr = request.getParameter("cursor");

// Here you check if cursor is not null and not empty
// If so, we start our query from the last check point
if (cursorStr != null && !cursorStr.isEmpty())
    query = query.startAt(Cursor.fromWebSafeString(cursorStr));

// We need this variable to know when we have been loaded all the entries
boolean remaining = false;
QueryResultIterator<Car> iterator = query.iterator();
while (iterator.hasNext()) {
    Car car = iterator.next();

    ... // your code here

    // We have found entries, so we set this variable to true.
    // That means, we have probably another page to fetch
    remaining = true;
}

// If we have found entries, we send the last check point
if (remaining) {
    // we take the last check point by calling "toWebSafeString()" from the iterator's cursor
    Cursor cursor = iterator.getCursor();
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(url("/pathToThisServlet").param("cursor", cursor.toWebSafeString()));
}

I just want make sure you don't have any errors in your code since you can copy and past the Igor Artamonov code.
Here is a cleaner code from Objectify Wiki with less errors and some documentation:

// create the query and set the limit to 1000
Query<Car> query = ofy().load().type(Car.class).limit(1000);

// Here you get the cursor (if exists) from the request
// For the first request, i-e the first page, this parameter(cursor) will be null
String cursorStr = request.getParameter("cursor");

// Here you check if cursor is not null and not empty
// If so, we start our query from the last check point
if (cursorStr != null && !cursorStr.isEmpty())
    query = query.startAt(Cursor.fromWebSafeString(cursorStr));

// We need this variable to know when we have been loaded all the entries
boolean remaining = false;
QueryResultIterator<Car> iterator = query.iterator();
while (iterator.hasNext()) {
    Car car = iterator.next();

    ... // your code here

    // We have found entries, so we set this variable to true.
    // That means, we have probably another page to fetch
    remaining = true;
}

// If we have found entries, we send the last check point
if (remaining) {
    // we take the last check point by calling "toWebSafeString()" from the iterator's cursor
    Cursor cursor = iterator.getCursor();
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(url("/pathToThisServlet").param("cursor", cursor.toWebSafeString()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文