Skip to content

Expand the scope of seek-based pagination on search #7983

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

Merged
merged 6 commits into from
Feb 20, 2024

Conversation

eth3lbert
Copy link
Contributor

@eth3lbert eth3lbert commented Jan 22, 2024

This PR add seek-based pagination support for all sorting.

This PR is best reviewed commit-by-commit.

Optimization opportunities remain, but not within this PR:

  • Instead of simply limiting the query to per_page, we could check for additional records by querying with per_page + 1. If the actual count exceeds per_page, it indicates there's a next page; otherwise, the next page is None. This eliminates unnecessary requests when no further pages exist.

Copy link

codecov bot commented Jan 22, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (93e5a53) 87.67% compared to head (64d05d5) 87.55%.

❗ Current head 64d05d5 differs from pull request most recent head 548be21. Consider uploading reports for the commit 548be21 to get more accurate results

Files Patch % Lines
src/tests/routes/crates/list.rs 99.48% 2 Missing ⚠️
src/controllers/helpers/pagination.rs 99.03% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7983      +/-   ##
==========================================
- Coverage   87.67%   87.55%   -0.12%     
==========================================
  Files         271      271              
  Lines       26750    26574     -176     
==========================================
- Hits        23452    23268     -184     
- Misses       3298     3306       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Turbo87 Turbo87 added C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works A-backend ⚙️ labels Jan 22, 2024

query = query.order(crates::created_at.desc());
seek = Some(Seek::New);
query = query.order((crates::created_at.desc(), crates::id.asc()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If larger ids signify newer items with the same created_at timestamp, it might be more intuitive to switch the sorting order from ascending crates::id.asc() to descending crates::id.desc().

Copy link
Member

Choose a reason for hiding this comment

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

If larger ids signify newer items with the same created_at timestamp

yep, that's how it would be. I'm not sure if it's worth breaking the consistency. I haven't checked, but I would be surprised if we actually had crates in the database with the exact same created_at timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we can guarantee that timestamps will never be identical, then the auxiliary ordering column becomes unnecessary and can be removed. However, if the possibility of duplicate timestamps exists, then it's better to keep it.


Since we all use descending order for sorting, I think it would be more intuitive to change all the auxiliary column id.asc() to id.desc(). What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be more intuitive to change all the auxiliary column id.asc() to id.desc()

yeah, that should also work :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is implemented in the new commit.

Copy link
Member

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

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

looks good to me on first/second glance :)

@rust-lang/crates-io a second pair of eyes won't hurt though

Comment on lines 459 to 480
// Test for bug with showing null results first when sorting
// by descending downloads
let json = anon.search("sort=recent-downloads");
assert_eq!(json.meta.total, 4);
assert_eq!(json.crates[0].name, "foo_sort");
assert_eq!(json.crates[1].name, "baz_sort");
assert_eq!(json.crates[2].name, "bar_sort");
assert_eq!(json.crates[3].name, "other_sort");
for json in search_both(&anon, "sort=recent-downloads") {
assert_eq!(json.meta.total, 4);
assert_eq!(json.crates[0].name, "foo_sort");
assert_eq!(json.crates[1].name, "baz_sort");
assert_eq!(json.crates[2].name, "bar_sort");
assert_eq!(json.crates[3].name, "other_sort");
}
let (resp, calls) = page_with_seek(&anon, "sort=recent-downloads");
assert_eq!(resp[0].crates[0].name, "foo_sort");
assert_eq!(resp[1].crates[0].name, "baz_sort");
assert_eq!(resp[2].crates[0].name, "bar_sort");
assert_eq!(resp[3].crates[0].name, "other_sort");
assert_eq!(resp[3].meta.total, 4);
assert_eq!(calls, 5);
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 code snippet can be safely removed as it's already covered by above test case.

@Astrabacus Astrabacus mentioned this pull request Jan 30, 2024
@bors
Copy link
Contributor

bors commented Feb 2, 2024

☔ The latest upstream changes (presumably #8050) made this pull request unmergeable. Please resolve the merge conflicts.

@eth3lbert
Copy link
Contributor Author

Rebased and resolved all merge conflicts.

@Turbo87
Copy link
Member

Turbo87 commented Feb 20, 2024

Rebased once more. AFAICT all of the existing functionality should still work the same as before, so I think we can try this out as an experimental feature. If it works, great, if it doesn't, we can always revert :)

@Turbo87 Turbo87 enabled auto-merge February 20, 2024 11:19
@Turbo87 Turbo87 merged commit fd79e73 into rust-lang:main Feb 20, 2024
@eth3lbert eth3lbert deleted the search-seek branch February 26, 2024 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend ⚙️ C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants