Minor improvements to indexing:
- Directories now appear before their content during indexing - No longer error when cleaning a large number of missing items
This commit is contained in:
parent
3089c6f9ab
commit
9ee523f8e1
1 changed files with 67 additions and 49 deletions
116
src/db/index.rs
116
src/db/index.rs
|
@ -16,6 +16,7 @@ use metadata;
|
||||||
use vfs::Vfs;
|
use vfs::Vfs;
|
||||||
|
|
||||||
const INDEX_BUILDING_INSERT_BUFFER_SIZE: usize = 1000; // Insertions in each transaction
|
const INDEX_BUILDING_INSERT_BUFFER_SIZE: usize = 1000; // Insertions in each transaction
|
||||||
|
const INDEX_BUILDING_CLEAN_BUFFER_SIZE: usize = 500; // Insertions in each transaction
|
||||||
|
|
||||||
pub struct IndexConfig {
|
pub struct IndexConfig {
|
||||||
pub album_art_pattern: Option<Regex>,
|
pub album_art_pattern: Option<Regex>,
|
||||||
|
@ -174,10 +175,15 @@ impl Index {
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let connection = self.connection.lock().unwrap();
|
{
|
||||||
let connection = connection.deref();
|
let connection = self.connection.lock().unwrap();
|
||||||
diesel::delete(songs::table.filter(songs::columns::path.eq_any(missing_songs)))
|
let connection = connection.deref();
|
||||||
.execute(connection)?;
|
for chunk in missing_songs[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
|
||||||
|
diesel::delete(songs::table.filter(songs::columns::path.eq_any(chunk)))
|
||||||
|
.execute(connection)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -198,11 +204,15 @@ impl Index {
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let connection = self.connection.lock().unwrap();
|
{
|
||||||
let connection = connection.deref();
|
let connection = self.connection.lock().unwrap();
|
||||||
diesel::delete(directories::table.filter(directories::columns::path
|
let connection = connection.deref();
|
||||||
.eq_any(missing_directories)))
|
for chunk in missing_directories[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
|
||||||
.execute(connection)?;
|
diesel::delete(directories::table.filter(directories::columns::path
|
||||||
|
.eq_any(chunk)))
|
||||||
|
.execute(connection)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -269,6 +279,9 @@ impl Index {
|
||||||
let mut inconsistent_directory_year = false;
|
let mut inconsistent_directory_year = false;
|
||||||
let mut inconsistent_directory_artist = false;
|
let mut inconsistent_directory_artist = false;
|
||||||
|
|
||||||
|
// Sub directories
|
||||||
|
let mut sub_directories = Vec::new();
|
||||||
|
|
||||||
// Insert content
|
// Insert content
|
||||||
if let Ok(dir_content) = fs::read_dir(path) {
|
if let Ok(dir_content) = fs::read_dir(path) {
|
||||||
for file in dir_content {
|
for file in dir_content {
|
||||||
|
@ -278,48 +291,48 @@ impl Index {
|
||||||
};
|
};
|
||||||
|
|
||||||
if file_path.is_dir() {
|
if file_path.is_dir() {
|
||||||
self.populate_directory(builder, Some(path), file_path.as_path())?;
|
sub_directories.push(file_path.to_path_buf());
|
||||||
} else {
|
continue;
|
||||||
if let Some(file_path_string) = file_path.to_str() {
|
}
|
||||||
if let Ok(tags) = metadata::read(file_path.as_path()) {
|
|
||||||
if tags.year.is_some() {
|
|
||||||
inconsistent_directory_year |= directory_year.is_some() &&
|
|
||||||
directory_year != tags.year;
|
|
||||||
directory_year = tags.year;
|
|
||||||
}
|
|
||||||
|
|
||||||
if tags.album.is_some() {
|
if let Some(file_path_string) = file_path.to_str() {
|
||||||
inconsistent_directory_album |= directory_album.is_some() &&
|
if let Ok(tags) = metadata::read(file_path.as_path()) {
|
||||||
directory_album != tags.album;
|
if tags.year.is_some() {
|
||||||
directory_album = tags.album.as_ref().map(|a| a.clone());
|
inconsistent_directory_year |= directory_year.is_some() &&
|
||||||
}
|
directory_year != tags.year;
|
||||||
|
directory_year = tags.year;
|
||||||
if tags.album_artist.is_some() {
|
|
||||||
inconsistent_directory_artist |= directory_artist.is_some() &&
|
|
||||||
directory_artist !=
|
|
||||||
tags.album_artist;
|
|
||||||
directory_artist = tags.album_artist.as_ref().map(|a| a.clone());
|
|
||||||
} else if tags.artist.is_some() {
|
|
||||||
inconsistent_directory_artist |= directory_artist.is_some() &&
|
|
||||||
directory_artist != tags.artist;
|
|
||||||
directory_artist = tags.artist.as_ref().map(|a| a.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
let song = NewSong {
|
|
||||||
path: file_path_string.to_owned(),
|
|
||||||
parent: path_string.to_owned(),
|
|
||||||
disc_number: tags.disc_number.map(|n| n as i32),
|
|
||||||
track_number: tags.track_number.map(|n| n as i32),
|
|
||||||
title: tags.title,
|
|
||||||
artist: tags.artist,
|
|
||||||
album_artist: tags.album_artist,
|
|
||||||
album: tags.album,
|
|
||||||
year: tags.year,
|
|
||||||
artwork: artwork.as_ref().map(|s| s.to_owned()),
|
|
||||||
};
|
|
||||||
|
|
||||||
builder.push_song(song)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tags.album.is_some() {
|
||||||
|
inconsistent_directory_album |= directory_album.is_some() &&
|
||||||
|
directory_album != tags.album;
|
||||||
|
directory_album = tags.album.as_ref().map(|a| a.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
if tags.album_artist.is_some() {
|
||||||
|
inconsistent_directory_artist |= directory_artist.is_some() &&
|
||||||
|
directory_artist != tags.album_artist;
|
||||||
|
directory_artist = tags.album_artist.as_ref().map(|a| a.clone());
|
||||||
|
} else if tags.artist.is_some() {
|
||||||
|
inconsistent_directory_artist |= directory_artist.is_some() &&
|
||||||
|
directory_artist != tags.artist;
|
||||||
|
directory_artist = tags.artist.as_ref().map(|a| a.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let song = NewSong {
|
||||||
|
path: file_path_string.to_owned(),
|
||||||
|
parent: path_string.to_owned(),
|
||||||
|
disc_number: tags.disc_number.map(|n| n as i32),
|
||||||
|
track_number: tags.track_number.map(|n| n as i32),
|
||||||
|
title: tags.title,
|
||||||
|
artist: tags.artist,
|
||||||
|
album_artist: tags.album_artist,
|
||||||
|
album: tags.album,
|
||||||
|
year: tags.year,
|
||||||
|
artwork: artwork.as_ref().map(|s| s.to_owned()),
|
||||||
|
};
|
||||||
|
|
||||||
|
builder.push_song(song)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -347,6 +360,11 @@ impl Index {
|
||||||
};
|
};
|
||||||
builder.push_directory(directory)?;
|
builder.push_directory(directory)?;
|
||||||
|
|
||||||
|
// Populate subdirectories
|
||||||
|
for sub_directory in sub_directories {
|
||||||
|
self.populate_directory(builder, Some(path), &sub_directory)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue