Better remove history files (for example, after expiry)

This commit is contained in:
timvisee 2018-04-22 16:20:36 +02:00
parent b984999120
commit 943f309e98
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2

View file

@ -109,12 +109,6 @@ impl History {
Ok(()) Ok(())
} }
/// Add the given remote file to the history.
pub fn add(&mut self, file: RemoteFile) {
self.files.push(file);
self.changed = true;
}
/// Load the history from the given path, add the given file, and save it /// Load the history from the given path, add the given file, and save it
/// again. /// again.
/// If there is not history file at the given path, a new empty one will /// If there is not history file at the given path, a new empty one will
@ -125,6 +119,35 @@ impl History {
history.save().map_err(|err| err.into()) history.save().map_err(|err| err.into())
} }
/// Add the given remote file to the history.
pub fn add(&mut self, file: RemoteFile) {
self.files.push(file);
self.changed = true;
}
/// Remove the given remote file, matched by it's file ID.
///
/// If any file was removed, true is returned.
pub fn remove(&mut self, file: &RemoteFile) -> bool {
// Get the indices of files that have expired
let expired_indices: Vec<usize> = self.files.iter()
.enumerate()
.filter(|(_, f)| f.id() == file.id())
.map(|(i, _)| i)
.collect();
// Remove these specific files
for i in expired_indices.iter().rev() {
self.files.remove(*i);
}
// Set the changed flag, and return
if expired_indices.is_empty() {
self.changed = true;
}
!expired_indices.is_empty()
}
/// Get all files. /// Get all files.
pub fn files(&self) -> &Vec<RemoteFile> { pub fn files(&self) -> &Vec<RemoteFile> {
&self.files &self.files
@ -137,25 +160,25 @@ impl History {
/// ///
/// The number of exired files is returned. /// The number of exired files is returned.
pub fn gc(&mut self) -> usize { pub fn gc(&mut self) -> usize {
// Get the indices of files that have expired // Get a list of expired files
let expired_indices: Vec<usize> = self.files.iter() let expired: Vec<RemoteFile> = self.files
.enumerate() .iter()
.filter(|(_, f)| f.has_expired(false)) .filter(|f| f.has_expired(false))
.map(|(i, _)| i) .cloned()
.collect(); .collect();
// Remove these specific files // Remove the files
for i in &expired_indices { for f in &expired {
self.files.remove(*i); self.remove(f);
} }
// Set the changed flag // Set the changed flag
if !expired_indices.is_empty() { if !expired.is_empty() {
self.changed = true; self.changed = true;
} }
// Return the number of expired files // Return the number of expired files
expired_indices.len() expired.len()
} }
} }