Skip to content

Commit

Permalink
feat(remove_fully): add option for remove cache entry (#54)
Browse files Browse the repository at this point in the history
Fixes: #53
  • Loading branch information
fiag authored and zkat committed Oct 7, 2023
1 parent 0748e51 commit 34ee679
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,57 @@ async fn bucket_entries_async(bucket: &Path) -> std::io::Result<Vec<Serializable
Ok(vec)
}

/// Builder for options and flags for remove cache entry.
#[derive(Clone, Default)]
pub struct RemoveOpts {
pub(crate) remove_fully: bool,
}

impl RemoveOpts {
/// Creates cache remove options.
pub fn new() -> Self {
Default::default()
}

/// Set the remove fully option
/// If remove_fully is set to true then the index file itself will be physically deleted rather than appending a null.
pub fn remove_fully(mut self, remove_fully: bool) -> Self {
self.remove_fully = remove_fully;
self
}

/// Removes an individual index metadata entry. The associated content will be left in the cache.
pub fn remove_sync<P, K>(self, cache: P, key: K) -> Result<()>
where
P: AsRef<Path>,
K: AsRef<str>,
{
if !self.remove_fully {
delete(cache.as_ref(), key.as_ref())
} else {
let bucket = bucket_path(cache.as_ref(), key.as_ref());
fs::remove_file(&bucket)
.with_context(|| format!("Failed to remove bucket at {bucket:?}"))
}
}

/// Removes an individual index metadata entry. The associated content will be left in the cache.
pub async fn remove<P, K>(self, cache: P, key: K) -> Result<()>
where
P: AsRef<Path>,
K: AsRef<str>,
{
if !self.remove_fully {
delete_async(cache.as_ref(), key.as_ref()).await
} else {
let bucket = bucket_path(cache.as_ref(), key.as_ref());
crate::async_lib::remove_file(&bucket)
.await
.with_context(|| format!("Failed to remove bucket at {bucket:?}"))
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod put;
mod rm;

pub use errors::{Error, Result};
pub use index::Metadata;
pub use index::{Metadata, RemoveOpts};

pub use get::*;
#[cfg(feature = "link_to")]
Expand Down

0 comments on commit 34ee679

Please sign in to comment.