Formatting
This commit is contained in:
parent
312eb15a2b
commit
186e3173cd
9 changed files with 140 additions and 129 deletions
|
@ -21,15 +21,20 @@ pub struct DB {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ConnectionCustomizer {}
|
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> {
|
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 busy_timeout = 60000;
|
||||||
PRAGMA journal_mode = WAL;
|
PRAGMA journal_mode = WAL;
|
||||||
PRAGMA synchronous = NORMAL;
|
PRAGMA synchronous = NORMAL;
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
"#);
|
"#,
|
||||||
query.execute(connection)
|
);
|
||||||
|
query
|
||||||
|
.execute(connection)
|
||||||
.map_err(|e| diesel::r2d2::Error::QueryError(e))?;
|
.map_err(|e| diesel::r2d2::Error::QueryError(e))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,11 @@ use diesel::prelude::*;
|
||||||
#[cfg(feature = "profile-index")]
|
#[cfg(feature = "profile-index")]
|
||||||
use flame;
|
use flame;
|
||||||
use log::error;
|
use log::error;
|
||||||
use std::sync::{Arc, Mutex, Condvar};
|
use std::sync::{Arc, Condvar, Mutex};
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
use crate::db::{misc_settings, DB};
|
|
||||||
use crate::config::MiscSettings;
|
use crate::config::MiscSettings;
|
||||||
|
use crate::db::{misc_settings, DB};
|
||||||
use crate::vfs::VFS;
|
use crate::vfs::VFS;
|
||||||
|
|
||||||
mod metadata;
|
mod metadata;
|
||||||
|
@ -18,9 +18,9 @@ mod test;
|
||||||
mod types;
|
mod types;
|
||||||
mod update;
|
mod update;
|
||||||
|
|
||||||
pub use self::update::*;
|
|
||||||
pub use self::query::*;
|
pub use self::query::*;
|
||||||
pub use self::types::*;
|
pub use self::types::*;
|
||||||
|
pub use self::update::*;
|
||||||
|
|
||||||
pub fn builder(db: DB) -> IndexBuilder {
|
pub fn builder(db: DB) -> IndexBuilder {
|
||||||
IndexBuilder {
|
IndexBuilder {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::db::{directories, songs};
|
use crate::db::{directories, songs};
|
||||||
use crate::index::*;
|
use crate::index::*;
|
||||||
|
|
|
@ -8,8 +8,8 @@ use rayon::prelude::*;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time;
|
|
||||||
use std::sync::mpsc::*;
|
use std::sync::mpsc::*;
|
||||||
|
use std::time;
|
||||||
|
|
||||||
use crate::config::MiscSettings;
|
use crate::config::MiscSettings;
|
||||||
use crate::db::{directories, misc_settings, songs, DB};
|
use crate::db::{directories, misc_settings, songs, DB};
|
||||||
|
@ -69,7 +69,11 @@ struct IndexUpdater {
|
||||||
|
|
||||||
impl IndexUpdater {
|
impl IndexUpdater {
|
||||||
#[cfg_attr(feature = "profile-index", flame)]
|
#[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 {
|
Ok(IndexUpdater {
|
||||||
directory_sender,
|
directory_sender,
|
||||||
song_sender,
|
song_sender,
|
||||||
|
@ -100,12 +104,12 @@ impl IndexUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> {
|
fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> {
|
||||||
|
|
||||||
#[cfg(feature = "profile-index")]
|
#[cfg(feature = "profile-index")]
|
||||||
let _guard = flame::start_guard(format!("dir: {}",
|
let _guard = flame::start_guard(format!(
|
||||||
path.file_name().map(|s| {
|
"dir: {}",
|
||||||
s.to_string_lossy().into_owned()
|
path.file_name()
|
||||||
}).unwrap_or("Unknown".to_owned())
|
.map(|s| { s.to_string_lossy().into_owned() })
|
||||||
|
.unwrap_or("Unknown".to_owned())
|
||||||
));
|
));
|
||||||
|
|
||||||
// Find artwork
|
// Find artwork
|
||||||
|
@ -147,7 +151,6 @@ impl IndexUpdater {
|
||||||
|
|
||||||
// Insert content
|
// Insert content
|
||||||
for file in fs::read_dir(path)? {
|
for file in fs::read_dir(path)? {
|
||||||
|
|
||||||
let file_path = match file {
|
let file_path = match file {
|
||||||
Ok(ref f) => f.path(),
|
Ok(ref f) => f.path(),
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -157,10 +160,13 @@ impl IndexUpdater {
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "profile-index")]
|
#[cfg(feature = "profile-index")]
|
||||||
let _guard = flame::start_guard(format!("file: {}",
|
let _guard = flame::start_guard(format!(
|
||||||
file_path.as_path().file_name().map(|s| {
|
"file: {}",
|
||||||
s.to_string_lossy().into_owned()
|
file_path
|
||||||
}).unwrap_or("Unknown".to_owned())
|
.as_path()
|
||||||
|
.file_name()
|
||||||
|
.map(|s| { s.to_string_lossy().into_owned() })
|
||||||
|
.unwrap_or("Unknown".to_owned())
|
||||||
));
|
));
|
||||||
|
|
||||||
if file_path.is_dir() {
|
if file_path.is_dir() {
|
||||||
|
@ -170,7 +176,6 @@ impl IndexUpdater {
|
||||||
|
|
||||||
if let Some(file_path_string) = file_path.to_str() {
|
if let Some(file_path_string) = file_path.to_str() {
|
||||||
if let Some(tags) = metadata::read(file_path.as_path()) {
|
if let Some(tags) = metadata::read(file_path.as_path()) {
|
||||||
|
|
||||||
if tags.year.is_some() {
|
if tags.year.is_some() {
|
||||||
inconsistent_directory_year |=
|
inconsistent_directory_year |=
|
||||||
directory_year.is_some() && directory_year != tags.year;
|
directory_year.is_some() && directory_year != tags.year;
|
||||||
|
@ -339,12 +344,18 @@ pub fn populate(db: &DB) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
match directories_thread.join() {
|
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() {
|
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>) {
|
fn flush_directories(db: &DB, entries: &Vec<NewDirectory>) {
|
||||||
if db.connect()
|
if db
|
||||||
|
.connect()
|
||||||
.and_then(|connection| {
|
.and_then(|connection| {
|
||||||
diesel::insert_into(directories::table)
|
diesel::insert_into(directories::table)
|
||||||
.values(entries)
|
.values(entries)
|
||||||
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
|
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
|
||||||
.map_err(Error::new)
|
.map_err(Error::new)
|
||||||
})
|
})
|
||||||
.is_err() {
|
.is_err()
|
||||||
|
{
|
||||||
error!("Could not insert new directories in database");
|
error!("Could not insert new directories in database");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush_songs(db: &DB, entries: &Vec<NewSong>) {
|
fn flush_songs(db: &DB, entries: &Vec<NewSong>) {
|
||||||
if db.connect()
|
if db
|
||||||
|
.connect()
|
||||||
.and_then(|connection| {
|
.and_then(|connection| {
|
||||||
diesel::insert_into(songs::table)
|
diesel::insert_into(songs::table)
|
||||||
.values(entries)
|
.values(entries)
|
||||||
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
|
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822
|
||||||
.map_err(Error::new)
|
.map_err(Error::new)
|
||||||
})
|
})
|
||||||
.is_err() {
|
.is_err()
|
||||||
|
{
|
||||||
error!("Could not insert new songs in database");
|
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);
|
flush_directories(&db, &new_entries);
|
||||||
new_entries.clear();
|
new_entries.clear();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -411,7 +426,7 @@ fn insert_songs(receiver: Receiver<NewSong>, db: DB) {
|
||||||
flush_songs(&db, &new_entries);
|
flush_songs(&db, &new_entries);
|
||||||
new_entries.clear();
|
new_entries.clear();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,9 +173,7 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
// Init index
|
// Init index
|
||||||
info!("Initializing index");
|
info!("Initializing index");
|
||||||
let index = index::builder(db.clone())
|
let index = index::builder(db.clone()).periodic_updates(true).build();
|
||||||
.periodic_updates(true)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// API mount target
|
// API mount target
|
||||||
let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string());
|
let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string());
|
||||||
|
|
|
@ -230,10 +230,7 @@ fn put_preferences(db: State<'_, DB>, auth: Auth, preferences: Json<Preferences>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/trigger_index")]
|
#[post("/trigger_index")]
|
||||||
fn trigger_index(
|
fn trigger_index(index: State<'_, Index>, _admin_rights: AdminRights) -> Result<()> {
|
||||||
index: State<'_, Index>,
|
|
||||||
_admin_rights: AdminRights,
|
|
||||||
) -> Result<()> {
|
|
||||||
index.trigger_reindex();
|
index.trigger_reindex();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,9 +82,7 @@ impl TestService for RocketTestService {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let client = Client::new(server).unwrap();
|
let client = Client::new(server).unwrap();
|
||||||
RocketTestService {
|
RocketTestService { client }
|
||||||
client,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&mut self, url: &str) -> Response<()> {
|
fn get(&mut self, url: &str) -> Response<()> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue