Formatting

This commit is contained in:
Antoine Gersant 2020-01-31 19:16:55 -08:00
parent 312eb15a2b
commit 186e3173cd
9 changed files with 140 additions and 129 deletions

View file

@ -21,15 +21,20 @@ pub struct DB {
#[derive(Debug)]
struct ConnectionCustomizer {}
impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error> for ConnectionCustomizer {
impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error>
for ConnectionCustomizer
{
fn on_acquire(&self, connection: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> {
let query = diesel::sql_query(r#"
let query = diesel::sql_query(
r#"
PRAGMA busy_timeout = 60000;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = ON;
"#);
query.execute(connection)
"#,
);
query
.execute(connection)
.map_err(|e| diesel::r2d2::Error::QueryError(e))?;
Ok(())
}

View file

@ -4,11 +4,11 @@ use diesel::prelude::*;
#[cfg(feature = "profile-index")]
use flame;
use log::error;
use std::sync::{Arc, Mutex, Condvar};
use std::sync::{Arc, Condvar, Mutex};
use std::time;
use crate::db::{misc_settings, DB};
use crate::config::MiscSettings;
use crate::db::{misc_settings, DB};
use crate::vfs::VFS;
mod metadata;
@ -18,9 +18,9 @@ mod test;
mod types;
mod update;
pub use self::update::*;
pub use self::query::*;
pub use self::types::*;
pub use self::update::*;
pub fn builder(db: DB) -> IndexBuilder {
IndexBuilder {

View file

@ -1,7 +1,5 @@
use std::path::{Path, PathBuf};
use crate::db;
use crate::db::{directories, songs};
use crate::index::*;

View file

@ -8,8 +8,8 @@ use rayon::prelude::*;
use regex::Regex;
use std::fs;
use std::path::Path;
use std::time;
use std::sync::mpsc::*;
use std::time;
use crate::config::MiscSettings;
use crate::db::{directories, misc_settings, songs, DB};
@ -69,7 +69,11 @@ struct IndexUpdater {
impl IndexUpdater {
#[cfg_attr(feature = "profile-index", flame)]
fn new(album_art_pattern: Regex, directory_sender: Sender<NewDirectory>, song_sender: Sender<NewSong>) -> Result<IndexUpdater> {
fn new(
album_art_pattern: Regex,
directory_sender: Sender<NewDirectory>,
song_sender: Sender<NewSong>,
) -> Result<IndexUpdater> {
Ok(IndexUpdater {
directory_sender,
song_sender,
@ -100,12 +104,12 @@ impl IndexUpdater {
}
fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> {
#[cfg(feature = "profile-index")]
let _guard = flame::start_guard(format!("dir: {}",
path.file_name().map(|s| {
s.to_string_lossy().into_owned()
}).unwrap_or("Unknown".to_owned())
let _guard = flame::start_guard(format!(
"dir: {}",
path.file_name()
.map(|s| { s.to_string_lossy().into_owned() })
.unwrap_or("Unknown".to_owned())
));
// Find artwork
@ -147,7 +151,6 @@ impl IndexUpdater {
// Insert content
for file in fs::read_dir(path)? {
let file_path = match file {
Ok(ref f) => f.path(),
_ => {
@ -157,10 +160,13 @@ impl IndexUpdater {
};
#[cfg(feature = "profile-index")]
let _guard = flame::start_guard(format!("file: {}",
file_path.as_path().file_name().map(|s| {
s.to_string_lossy().into_owned()
}).unwrap_or("Unknown".to_owned())
let _guard = flame::start_guard(format!(
"file: {}",
file_path
.as_path()
.file_name()
.map(|s| { s.to_string_lossy().into_owned() })
.unwrap_or("Unknown".to_owned())
));
if file_path.is_dir() {
@ -170,7 +176,6 @@ impl IndexUpdater {
if let Some(file_path_string) = file_path.to_str() {
if let Some(tags) = metadata::read(file_path.as_path()) {
if tags.year.is_some() {
inconsistent_directory_year |=
directory_year.is_some() && directory_year != tags.year;
@ -339,12 +344,18 @@ pub fn populate(db: &DB) -> Result<()> {
}
match directories_thread.join() {
Err(e) => error!("Error while waiting for directory insertions to complete: {:?}", e),
Err(e) => error!(
"Error while waiting for directory insertions to complete: {:?}",
e
),
_ => (),
}
match songs_thread.join() {
Err(e) => error!("Error while waiting for song insertions to complete: {:?}", e),
Err(e) => error!(
"Error while waiting for song insertions to complete: {:?}",
e
),
_ => (),
}
@ -352,27 +363,31 @@ pub fn populate(db: &DB) -> Result<()> {
}
fn flush_directories(db: &DB, entries: &Vec<NewDirectory>) {
if db.connect()
.and_then(|connection|{
if db
.connect()
.and_then(|connection| {
diesel::insert_into(directories::table)
.values(entries)
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
.map_err(Error::new)
})
.is_err() {
.is_err()
{
error!("Could not insert new directories in database");
}
}
fn flush_songs(db: &DB, entries: &Vec<NewSong>) {
if db.connect()
.and_then(|connection|{
if db
.connect()
.and_then(|connection| {
diesel::insert_into(songs::table)
.values(entries)
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
.map_err(Error::new)
})
.is_err() {
.is_err()
{
error!("Could not insert new songs in database");
}
}
@ -389,7 +404,7 @@ fn insert_directories(receiver: Receiver<NewDirectory>, db: DB) {
flush_directories(&db, &new_entries);
new_entries.clear();
}
},
}
Err(_) => break,
}
}
@ -411,7 +426,7 @@ fn insert_songs(receiver: Receiver<NewSong>, db: DB) {
flush_songs(&db, &new_entries);
new_entries.clear();
}
},
}
Err(_) => break,
}
}

View file

@ -173,9 +173,7 @@ fn main() -> Result<()> {
// Init index
info!("Initializing index");
let index = index::builder(db.clone())
.periodic_updates(true)
.build();
let index = index::builder(db.clone()).periodic_updates(true).build();
// API mount target
let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string());

View file

@ -230,10 +230,7 @@ fn put_preferences(db: State<'_, DB>, auth: Auth, preferences: Json<Preferences>
}
#[post("/trigger_index")]
fn trigger_index(
index: State<'_, Index>,
_admin_rights: AdminRights,
) -> Result<()> {
fn trigger_index(index: State<'_, Index>, _admin_rights: AdminRights) -> Result<()> {
index.trigger_reindex();
Ok(())
}

View file

@ -82,9 +82,7 @@ impl TestService for RocketTestService {
)
.unwrap();
let client = Client::new(server).unwrap();
RocketTestService {
client,
}
RocketTestService { client }
}
fn get(&mut self, url: &str) -> Response<()> {