Skip to content

Commit 8cc8c73

Browse files
authored
Merge pull request #155 from tsirysndr/refactor/scanner
fix(scanner): re-write the scanner and the search engine, upgrade tantivy to 0.19.2
2 parents d6e029f + 65861cf commit 8cc8c73

File tree

23 files changed

+383
-371
lines changed

23 files changed

+383
-371
lines changed

Cargo.lock

+34-55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphql/src/schema/library.rs

+67-62
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use music_player_entity::{album as album_entity, artist as artist_entity, track
66
use music_player_scanner::scan_directory;
77
use music_player_storage::{
88
repo::{album::AlbumRepository, artist::ArtistRepository, track::TrackRepository},
9+
searcher::Searcher,
910
Database,
1011
};
1112
use music_player_types::types::{self, RemoteCoverUrl, RemoteTrackUrl};
@@ -58,7 +59,6 @@ impl LibraryQuery {
5859
let results = TrackRepository::new(db.lock().await.get_connection())
5960
.find_all(100)
6061
.await?;
61-
println!("album results:");
6262

6363
Ok(results.into_iter().map(Into::into).collect())
6464
}
@@ -247,8 +247,9 @@ impl LibraryQuery {
247247

248248
async fn search(&self, ctx: &Context<'_>, keyword: String) -> Result<SearchResult, Error> {
249249
let db = ctx.data::<Arc<Mutex<Database>>>().unwrap();
250-
let db = db.lock().await;
251-
let indexer = db.get_searcher();
250+
let searcher = ctx.data::<Arc<Mutex<Searcher>>>().unwrap();
251+
let mut db = db.lock().await;
252+
let indexer = searcher.lock().await;
252253
let artists = indexer.artist.search(&keyword)?;
253254
let albums = indexer.album.search(&keyword)?;
254255
let tracks = indexer.track.search(&keyword)?;
@@ -266,68 +267,72 @@ pub struct LibraryMutation;
266267
#[Object]
267268
impl LibraryMutation {
268269
async fn scan(&self, _ctx: &Context<'_>) -> Result<bool, Error> {
269-
scan_directory(move |song, db| {
270-
async move {
271-
let id = format!("{:x}", md5::compute(song.artist.to_string()));
272-
let item = artist_entity::ActiveModel {
273-
id: ActiveValue::set(id),
274-
name: ActiveValue::Set(song.artist.clone()),
275-
};
276-
match item.insert(db.get_connection()).await {
277-
Ok(_) => (),
278-
Err(_) => (),
279-
}
280-
281-
let id = format!(
282-
"{:x}",
283-
md5::compute(format!("{}{}", song.album, song.artist))
284-
);
285-
let item = album_entity::ActiveModel {
286-
id: ActiveValue::set(id),
287-
artist: ActiveValue::set(song.artist.clone()),
288-
title: ActiveValue::Set(song.album.clone()),
289-
artist_id: ActiveValue::Set(Some(format!(
290-
"{:x}",
291-
md5::compute(song.artist.to_string())
292-
))),
293-
year: ActiveValue::Set(song.year),
294-
cover: ActiveValue::Set(song.cover.clone()),
295-
};
296-
match item.insert(db.get_connection()).await {
297-
Ok(_) => (),
298-
Err(_) => (),
299-
}
300-
let id = format!("{:x}", md5::compute(song.uri.as_ref().unwrap()));
301-
let item = track_entity::ActiveModel {
302-
id: ActiveValue::set(id),
303-
title: ActiveValue::Set(song.title.clone()),
304-
artist: ActiveValue::set(song.artist.clone()),
305-
genre: ActiveValue::Set(song.genre.clone()),
306-
year: ActiveValue::Set(song.year),
307-
track: ActiveValue::Set(song.track),
308-
bitrate: ActiveValue::Set(song.bitrate),
309-
sample_rate: ActiveValue::Set(song.sample_rate),
310-
bit_depth: ActiveValue::Set(song.bit_depth),
311-
channels: ActiveValue::Set(song.channels),
312-
duration: ActiveValue::Set(Some(song.duration.as_secs_f32())),
313-
uri: ActiveValue::Set(song.uri.clone().unwrap_or_default()),
314-
album_id: ActiveValue::Set(Some(format!(
270+
scan_directory(
271+
move |song, db| {
272+
async move {
273+
let id = format!("{:x}", md5::compute(song.artist.to_string()));
274+
let item = artist_entity::ActiveModel {
275+
id: ActiveValue::set(id),
276+
name: ActiveValue::Set(song.artist.clone()),
277+
};
278+
match item.insert(db.get_connection()).await {
279+
Ok(_) => (),
280+
Err(_) => (),
281+
}
282+
283+
let id = format!(
315284
"{:x}",
316285
md5::compute(format!("{}{}", song.album, song.artist))
317-
))),
318-
artist_id: ActiveValue::Set(Some(format!(
319-
"{:x}",
320-
md5::compute(song.artist.to_string())
321-
))),
322-
};
323-
324-
match item.insert(db.get_connection()).await {
325-
Ok(_) => (),
326-
Err(_) => (),
286+
);
287+
let item = album_entity::ActiveModel {
288+
id: ActiveValue::set(id),
289+
artist: ActiveValue::set(song.artist.clone()),
290+
title: ActiveValue::Set(song.album.clone()),
291+
artist_id: ActiveValue::Set(Some(format!(
292+
"{:x}",
293+
md5::compute(song.artist.to_string())
294+
))),
295+
year: ActiveValue::Set(song.year),
296+
cover: ActiveValue::Set(song.cover.clone()),
297+
};
298+
match item.insert(db.get_connection()).await {
299+
Ok(_) => (),
300+
Err(_) => (),
301+
}
302+
let id = format!("{:x}", md5::compute(song.uri.as_ref().unwrap()));
303+
let item = track_entity::ActiveModel {
304+
id: ActiveValue::set(id),
305+
title: ActiveValue::Set(song.title.clone()),
306+
artist: ActiveValue::set(song.artist.clone()),
307+
genre: ActiveValue::Set(song.genre.clone()),
308+
year: ActiveValue::Set(song.year),
309+
track: ActiveValue::Set(song.track),
310+
bitrate: ActiveValue::Set(song.bitrate),
311+
sample_rate: ActiveValue::Set(song.sample_rate),
312+
bit_depth: ActiveValue::Set(song.bit_depth),
313+
channels: ActiveValue::Set(song.channels),
314+
duration: ActiveValue::Set(Some(song.duration.as_secs_f32())),
315+
uri: ActiveValue::Set(song.uri.clone().unwrap_or_default()),
316+
album_id: ActiveValue::Set(Some(format!(
317+
"{:x}",
318+
md5::compute(format!("{}{}", song.album, song.artist))
319+
))),
320+
artist_id: ActiveValue::Set(Some(format!(
321+
"{:x}",
322+
md5::compute(song.artist.to_string())
323+
))),
324+
};
325+
326+
match item.insert(db.get_connection()).await {
327+
Ok(_) => (),
328+
Err(_) => (),
329+
}
327330
}
328-
}
329-
.boxed()
330-
})
331+
.boxed()
332+
},
333+
&Database::new().await,
334+
&Searcher::new(),
335+
)
331336
.await?;
332337

333338
Ok(false)

graphql/src/schema/playback.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use super::objects::{
1313
current_track::CurrentlyPlayingSong, player_state::PlayerState, track::Track,
1414
};
1515

16-
use music_player_types::types;
17-
1816
#[derive(Default)]
1917
pub struct PlaybackQuery;
2018

0 commit comments

Comments
 (0)