From 400f081487b64f48bb1a391fe96374c4278d31ce Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 13 Oct 2021 10:24:46 +0200 Subject: [PATCH] proxmox-rrd: split out load_rrd (cleanup) Signed-off-by: Dietmar Maurer Signed-off-by: Thomas Lamprecht --- proxmox-rrd/src/cache.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/proxmox-rrd/src/cache.rs b/proxmox-rrd/src/cache.rs index 5e359d9b..5225bd49 100644 --- a/proxmox-rrd/src/cache.rs +++ b/proxmox-rrd/src/cache.rs @@ -194,15 +194,8 @@ impl RRDCache { path.push(&entry.rel_path); create_path(path.parent().unwrap(), Some(self.dir_options.clone()), Some(self.dir_options.clone()))?; - let mut rrd = match RRD::load(&path) { - Ok(rrd) => rrd, - Err(err) => { - if err.kind() != std::io::ErrorKind::NotFound { - log::warn!("overwriting RRD file {:?}, because of load error: {}", path, err); - } - Self::create_default_rrd(entry.dst) - }, - }; + let mut rrd = Self::load_rrd(&path, entry.dst); + if entry.time > get_last_update(&entry.rel_path, &rrd) { rrd.update(entry.time, entry.value); } @@ -235,7 +228,19 @@ impl RRDCache { Ok(()) } - /// Update data in RAM and write file back to disk (if `save` is set) + fn load_rrd(path: &Path, dst: DST) -> RRD { + match RRD::load(path) { + Ok(rrd) => rrd, + Err(err) => { + if err.kind() != std::io::ErrorKind::NotFound { + log::warn!("overwriting RRD file {:?}, because of load error: {}", path, err); + } + Self::create_default_rrd(dst) + }, + } + } + + /// Update data in RAM and write file back to disk (journal) pub fn update_value( &self, rel_path: &str, @@ -261,15 +266,9 @@ impl RRDCache { let mut path = self.basedir.clone(); path.push(rel_path); create_path(path.parent().unwrap(), Some(self.dir_options.clone()), Some(self.dir_options.clone()))?; - let mut rrd = match RRD::load(&path) { - Ok(rrd) => rrd, - Err(err) => { - if err.kind() != std::io::ErrorKind::NotFound { - log::warn!("overwriting RRD file {:?}, because of load error: {}", path, err); - } - Self::create_default_rrd(dst) - }, - }; + + let mut rrd = Self::load_rrd(&path, dst); + rrd.update(now, value); state.rrd_map.insert(rel_path.into(), rrd); }