Improve cached version size by halving it again

feature/lookup-installed
trivernis 1 year ago
parent 2eb67854a6
commit 643c5999f0
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

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

@ -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<PathBuf> {
async fn download_version(&self, version: &SimpleVersion) -> Result<PathBuf> {
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<Versions> {
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))

@ -12,6 +12,9 @@ use miette::{Context, IntoDiagnostic, Result};
pub struct Versions {
lts_versions: HashMap<String, u16>,
versions: HashMap<SimpleVersion, SimpleVersionInfo>,
// as this field is not serialized
// it needs to be calculated after serialization
#[serde(skip)]
sorted_versions: Vec<SimpleVersion>,
}
@ -51,14 +54,14 @@ impl Display for SimpleVersion {
#[derive(Clone, Serialize, Deserialize)]
pub struct SimpleVersionInfo {
pub version: Version,
pub version: SimpleVersion,
pub lts: Option<String>,
}
impl From<VersionInfo> 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::<Versions>(&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::<Vec<_>>();
self.sorted_versions.sort();
}
}

Loading…
Cancel
Save