Keep directory entries sorted as we add them

This commit is contained in:
Antoine Gersant 2024-08-09 08:30:10 -07:00
parent 782da35a7b
commit 169b2b5cb8
2 changed files with 8 additions and 14 deletions
src/app

View file

@ -212,9 +212,9 @@ impl Builder {
self.collection_builder.add_song(song);
}
pub fn build(mut self) -> Index {
pub fn build(self) -> Index {
Index {
browser: self.browser_builder.build(&mut self.strings),
browser: self.browser_builder.build(),
collection: self.collection_builder.build(),
strings: self.strings,
}

View file

@ -1,5 +1,5 @@
use std::{
collections::HashMap,
collections::{BTreeSet, HashMap},
ffi::OsStr,
hash::Hash,
path::{Path, PathBuf},
@ -21,7 +21,7 @@ pub enum File {
#[derive(Serialize, Deserialize)]
pub struct Browser {
directories: HashMap<PathID, Vec<storage::File>>,
directories: HashMap<PathID, BTreeSet<storage::File>>,
flattened: Trie<lasso2::Spur>,
}
@ -97,7 +97,7 @@ impl Browser {
#[derive(Default)]
pub struct Builder {
directories: HashMap<PathID, Vec<storage::File>>,
directories: HashMap<PathID, BTreeSet<storage::File>>,
flattened: TrieBuilder<lasso2::Spur>,
}
@ -119,7 +119,7 @@ impl Builder {
self.directories
.entry(parent_id)
.or_default()
.push(storage::File::Directory(path_id));
.insert(storage::File::Directory(path_id));
}
pub fn add_song(&mut self, strings: &mut ThreadedRodeo, song: &scanner::Song) {
@ -134,7 +134,7 @@ impl Builder {
self.directories
.entry(parent_id)
.or_default()
.push(storage::File::Song(path_id));
.insert(storage::File::Song(path_id));
self.flattened.push(
song.virtual_path
@ -144,13 +144,7 @@ impl Builder {
);
}
pub fn build(mut self, strings: &mut ThreadedRodeo) -> Browser {
for directory in self.directories.values_mut() {
directory.sort_by_key(|f| match f {
storage::File::Directory(p) => strings.resolve(&p.0),
storage::File::Song(p) => strings.resolve(&p.0),
});
}
pub fn build(self) -> Browser {
Browser {
directories: self.directories,
flattened: self.flattened.build(),