mirror of https://github.com/zkat/cacache-rs.git
added write_hash function
This commit is contained in:
parent
d748889d9f
commit
d41bb21a69
|
|
@ -181,9 +181,8 @@ fn write_hash_async(c: &mut Criterion) {
|
||||||
b.iter_custom(|iters| {
|
b.iter_custom(|iters| {
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
for i in 0..iters {
|
for i in 0..iters {
|
||||||
task::block_on(cacache::write(
|
task::block_on(cacache::write_hash(
|
||||||
&cache,
|
&cache,
|
||||||
format!("hello{}", i),
|
|
||||||
format!("hello world{}", i),
|
format!("hello world{}", i),
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
||||||
61
src/put.rs
61
src/put.rs
|
|
@ -43,10 +43,40 @@ where
|
||||||
writer.commit().await
|
writer.commit().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Writes `data` to the `cache`, skipping associating an index key with it.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```no_run
|
||||||
|
/// use async_attributes;
|
||||||
|
///
|
||||||
|
/// #[async_attributes::main]
|
||||||
|
/// async fn main() -> cacache::Result<()> {
|
||||||
|
/// cacache::write_hash("./my-cache", b"hello").await?;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub async fn write_hash<P, D>(cache: P, data: D) -> Result<Integrity>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
D: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
let mut writer = WriteOpts::new()
|
||||||
|
.algorithm(Algorithm::Sha256)
|
||||||
|
.open_hash(cache.as_ref())
|
||||||
|
.await?;
|
||||||
|
writer.write_all(data.as_ref()).await.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Failed to write to cache data for cache at {:?}",
|
||||||
|
cache.as_ref()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
writer.commit().await
|
||||||
|
}
|
||||||
|
|
||||||
/// A reference to an open file writing to the cache.
|
/// A reference to an open file writing to the cache.
|
||||||
pub struct Writer {
|
pub struct Writer {
|
||||||
cache: PathBuf,
|
cache: PathBuf,
|
||||||
key: String,
|
key: Option<String>,
|
||||||
written: usize,
|
written: usize,
|
||||||
pub(crate) writer: write::AsyncWriter,
|
pub(crate) writer: write::AsyncWriter,
|
||||||
opts: WriteOpts,
|
opts: WriteOpts,
|
||||||
|
|
@ -103,7 +133,6 @@ impl Writer {
|
||||||
/// Must be called manually in order to complete the writing process,
|
/// Must be called manually in order to complete the writing process,
|
||||||
/// otherwise everything will be thrown out.
|
/// otherwise everything will be thrown out.
|
||||||
pub async fn commit(mut self) -> Result<Integrity> {
|
pub async fn commit(mut self) -> Result<Integrity> {
|
||||||
let key = self.key;
|
|
||||||
let cache = self.cache;
|
let cache = self.cache;
|
||||||
let writer_sri = self.writer.close().await?;
|
let writer_sri = self.writer.close().await?;
|
||||||
if let Some(sri) = &self.opts.sri {
|
if let Some(sri) = &self.opts.sri {
|
||||||
|
|
@ -111,14 +140,18 @@ impl Writer {
|
||||||
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri))?;
|
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri))?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.opts.sri = Some(writer_sri);
|
self.opts.sri = Some(writer_sri.clone());
|
||||||
}
|
}
|
||||||
if let Some(size) = self.opts.size {
|
if let Some(size) = self.opts.size {
|
||||||
if size != self.written {
|
if size != self.written {
|
||||||
return Err(Error::SizeError(size, self.written));
|
return Err(Error::SizeError(size, self.written));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index::insert_async(&cache, &key, self.opts).await
|
if let Some(key) = self.key {
|
||||||
|
index::insert_async(&cache, &key, self.opts).await
|
||||||
|
} else {
|
||||||
|
Ok(writer_sri)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,7 +207,25 @@ impl WriteOpts {
|
||||||
{
|
{
|
||||||
Ok(Writer {
|
Ok(Writer {
|
||||||
cache: cache.as_ref().to_path_buf(),
|
cache: cache.as_ref().to_path_buf(),
|
||||||
key: String::from(key.as_ref()),
|
key: Some(String::from(key.as_ref())),
|
||||||
|
written: 0,
|
||||||
|
writer: write::AsyncWriter::new(
|
||||||
|
cache.as_ref(),
|
||||||
|
*self.algorithm.as_ref().unwrap_or(&Algorithm::Sha256),
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
opts: self,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens the file handle for writing, without a key returning an Writer instance.
|
||||||
|
pub async fn open_hash<P>(self, cache: P) -> Result<Writer>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
Ok(Writer {
|
||||||
|
cache: cache.as_ref().to_path_buf(),
|
||||||
|
key: None,
|
||||||
written: 0,
|
written: 0,
|
||||||
writer: write::AsyncWriter::new(
|
writer: write::AsyncWriter::new(
|
||||||
cache.as_ref(),
|
cache.as_ref(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue