mirror of https://github.com/zkat/cacache-rs.git
feat(api): get::read -> get::data
This should improve the ergonomics a bit by being more symmetrical with put BREAKING CHANGE: The `get` API now uses "data" instead of "read" as the "verb". You will need to change anything that used get::read/hash/etc to use "data" now.
This commit is contained in:
parent
7aabb55de5
commit
b02f41e07f
|
|
@ -1,51 +1,47 @@
|
||||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
||||||
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use cacache;
|
use cacache;
|
||||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||||
use tempfile;
|
use tempfile;
|
||||||
|
|
||||||
fn read_hash(c: &mut Criterion) {
|
fn get_hash(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = b"hello world".to_vec();
|
let data = b"hello world".to_vec();
|
||||||
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
c.bench_function("read_hash", move |b| {
|
c.bench_function("get_hash", move |b| {
|
||||||
b.iter(|| cacache::get::read_hash(black_box(&cache), black_box(&sri)).unwrap())
|
b.iter(|| cacache::get::data_hash(black_box(&cache), black_box(&sri)).unwrap())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(c: &mut Criterion) {
|
fn get(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = b"hello world".to_vec();
|
let data = b"hello world".to_vec();
|
||||||
cacache::put::data(&cache, "hello", data).unwrap();
|
cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
cacache::get::read(&cache, "hello").unwrap();
|
cacache::get::data(&cache, "hello").unwrap();
|
||||||
c.bench_function("read", move |b| {
|
c.bench_function("get", move |b| {
|
||||||
b.iter(|| cacache::get::read(black_box(&cache), black_box(String::from("hello"))).unwrap())
|
b.iter(|| cacache::get::data(black_box(&cache), black_box(String::from("hello"))).unwrap())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_hash_big_data(c: &mut Criterion) {
|
fn get_hash_big_data(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = vec![1; 1024 * 1024 * 5];
|
let data = vec![1; 1024 * 1024 * 5];
|
||||||
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
c.bench_function("read_hash_big_data", move |b| {
|
c.bench_function("get_hash_big_data", move |b| {
|
||||||
b.iter(|| cacache::get::read_hash(black_box(&cache), black_box(&sri)).unwrap())
|
b.iter(|| cacache::get::data_hash(black_box(&cache), black_box(&sri)).unwrap())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn async_read_hash(c: &mut Criterion) {
|
fn async_get_hash(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = b"hello world".to_vec();
|
let data = b"hello world".to_vec();
|
||||||
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
c.bench_function("async_read_hash", move |b| {
|
c.bench_function("async_get_hash", move |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
task::block_on(cacache::async_get::read_hash(
|
task::block_on(cacache::async_get::data_hash(
|
||||||
black_box(&cache),
|
black_box(&cache),
|
||||||
black_box(&sri),
|
black_box(&sri),
|
||||||
))
|
))
|
||||||
|
|
@ -54,14 +50,14 @@ fn async_read_hash(c: &mut Criterion) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn async_read(c: &mut Criterion) {
|
fn async_get(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = b"hello world".to_vec();
|
let data = b"hello world".to_vec();
|
||||||
cacache::put::data(&cache, "hello", data).unwrap();
|
cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
c.bench_function("async_read", move |b| {
|
c.bench_function("async_get", move |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
task::block_on(cacache::async_get::read(
|
task::block_on(cacache::async_get::data(
|
||||||
black_box(&cache),
|
black_box(&cache),
|
||||||
black_box("hello"),
|
black_box("hello"),
|
||||||
))
|
))
|
||||||
|
|
@ -70,14 +66,14 @@ fn async_read(c: &mut Criterion) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn async_read_hash_big_data(c: &mut Criterion) {
|
fn async_get_hash_big_data(c: &mut Criterion) {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cache = tmp.path().to_owned();
|
let cache = tmp.path().to_owned();
|
||||||
let data = vec![1; 1024 * 1024 * 5];
|
let data = vec![1; 1024 * 1024 * 5];
|
||||||
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
let sri = cacache::put::data(&cache, "hello", data).unwrap();
|
||||||
c.bench_function("async_read_hash_big_data", move |b| {
|
c.bench_function("async_get_hash_big_data", move |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
task::block_on(cacache::async_get::read_hash(
|
task::block_on(cacache::async_get::data_hash(
|
||||||
black_box(&cache),
|
black_box(&cache),
|
||||||
black_box(&sri),
|
black_box(&sri),
|
||||||
))
|
))
|
||||||
|
|
@ -88,11 +84,11 @@ fn async_read_hash_big_data(c: &mut Criterion) {
|
||||||
|
|
||||||
criterion_group!(
|
criterion_group!(
|
||||||
benches,
|
benches,
|
||||||
read_hash,
|
get_hash,
|
||||||
read,
|
get,
|
||||||
async_read_hash,
|
async_get_hash,
|
||||||
async_read,
|
async_get,
|
||||||
read_hash_big_data,
|
get_hash_big_data,
|
||||||
async_read_hash_big_data,
|
async_get_hash_big_data,
|
||||||
);
|
);
|
||||||
criterion_main!(benches);
|
criterion_main!(benches);
|
||||||
|
|
|
||||||
|
|
@ -67,13 +67,13 @@ where
|
||||||
|
|
||||||
/// 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
|
||||||
/// data up by key.
|
/// data up by key.
|
||||||
pub async fn read<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
pub async fn data<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
||||||
where
|
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? {
|
if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? {
|
||||||
read_hash(cache, &entry.integrity).await
|
data_hash(cache, &entry.integrity).await
|
||||||
} else {
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
|
|
@ -81,8 +81,7 @@ where
|
||||||
|
|
||||||
/// 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
|
||||||
/// data up by its content address.
|
/// data up by its content address.
|
||||||
#[allow(clippy::needless_lifetimes)]
|
pub async fn data_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
||||||
pub async fn read_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
|
|
@ -104,7 +103,6 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copies a cache entry by integrity address to a specified location.
|
/// Copies a cache entry by integrity address to a specified location.
|
||||||
#[allow(clippy::needless_lifetimes)]
|
|
||||||
pub async fn copy_hash<P, Q>(cache: P, sri: &Integrity, to: Q) -> Result<u64, Error>
|
pub async fn copy_hash<P, Q>(cache: P, sri: &Integrity, to: Q) -> Result<u64, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ mod tests {
|
||||||
task::block_on(async {
|
task::block_on(async {
|
||||||
data(&dir, "hello", b"hello").await.unwrap();
|
data(&dir, "hello", b"hello").await.unwrap();
|
||||||
});
|
});
|
||||||
let data = task::block_on(async { async_get::read(&dir, "hello").await.unwrap() });
|
let data = task::block_on(async { async_get::data(&dir, "hello").await.unwrap() });
|
||||||
assert_eq!(data, b"hello");
|
assert_eq!(data, b"hello");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,13 +57,13 @@ where
|
||||||
|
|
||||||
/// 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
|
||||||
/// data up by key.
|
/// data up by key.
|
||||||
pub fn read<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
pub fn data<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
||||||
where
|
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())? {
|
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||||
read_hash(cache, &entry.integrity)
|
data_hash(cache, &entry.integrity)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
|
|
@ -71,7 +71,7 @@ where
|
||||||
|
|
||||||
/// 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
|
||||||
/// data up by its content address.
|
/// data up by its content address.
|
||||||
pub fn read_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
pub fn data_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -160,14 +160,13 @@ impl Put {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::get;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn round_trip() {
|
fn round_trip() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let dir = tmp.path().to_owned();
|
let dir = tmp.path().to_owned();
|
||||||
data(&dir, "hello", b"hello").unwrap();
|
data(&dir, "hello", b"hello").unwrap();
|
||||||
let data = get::read(&dir, "hello").unwrap();
|
let data = crate::get::data(&dir, "hello").unwrap();
|
||||||
assert_eq!(data, b"hello");
|
assert_eq!(data, b"hello");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue