diff options
| author | Fuwn <[email protected]> | 2026-01-12 05:56:09 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-12 05:56:09 -0800 |
| commit | cb6df8c2749d8d3b88342398f3e5c13457206a2f (patch) | |
| tree | a56230a03f370f6b263db469d5e74e5786e3a7cb | |
| parent | feat(directory): Sort routes by hits (diff) | |
| download | locus-cb6df8c2749d8d3b88342398f3e5c13457206a2f.tar.xz locus-cb6df8c2749d8d3b88342398f3e5c13457206a2f.zip | |
fix: Various optimisations
| -rw-r--r-- | src/modules/blog/module.rs | 104 | ||||
| -rw-r--r-- | src/modules/directory.rs | 26 | ||||
| -rw-r--r-- | src/modules/index.rs | 4 | ||||
| -rw-r--r-- | src/modules/router/ticker.rs | 26 | ||||
| -rw-r--r-- | src/modules/search.rs | 63 | ||||
| -rw-r--r-- | src/response.rs | 2 |
6 files changed, 105 insertions, 120 deletions
diff --git a/src/modules/blog/module.rs b/src/modules/blog/module.rs index f68c2bc..8ffff90 100644 --- a/src/modules/blog/module.rs +++ b/src/modules/blog/module.rs @@ -34,23 +34,31 @@ pub fn module(router: &mut windmark::router::Router) { fs::File::open(&file).unwrap().read_to_string(&mut contents).unwrap(); - entries.insert( - file.replace(&blog, "").replace(".gmi", "").replace( - { - #[cfg(windows)] - { - '\\' - } + let mut entry_key = + file.strip_prefix(&blog).unwrap_or(&file).to_string(); + + if std::path::Path::new(&entry_key) + .extension() + .map_or(false, |extension| extension.eq_ignore_ascii_case("gmi")) + { + entry_key.truncate(entry_key.len() - 4); + } - #[cfg(unix)] - { - '/' - } - }, - "", - ), - contents, - ); + if entry_key.starts_with({ + #[cfg(windows)] + { + '\\' + } + + #[cfg(unix)] + { + '/' + } + }) { + entry_key.remove(0); + } + + entries.insert(entry_key, contents); }, ); @@ -111,31 +119,26 @@ pub fn module(router: &mut windmark::router::Router) { blog_clone.len(), blog_clone .iter() - .map(|(title, _, entries)| (title.clone(), entries.clone())) - .collect::<Vec<(_, _)>>() - .into_iter() - .map(|(title, entries)| { + .map(|(title, _, entries)| { let config: Option<Blog> = entries .get("blog.json") .and_then(|content| Blog::from_string(content).ok()); let name = config - .clone() - .unwrap_or_default() - .name() - .clone() + .as_ref() + .and_then(|c| c.name().as_ref()) + .cloned() .unwrap_or_else(|| title.clone()); - let description = config.unwrap_or_default().description().clone(); - // .unwrap_or_else(|| "One of Fuwn's blogs".to_string()); + let description = + config.as_ref().and_then(|c| c.description().as_ref()).cloned(); format!( "=> {} {}{}", format_args!("/blog/{}", name.replace(' ', "_").to_lowercase(),), name, - if description.is_some() { - format!(" ― {}", description.unwrap()) - } else { - String::new() - } + description + .map_or_else(String::new, |description_reference| format!( + " ― {description_reference}" + )) ) }) .collect::<Vec<_>>() @@ -154,21 +157,20 @@ pub fn module(router: &mut windmark::router::Router) { .and_then(|(_, content)| Blog::from_string(&content).ok()); let entries_clone = entries.clone(); let name = config - .clone() - .unwrap_or_default() - .name() - .clone() + .as_ref() + .and_then(|c| c.name().as_ref()) + .cloned() .unwrap_or_else(|| blog.clone()); - let description = config.clone().unwrap_or_default().description().clone(); - // .unwrap_or_else(|| "One of Fuwn's blogs".to_string()); + let description = + config.as_ref().and_then(|c| c.description().as_ref()).cloned(); let config_clone = config.clone(); let mut xml = XmlWriter::builder(); xml.add_field("title", &name); xml.add_field("link", &format!("{ROOT_GEMINI_URL}/blog/{fixed_blog_name}")); - if description.is_some() { - xml.add_field("description", &description.clone().unwrap()); + if let Some(ref description_reference) = description { + xml.add_field("description", description_reference); } xml.add_field("generator", "locus"); @@ -180,11 +182,11 @@ pub fn module(router: &mut windmark::router::Router) { &format!("/blog/{fixed_blog_name}"), &format!( "{name}{}", - if description.clone().is_some() { - format!(" ― {}", description.clone().unwrap()) - } else { - String::new() - } + description + .as_ref() + .map_or_else(String::new, |description_reference| format!( + " ― {description_reference}" + )) ), move |context| { let fixed_blog_name = fixed_blog_name_clone.clone(); @@ -195,19 +197,15 @@ pub fn module(router: &mut windmark::router::Router) { {0}'s RSS feed\n\n=> {} here!", blog, entries_clone.len(), - description.clone().unwrap_or_default(), + description.as_deref().unwrap_or(""), entries_clone .keys() - .map(Clone::clone) - .collect::<Vec<_>>() - .into_iter() .map(|title| { let postish = config_clone - .clone() - .unwrap_or_default() - .posts() - .clone() - .and_then(|posts| posts.get(&title).cloned()) + .as_ref() + .and_then(|c| c.posts().as_ref()) + .and_then(|posts| posts.get(title)) + .cloned() .unwrap_or_default(); format!( diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 4179175..23be14c 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -11,22 +11,22 @@ pub fn module(router: &mut windmark::router::Router) { "# Directory\n\nA map of all publicly available routes on this \ Gemini capsule\n\n{}", { - let mut lines = (*crate::route::ROUTES.lock().unwrap()) + let available_routes = crate::route::ROUTES.lock().unwrap(); + let mut routes_with_hits: Vec<_> = available_routes .iter() - .map(|(r, d)| format!("=> {} {}", r, d.description)) - .collect::<Vec<_>>(); - let standard_transform = |route: &str| { - route.replace("=> ", "").split(' ').collect::<Vec<_>>()[0] - .to_string() - }; + .map(|(r, d)| { + let hits = hits_from(r); + (hits, format!("=> {} {}", r, d.description)) + }) + .collect(); - lines.sort_by(|a, b| { - hits_from(&standard_transform(a)) - .cmp(&hits_from(&standard_transform(b))) - }); - lines.reverse(); + routes_with_hits.sort_by(|a, b| b.0.cmp(&a.0)); - lines.join("\n") + routes_with_hits + .into_iter() + .map(|(_, line)| line) + .collect::<Vec<_>>() + .join("\n") } ), &context, diff --git a/src/modules/index.rs b/src/modules/index.rs index cd45150..0f7749a 100644 --- a/src/modules/index.rs +++ b/src/modules/index.rs @@ -41,10 +41,8 @@ Don't know where to start? Check out The Directory or test your luck! posts .iter() - .map(|post| format!("=> {} {}", post.link(), post.title())) - .collect::<Vec<String>>() - .into_iter() .take(3) + .map(|post| format!("=> {} {}", post.link(), post.title())) .collect::<Vec<String>>() .join("\n") }, diff --git a/src/modules/router/ticker.rs b/src/modules/router/ticker.rs index 38b7499..5b44d41 100644 --- a/src/modules/router/ticker.rs +++ b/src/modules/router/ticker.rs @@ -1,26 +1,10 @@ use crate::DATABASE; pub fn module(context: &windmark::context::HookContext) { - let url_path = if context.url.path().is_empty() { - "/" - } else { - context.url.path() - }; + let url_path = + if context.url.path().is_empty() { "/" } else { context.url.path() }; + let mut database = DATABASE.lock().unwrap(); + let current_hits = database.get::<i32>(url_path).unwrap_or(0); - let previous_database = (*DATABASE.lock().unwrap()).get::<i32>(url_path); - - match previous_database { - None => { - (*DATABASE.lock().unwrap()) - .set::<i32>(url_path, &0) - .unwrap(); - } - Some(_) => {} - } - - let new_database = (*DATABASE.lock().unwrap()).get::<i32>(url_path); - - (*DATABASE.lock().unwrap()) - .set(url_path, &(new_database.unwrap() + 1)) - .unwrap(); + database.set(url_path, &(current_hits + 1)).unwrap(); } diff --git a/src/modules/search.rs b/src/modules/search.rs index bafafed..80c5c6f 100644 --- a/src/modules/search.rs +++ b/src/modules/search.rs @@ -56,31 +56,32 @@ pub(super) fn module(router: &mut windmark::router::Router) { } { - let path = (*SCHEMA.lock().unwrap()).get_field("path").unwrap(); - let description = - (*SCHEMA.lock().unwrap()).get_field("description").unwrap(); - let content = (*SCHEMA.lock().unwrap()).get_field("content").unwrap(); + let schema = (*SCHEMA.lock().unwrap()).clone(); + let path = schema.get_field("path").unwrap(); + let description = schema.get_field("description").unwrap(); + let content = schema.get_field("content").unwrap(); let mut results = String::new(); - - let searcher = (*INDEX.lock().unwrap()) + let index = INDEX.lock().unwrap(); + let searcher = index .reader_builder() .reload_policy(tantivy::ReloadPolicy::OnCommit) .try_into() .unwrap() .searcher(); - let top_docs = searcher + let top_documents = searcher .search( - &tantivy::query::QueryParser::for_index( - &INDEX.lock().unwrap(), - vec![path, description, content], - ) + &tantivy::query::QueryParser::for_index(&index, vec![ + path, + description, + content, + ]) .parse_query(&query.0) .unwrap(), &tantivy::collector::TopDocs::with_limit(SEARCH_SIZE), ) .unwrap(); - for (_score, document_address) in top_docs { + for (_score, document_address) in top_documents { let retrieved_document = searcher.doc(document_address).unwrap(); macro_rules! text { @@ -149,29 +150,33 @@ pub fn index() { info!("spawned search indexer"); loop { - let path = (*SCHEMA.lock().unwrap()).get_field("path").unwrap(); - let description = - (*SCHEMA.lock().unwrap()).get_field("description").unwrap(); - let content = (*SCHEMA.lock().unwrap()).get_field("content").unwrap(); + let schema = (*SCHEMA.lock().unwrap()).clone(); + let path = schema.get_field("path").unwrap(); + let description = schema.get_field("description").unwrap(); + let content = schema.get_field("content").unwrap(); let time = tokio::time::Instant::now(); let mut new = 0; - for (route_path, information) in &(*crate::route::ROUTES.lock().unwrap()) { - // Pretty inefficient, but I'll figure this out later. - (*INDEX_WRITER.lock().unwrap()).delete_all_documents().unwrap(); + { + let routes = crate::route::ROUTES.lock().unwrap(); + let mut index_writer = INDEX_WRITER.lock().unwrap(); - (*INDEX_WRITER.lock().unwrap()) - .add_document(tantivy::doc!( - path => route_path.clone(), - description => information.description.clone(), - content => information.text_cache.clone() - )) - .unwrap(); + index_writer.delete_all_documents().unwrap(); - new += 1; - } + for (route_path, information) in routes.iter() { + index_writer + .add_document(tantivy::doc!( + path => route_path.clone(), + description => information.description.clone(), + content => information.text_cache.clone() + )) + .unwrap(); - (*INDEX_WRITER.lock().unwrap()).commit().unwrap(); + new += 1; + } + + index_writer.commit().unwrap(); + } info!( "commit {} new items into search index in {}ms", diff --git a/src/response.rs b/src/response.rs index b07a9ad..9c1cc69 100644 --- a/src/response.rs +++ b/src/response.rs @@ -27,7 +27,7 @@ pub fn success( quote: { use rand::prelude::SliceRandom; - "ES.choose(&mut rand::thread_rng()).unwrap().to_string() + QUOTES.choose(&mut rand::thread_rng()).unwrap() }, commit: &format!("/tree/{}", env!("VERGEN_GIT_SHA")), mini_commit: env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), |