Skip to content
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

Add a Places API to recursively count the number of descendants in a … [firefox-android: mhammond/count-bookmarks-in-trees] #5853

Merged
merged 1 commit into from
Oct 12, 2023

Conversation

mhammond
Copy link
Member

@mhammond mhammond commented Oct 4, 2023

…set of folders

Fixes #5789 - that issue implies the API should just count in a single folder, but the link android-components code ends up wanting to do multiple folders, so having this API take a list of folders makes life easier for them - ie, a-c does something like:

bookmarksStorage.countBookmarksInTrees(listOf(BookmarkRoot.Menu.id, BookmarkRoot.Toolbar.id, BookmarkRoot.Unfiled.id)

(I'm shaving many many yaks trying to get a-c to build with this, which are unrelated to the patch, so thought I'd get this up while I fight that)

@linabutler, wdyt?

@mhammond mhammond requested a review from linabutler October 4, 2023 15:44
@mhammond mhammond force-pushed the count_in_tree branch 2 times, most recently from 9e05753 to e88f6a9 Compare October 4, 2023 15:55
@mhammond mhammond changed the title Add a Places API to recursively count the number of descendants in a … Add a Places API to recursively count the number of descendants in a … [firefox-android: mhammond/count-bookmarks-in-trees] Oct 5, 2023
Copy link
Contributor

@linabutler linabutler left a comment

Choose a reason for hiding this comment

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

This is exactly what I had in mind, thanks @mhammond!

* @param guids The guids of folders to query.
* @return Count of all bookmark items (ie, not folders or separators) in all specified folders recursively.
* Empty folders, non-existing GUIDs and non-existing items will return zero.
* The result is implementation dependant if the trees overlap.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for calling this out! I think that's why I was leaning toward having this API take a single folder, instead of a list—but it's a corner case for sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah - my first version took just one arg, but then looking at the code in a-c it was clear that it would always need to sum the values from Menu, Toolbar and Unfiled, then use a separate count for Mobile - so I figured I'd optimize for the actual use-case. FWIW, https://github.com/mhammond/firefox-android/tree/count-bookmarks-in-trees is that branch.

WITH RECURSIVE bookmark_tree(id, parent, type)
AS (
SELECT id, parent, type FROM moz_bookmarks
WHERE parent IN (SELECT id from moz_bookmarks WHERE guid IN ({vars}))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use sql_support::repeat_sql_vars here? I thought for sure we had a helper like repeat_vars already... 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

doh - yes, thanks!

BookmarkType::Bookmark as u8
);
let params = rusqlite::params_from_iter(item_guids);
Ok(db.query_row_and_then(&sql, params, |row| row.get(0))?)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the types work out, could we use our db.query_one extension from sql_support here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's unfortunate that db.query_one doesn't take params :( try_query_one does, so there's odd asymmetry there - I moved to try_query_one anyway even though I don't think we could ever return 0 rows.

@codecov-commenter
Copy link

Codecov Report

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

Comparison is base (e9e3d51) 36.73% compared to head (d1d7d36) 36.72%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5853      +/-   ##
==========================================
- Coverage   36.73%   36.72%   -0.02%     
==========================================
  Files         348      348              
  Lines       33522    33532      +10     
==========================================
  Hits        12313    12313              
- Misses      21209    21219      +10     
Files Coverage Δ
components/places/src/ffi.rs 0.00% <0.00%> (ø)
components/places/src/storage/bookmarks.rs 0.00% <0.00%> (ø)

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

@mhammond mhammond added this pull request to the merge queue Oct 12, 2023
Merged via the queue into mozilla:main with commit 57d348d Oct 12, 2023
17 checks passed
@mhammond mhammond deleted the count_in_tree branch October 12, 2023 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a Places API to recursively count the number of descendants in a folder
3 participants