Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remove_fully) add option for remove cache entry #54

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,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 @@ -167,7 +167,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