Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,35 @@ flat-tree = "6"
merkle-tree-stream = "0.12"
pretty-hash = "0.4"
rand = "0.8"
random-access-memory = "3"
random-access-storage = "5"
sha2 = "0.10"
futures = "0.3"
crc32fast = "1"
intmap = "2"
moka = { version = "0.12", optional = true, features = ["sync"] }
async-broadcast = { version = "0.7.1", optional = true }
async-lock = {version = "3.4.0", optional = true }
futures-lite = "2.6.1"

[dependencies.hypercore_schema]
version = "0.2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
random-access-disk = { version = "3", default-features = false }
[dependencies.hypercore-protocol]
optional = true
path = "../protocol/"

[dependencies.hypercore_handshake]
optional = true
path = "../handshake/"

[dependencies.random-access-storage]
path = "../ram/storage"

[dependencies.random-access-memory]
path = "../ram/mem/"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies.random-access-disk]
path = "../ram/disk"
default-features = false

[dev-dependencies]
anyhow = "1.0.70"
Expand All @@ -65,11 +79,10 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter", "fmt"] }

[features]
default = ["tokio", "sparse", "replication", "cache"]
replication = ["dep:async-broadcast"]
replication = ["dep:async-broadcast", "dep:hypercore-protocol", "dep:hypercore_handshake"]
shared-core = ["replication", "dep:async-lock"]
sparse = ["random-access-disk/sparse"]
tokio = ["random-access-disk/tokio"]
async-std = ["random-access-disk/async-std"]
cache = ["moka"]
# Used only in interoperability tests under tests/js-interop which use the javascript version of hypercore
# to verify that this crate works. To run them, use:
Expand Down
19 changes: 0 additions & 19 deletions benches/disk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::time::{Duration, Instant};

#[cfg(feature = "async-std")]
use criterion::async_executor::AsyncStdExecutor;
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use hypercore::{Hypercore, HypercoreBuilder, HypercoreError, Storage};
use tempfile::Builder as TempfileBuilder;
Expand All @@ -10,11 +8,6 @@ fn bench_create_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));

#[cfg(feature = "async-std")]
group.bench_function("create_disk", move |b| {
b.to_async(AsyncStdExecutor)
.iter(|| create_hypercore("create"));
});
#[cfg(feature = "tokio")]
group.bench_function("create_disk", move |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand Down Expand Up @@ -51,10 +44,6 @@ fn bench_write_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));

