intermodal/src/opt/torrent/piece_length.rs
Casey Rodarmor 5a1de1acd2
Select piece length when none is provided
When no piece length is provided to imdl torrent create, a piece
length is selected based on the size of the input. The hueristic is
lifted directly from libtorrent.

Also adds a imdl torrent piece-length command, which prints a table
of the piece lengths chosen at different content sizes, which is useful
for understanding and debugging the piece length selection algorithm.

type: added
2020-04-07 19:01:03 -07:00

68 lines
1.6 KiB
Rust

use crate::common::*;
#[derive(StructOpt)]
#[structopt(
help_message(consts::HELP_MESSAGE),
version_message(consts::VERSION_MESSAGE),
about("Display information about automatic piece length selection.")
)]
pub(crate) struct PieceLength {}
#[allow(clippy::unused_self)]
impl PieceLength {
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
let mut rows: Vec<(String, String, String, String)> = Vec::new();
rows.push((
"Content".into(),
"Piece Length".into(),
"Count".into(),
"Metainfo Size".into(),
));
for i in 14..51 {
let content_size = Bytes::from(1u128 << i);
let piece_length = PieceLengthPicker::from_content_size(content_size);
let metainfo_size = PieceLengthPicker::metainfo_size(content_size, piece_length);
let piece_count = PieceLengthPicker::piece_count(content_size, piece_length);
rows.push((
content_size.to_string(),
piece_length.to_string(),
piece_count.to_string(),
metainfo_size.to_string(),
));
}
let mut w = (0, 0, 0, 0);
for (c0, c1, c2, c3) in &rows {
w = (
w.0.max(c0.len()),
w.1.max(c1.len()),
w.2.max(c2.len()),
w.3.max(c3.len()),
);
}
for (content_size, piece_length, metainfo_size, piece_count) in rows {
outln!(
env,
"{:w0$} -> {:w1$} x {:w2$} = {:w3$}",
content_size,
piece_length,
metainfo_size,
piece_count,
w0 = w.0,
w1 = w.1,
w2 = w.2,
w3 = w.3,
);
}
Ok(())
}
}