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

View file

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

View file

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