#[cfg(feature = "async-std")]
group.bench_function("write disk", |b| {
b.to_async(AsyncStdExecutor).iter_custom(write_disk);
});
#[cfg(feature = "tokio")]
group.bench_function("write disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand All @@ -76,10 +65,6 @@ fn bench_read_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));

#[cfg(feature = "async-std")]
group.bench_function("read disk", |b| {
b.to_async(AsyncStdExecutor).iter_custom(read_disk);
});
#[cfg(feature = "tokio")]
group.bench_function("read disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand All @@ -104,10 +89,6 @@ fn bench_clear_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));

#[cfg(feature = "async-std")]
group.bench_function("clear disk", |b| {
b.to_async(AsyncStdExecutor).iter_custom(clear_disk);
});
#[cfg(feature = "tokio")]
group.bench_function("clear disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand Down
24 changes: 3 additions & 21 deletions benches/memory.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use std::time::{Duration, Instant};
use std::{sync::Arc, time::{Duration, Instant}};

#[cfg(feature = "async-std")]
use criterion::async_executor::AsyncStdExecutor;
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use hypercore::{Hypercore, HypercoreBuilder, HypercoreError, Storage};
use random_access_memory::RandomAccessMemory;

fn bench_create_memory(c: &mut Criterion) {
#[cfg(feature = "async-std")]
c.bench_function("create memory", |b| {
b.to_async(AsyncStdExecutor).iter(|| create_hypercore(1024));
});
#[cfg(feature = "tokio")]
c.bench_function("create memory", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand All @@ -25,7 +19,7 @@ async fn create_hypercore(page_size: usize) -> Result<Hypercore, HypercoreError>
let storage = Storage::open(
|_| {
Box::pin(async move {
Ok(Box::new(RandomAccessMemory::new(page_size)) as Box<dyn StorageTraits + Send>)
Ok(Arc::new(RandomAccessMemory::new(page_size)) as Arc<dyn StorageTraits>)
})
},
false,
Expand All @@ -44,7 +38,7 @@ async fn create_hypercore(page_size: usize) -> Result<Hypercore, HypercoreError>
let storage = Storage::open(
|_| {
Box::pin(async move {
Ok(Box::new(RandomAccessMemory::new(page_size)) as Box<dyn StorageTraits + Send>)
Ok(Arc::new(RandomAccessMemory::new(page_size)) as Arc<dyn StorageTraits>)
})
},
false,
Expand All @@ -54,10 +48,6 @@ async fn create_hypercore(page_size: usize) -> Result<Hypercore, HypercoreError>
}

fn bench_write_memory(c: &mut Criterion) {
#[cfg(feature = "async-std")]
c.bench_function("write memory", |b| {
b.to_async(AsyncStdExecutor).iter_custom(write_memory);
});
#[cfg(feature = "tokio")]
c.bench_function("write memory", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand All @@ -76,10 +66,6 @@ async fn write_memory(iters: u64) -> Duration {
}

fn bench_read_memory(c: &mut Criterion) {
#[cfg(feature = "async-std")]
c.bench_function("read memory", |b| {
b.to_async(AsyncStdExecutor).iter_custom(read_memory);
});
#[cfg(feature = "tokio")]
c.bench_function("read memory", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand All @@ -101,10 +87,6 @@ async fn read_memory(iters: u64) -> Duration {
}

fn bench_clear_memory(c: &mut Criterion) {
#[cfg(feature = "async-std")]
c.bench_function("clear memory", |b| {
b.to_async(AsyncStdExecutor).iter_custom(clear_memory);
});
#[cfg(feature = "tokio")]
c.bench_function("clear memory", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
Expand Down
2 changes: 0 additions & 2 deletions examples/disk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "async-std")]
use async_std::main as async_main;
use hypercore::{HypercoreBuilder, HypercoreError, Storage};
use tempfile::Builder;
#[cfg(feature = "tokio")]
Expand Down
2 changes: 0 additions & 2 deletions examples/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "async-std")]
use async_std::main as async_main;
use hypercore::{HypercoreBuilder, HypercoreError, Storage};
#[cfg(feature = "tokio")]
use tokio::main as async_main;
Expand Down
2 changes: 0 additions & 2 deletions examples/replication.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "async-std")]
use async_std::main as async_main;
use hypercore::{Hypercore, HypercoreBuilder, HypercoreError, PartialKeypair, Storage};
use hypercore_schema::{RequestBlock, RequestUpgrade};
use tempfile::Builder;
Expand Down
35 changes: 35 additions & 0 deletions src/common/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use compact_encoding::EncodingError;
use random_access_storage::RandomAccessError;
use thiserror::Error;

use crate::Store;
Expand Down Expand Up @@ -58,6 +59,10 @@ pub enum HypercoreError {
#[source]
source: std::io::Error,
},

#[cfg(feature = "replication")]
#[error("hypercore_protocol Error")]
Protocol(#[from] hypercore_protocol::Error),
}

impl From<std::io::Error> for HypercoreError {
Expand All @@ -76,3 +81,33 @@ impl From<EncodingError> for HypercoreError {
}
}
}

impl From<RandomAccessError> for HypercoreError {
fn from(value: RandomAccessError) -> Self {
map_random_access_err(value)
}
}

pub(crate) fn map_random_access_err(err: RandomAccessError) -> HypercoreError {
match err {
RandomAccessError::IO {
return_code,
context,
source,
} => HypercoreError::IO {
context: Some(format!(
"RandomAccess IO error. Context: {context:?}, return_code: {return_code:?}",
)),
source,
},
RandomAccessError::OutOfBounds {
offset,
end,
length,
} => HypercoreError::InvalidOperation {
context: format!(
"RandomAccess out of bounds. Offset: {offset}, end: {end:?}, length: {length}",
),
},
}
}
Loading
Loading