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