diff --git a/Cargo.lock b/Cargo.lock index 5d904dc..1be4c3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,7 +418,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "sensorlog" -version = "1.0.0" +version = "1.1.0" dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 5419837..b3a78df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sensorlog" description = "A lightweight data logging service" repository = "https://github.com/nyantec.sensorlog" -version = "1.0.0" +version = "1.1.0" authors = ["The sensorlog Authors "] license = "MirOS" diff --git a/src/lib.rs b/src/lib.rs index ca523d9..caf5af9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,7 +116,7 @@ impl Sensorlog { Ok(measurements) } - pub fn set_storage_quota_for(&mut self, sensor_id: &str, quota: ::quota::StorageQuota) { + pub fn set_storage_quota_for(&mut self, sensor_id: &str, quota: ::quota::StorageQuota) -> Result<(), Error> { let logfile_id = LogfileID::from_string(sensor_id.to_string()); self.logfile_map.set_storage_quota_for(&logfile_id, quota) } diff --git a/src/logfile.rs b/src/logfile.rs index 4a05a9d..253af27 100644 --- a/src/logfile.rs +++ b/src/logfile.rs @@ -150,8 +150,13 @@ impl Logfile { storage_locked.allocate(measurement_size)?; // insert the new measurement into the head partition - match storage_locked.partitions.last_mut() { - Some(p) => p.append_measurement(measurement)?, + match &mut storage_locked.partitions.last_mut() { + Some(p) => { + p.append_measurement(measurement)?; + let part_name = p.get_file_name(); + // Make sure that the currently used partition is not in self.deleted_partition + storage_locked.partitions_deleted.retain(|x| x.get_file_name() != part_name); + }, None => return Err(err_server!("corrupt partition map")), }; @@ -173,6 +178,21 @@ impl Logfile { let reader = LogfileReader::new(&storage_locked.partitions); reader.fetch_measurements(time_start, time_limit, limit) } + + pub fn set_storage_quota(&self, quota: StorageQuota) -> Result<(), ::Error> { + if quota.is_zero() { + return Err(err_quota!("insufficient quota")); + } + + // lock the storage + let mut storage_locked = match self.storage.write() { + Ok(l) => l, + Err(_) => fatal!("lock is poisoned"), + }; + storage_locked.storage_quota = quota; + + Ok(()) + } } impl LogfileStorage { diff --git a/src/logfile_map.rs b/src/logfile_map.rs index 41c529a..25ef0ce 100644 --- a/src/logfile_map.rs +++ b/src/logfile_map.rs @@ -86,13 +86,17 @@ impl LogfileMap { Ok(logfile) } - pub fn set_storage_quota_for(&mut self, logfile_id: &LogfileID, quota: ::quota::StorageQuota) { - self.config.set_storage_quota_for(&logfile_id, quota); - // grab write lock - let mut logfiles_locked = match self.logfiles.write() { + pub fn set_storage_quota_for(&mut self, logfile_id: &LogfileID, quota: ::quota::StorageQuota) -> Result<(), ::Error> { + self.config.set_storage_quota_for(&logfile_id, quota.clone()); + + let logfiles_locked = match self.logfiles.read() { Ok(l) => l, Err(_) => fatal!("lock is poisoned"), }; - logfiles_locked.remove(&logfile_id.get_string()); + if let Some(logfile) = logfiles_locked.get(&logfile_id.get_string()) { + logfile.set_storage_quota(quota)?; + } + + Ok(()) } } diff --git a/src/quota.rs b/src/quota.rs index 446d9c2..051330d 100644 --- a/src/quota.rs +++ b/src/quota.rs @@ -19,7 +19,7 @@ * of said person’s immediate fault when using the work as intended. */ -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub enum StorageQuota { Unlimited, Limited { limit_bytes: u64 },