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)] #[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(())
} }

View file

@ -1,100 +1,100 @@
table! { table! {
ddns_config (id) { ddns_config (id) {
id -> Integer, id -> Integer,
host -> Text, host -> Text,
username -> Text, username -> Text,
password -> Text, password -> Text,
} }
} }
table! { table! {
directories (id) { directories (id) {
id -> Integer, id -> Integer,
path -> Text, path -> Text,
parent -> Nullable<Text>, parent -> Nullable<Text>,
artist -> Nullable<Text>, artist -> Nullable<Text>,
year -> Nullable<Integer>, year -> Nullable<Integer>,
album -> Nullable<Text>, album -> Nullable<Text>,
artwork -> Nullable<Text>, artwork -> Nullable<Text>,
date_added -> Integer, date_added -> Integer,
} }
} }
table! { table! {
misc_settings (id) { misc_settings (id) {
id -> Integer, id -> Integer,
auth_secret -> Binary, auth_secret -> Binary,
index_sleep_duration_seconds -> Integer, index_sleep_duration_seconds -> Integer,
index_album_art_pattern -> Text, index_album_art_pattern -> Text,
prefix_url -> Text, prefix_url -> Text,
} }
} }
table! { table! {
mount_points (id) { mount_points (id) {
id -> Integer, id -> Integer,
source -> Text, source -> Text,
name -> Text, name -> Text,
} }
} }
table! { table! {
playlist_songs (id) { playlist_songs (id) {
id -> Integer, id -> Integer,
playlist -> Integer, playlist -> Integer,
path -> Text, path -> Text,
ordering -> Integer, ordering -> Integer,
} }
} }
table! { table! {
playlists (id) { playlists (id) {
id -> Integer, id -> Integer,
owner -> Integer, owner -> Integer,
name -> Text, name -> Text,
} }
} }
table! { table! {
songs (id) { songs (id) {
id -> Integer, id -> Integer,
path -> Text, path -> Text,
parent -> Text, parent -> Text,
track_number -> Nullable<Integer>, track_number -> Nullable<Integer>,
disc_number -> Nullable<Integer>, disc_number -> Nullable<Integer>,
title -> Nullable<Text>, title -> Nullable<Text>,
artist -> Nullable<Text>, artist -> Nullable<Text>,
album_artist -> Nullable<Text>, album_artist -> Nullable<Text>,
year -> Nullable<Integer>, year -> Nullable<Integer>,
album -> Nullable<Text>, album -> Nullable<Text>,
artwork -> Nullable<Text>, artwork -> Nullable<Text>,
duration -> Nullable<Integer>, duration -> Nullable<Integer>,
} }
} }
table! { table! {
users (id) { users (id) {
id -> Integer, id -> Integer,
name -> Text, name -> Text,
password_hash -> Text, password_hash -> Text,
admin -> Integer, admin -> Integer,
lastfm_username -> Nullable<Text>, lastfm_username -> Nullable<Text>,
lastfm_session_key -> Nullable<Text>, lastfm_session_key -> Nullable<Text>,
web_theme_base -> Nullable<Text>, web_theme_base -> Nullable<Text>,
web_theme_accent -> Nullable<Text>, web_theme_accent -> Nullable<Text>,
} }
} }
joinable!(playlist_songs -> playlists (playlist)); joinable!(playlist_songs -> playlists (playlist));
joinable!(playlists -> users (owner)); joinable!(playlists -> users (owner));
allow_tables_to_appear_in_same_query!( allow_tables_to_appear_in_same_query!(
ddns_config, ddns_config,
directories, directories,
misc_settings, misc_settings,
mount_points, mount_points,
playlist_songs, playlist_songs,
playlists, playlists,
songs, songs,
users, users,
); );

View file

@ -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 {

View file

@ -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::*;

View file

@ -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
@ -129,10 +133,10 @@ impl IndexUpdater {
#[cfg(feature = "profile-index")] #[cfg(feature = "profile-index")]
let _guard = flame::start_guard("created_date"); let _guard = flame::start_guard("created_date");
metadata metadata
.created() .created()
.or_else(|_| metadata.modified())? .or_else(|_| metadata.modified())?
.duration_since(time::UNIX_EPOCH)? .duration_since(time::UNIX_EPOCH)?
.as_secs() as i32 .as_secs() as i32
}; };
let mut directory_album = None; let mut directory_album = None;
@ -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;
@ -311,7 +316,7 @@ pub fn populate(db: &DB) -> Result<()> {
let vfs = db.get_vfs()?; let vfs = db.get_vfs()?;
let mount_points = vfs.get_mount_points(); let mount_points = vfs.get_mount_points();
let album_art_pattern = { let album_art_pattern = {
let connection = db.connect()?; let connection = db.connect()?;
let settings: MiscSettings = misc_settings::table.get_result(&connection)?; let settings: MiscSettings = misc_settings::table.get_result(&connection)?;
Regex::new(&settings.index_album_art_pattern)? Regex::new(&settings.index_album_art_pattern)?
@ -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
.and_then(|connection|{ .connect()
diesel::insert_into(directories::table) .and_then(|connection| {
.values(entries) diesel::insert_into(directories::table)
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 .values(entries)
.map_err(Error::new) .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"); 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
.and_then(|connection|{ .connect()
diesel::insert_into(songs::table) .and_then(|connection| {
.values(entries) diesel::insert_into(songs::table)
.execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 .values(entries)
.map_err(Error::new) .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"); 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,
} }
} }

View file

@ -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());

View file

@ -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(())
} }

View file

@ -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<()> {