Cosine Cosine - 20 days ago 6x
Python Question

Find Maximum Index in Google Datastore (Pagination in Blog System)

I have a series of blog posts as entities. I will receive a URL that looks like this:

. I would like to access the most recent 5 posts in this case. In the case of
, I want the 6-10th most recent.

So allow me to do an X-Y, because I think this is the only way:

How do I find the maximum of a value in numerous entities with Google Cloud Platform Datastore? (I'm using

I can give each entity an ID value, and then fetch 5 from a query where
ID < maxIndex - page * 5
sorted by

But, how do I find
? Do I fetch 1 from a query ordered by
, find it's
, and then run the previous operation? That seems somewhat slow for every pageview.

How can I either A) Find the max index quickly or B) Implement pagination otherwise?



For cursors you can use a Datastore entry to store the cursor then send the Datastore key back and forth. Obscuring it by sending data via post requests is another option.

To answer your original question: To get the the top entry do a query with a sort with a limit of 1. This will actually read all the Datastore entries so you should do a keys only search to get the ID (keys only is free) then get the actual posts. So something like this:

class IndexPage(webapp2.RequestHandler):
def get(self):
    maxIndex = Posts.query().order(-Posts.PostIndex).fetch(limit=1, keys_only=True)[0].get().PostIndex        
    page_number = 0
    post_lists = getPageResults(maxIndex, page_number)
    while len(post_lists) > 1:
        self.response.write("====================PAGE NUMBER %i===================</br>" % page_number)
        for post in post_lists:
            self.response.write(str(post.get().PostIndex) + "</br>")
        page_number += 1
        post_lists = getPageResults(maxIndex, page_number)

def getPageResults(maxIndex, page):
    index_range = (maxIndex - (page*5))
    post_index_list = range(index_range, index_range-5, -1)
    return Posts.query(Posts.PostIndex.IN(post_index_list)).order(-Posts.PostIndex).fetch(limit=5, keys_only=True)

Keep in mind I threw this together in a few minutes to illustrate using keys_only and the other points I mentioned above.