Adding Pagination Support to My Function App

Brad Malgas
Author
Azure Functions are server less compute functions that can be triggered by HTTP requests. But can we break up calls that are inherently stateless?
Hey there! So, I've been working on making my GetAllPosts method a bit more efficient, especially when dealing with a large number of items. Initially, the feed iterator's default behavior was kind of messy. It used a while loop to keep appending results to a list. This wasn't a big deal with just 5 test items, but imagine having to deal with 500 or even 5000 items! Clearly, I needed a way to limit the number of items returned at once. Enter pagination.
Here's how I added pagination support to my function app, step-by-step:
Step 1: Add a Limit to the Query
First, I needed to add a limit to the QueryRequestOptions payload. This involves specifying the MaxItemCount, which controls the maximum number of items returned in one go.
{
"QueryRequestOptions": {
"MaxItemCount": 5
}
}
You might wonder, "If we have 50 items and set MaxItemCount to 5, won't we keep getting the same 5 items?" That's where the continuation token comes into play.
Step 2: Understanding the Continuation Token
The continuation token is like a bookmark. It keeps track of how far we've gone in our list of results. Here's a breakdown of a sample continuation token:
{
"continuationToken": [
{
"compositeToken": {
"token": "+RID:~mwF6AOFtPloFAAAAAAAAAA==#RT:1#TRC:4#RTD:WAhG8BiSMX8iThoI3+sZBTE1IUt2b2YhMzEzNQA=#ISV:2#IEO:65567#QCF:8",
"range": {
"min": "",
"max": "FF"
}
},
"resumeValues": ["04 June 2024"],
"rid": "mwF6AOFtPloFAAAAAAAAAA==",
"skipCount": 0
}
]
}
Let's break this down further:
- continuationToken: A list containing tokens that track the query's progress.
- compositeToken:
- token: This complex string encodes all the information needed to resume the query.
- +RID: Resource ID of the last item read.
- #RT: Read Type, indicating the type of read operation.
- #TRC: Total Record Count read so far.
- #RTD: Read Token Data for detailed continuation state.
- #ISV: Index Scan Vector, the state of index scanning.
- #IEO: Index Entry Offset, the position within the index to resume.
- #QCF: Query Continuation Flag for additional query state flags.
- range: Defines the valid partition range for the token.
- min: Start of the partition range.
- max: End of the partition range, typically "FF" in hexadecimal.
- resumeValues: Indicates where to resume, such as the last timestamp.
- rid: Resource ID of the last item read.
- skipCount: Number of items to skip, often 0.
How It Works
When querying, we check for a continuation token. If there's one, we continue from that point. If not, we return the first set of items as specified by MaxItemCount.
By implementing this, we can efficiently handle large datasets without overwhelming the system or the user.
In Summary
- Add a limit: Use
MaxItemCountinQueryRequestOptionsto control batch size. - Use continuation tokens: These tokens act as bookmarks to track query progress.
- Paginated results: Check for a continuation token to continue fetching items or start fresh.
This approach keeps things neat and ensures our function app can handle large datasets gracefully. Happy coding!
Loading reactions…
Loading comments…