views:

45

answers:

1

Hi.

Currently, to determine whether or not there is a next page of entities I'm using the following code:

q = Entity.all().fetch(10)
cursor = q.cursor()
extra = q.fetch(1)

has_next_page = False
if extra:
  has_next_page = True

However, this is very expensive in terms of the time it takes to execute the 'extra' query. I need to extract the cursor after 10 results, but I need to fetch 11 to see if there is a succeeding page.

Anyone have any better methods?

+1  A: 

If you fetch 11 items straight away you'll only have to fetch 1 extra item to know if there is a next page or not. And you can just display the first 10 results and use the 11th result only as a "next page" indicator.

WoLpH
That's what I thought at first, but querying for 11 up front doesn't yield a cursor positioned after the 10th item, which the questioner wants.
Steve Jessop
But then my cursor (which I use as a starting point for the next page) will be invalid (it will start the next query at the 12th item). This will lead to one entity missing per page.
Matt H
@Matt, just put the "extra item" in memcache at a suitably unique key, and on "next page" just check if there's something at that unique key (and if so delete it and prepend it to the set fetched from the cursor).
Alex Martelli
@Matt H: that is indeed a downside, but easily worked around. As Alex Martelli already explained.
WoLpH