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:
Antoine Gersant 2017-06-27 00:50:42 -07:00
parent 3089c6f9ab
commit 9ee523f8e1

View file

@ -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,12 +175,17 @@ impl Index {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
{
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
diesel::delete(songs::table.filter(songs::columns::path.eq_any(missing_songs))) for chunk in missing_songs[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
diesel::delete(songs::table.filter(songs::columns::path.eq_any(chunk)))
.execute(connection)?; .execute(connection)?;
} }
}
}
{ {
let all_directories: Vec<String>; let all_directories: Vec<String>;
{ {
@ -198,12 +204,16 @@ impl Index {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
{
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
for chunk in missing_directories[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
diesel::delete(directories::table.filter(directories::columns::path diesel::delete(directories::table.filter(directories::columns::path
.eq_any(missing_directories))) .eq_any(chunk)))
.execute(connection)?; .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,8 +291,10 @@ 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 Some(file_path_string) = file_path.to_str() {
if let Ok(tags) = metadata::read(file_path.as_path()) { if let Ok(tags) = metadata::read(file_path.as_path()) {
if tags.year.is_some() { if tags.year.is_some() {
@ -296,8 +311,7 @@ impl Index {
if tags.album_artist.is_some() { if tags.album_artist.is_some() {
inconsistent_directory_artist |= directory_artist.is_some() && inconsistent_directory_artist |= directory_artist.is_some() &&
directory_artist != directory_artist != tags.album_artist;
tags.album_artist;
directory_artist = tags.album_artist.as_ref().map(|a| a.clone()); directory_artist = tags.album_artist.as_ref().map(|a| a.clone());
} else if tags.artist.is_some() { } else if tags.artist.is_some() {
inconsistent_directory_artist |= directory_artist.is_some() && inconsistent_directory_artist |= directory_artist.is_some() &&
@ -323,7 +337,6 @@ impl Index {
} }
} }
} }
}
// Insert directory // Insert directory
if inconsistent_directory_year { if inconsistent_directory_year {
@ -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(())
} }