Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if the returned cursor is the last cursor in App Engine

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!

like image 213
Chris Dutrow Avatar asked Aug 03 '11 23:08

Chris Dutrow


4 Answers

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.

like image 195
moraes Avatar answered Oct 27 '22 06:10

moraes


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
like image 32
Chris Farmiloe Avatar answered Oct 27 '22 08:10

Chris Farmiloe


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.

like image 5
Dave W. Smith Avatar answered Oct 27 '22 08:10

Dave W. Smith


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]
like image 1
erickCo Avatar answered Oct 27 '22 08:10

erickCo