Index -> IndexManager

This commit is contained in:
Antoine Gersant 2024-07-29 18:13:40 -07:00
parent 2012258a72
commit 64ef7cb21f
6 changed files with 34 additions and 34 deletions

View file

@ -37,7 +37,7 @@ pub struct App {
pub swagger_dir_path: PathBuf,
pub updater: collection::Updater,
pub browser: collection::Browser,
pub index: collection::Index,
pub index_manager: collection::IndexManager,
pub config_manager: config::Manager,
pub ddns_manager: ddns::Manager,
pub lastfm_manager: lastfm::Manager,
@ -65,11 +65,11 @@ impl App {
let auth_secret = settings_manager.get_auth_secret().await?;
let ddns_manager = ddns::Manager::new(db.clone());
let user_manager = user::Manager::new(db.clone(), auth_secret);
let index = collection::Index::new();
let index_manager = collection::IndexManager::new();
let browser = collection::Browser::new(db.clone(), vfs_manager.clone());
let updater = collection::Updater::new(
db.clone(),
index.clone(),
index_manager.clone(),
settings_manager.clone(),
vfs_manager.clone(),
);
@ -94,7 +94,7 @@ impl App {
swagger_dir_path: paths.swagger_dir_path,
updater,
browser,
index,
index_manager,
config_manager,
ddns_manager,
lastfm_manager,

View file

@ -9,27 +9,27 @@ use tokio::sync::RwLock;
use crate::app::collection;
#[derive(Clone)]
pub struct Index {
lookups: Arc<RwLock<Lookups>>,
pub struct IndexManager {
index: Arc<RwLock<Index>>,
}
impl Index {
impl IndexManager {
pub fn new() -> Self {
Self {
lookups: Arc::default(),
index: Arc::default(),
}
}
pub(super) async fn replace_lookup_tables(&mut self, new_lookups: Lookups) {
let mut lock = self.lookups.write().await;
*lock = new_lookups;
pub(super) async fn replace_index(&mut self, new_index: Index) {
let mut lock = self.index.write().await;
*lock = new_index;
}
pub async fn get_random_albums(
&self,
count: usize,
) -> Result<Vec<collection::Album>, collection::Error> {
let lookups = self.lookups.read().await;
let lookups = self.index.read().await;
Ok(lookups
.songs_by_albums
.keys()
@ -56,12 +56,12 @@ struct AlbumKey {
}
#[derive(Default)]
pub(super) struct Lookups {
pub(super) struct Index {
all_songs: HashMap<String, collection::Song>,
songs_by_albums: HashMap<AlbumKey, HashSet<String>>, // TODO should this store collection::Album structs instead?
}
impl Lookups {
impl Index {
pub fn add_song(&mut self, song: &collection::Song) {
self.all_songs
.insert(song.virtual_path.clone(), song.clone());
@ -125,7 +125,7 @@ mod test {
.build()
.await;
ctx.updater.update().await.unwrap();
let albums = ctx.index.get_random_albums(1).await.unwrap();
let albums = ctx.index_manager.get_random_albums(1).await.unwrap();
assert_eq!(albums.len(), 1);
}
@ -136,7 +136,7 @@ mod test {
.build()
.await;
ctx.updater.update().await.unwrap();
let albums = ctx.index.get_recent_albums(2).await.unwrap();
let albums = ctx.index_manager.get_recent_albums(2).await.unwrap();
assert_eq!(albums.len(), 2);
}
}

View file

@ -14,7 +14,7 @@ use crate::{
#[derive(Clone)]
pub struct Updater {
db: DB,
index: Index,
index_manager: IndexManager,
settings_manager: settings::Manager,
vfs_manager: vfs::Manager,
pending_scan: Arc<Notify>,
@ -23,13 +23,13 @@ pub struct Updater {
impl Updater {
pub fn new(
db: DB,
index: Index,
index_manager: IndexManager,
settings_manager: settings::Manager,
vfs_manager: vfs::Manager,
) -> Self {
let updater = Self {
db,
index,
index_manager,
vfs_manager,
settings_manager,
pending_scan: Arc::new(Notify::new()),
@ -123,7 +123,7 @@ impl Updater {
let song_task = tokio::spawn(async move {
let capacity = 500;
let mut lookup_tables = Lookups::default();
let mut index = Index::default();
let mut buffer: Vec<Song> = Vec::with_capacity(capacity);
loop {
@ -134,18 +134,18 @@ impl Updater {
0 => break,
_ => {
for song in buffer.drain(0..) {
lookup_tables.add_song(&song);
index.add_song(&song);
song_inserter.insert(song).await;
}
}
}
}
song_inserter.flush().await;
lookup_tables
index
});
let lookup_tables = tokio::join!(scanner.scan(), directory_task, song_task).2?;
self.index.replace_lookup_tables(lookup_tables).await;
let index = tokio::join!(scanner.scan(), directory_task, song_task).2?;
self.index_manager.replace_index(index).await;
info!(
"Library index update took {} seconds",

View file

@ -7,7 +7,7 @@ use crate::test::*;
pub struct Context {
pub db: DB,
pub browser: collection::Browser,
pub index: collection::Index,
pub index_manager: collection::IndexManager,
pub updater: collection::Updater,
pub config_manager: config::Manager,
pub ddns_manager: ddns::Manager,
@ -68,10 +68,10 @@ impl ContextBuilder {
ddns_manager.clone(),
);
let browser = collection::Browser::new(db.clone(), vfs_manager.clone());
let index = collection::Index::new();
let index_manager = collection::IndexManager::new();
let updater = collection::Updater::new(
db.clone(),
index.clone(),
index_manager.clone(),
settings_manager.clone(),
vfs_manager.clone(),
);
@ -82,7 +82,7 @@ impl ContextBuilder {
Context {
db,
browser,
index,
index_manager,
updater,
config_manager,
ddns_manager,

View file

@ -33,9 +33,9 @@ impl FromRef<App> for app::collection::Browser {
}
}
impl FromRef<App> for app::collection::Index {
impl FromRef<App> for app::collection::IndexManager {
fn from_ref(app: &App) -> Self {
app.index.clone()
app.index_manager.clone()
}
}

View file

@ -368,9 +368,9 @@ async fn get_flatten(
async fn get_random(
_auth: Auth,
api_version: APIMajorVersion,
State(index): State<collection::Index>,
State(index_manager): State<collection::IndexManager>,
) -> Response {
let albums = match index.get_random_albums(20).await {
let albums = match index_manager.get_random_albums(20).await {
Ok(d) => d,
Err(e) => return APIError::from(e).into_response(),
};
@ -380,9 +380,9 @@ async fn get_random(
async fn get_recent(
_auth: Auth,
api_version: APIMajorVersion,
State(index): State<collection::Index>,
State(index_manager): State<collection::IndexManager>,
) -> Response {
let albums = match index.get_recent_albums(20).await {
let albums = match index_manager.get_recent_albums(20).await {
Ok(d) => d,
Err(e) => return APIError::from(e).into_response(),
};