I know how to page forward with SimpleDB data by using NextToken. However, how exactly does one handle previous pages? I'm on .NET, but I don't think that matters. I'm more interested in the general strategy.
Mike Culver's An Introduction to Amazon SimpleDB webinar mentions that breadcrumbs are used, but he doesn't implement them in the video.
EDIT: The video mentions a sample project which implements backwards paging, but the video ends before the URL for the download can be displayed. The one sample project I found didn't deal with paging.
Last Updated : 28 Jun, 2021 Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. This scheme permits the physical address space of a process to be non – contiguous. Logical Address or Virtual Address (represented in bits): An address generated by the CPU
Page number (p): Number of bits required to represent the pages in Logical Address Space or Page number Page offset (d): Number of bits required to represent particular word in a page or page size of Logical Address Space or word number of a page or page offset.
But the usage of register for the page table is satisfactory only if page table is small. If page table contain large number of entries then we can use TLB (translation Look-aside buffer), a special, small, fast look up hardware cache. The TLB is associative, high speed memory.
When going to the next page you may be able to simplify the use case by only allowing a "next page" and not arbitrary paging. You can do this in SimpleDB by using the LIMIT clause:
SELECT title, summary, votecount FROM posts WHERE userid = '000022656' LIMIT 25
You already know how to handle the NextToken, but if you use this tactic, you can support "previous page" by storing the breadcrumb trail of next tokens (e.g. in the web session) and re-issuing the query with a previous NextToken rather than a subsequent one.
However, the general case for handling arbitrary pagination in SimpleDB is the same for previous and next. In the general case, the user may click on an arbitrary page number, like 5, without ever having visited page 4 or 6.
You handle this in SimpleDB by using the fact that NextToken only requires the WHERE clause to be the same to work properly. So rather than querying through every page in sequence pulling down all the intervening items, you can usually do it in two steps.
So in pseudo code:
int targetPage, pageSize;
...
int jumpLimit = pageSize * (targetPage - 1);
String query = "SELECT %1 FROM posts WHERE userid = '000022656' LIMIT %2";
String output = "title, summary, votecount";
Result temp = sdb.select(query, "count(*)", jumpLimit);
Result data = sdb.select(query, output, pageSize, temp.getToken());
Where %1 and %2 are String substitutions and "sdb.select()" is a fictitious method that includes the String substitution code along with the SimpleDB call.
Whether or not you can accomplish this in two calls to SimpleDB (as shown in the code) will depend on the complexity of your WHERE clause and the size of your data set. The above code is simplified in that the temp result may have returned a partial count if the query took more than 5 seconds to run. You would really want to put that line in a loop until the proper count is reached. To make the code a little more realistic I'll put it within methods and get rid of the String substitutions:
private Result fetchPage(String query, int targetPage)
{
int pageSize = extractLimitValue(query);
int skipLimit = pageSize * (targetPage - 1);
String token = skipAhead(query, skipLimit);
return sdb.select(query, token);
}
private String skipAhead(String query, int skipLimit)
{
String tempQuery = replaceClause(query, "SELECT", "count(*)");
int accumulatedCount = 0;
String token = "";
do {
int tempLimit = skipLimit - accumulatedCount;
tempQuery = replaceClause(tempQuery , "LIMIT", tempLimit + "");
Result tempResult = sdb.select(query, token);
token = tempResult.getToken();
accumulatedCount += tempResult.getCount();
} while (accumulatedCount < skipLimit);
return token;
}
private int extractLimitValue(String query) {...}
private String replaceClause(String query, String clause, String value){...}
This is the general idea without error handling, and works for any arbitrary page, excluding page 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With