mirror of https://github.com/zkat/cacache-rs.git
fix(perf): do the inner fn trick to reduce generic compilation load
This commit is contained in:
parent
2767a6a671
commit
da259ae432
84
src/get.rs
84
src/get.rs
|
|
@ -93,14 +93,14 @@ impl Reader {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? {
|
||||
Reader::open_hash(cache, entry.integrity).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
async fn inner(cache: &Path, key: &str) -> Result<Reader> {
|
||||
if let Some(entry) = index::find_async(cache, key).await? {
|
||||
Reader::open_hash(cache, entry.integrity).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref()).await
|
||||
}
|
||||
|
||||
/// Opens a new file handle into the cache, based on its integrity address.
|
||||
|
|
@ -150,14 +150,14 @@ where
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? {
|
||||
read_hash(cache, &entry.integrity).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
async fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> {
|
||||
if let Some(entry) = index::find_async(cache, key).await? {
|
||||
read_hash(cache, &entry.integrity).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref()).await
|
||||
}
|
||||
|
||||
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
||||
|
|
@ -202,14 +202,14 @@ where
|
|||
K: AsRef<str>,
|
||||
Q: AsRef<Path>,
|
||||
{
|
||||
if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? {
|
||||
copy_hash(cache, &entry.integrity, to).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
async fn inner(cache: &Path, key: &str, to: &Path) -> Result<u64> {
|
||||
if let Some(entry) = index::find_async(cache, key).await? {
|
||||
copy_hash(cache, &entry.integrity, to).await
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref(), to.as_ref()).await
|
||||
}
|
||||
|
||||
/// Copies a cache data by hash to a specified location. Returns the number of
|
||||
|
|
@ -315,14 +315,14 @@ impl SyncReader {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||
SyncReader::open_hash(cache, entry.integrity)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
fn inner(cache: &Path, key: &str) -> Result<SyncReader> {
|
||||
if let Some(entry) = index::find(cache, key)? {
|
||||
SyncReader::open_hash(cache, entry.integrity)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref())
|
||||
}
|
||||
|
||||
/// Opens a new synchronous file handle into the cache, based on its integrity address.
|
||||
|
|
@ -368,14 +368,14 @@ where
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||
read_hash_sync(cache, &entry.integrity)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> {
|
||||
if let Some(entry) = index::find(cache, key)? {
|
||||
read_hash_sync(cache, &entry.integrity)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref())
|
||||
}
|
||||
|
||||
/// Reads the entire contents of a cache file synchronously into a bytes
|
||||
|
|
@ -416,14 +416,14 @@ where
|
|||
K: AsRef<str>,
|
||||
Q: AsRef<Path>,
|
||||
{
|
||||
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||
copy_hash_sync(cache, &entry.integrity, to)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(
|
||||
cache.as_ref().to_path_buf(),
|
||||
key.as_ref().into(),
|
||||
));
|
||||
fn inner(cache: &Path, key: &str, to: &Path) -> Result<u64> {
|
||||
if let Some(entry) = index::find(cache, key)? {
|
||||
copy_hash_sync(cache, &entry.integrity, to)
|
||||
} else {
|
||||
return Err(Error::EntryNotFound(cache.to_path_buf(), key.into()));
|
||||
}
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref(), to.as_ref())
|
||||
}
|
||||
|
||||
/// Copies a cache entry by integrity address to a specified location. Returns
|
||||
|
|
|
|||
223
src/put.rs
223
src/put.rs
|
|
@ -31,19 +31,21 @@ where
|
|||
D: AsRef<[u8]>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.as_ref().len())
|
||||
.open(cache.as_ref(), key.as_ref())
|
||||
.await?;
|
||||
writer.write_all(data.as_ref()).await.with_context(|| {
|
||||
format!(
|
||||
"Failed to write to cache data for key {} for cache at {:?}",
|
||||
key.as_ref(),
|
||||
cache.as_ref()
|
||||
)
|
||||
})?;
|
||||
writer.commit().await
|
||||
async fn inner(cache: &Path, key: &str, data: &[u8]) -> Result<Integrity> {
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.len())
|
||||
.open(cache, key)
|
||||
.await?;
|
||||
writer.write_all(data).await.with_context(|| {
|
||||
format!(
|
||||
"Failed to write to cache data for key {} for cache at {:?}",
|
||||
key, cache
|
||||
)
|
||||
})?;
|
||||
writer.commit().await
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref(), data.as_ref()).await
|
||||
}
|
||||
|
||||
/// Writes `data` to the `cache`, skipping associating an index key with it.
|
||||
|
|
@ -63,18 +65,19 @@ where
|
|||
P: AsRef<Path>,
|
||||
D: AsRef<[u8]>,
|
||||
{
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.as_ref().len())
|
||||
.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
|
||||
async fn inner(cache: &Path, data: &[u8]) -> Result<Integrity> {
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.len())
|
||||
.open_hash(cache)
|
||||
.await?;
|
||||
writer
|
||||
.write_all(data)
|
||||
.await
|
||||
.with_context(|| format!("Failed to write to cache data for cache at {:?}", cache))?;
|
||||
writer.commit().await
|
||||
}
|
||||
inner(cache.as_ref(), data.as_ref()).await
|
||||
}
|
||||
|
||||
/// A reference to an open file writing to the cache.
|
||||
|
|
@ -137,10 +140,13 @@ impl Writer {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.open(cache.as_ref(), key.as_ref())
|
||||
.await
|
||||
async fn inner(cache: &Path, key: &str) -> Result<Writer> {
|
||||
WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.open(cache, key)
|
||||
.await
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref()).await
|
||||
}
|
||||
|
||||
/// Closes the Writer handle and writes content and index entries. Also
|
||||
|
|
@ -187,16 +193,18 @@ where
|
|||
D: AsRef<[u8]>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
let mut writer = SyncWriter::create(cache.as_ref(), key.as_ref())?;
|
||||
writer.write_all(data.as_ref()).with_context(|| {
|
||||
format!(
|
||||
"Failed to write to cache data for key {} for cache at {:?}",
|
||||
key.as_ref(),
|
||||
cache.as_ref()
|
||||
)
|
||||
})?;
|
||||
writer.written = data.as_ref().len();
|
||||
writer.commit()
|
||||
fn inner(cache: &Path, key: &str, data: &[u8]) -> Result<Integrity> {
|
||||
let mut writer = SyncWriter::create(cache, key)?;
|
||||
writer.write_all(data).with_context(|| {
|
||||
format!(
|
||||
"Failed to write to cache data for key {} for cache at {:?}",
|
||||
key, cache
|
||||
)
|
||||
})?;
|
||||
writer.written = data.as_ref().len();
|
||||
writer.commit()
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref(), data.as_ref())
|
||||
}
|
||||
|
||||
/// Writes `data` to the `cache` synchronously, skipping associating a key with it.
|
||||
|
|
@ -215,18 +223,18 @@ where
|
|||
P: AsRef<Path>,
|
||||
D: AsRef<[u8]>,
|
||||
{
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.as_ref().len())
|
||||
.open_hash_sync(cache.as_ref())?;
|
||||
writer.write_all(data.as_ref()).with_context(|| {
|
||||
format!(
|
||||
"Failed to write to cache data for cache at {:?}",
|
||||
cache.as_ref()
|
||||
)
|
||||
})?;
|
||||
writer.written = data.as_ref().len();
|
||||
writer.commit()
|
||||
fn inner(cache: &Path, data: &[u8]) -> Result<Integrity> {
|
||||
let mut writer = WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.size(data.len())
|
||||
.open_hash_sync(cache)?;
|
||||
writer
|
||||
.write_all(data)
|
||||
.with_context(|| format!("Failed to write to cache data for cache at {:?}", cache))?;
|
||||
writer.written = data.len();
|
||||
writer.commit()
|
||||
}
|
||||
inner(cache.as_ref(), data.as_ref())
|
||||
}
|
||||
|
||||
/// Builder for options and flags for opening a new cache file to write data into.
|
||||
|
|
@ -251,18 +259,21 @@ impl WriteOpts {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
Ok(Writer {
|
||||
cache: cache.as_ref().to_path_buf(),
|
||||
key: Some(String::from(key.as_ref())),
|
||||
written: 0,
|
||||
writer: write::AsyncWriter::new(
|
||||
cache.as_ref(),
|
||||
*self.algorithm.as_ref().unwrap_or(&Algorithm::Sha256),
|
||||
None,
|
||||
)
|
||||
.await?,
|
||||
opts: self,
|
||||
})
|
||||
async fn inner(me: WriteOpts, cache: &Path, key: &str) -> Result<Writer> {
|
||||
Ok(Writer {
|
||||
cache: cache.to_path_buf(),
|
||||
key: Some(String::from(key)),
|
||||
written: 0,
|
||||
writer: write::AsyncWriter::new(
|
||||
cache.as_ref(),
|
||||
me.algorithm.unwrap_or(Algorithm::Sha256),
|
||||
None,
|
||||
)
|
||||
.await?,
|
||||
opts: me,
|
||||
})
|
||||
}
|
||||
inner(self, cache.as_ref(), key.as_ref()).await
|
||||
}
|
||||
|
||||
/// Opens the file handle for writing, without a key returning an Writer instance.
|
||||
|
|
@ -270,18 +281,21 @@ impl WriteOpts {
|
|||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
Ok(Writer {
|
||||
cache: cache.as_ref().to_path_buf(),
|
||||
key: None,
|
||||
written: 0,
|
||||
writer: write::AsyncWriter::new(
|
||||
cache.as_ref(),
|
||||
*self.algorithm.as_ref().unwrap_or(&Algorithm::Sha256),
|
||||
self.size,
|
||||
)
|
||||
.await?,
|
||||
opts: self,
|
||||
})
|
||||
async fn inner(me: WriteOpts, cache: &Path) -> Result<Writer> {
|
||||
Ok(Writer {
|
||||
cache: cache.to_path_buf(),
|
||||
key: None,
|
||||
written: 0,
|
||||
writer: write::AsyncWriter::new(
|
||||
cache,
|
||||
me.algorithm.unwrap_or(Algorithm::Sha256),
|
||||
me.size,
|
||||
)
|
||||
.await?,
|
||||
opts: me,
|
||||
})
|
||||
}
|
||||
inner(self, cache.as_ref()).await
|
||||
}
|
||||
|
||||
/// Opens the file handle for writing synchronously, returning a SyncWriter instance.
|
||||
|
|
@ -290,17 +304,20 @@ impl WriteOpts {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
Ok(SyncWriter {
|
||||
cache: cache.as_ref().to_path_buf(),
|
||||
key: Some(String::from(key.as_ref())),
|
||||
written: 0,
|
||||
writer: write::Writer::new(
|
||||
cache.as_ref(),
|
||||
*self.algorithm.as_ref().unwrap_or(&Algorithm::Sha256),
|
||||
self.size,
|
||||
)?,
|
||||
opts: self,
|
||||
})
|
||||
fn inner(me: WriteOpts, cache: &Path, key: &str) -> Result<SyncWriter> {
|
||||
Ok(SyncWriter {
|
||||
cache: cache.to_path_buf(),
|
||||
key: Some(String::from(key)),
|
||||
written: 0,
|
||||
writer: write::Writer::new(
|
||||
cache.as_ref(),
|
||||
me.algorithm.unwrap_or(Algorithm::Sha256),
|
||||
me.size,
|
||||
)?,
|
||||
opts: me,
|
||||
})
|
||||
}
|
||||
inner(self, cache.as_ref(), key.as_ref())
|
||||
}
|
||||
|
||||
/// Opens the file handle for writing, without a key returning an SyncWriter instance.
|
||||
|
|
@ -308,17 +325,20 @@ impl WriteOpts {
|
|||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
Ok(SyncWriter {
|
||||
cache: cache.as_ref().to_path_buf(),
|
||||
key: None,
|
||||
written: 0,
|
||||
writer: write::Writer::new(
|
||||
cache.as_ref(),
|
||||
*self.algorithm.as_ref().unwrap_or(&Algorithm::Sha256),
|
||||
self.size,
|
||||
)?,
|
||||
opts: self,
|
||||
})
|
||||
fn inner(me: WriteOpts, cache: &Path) -> Result<SyncWriter> {
|
||||
Ok(SyncWriter {
|
||||
cache: cache.to_path_buf(),
|
||||
key: None,
|
||||
written: 0,
|
||||
writer: write::Writer::new(
|
||||
cache,
|
||||
me.algorithm.unwrap_or(Algorithm::Sha256),
|
||||
me.size,
|
||||
)?,
|
||||
opts: me,
|
||||
})
|
||||
}
|
||||
inner(self, cache.as_ref())
|
||||
}
|
||||
|
||||
/// Configures the algorithm to write data under.
|
||||
|
|
@ -397,9 +417,12 @@ impl SyncWriter {
|
|||
P: AsRef<Path>,
|
||||
K: AsRef<str>,
|
||||
{
|
||||
WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.open_sync(cache.as_ref(), key.as_ref())
|
||||
fn inner(cache: &Path, key: &str) -> Result<SyncWriter> {
|
||||
WriteOpts::new()
|
||||
.algorithm(Algorithm::Sha256)
|
||||
.open_sync(cache, key)
|
||||
}
|
||||
inner(cache.as_ref(), key.as_ref())
|
||||
}
|
||||
|
||||
/// Closes the Writer handle and writes content and index entries. Also
|
||||
|
|
|
|||
22
src/rm.rs
22
src/rm.rs
|
|
@ -90,12 +90,15 @@ pub async fn remove_hash<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()
|
|||
/// }
|
||||
/// ```
|
||||
pub async fn clear<P: AsRef<Path>>(cache: P) -> Result<()> {
|
||||
for entry in (cache.as_ref().read_dir().to_internal()?).flatten() {
|
||||
crate::async_lib::remove_dir_all(entry.path())
|
||||
.await
|
||||
.to_internal()?;
|
||||
async fn inner(cache: &Path) -> Result<()> {
|
||||
for entry in cache.read_dir().to_internal()?.flatten() {
|
||||
crate::async_lib::remove_dir_all(entry.path())
|
||||
.await
|
||||
.to_internal()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
inner(cache.as_ref()).await
|
||||
}
|
||||
|
||||
/// Removes an individual index entry synchronously. The associated content
|
||||
|
|
@ -174,10 +177,13 @@ pub fn remove_hash_sync<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()>
|
|||
/// }
|
||||
/// ```
|
||||
pub fn clear_sync<P: AsRef<Path>>(cache: P) -> Result<()> {
|
||||
for entry in (cache.as_ref().read_dir().to_internal()?).flatten() {
|
||||
fs::remove_dir_all(entry.path()).to_internal()?;
|
||||
fn inner(cache: &Path) -> Result<()> {
|
||||
for entry in cache.read_dir().to_internal()?.flatten() {
|
||||
fs::remove_dir_all(entry.path()).to_internal()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
inner(cache.as_ref())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue