Index -> IndexManager
This commit is contained in:
parent
2012258a72
commit
64ef7cb21f
6 changed files with 34 additions and 34 deletions
|
@ -37,7 +37,7 @@ pub struct App {
|
||||||
pub swagger_dir_path: PathBuf,
|
pub swagger_dir_path: PathBuf,
|
||||||
pub updater: collection::Updater,
|
pub updater: collection::Updater,
|
||||||
pub browser: collection::Browser,
|
pub browser: collection::Browser,
|
||||||
pub index: collection::Index,
|
pub index_manager: collection::IndexManager,
|
||||||
pub config_manager: config::Manager,
|
pub config_manager: config::Manager,
|
||||||
pub ddns_manager: ddns::Manager,
|
pub ddns_manager: ddns::Manager,
|
||||||
pub lastfm_manager: lastfm::Manager,
|
pub lastfm_manager: lastfm::Manager,
|
||||||
|
@ -65,11 +65,11 @@ impl App {
|
||||||
let auth_secret = settings_manager.get_auth_secret().await?;
|
let auth_secret = settings_manager.get_auth_secret().await?;
|
||||||
let ddns_manager = ddns::Manager::new(db.clone());
|
let ddns_manager = ddns::Manager::new(db.clone());
|
||||||
let user_manager = user::Manager::new(db.clone(), auth_secret);
|
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 browser = collection::Browser::new(db.clone(), vfs_manager.clone());
|
||||||
let updater = collection::Updater::new(
|
let updater = collection::Updater::new(
|
||||||
db.clone(),
|
db.clone(),
|
||||||
index.clone(),
|
index_manager.clone(),
|
||||||
settings_manager.clone(),
|
settings_manager.clone(),
|
||||||
vfs_manager.clone(),
|
vfs_manager.clone(),
|
||||||
);
|
);
|
||||||
|
@ -94,7 +94,7 @@ impl App {
|
||||||
swagger_dir_path: paths.swagger_dir_path,
|
swagger_dir_path: paths.swagger_dir_path,
|
||||||
updater,
|
updater,
|
||||||
browser,
|
browser,
|
||||||
index,
|
index_manager,
|
||||||
config_manager,
|
config_manager,
|
||||||
ddns_manager,
|
ddns_manager,
|
||||||
lastfm_manager,
|
lastfm_manager,
|
||||||
|
|
|
@ -9,27 +9,27 @@ use tokio::sync::RwLock;
|
||||||
use crate::app::collection;
|
use crate::app::collection;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Index {
|
pub struct IndexManager {
|
||||||
lookups: Arc<RwLock<Lookups>>,
|
index: Arc<RwLock<Index>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index {
|
impl IndexManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
lookups: Arc::default(),
|
index: Arc::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn replace_lookup_tables(&mut self, new_lookups: Lookups) {
|
pub(super) async fn replace_index(&mut self, new_index: Index) {
|
||||||
let mut lock = self.lookups.write().await;
|
let mut lock = self.index.write().await;
|
||||||
*lock = new_lookups;
|
*lock = new_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_random_albums(
|
pub async fn get_random_albums(
|
||||||
&self,
|
&self,
|
||||||
count: usize,
|
count: usize,
|
||||||
) -> Result<Vec<collection::Album>, collection::Error> {
|
) -> Result<Vec<collection::Album>, collection::Error> {
|
||||||
let lookups = self.lookups.read().await;
|
let lookups = self.index.read().await;
|
||||||
Ok(lookups
|
Ok(lookups
|
||||||
.songs_by_albums
|
.songs_by_albums
|
||||||
.keys()
|
.keys()
|
||||||
|
@ -56,12 +56,12 @@ struct AlbumKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(super) struct Lookups {
|
pub(super) struct Index {
|
||||||
all_songs: HashMap<String, collection::Song>,
|
all_songs: HashMap<String, collection::Song>,
|
||||||
songs_by_albums: HashMap<AlbumKey, HashSet<String>>, // TODO should this store collection::Album structs instead?
|
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) {
|
pub fn add_song(&mut self, song: &collection::Song) {
|
||||||
self.all_songs
|
self.all_songs
|
||||||
.insert(song.virtual_path.clone(), song.clone());
|
.insert(song.virtual_path.clone(), song.clone());
|
||||||
|
@ -125,7 +125,7 @@ mod test {
|
||||||
.build()
|
.build()
|
||||||
.await;
|
.await;
|
||||||
ctx.updater.update().await.unwrap();
|
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);
|
assert_eq!(albums.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ mod test {
|
||||||
.build()
|
.build()
|
||||||
.await;
|
.await;
|
||||||
ctx.updater.update().await.unwrap();
|
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);
|
assert_eq!(albums.len(), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ use crate::{
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Updater {
|
pub struct Updater {
|
||||||
db: DB,
|
db: DB,
|
||||||
index: Index,
|
index_manager: IndexManager,
|
||||||
settings_manager: settings::Manager,
|
settings_manager: settings::Manager,
|
||||||
vfs_manager: vfs::Manager,
|
vfs_manager: vfs::Manager,
|
||||||
pending_scan: Arc<Notify>,
|
pending_scan: Arc<Notify>,
|
||||||
|
@ -23,13 +23,13 @@ pub struct Updater {
|
||||||
impl Updater {
|
impl Updater {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
db: DB,
|
db: DB,
|
||||||
index: Index,
|
index_manager: IndexManager,
|
||||||
settings_manager: settings::Manager,
|
settings_manager: settings::Manager,
|
||||||
vfs_manager: vfs::Manager,
|
vfs_manager: vfs::Manager,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let updater = Self {
|
let updater = Self {
|
||||||
db,
|
db,
|
||||||
index,
|
index_manager,
|
||||||
vfs_manager,
|
vfs_manager,
|
||||||
settings_manager,
|
settings_manager,
|
||||||
pending_scan: Arc::new(Notify::new()),
|
pending_scan: Arc::new(Notify::new()),
|
||||||
|
@ -123,7 +123,7 @@ impl Updater {
|
||||||
|
|
||||||
let song_task = tokio::spawn(async move {
|
let song_task = tokio::spawn(async move {
|
||||||
let capacity = 500;
|
let capacity = 500;
|
||||||
let mut lookup_tables = Lookups::default();
|
let mut index = Index::default();
|
||||||
let mut buffer: Vec<Song> = Vec::with_capacity(capacity);
|
let mut buffer: Vec<Song> = Vec::with_capacity(capacity);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -134,18 +134,18 @@ impl Updater {
|
||||||
0 => break,
|
0 => break,
|
||||||
_ => {
|
_ => {
|
||||||
for song in buffer.drain(0..) {
|
for song in buffer.drain(0..) {
|
||||||
lookup_tables.add_song(&song);
|
index.add_song(&song);
|
||||||
song_inserter.insert(song).await;
|
song_inserter.insert(song).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
song_inserter.flush().await;
|
song_inserter.flush().await;
|
||||||
lookup_tables
|
index
|
||||||
});
|
});
|
||||||
|
|
||||||
let lookup_tables = tokio::join!(scanner.scan(), directory_task, song_task).2?;
|
let index = tokio::join!(scanner.scan(), directory_task, song_task).2?;
|
||||||
self.index.replace_lookup_tables(lookup_tables).await;
|
self.index_manager.replace_index(index).await;
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Library index update took {} seconds",
|
"Library index update took {} seconds",
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::test::*;
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub db: DB,
|
pub db: DB,
|
||||||
pub browser: collection::Browser,
|
pub browser: collection::Browser,
|
||||||
pub index: collection::Index,
|
pub index_manager: collection::IndexManager,
|
||||||
pub updater: collection::Updater,
|
pub updater: collection::Updater,
|
||||||
pub config_manager: config::Manager,
|
pub config_manager: config::Manager,
|
||||||
pub ddns_manager: ddns::Manager,
|
pub ddns_manager: ddns::Manager,
|
||||||
|
@ -68,10 +68,10 @@ impl ContextBuilder {
|
||||||
ddns_manager.clone(),
|
ddns_manager.clone(),
|
||||||
);
|
);
|
||||||
let browser = collection::Browser::new(db.clone(), vfs_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(
|
let updater = collection::Updater::new(
|
||||||
db.clone(),
|
db.clone(),
|
||||||
index.clone(),
|
index_manager.clone(),
|
||||||
settings_manager.clone(),
|
settings_manager.clone(),
|
||||||
vfs_manager.clone(),
|
vfs_manager.clone(),
|
||||||
);
|
);
|
||||||
|
@ -82,7 +82,7 @@ impl ContextBuilder {
|
||||||
Context {
|
Context {
|
||||||
db,
|
db,
|
||||||
browser,
|
browser,
|
||||||
index,
|
index_manager,
|
||||||
updater,
|
updater,
|
||||||
config_manager,
|
config_manager,
|
||||||
ddns_manager,
|
ddns_manager,
|
||||||
|
|
|
@ -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 {
|
fn from_ref(app: &App) -> Self {
|
||||||
app.index.clone()
|
app.index_manager.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -368,9 +368,9 @@ async fn get_flatten(
|
||||||
async fn get_random(
|
async fn get_random(
|
||||||
_auth: Auth,
|
_auth: Auth,
|
||||||
api_version: APIMajorVersion,
|
api_version: APIMajorVersion,
|
||||||
State(index): State<collection::Index>,
|
State(index_manager): State<collection::IndexManager>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let albums = match index.get_random_albums(20).await {
|
let albums = match index_manager.get_random_albums(20).await {
|
||||||
Ok(d) => d,
|
Ok(d) => d,
|
||||||
Err(e) => return APIError::from(e).into_response(),
|
Err(e) => return APIError::from(e).into_response(),
|
||||||
};
|
};
|
||||||
|
@ -380,9 +380,9 @@ async fn get_random(
|
||||||
async fn get_recent(
|
async fn get_recent(
|
||||||
_auth: Auth,
|
_auth: Auth,
|
||||||
api_version: APIMajorVersion,
|
api_version: APIMajorVersion,
|
||||||
State(index): State<collection::Index>,
|
State(index_manager): State<collection::IndexManager>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let albums = match index.get_recent_albums(20).await {
|
let albums = match index_manager.get_recent_albums(20).await {
|
||||||
Ok(d) => d,
|
Ok(d) => d,
|
||||||
Err(e) => return APIError::from(e).into_response(),
|
Err(e) => return APIError::from(e).into_response(),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue