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

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

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

@ -39,4 +39,4 @@ pub struct Directory {
pub enum CollectionFile {
Directory(Directory),
Song(Song),
}
}

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