Move mmap to storages from example

This commit is contained in:
Igor Katson 2024-05-01 11:46:10 +01:00
parent 3398babba9
commit bc5098355f
4 changed files with 63 additions and 62 deletions

View file

@ -58,7 +58,6 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"env-filter",
] }
uuid = { version = "1.2", features = ["v4"] }
futures = "0.3"
url = "2"
@ -71,6 +70,7 @@ tokio-util = "0.7.10"
bytes = "1.5.0"
rlimit = "0.10.1"
async-stream = "0.3.5"
memmap2 = "0.9.4"
[dev-dependencies]
futures = { version = "0.3" }
@ -78,4 +78,3 @@ tracing-subscriber = "0.3"
tokio-test = "0.4"
tempfile = "3"
rand = { version = "0.8", features = ["small_rng"] }
memmap2 = "0.9.4"

View file

@ -1,67 +1,8 @@
use std::time::Duration;
use anyhow::Context;
use librqbit::{
storage::{StorageFactory, TorrentStorage},
FileInfos, ManagedTorrentInfo, SessionOptions,
};
use memmap2::{MmapMut, MmapOptions};
use parking_lot::RwLock;
use librqbit::{storage::mmap::MmapStorageFactory, SessionOptions};
use tracing::info;
struct MmapStorageFactory {}
struct MmapStorage {
mmap: RwLock<MmapMut>,
file_infos: FileInfos,
}
impl StorageFactory for MmapStorageFactory {
fn init_storage(
&self,
info: &ManagedTorrentInfo,
) -> anyhow::Result<Box<dyn librqbit::storage::TorrentStorage>> {
Ok(Box::new(MmapStorage {
mmap: RwLock::new(
MmapOptions::new()
.len(info.lengths.total_length().try_into()?)
.map_anon()?,
),
file_infos: info.file_infos.clone(),
}))
}
}
impl TorrentStorage for MmapStorage {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
let start: usize = (self.file_infos[file_id].offset_in_torrent + offset).try_into()?;
let end = start + buf.len();
buf.copy_from_slice(self.mmap.read().get(start..end).context("bad range")?);
Ok(())
}
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()> {
let start: usize = (self.file_infos[file_id].offset_in_torrent + offset).try_into()?;
let end = start + buf.len();
let mut g = self.mmap.write();
let target = g.get_mut(start..end).context("bad range")?;
target.copy_from_slice(buf);
Ok(())
}
fn remove_file(&self, _file_id: usize, _filename: &std::path::Path) -> anyhow::Result<()> {
Ok(())
}
fn ensure_file_length(&self, _file_id: usize, _length: u64) -> anyhow::Result<()> {
Ok(())
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
anyhow::bail!("not implemented")
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Output logs to console.

View file

@ -0,0 +1,60 @@
use anyhow::Context;
use memmap2::{MmapMut, MmapOptions};
use parking_lot::RwLock;
use crate::{FileInfos, ManagedTorrentInfo};
use super::{StorageFactory, TorrentStorage};
pub struct MmapStorageFactory {}
struct MmapStorage {
mmap: RwLock<MmapMut>,
file_infos: FileInfos,
}
impl StorageFactory for MmapStorageFactory {
fn init_storage(
&self,
info: &ManagedTorrentInfo,
) -> anyhow::Result<Box<dyn crate::storage::TorrentStorage>> {
Ok(Box::new(MmapStorage {
mmap: RwLock::new(
MmapOptions::new()
.len(info.lengths.total_length().try_into()?)
.map_anon()?,
),
file_infos: info.file_infos.clone(),
}))
}
}
impl TorrentStorage for MmapStorage {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
let start: usize = (self.file_infos[file_id].offset_in_torrent + offset).try_into()?;
let end = start + buf.len();
buf.copy_from_slice(self.mmap.read().get(start..end).context("bad range")?);
Ok(())
}
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()> {
let start: usize = (self.file_infos[file_id].offset_in_torrent + offset).try_into()?;
let end = start + buf.len();
let mut g = self.mmap.write();
let target = g.get_mut(start..end).context("bad range")?;
target.copy_from_slice(buf);
Ok(())
}
fn remove_file(&self, _file_id: usize, _filename: &std::path::Path) -> anyhow::Result<()> {
Ok(())
}
fn ensure_file_length(&self, _file_id: usize, _length: u64) -> anyhow::Result<()> {
Ok(())
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
anyhow::bail!("not implemented")
}
}

View file

@ -1,5 +1,6 @@
pub mod example;
pub mod filesystem;
pub mod mmap;
use std::{any::Any, path::Path};