Make all experimental storages optional

This commit is contained in:
Igor Katson 2024-05-02 22:08:00 +01:00
parent 0a1d389bc7
commit 0b1499aa10
7 changed files with 81 additions and 14 deletions

View file

@ -1,3 +1,7 @@
/*
A storage middleware that slows down the underlying storage.
*/
use std::time::Duration;
use rand_distr::Distribution;

View file

@ -1,3 +1,7 @@
/*
A storage middleware that logs the time underlying storage operations took.
*/
use crate::storage::{StorageFactory, StorageFactoryExt, TorrentStorage};
#[derive(Clone)]

View file

@ -1,3 +1,10 @@
/*
A storage middleware that caches pieces in memory, so that subsequent reads (for checksumming) are
free.
An example, untested and unproven to be useful.
*/
use std::num::NonZeroUsize;
use anyhow::Context;

View file

@ -1,5 +1,9 @@
pub mod examples;
pub mod filesystem;
#[cfg(feature = "storage_examples")]
pub mod examples;
#[cfg(feature = "storage_middleware")]
pub mod middleware;
use std::{
@ -65,14 +69,22 @@ impl<U: StorageFactory + ?Sized> StorageFactory for Box<U> {
}
pub trait TorrentStorage: Send + Sync {
/// Given a file_id (which you can get more info from in init_storage() through torrent info)
/// read buf.len() bytes into buf at offset.
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()>;
/// Given a file_id (which you can get more info from in init_storage() through torrent info)
/// write buf.len() bytes into the file at offset.
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()>;
/// Remove a file from the storage. If not supported, or it doesn't matter, just return Ok(())
fn remove_file(&self, file_id: usize, filename: &Path) -> anyhow::Result<()>;
/// E.g. for filesystem backend ensure that the file has a certain length, and grow/shrink as needed.
fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()>;
/// Replace the current storage with a dummy, and return a new one that should be used instead.
/// This is used to make the underlying object useless when e.g. pausing the torrent.
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;
}

View file

@ -4,11 +4,7 @@ use anyhow::Context;
use tokio::{io::AsyncReadExt, time::timeout};
use tracing::info;
use crate::{
create_torrent,
storage::{examples::inmemory::InMemoryExampleStorageFactory, StorageFactoryExt},
AddTorrent, CreateTorrentOptions, Session,
};
use crate::{create_torrent, AddTorrent, CreateTorrentOptions, Session};
use super::test_util::create_default_random_dir_with_torrents;
@ -86,7 +82,6 @@ async fn e2e_stream() -> anyhow::Result<()> {
AddTorrent::from_bytes(torrent.as_bytes()?),
Some(crate::AddTorrentOptions {
paused: false,
storage_factory: Some(InMemoryExampleStorageFactory::default().boxed()),
initial_peers: Some(vec![peer]),
..Default::default()
}),