如何理解“光标"?正确地 [英] how to understand "cursor" correctly

查看:110
本文介绍了如何理解“光标"?正确地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将光标应用于我的应用程序,但是文档对我来说不够清晰. Google对光标的描述 http://code.google.com/appengine/docs/python /datastore/queries.html#Query_Cursors

I'm trying to apply cursor to my app, however, the document is not clear enough for me. Google's description for cursor http://code.google.com/appengine/docs/python/datastore/queries.html#Query_Cursors

将光标的位置定义为结果列表中最后返回的结果之后的位置.游标不是列表中的相对位置(不是偏移量);它是开始对结果进行索引扫描时数据存储可以跳转到的标记.如果查询结果在两次使用游标之间发生变化,则查询仅注意到游标之后结果中发生的变化.如果新结果出现在查询的光标位置之前,则获取游标之后的结果时将不会返回该结果.同样,如果实体不再是查询结果,而是出现在光标之前,则在光标之后出现的结果不会更改.如果从结果集中删除了返回的最后一个结果,则光标仍然知道如何定位下一个结果.

The cursor's position is defined as the location in the result list after the last result returned. A cursor is not a relative position in the list (it's not an offset); it's a marker to which the datastore can jump when starting an index scan for results. If the results for a query change between uses of a cursor, the query notices only changes that occur in results after the cursor. If a new result appears before the cursor's position for the query, it will not be returned when the results after the cursor are fetched. Similarly, if an entity is no longer a result for a query but had appeared before the cursor, the results that appear after the cursor do not change. If the last result returned is removed from the result set, the cursor still knows how to locate the next result.

据我了解,查询结果似乎总是以默认顺序返回(例如__键__).然后,使用指定游标,它将添加一个过滤器以滤除此游标之前的所有结果.就像Google过去提到的那样.是真的吗?

In my understanding, it looks like the query results will always return with a default order (such as __ key __). Then, with the specify cursor, it will add a filter to filter out all results before this cursor. Just as google has mentioned in the past. Is that true?

使用__键__和非唯一属性进行分页 http://code.google.com/appengine/articles/paging.html

Paging with __ key __ and a non-unique property http://code.google.com/appengine/articles/paging.html

另一个问题,游标可以与迭代或任务一起使用吗? 由于某些原因,此功能无法正常工作. 通常,它可能会在迭代过程中生成找不到查询".

Another question, can cursor be used with iteration or task? For some reasons, this function won't work correctly. Usually it may generate "query not found" in the iteration process.

这是我的示例:

people = Person.all().filter("age > ", 30)
if cursor:
     people.with_cursor(cursor)

try:
     for person in people: # query not found
        cursor = people.cursor()

except DeadlineExceededError:
     taskqueue.add(url="/people", params= {"cursor", cursor})

推荐答案

您的理解或多或少是正确的,但不能将光标视为简单地添加过滤器.假设您有一个结果集,该结果集首先按年龄排序,然后按名称排序.如果您最后一次返回的结果是age = 30并且name = Bob,则没有一组条件可以准确地返回此后的结果-age> = 30,name> Bob不会返回31岁的爱丽丝.

Your understanding is more or less correct, but a cursor can't be thought of as simply adding filters. Supposing you have a result set ordered first by age, then by name. If your last returned result has age=30 and name=Bob, there's no set of criteria that will return exactly the results after that - age>=30 and name>Bob won't return Alice, who's 31.

光标更像是结果集中的书签.它表示您离开的地方,因此您可以稍后再回来.如果在光标之前或之后修改了结果集,则光标将保留在同一位置-因此您将始终从上次停下的地方开始.

A cursor is more like a bookmark into your result set. It denotes the place you left off, so you can come back later. If the result set is modified before or after your cursor, the cursor remains in the same place - so you'll always pick up where you left off.

要回答其他问题:是的,查询始终具有隐含顺序.具体取决于所查询的内容(在您的情况下,将首先按年龄,然后按),但它可以确保对结果进行总排序.您引用的分页文章已过时,并且提供了分页前驱方法.您可以忽略它,而使用光标.

To answer your other questions: Yes, queries always have an implied order. What that is depends on the query in question (in your case, it will be first by age, then by key), but it ensures there's a total order over the results. The paging article you refer to is out of date, and provides a pre-cursor approach to pagination. You can ignore it in favor of cursors.

您可以在任务之间(以及与用户之间)传递光标.如果您看到错误,则必须向我们展示stacktrace,我们才能提供任何帮助.

You can pass cursors between tasks (and to and from users) just fine. If you're seeing an error, you'll have to show us the stacktrace before we can be of any help.

这篇关于如何理解“光标"?正确地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