如何判断返回的光标是否是 App Engine 中的最后一个光标

发布于 2024-11-27 22:48:59 字数 167 浏览 0 评论 0原文

如果我遗漏了一些非常明显的东西,我深表歉意。

我正在使用光标连续调用应用程序引擎。如何判断我是否位于最后一个光标上?我现在执行的当前方法是保存最后一个光标,然后测试该光标是否等于当前返回的光标。这需要对数据存储进行额外的调用,但这可能是不必要的。

有更好的方法吗?

谢谢!

I apologize if I am missing something really obvious.

I'm making successive calls to app engine using cursors. How do I tell if the I'm on the last cursor? The current way I'm doing it now is to save the last cursor and then testing to see if that cursor equals the currently returned cursor. This requires an extra call to the datastore which is probably unnecessary though.

Is there a better way to do this?

Thanks!

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

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

发布评论

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

评论(5

寄意 2024-12-04 22:48:59

我认为没有办法在单个数据存储区调用中使用 ext.db 来执行此操作,而是使用 ndb 这是可能的。示例:

query = Person.query(Person.name == 'Guido')
result, cursor, more = query.fetch_page(10)

如果使用返回的游标将导致更多记录,则 more 将为 True。这是在单个 RPC 调用中巧妙完成的。

I don't think there's a way to do this with ext.db in a single datastore call, but with ndb it is possible. Example:

query = Person.query(Person.name == 'Guido')
result, cursor, more = query.fetch_page(10)

If using the returned cursor will result in more records, more will be True. This is done smartly, in a single RPC call.

何止钟意 2024-12-04 22:48:59

既然您说“最后一个游标”,我假设您正在使用游标进行某种分页,这意味着您将使用限制批量获取结果。

在这种情况下,当返回的结果少于限制时,您就知道您位于最后一个光标。

limit = 100
results = Entity.all().with_cursor('x').fetch(limit)
if len(results)<limit:
    # then there's no point trying to fetch another batch after this one

Since you say 'last cursor' I assume you are using cursors for some kind of pagination, which implies you will be fetching results in batches with a limit.

In this case then you know you are on the last cursor when you have less results returned than your limit.

limit = 100
results = Entity.all().with_cursor('x').fetch(limit)
if len(results)<limit:
    # then there's no point trying to fetch another batch after this one
心在旅行 2024-12-04 22:48:59

如果您的意思是“此光标是否已到达搜索结果的末尾”,那么不,除非拿起光标并再次尝试。如果添加更多与原始搜索条件匹配的实体,以便它们在逻辑上落在光标“之后”(例如,按升序时间戳排序的查询),则重用已保存的光标将允许您检索这些新实体。

If you mean "has this cursor hit the end of the search results", then no, not without picking the cursor up and trying it again. If more entities are added that match the original search criteria, such that they logically land "after" the cursor (e.g., a query that sorts by an ascending timestamp), then reusing that saved cursor will let you retrieve those new entities.

诗化ㄋ丶相逢 2024-12-04 22:48:59

我使用 Chris Familoe 描述的相同技术,但设置的限制比我希望返回的多 1。因此,在 Chris 的示例中,我将获取 101 个实体。返回 101 意味着我还有另一页至少有 1 个。

    recs = db_query.fetch(limit + 1, offset)

    # if less records returned than requested, we've reached the end
    if len(recs) < limit + 1:
        lastpage = True
        entries = recs
    else:
        lastpage = False
        entries = recs[:-1]

I use the same technique Chris Familoe describes, but set the limit 1 more than I wish to return. So, in Chris' example, I would fetch 101 entities. 101 returned means I have another page with at least 1 on.

    recs = db_query.fetch(limit + 1, offset)

    # if less records returned than requested, we've reached the end
    if len(recs) < limit + 1:
        lastpage = True
        entries = recs
    else:
        lastpage = False
        entries = recs[:-1]
擦肩而过的背影 2024-12-04 22:48:59

我知道这篇文章有点旧,但我一直在寻找同一问题的解决方案。我在这本优秀的书中找到了它:

http://shop.oreilly.com/product/0636920017547.do

这是提示:

    results = query.fetch(RESULTS_FOR_PAGE)

    new_cursor = query.cursor()
    query.with_cursor(new_cursor)
    has_more_results = query.count(1) == 1

I know this post is kind of old but I was looking for a solution to the same problem. I found it in this excellent book:

http://shop.oreilly.com/product/0636920017547.do

Here is the tip:

    results = query.fetch(RESULTS_FOR_PAGE)

    new_cursor = query.cursor()
    query.with_cursor(new_cursor)
    has_more_results = query.count(1) == 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文