Write through cache impl
This commit is contained in:
parent
427f490a61
commit
54b17d5ee1
6 changed files with 203 additions and 34 deletions
|
|
@ -11,7 +11,7 @@ use std::{
|
|||
use anyhow::Context;
|
||||
use dashmap::DashMap;
|
||||
|
||||
use librqbit_core::lengths::{Lengths, ValidPieceIndex};
|
||||
use librqbit_core::lengths::{CurrentPiece, Lengths, ValidPieceIndex};
|
||||
use tokio::io::{AsyncRead, AsyncSeek};
|
||||
use tracing::{debug, trace};
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ struct StreamState {
|
|||
|
||||
impl StreamState {
|
||||
fn current_piece(&self, lengths: &Lengths) -> Option<CurrentPiece> {
|
||||
compute_current_piece(lengths, self.position, self.file_abs_offset)
|
||||
lengths.compute_current_piece(self.position, self.file_abs_offset)
|
||||
}
|
||||
|
||||
fn queue<'a>(&self, lengths: &'a Lengths) -> impl Iterator<Item = ValidPieceIndex> + 'a {
|
||||
|
|
@ -53,32 +53,6 @@ pub(crate) struct TorrentStreams {
|
|||
streams: DashMap<StreamId, StreamState>,
|
||||
}
|
||||
|
||||
struct CurrentPiece {
|
||||
id: ValidPieceIndex,
|
||||
piece_remaining: u32,
|
||||
}
|
||||
|
||||
fn compute_current_piece(
|
||||
lengths: &Lengths,
|
||||
file_pos: u64,
|
||||
file_torrent_abs_offset: u64,
|
||||
) -> Option<CurrentPiece> {
|
||||
let dpl = lengths.default_piece_length();
|
||||
|
||||
let abs_pos = file_torrent_abs_offset + file_pos;
|
||||
let piece_id = abs_pos / dpl as u64;
|
||||
let piece_id: u32 = piece_id.try_into().ok()?;
|
||||
|
||||
let piece_id = lengths.validate_piece_index(piece_id)?;
|
||||
let piece_len = lengths.piece_length(piece_id);
|
||||
Some(CurrentPiece {
|
||||
id: piece_id,
|
||||
piece_remaining: (piece_len as u64 - (abs_pos % dpl as u64))
|
||||
.try_into()
|
||||
.ok()?,
|
||||
})
|
||||
}
|
||||
|
||||
impl TorrentStreams {
|
||||
fn next_id(&self) -> usize {
|
||||
self.next_stream_id.fetch_add(1, Ordering::Relaxed)
|
||||
|
|
@ -199,12 +173,12 @@ impl AsyncRead for FileStream {
|
|||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
|
||||
let current = poll_try_io!(compute_current_piece(
|
||||
&self.torrent.info().lengths,
|
||||
self.position,
|
||||
self.file_torrent_abs_offset
|
||||
)
|
||||
.context("invalid position"));
|
||||
let current = poll_try_io!(self
|
||||
.torrent
|
||||
.info()
|
||||
.lengths
|
||||
.compute_current_piece(self.position, self.file_torrent_abs_offset)
|
||||
.context("invalid position"));
|
||||
|
||||
// if the piece is not there, register to wake when it is
|
||||
// check if we have the piece for real
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue