Skip to content

Commit

Permalink
feat(api): get::read -> get::data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zkat committed Oct 17, 2019
1 parent 7aabb55 commit b02f41e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 40 deletions.
54 changes: 25 additions & 29 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -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 cacache;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use tempfile;

fn read_hash(c: &mut Criterion) {
fn get_hash(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
let data = b"hello world".to_vec();
let sri = cacache::put::data(&cache, "hello", data).unwrap();
c.bench_function("read_hash", move |b| {
b.iter(|| cacache::get::read_hash(black_box(&cache), black_box(&sri)).unwrap())
c.bench_function("get_hash", move |b| {
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 cache = tmp.path().to_owned();
let data = b"hello world".to_vec();
cacache::put::data(&cache, "hello", data).unwrap();
cacache::get::read(&cache, "hello").unwrap();
c.bench_function("read", move |b| {
b.iter(|| cacache::get::read(black_box(&cache), black_box(String::from("hello"))).unwrap())
cacache::get::data(&cache, "hello").unwrap();
c.bench_function("get", move |b| {
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 cache = tmp.path().to_owned();
let data = vec![1; 1024 * 1024 * 5];
let sri = cacache::put::data(&cache, "hello", data).unwrap();
c.bench_function("read_hash_big_data", move |b| {
b.iter(|| cacache::get::read_hash(black_box(&cache), black_box(&sri)).unwrap())
c.bench_function("get_hash_big_data", move |b| {
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 cache = tmp.path().to_owned();
let data = b"hello world".to_vec();
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(|| {
task::block_on(cacache::async_get::read_hash(
task::block_on(cacache::async_get::data_hash(
black_box(&cache),
black_box(&sri),
))
Expand All @@ -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 cache = tmp.path().to_owned();
let data = b"hello world".to_vec();
cacache::put::data(&cache, "hello", data).unwrap();
c.bench_function("async_read", move |b| {
c.bench_function("async_get", move |b| {
b.iter(|| {
task::block_on(cacache::async_get::read(
task::block_on(cacache::async_get::data(
black_box(&cache),
black_box("hello"),
))
Expand All @@ -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 cache = tmp.path().to_owned();
let data = vec![1; 1024 * 1024 * 5];
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(|| {
task::block_on(cacache::async_get::read_hash(
task::block_on(cacache::async_get::data_hash(
black_box(&cache),
black_box(&sri),
))
Expand All @@ -88,11 +84,11 @@ fn async_read_hash_big_data(c: &mut Criterion) {

criterion_group!(
benches,
read_hash,
read,
async_read_hash,
async_read,
read_hash_big_data,
async_read_hash_big_data,
get_hash,
get,
async_get_hash,
async_get,
get_hash_big_data,
async_get_hash_big_data,
);
criterion_main!(benches);
8 changes: 3 additions & 5 deletions src/async_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,21 @@ where

/// Reads the entire contents of a cache file into a bytes vector, looking the
/// 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
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
data_hash(cache, &entry.integrity).await
} else {
Err(Error::NotFound)
}
}

/// Reads the entire contents of a cache file into a bytes vector, looking the
/// data up by its content address.
#[allow(clippy::needless_lifetimes)]
pub async fn read_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
pub async fn data_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
where
P: AsRef<Path>,
{
Expand All @@ -104,7 +103,6 @@ where
}

/// 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>
where
P: AsRef<Path>,
Expand Down
2 changes: 1 addition & 1 deletion src/async_put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod tests {
task::block_on(async {
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");
}
}
6 changes: 3 additions & 3 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ where

/// Reads the entire contents of a cache file into a bytes vector, looking the
/// 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
P: AsRef<Path>,
K: AsRef<str>,
{
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
read_hash(cache, &entry.integrity)
data_hash(cache, &entry.integrity)
} else {
Err(Error::NotFound)
}
}

/// Reads the entire contents of a cache file into a bytes vector, looking the
/// 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
P: AsRef<Path>,
{
Expand Down
3 changes: 1 addition & 2 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,13 @@ impl Put {
#[cfg(test)]
mod tests {
use super::*;
use crate::get;

#[test]
fn round_trip() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
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");
}
}

0 comments on commit b02f41e

Please sign in to comment.