From 643c5999f0c863ede02681a2bf20845820b51a9c Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 23 Jan 2023 21:11:22 +0100 Subject: [PATCH] Improve cached version size by halving it again --- src/nenv.rs | 3 ++- src/repository/mod.rs | 8 ++++---- src/repository/versions.rs | 24 ++++++++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/nenv.rs b/src/nenv.rs index 640508f..77319cc 100644 --- a/src/nenv.rs +++ b/src/nenv.rs @@ -110,6 +110,7 @@ impl Nenv { pub async fn list_versions(&self) -> Result<()> { let versions = self.repo.installed_versions().await?; let active_version = self.repo.lookup_version(&self.active_version)?; + let active_version = active_version.version.into(); println!("{}", "Installed versions:".bold()); @@ -125,7 +126,7 @@ impl Nenv { .map(|l| format!(" ({})", l.to_owned().green())) .unwrap_or_default(); - if version == active_version.version { + if version == active_version { println!(" {}{} [current]", version.to_string().blue().bold(), lts) } else { println!(" {}{}", version.to_string().blue(), lts) diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 2e28cb4..acc8bb4 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -24,7 +24,7 @@ use miette::{IntoDiagnostic, Result}; use self::{ node_path::NodePath, - versions::{SimpleVersionInfo, Versions}, + versions::{SimpleVersion, SimpleVersionInfo, Versions}, }; pub(crate) mod extract; @@ -195,7 +195,7 @@ impl Repository { } #[tracing::instrument(level = "debug", skip(self))] - async fn download_version(&self, version: &Version) -> Result { + async fn download_version(&self, version: &SimpleVersion) -> Result { let download_path = CACHE_DIR.join(format!("node-v{}{}", version, *NODE_ARCHIVE_SUFFIX)); if download_path.exists() { @@ -211,7 +211,7 @@ impl Repository { } #[tracing::instrument(level = "debug", skip(self))] - fn extract_archive(&self, version: &Version, archive_path: &Path) -> Result<()> { + fn extract_archive(&self, version: &SimpleVersion, archive_path: &Path) -> Result<()> { let dst_path = NODE_VERSIONS_DIR.join(version.to_string()); extract::extract_file(archive_path, &dst_path)?; @@ -233,7 +233,7 @@ async fn load_versions(web_api: &WebApi) -> Result { Ok(versions) } -fn build_version_path(version: &Version) -> PathBuf { +fn build_version_path(version: &SimpleVersion) -> PathBuf { NODE_VERSIONS_DIR .join(version.to_string()) .join(format!("node-v{}-{}-{}", version, OS, ARCH)) diff --git a/src/repository/versions.rs b/src/repository/versions.rs index a5b4fbd..d4b5abf 100644 --- a/src/repository/versions.rs +++ b/src/repository/versions.rs @@ -12,6 +12,9 @@ use miette::{Context, IntoDiagnostic, Result}; pub struct Versions { lts_versions: HashMap, versions: HashMap, + // as this field is not serialized + // it needs to be calculated after serialization + #[serde(skip)] sorted_versions: Vec, } @@ -51,14 +54,14 @@ impl Display for SimpleVersion { #[derive(Clone, Serialize, Deserialize)] pub struct SimpleVersionInfo { - pub version: Version, + pub version: SimpleVersion, pub lts: Option, } impl From for SimpleVersionInfo { fn from(value: VersionInfo) -> Self { Self { - version: value.version, + version: value.version.into(), lts: value.lts.lts(), } } @@ -72,8 +75,14 @@ impl Versions { } let byte_contents = fs::read(&*VERSION_FILE_PATH).await.ok()?; - match bincode::deserialize(&byte_contents) { - Ok(versions) => Some(versions), + match bincode::deserialize::(&byte_contents) { + Ok(mut versions) => { + // create the list of sorted versions + // this is faster when done directly rather than + // storing it + versions.create_sorted_versions(); + Some(versions) + } Err(e) => { tracing::error!("Failed to deserialize cache {e}"); fs::remove_file(&*VERSION_FILE_PATH).await.ok()?; @@ -177,4 +186,11 @@ impl Versions { let version = fulfilling_versions.last()?; self.versions.get(&version).into() } + + /// Creates the list of sorted versions + /// It needs to be calculated once after creating the struct + fn create_sorted_versions(&mut self) { + self.sorted_versions = self.versions.keys().cloned().collect::>(); + self.sorted_versions.sort(); + } }