Skip to content

Add backward seek-based pagination support for the search endpoint #10793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
controllers/helpers/pagination: Generate prev_page for seek-based p…
…agination
  • Loading branch information
eth3lbert committed Mar 10, 2025
commit 847e8bce0898de9c2850e9c120b595dc6065402b
29 changes: 29 additions & 0 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,35 @@ impl<T> Paginated<T> {
Ok(Some(opts))
}

pub(crate) fn prev_seek_params<S, F>(&self, f: F) -> AppResult<Option<IndexMap<String, String>>>
where
F: Fn(&T) -> S,
S: Serialize,
{
// When the data size is smaller than the page size, we would expect the prev page to be
// unavailable during backward pagination but available during forward pagination.
if self.options.is_explicit()
|| self.records_and_total.is_empty()
|| (self.records_and_total.len() < self.options.per_page as usize
&& self.options.is_backward())
{
return Ok(None);
}

// We also like to return None for prev page when it's the first forward pagination.
let mut opts = IndexMap::new();
match self.options.page {
Page::Unspecified => return Ok(None),
Page::Seek(ref raw) if raw.is_empty() => return Ok(None),
Page::Seek(_) | Page::SeekBackward(_) => {
let seek = f(&self.records_and_total.first().unwrap().record);
opts.insert("seek".into(), format!("-{}", encode_seek(seek)?));
}
Page::Numeric(_) => unreachable!(),
};
Ok(Some(opts))
}

pub(crate) fn iter(&self) -> impl Iterator<Item = &T> {
self.records_and_total.iter().map(|row| &row.record)
}
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ pub async fn list_crates(
data.total(),
data.next_seek_params(|last| seek.to_payload(last))?
.map(|p| req.query_with_params(p)),
None,
data.prev_seek_params(|first| seek.to_payload(first))?
.map(|p| req.query_with_params(p)),
data.into_iter().collect::<Vec<_>>(),
)
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/tests/routes/crates/list.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently only tested with a page size of 1. It would be better to expand the tests to include varying page sizes.

Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,9 @@ async fn seek_based_pagination() -> anyhow::Result<()> {
assert_eq!(resp.meta.total, 0);
}

assert_eq!(resp.meta.prev_page, None);
if calls == 1 {
assert_eq!(resp.meta.prev_page, None);
}
}

assert_eq!(calls, 4);
Expand Down