-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathpagination.rs
150 lines (136 loc) · 4.84 KB
/
pagination.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::marker::PhantomData;
use futures::Stream;
use page_turner::PageTurner;
use page_turner::PageTurnerOutput;
use page_turner::TurnedPage;
use tonic::async_trait;
use crate::api_exec;
/// A simple wrapper around [`postgrest::Builder`] that lets us keep track of which page
/// it's currently on. Used in [`page_turner::PageTurner`] to implement pagination.
pub struct PaginationRequest {
builder: postgrest::Builder,
page: usize,
page_size: usize,
/// The original Builder's range value, if set. This is used to make sure we
/// don't paginate beyond the requested range
range: Option<(usize, usize)>,
}
impl PaginationRequest {
pub fn new(builder: postgrest::Builder) -> Self {
// Extract the limit and offset values out of the builder, if they are defined.
// We have to do this before calling [`set_page()`] since that will override the range header.
// Limit and offset live in the Range header, which looks like this:
// > Range: 4-12
// Where 4 is the offset, and 12 is the limit.
let range = match builder
.clone()
.build()
.build()
.unwrap()
.headers()
.get("Range")
{
Some(range) => range
.to_str()
.ok()
.and_then(|str| {
str.split("-")
.map(|num| num.parse::<usize>().ok())
.collect::<Option<Vec<usize>>>()
})
.and_then(|range| match &range[..] {
&[lower, upper] => Some((lower, upper)),
_ => None,
}),
None => None,
};
Self {
builder,
page: 0,
page_size: 1000,
range,
}
.set_page(0)
}
fn set_page(mut self, page: usize) -> Self {
self.page = page;
let (lower_page, upper_page) = ((page * self.page_size), ((page + 1) * self.page_size));
match self.range {
Some((offset, limit)) => {
self.builder = self.builder.range(
(lower_page + offset).min(limit),
(upper_page + offset).min(limit),
)
}
None => self.builder = self.builder.range(lower_page, upper_page),
}
self
}
}
/// A placeholder struct onto which we can implement [`page_turner::PageTurner`].
/// Normally this would be the API client responsible for actually executing the requests
/// defined in [`PaginationRequest`], but since a [`postgrest::Builder`] already has
/// its own client and is responsible for making its own requests, this is empty.
pub struct PaginationClient<Item>
where
Item: serde::de::DeserializeOwned + Send + Sync,
{
phantom: PhantomData<fn() -> Item>,
}
impl<T> PaginationClient<T>
where
T: serde::de::DeserializeOwned + Send + Sync,
{
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
pub fn into_items<T>(builder: postgrest::Builder) -> impl Stream<Item = Result<T, anyhow::Error>>
where
T: serde::de::DeserializeOwned + Send + Sync + 'static,
{
PaginationClient::<T>::new()
.into_pages(PaginationRequest::new(builder))
.items()
}
#[async_trait]
impl<Item> PageTurner<PaginationRequest> for PaginationClient<Item>
where
Item: serde::de::DeserializeOwned + Send + Sync,
{
type PageItem = Item;
type PageError = anyhow::Error;
async fn turn_page(
&self,
request: PaginationRequest,
) -> PageTurnerOutput<Self, PaginationRequest> {
let resp: Vec<Item> = api_exec::<Vec<Item>>(request.builder.clone()).await?;
// Sometimes, it seems, we can get back more than the requested page size.
// So far I've only seen this on a request of 1,000 and a response of 1,001.
if resp.len() >= request.page_size
// If the original builder had a limit set to the same value as page_size
// this ensures that we stop right at the limit, instead of issuing an extra
// request for 0 rows.
&& request.range.map_or(true, |(_, limit)| {
((request.page + 1) * request.page_size) < limit
})
{
let current_page = request.page;
tracing::debug!(
?current_page,
row_count = resp.len(),
"Got back a full response, progressing to the next page"
);
Ok(TurnedPage::next(resp, request.set_page(current_page + 1)))
} else {
tracing::debug!(
current_page = request.page,
row_count = resp.len(),
"Got back a non-full response or we reached the builder's original limit so we're done"
);
Ok(TurnedPage::last(resp))
}
}
}