1

Hi I'm using google app engine to create a picture hosting website and each page holds 5 images, I want to get a bool which will tell me if there is a subsequent page (they are stored in descending date order). I have a page limit and a page number so what I need is a datastore query that will:

get a single value for the rownum pagenumber * pagelimit (check if theres something stored after the 5 displaying on the page).

Currently I have:

    Query query = new Query("PicturePost").addSort("date",Query.SortDirection.DESCENDING);
    List<Entity> results;

    results = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(pageLimit)
                                    .offset(pageLimit*(pageNumber)));

    boolean lastPage = results.isEmpty();

but this would be inefficient as it gets all 5 entities from the next page.

If anyone could help me I'd be very grateful, thankyou !

4

1 回答 1

1

This seems to work pretty well for me. YMMV. I always include a date_created field [auto_now_add=True] on any entity that needs serial access. The browser client should call the handler function with two inputs: number_of_images to return, and date_limit. First call will have an empty string for date_limit, so you use the current system datetime. Other calls will include the earliest of the dates you sent back in the previous call.

Your query will be (date_created < date_limit) and date_created DESC. Do a keys_only fetch of the number of desired images, and send the keys back for the img src="key_goes_here" setup. If you return less than the desired number of images, or zero then your are out of new images.

If this is an active page, I would be sure to use ndb for the automatic memcaching and tell the browser to cache it in your content header. (You can very easily write a simple memcache function if using db.) Yes this is not perfect: a small % of the time you incur a wasted call because you return N images, and do not know that the next call will return zero. If super active site, you might have a duplicate of date_created on your image put(). You have the carry the overhead of a custom index, but I think you will incur this cost no matter what approach. As with many GAE things there are often tradeoffs. HTH. -stevep

于 2013-06-25T01:49:54.690 回答