From b545dd0f59c61793f0c06785b3cba4fccf079dc8 Mon Sep 17 00:00:00 2001 From: Reknij Date: Wed, 6 Mar 2024 17:00:00 +0800 Subject: [PATCH] fix search top medias only --- server/src/library_system.rs | 54 ++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/server/src/library_system.rs b/server/src/library_system.rs index c3aee46..3c58d0e 100644 --- a/server/src/library_system.rs +++ b/server/src/library_system.rs @@ -5,6 +5,7 @@ use std::{ }; use serde::{Deserialize, Serialize}; +use tokio::sync::RwLock; use tracing::{info, warn}; use walkdir::WalkDir; @@ -70,6 +71,7 @@ pub struct LibrarySystem { covers: HashMap, last_access_config: Arc, + last_search_medias: Arc)>>>, } impl MediaLibrary { @@ -107,7 +109,10 @@ fn get_categories_from_directory(path: &PathBuf, lib: &MediaLibrary) -> Vec(config: Arc, plgsys: &plugin_system::PluginSystem) -> LibrarySystem { + pub async fn new<'a>( + config: Arc, + plgsys: &plugin_system::PluginSystem, + ) -> LibrarySystem { let mut libraries_info = HashMap::with_capacity(config.libraries.len()); let mut albums_info = HashMap::new(); let mut categories_info = HashMap::new(); @@ -184,7 +189,7 @@ impl LibrarySystem { Ok(v) => { categories = v.categories.clone(); ArcMediaInfo::new(v) - }, + } Err(err) => { let c = match err.classify() { serde_json::error::Category::Io => "IO", @@ -192,7 +197,10 @@ impl LibrarySystem { serde_json::error::Category::Data => "Data", serde_json::error::Category::Eof => "EOF", }; - warn!("failed to parse media info json with {} error: {}\n{}", c, err, &json); + warn!( + "failed to parse media info json with {} error: {}\n{}", + c, err, &json + ); ArcMediaInfo::new(info) } } @@ -339,6 +347,7 @@ impl LibrarySystem { years_info, last_access_config: config.clone(), + last_search_medias: Arc::new(RwLock::new(None)), } } @@ -474,24 +483,39 @@ impl LibrarySystem { search: &str, index: usize, limit: usize, - ) -> Vec> { - let mut result = Vec::new(); - + ) -> Vec { + fn get_result(index: usize, limit: usize, result: &Vec) -> Vec { + let max = { + let m = index + limit; + if m > result.len() { + result.len() + } else { + m + } + }; + result[index..max].to_vec() + } let index = index * limit; - let max = { - let max = index + limit; - if max > medias.len() { - medias.len() - } else { - max + { + let last = self.last_search_medias.read().await; + if let Some((last_search, medias)) = last.as_ref() { + if search == last_search { + return get_result(index, limit, medias); + } } - }; + } - for m in &medias[index..max] { + let mut arr = Vec::with_capacity(30); + for m in medias { if m.contains(search) { - result.push(m.clone()); + arr.push(m.clone()); } } + let result = get_result(index, limit, &arr); + self.last_search_medias + .write() + .await + .replace((search.to_owned(), arr)); result }