Medlar/crates/core/src/part.rs
2026-11-25 01:18:06 -06:00

122 lines
3.6 KiB
Rust

//! A range of a file while it is being downloaded.
use crate::model::Part;
use std::sync::{Arc, Mutex};
/// Shared by the task that fills it, the journal and whoever draws it.
#[derive(Debug)]
pub struct LivePart(Mutex<Inner>);
#[derive(Debug)]
struct Inner {
part: Part,
/// Bytes now on their way to the disk. `part.done` counts only those that arrived.
claimed: u64,
/// Bytes in memory, the claimed ones too. They may reach beyond the end.
buffered: u64,
/// Bytes that came over the network, overlaps not counted. Never goes down.
received: u64,
}
impl LivePart {
pub fn new(part: Part) -> Arc<LivePart> {
Arc::new(LivePart(Mutex::new(Inner { part, claimed: 0, buffered: 0, received: 0 })))
}
pub fn get(&self) -> Part {
self.0.lock().unwrap().part
}
pub fn finished(&self) -> bool {
self.remaining() == Some(0)
}
/// Bytes still to write. None when the end is unknown.
pub fn remaining(&self) -> Option<u64> {
let p = self.get();
p.end.map(|end| end - p.start - p.done)
}
/// The next byte the network has to bring.
pub fn pos(&self) -> u64 {
let i = self.0.lock().unwrap();
let pos = i.part.start + i.part.done + i.buffered;
i.part.end.map_or(pos, |end| pos.min(end))
}
pub fn received(&self) -> u64 {
self.0.lock().unwrap().received
}
/// Moves the end. Refused when it would cut bytes that are here already.
pub fn set_end(&self, end: u64) -> bool {
let mut i = self.0.lock().unwrap();
let ok = end >= i.part.start + i.part.done + i.claimed.max(i.buffered);
if ok {
i.part.end = Some(end);
}
ok
}
/// A connection starts with nothing in memory.
pub(crate) fn begin(&self) {
self.0.lock().unwrap().buffered = 0;
}
/// New bytes are in memory.
pub(crate) fn arrive(&self, n: u64) {
let mut i = self.0.lock().unwrap();
i.buffered += n;
i.received += n;
}
/// Reserves the next bytes for writing: where they go, and how many of `n` still belong to this part. One writer at a time.
pub(crate) fn claim(&self, n: u64) -> (u64, u64) {
let mut i = self.0.lock().unwrap();
let at = i.part.start + i.part.done;
i.claimed = i.part.end.map_or(n, |end| n.min(end - at));
(at, i.claimed)
}
/// The claimed bytes are written (or not).
pub(crate) fn settle(&self, written: bool) {
let mut i = self.0.lock().unwrap();
if written {
i.part.done += i.claimed;
}
i.claimed = 0;
i.buffered = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_end_never_cuts_written_bytes() {
let part = LivePart::new(Part { start: 100, end: Some(200), done: 10 });
assert_eq!(part.claim(30), (110, 30));
assert!(!part.set_end(139));
assert!(part.set_end(140));
part.settle(true);
assert!(part.finished());
assert!(part.set_end(150));
assert_eq!(part.claim(30), (140, 10));
part.settle(false);
assert_eq!(part.remaining(), Some(10));
}
#[test]
fn bytes_in_memory_are_kept_too() {
let part = LivePart::new(Part { start: 100, end: Some(200), done: 10 });
part.arrive(30);
assert_eq!((part.pos(), part.received()), (140, 30));
assert!(!part.set_end(139));
assert!(part.set_end(150));
part.arrive(30);
assert_eq!(part.pos(), 150);
assert_eq!(part.claim(60), (110, 40));
part.settle(true);
assert_eq!((part.pos(), part.received(), part.finished()), (150, 60, true));
}
}