pub struct BuildOptions { /* private fields */ }Expand description
How a tree is built from disk.
Implementations§
Source§impl BuildOptions
impl BuildOptions
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/speed.rs (line 95)
43fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let megabytes: usize = match std::env::args().nth(1) {
45 Some(argument) => argument.parse()?,
46 None => 1024,
47 };
48 let data = filler(megabytes << 20);
49
50 let started = Instant::now();
51 let chunks = Chunking::FastCdcV1.cut(&data);
52 report("chunked", started, data.len());
53 println!("{:<22} {} chunks", "", chunks.len());
54
55 let started = Instant::now();
56 let mut mixed = 0_u8;
57 for chunk in &chunks {
58 mixed ^= Hash::of(chunk).as_bytes()[0];
59 }
60 report("hashed", started, data.len());
61
62 let store = MemoryStore::new();
63 let started = Instant::now();
64 let mut builder = Builder::new(&store, Chunking::FastCdcV1);
65 let file = builder.file_from_bytes(&data)?;
66 report("built", started, data.len());
67
68 let root = builder.finish(file, ExtraData::new())?;
69 let started = Instant::now();
70 verify(&store, &root)?;
71 report("checked", started, data.len());
72 println!("root {root} ({mixed:02x})");
73
74 // The tree is fetched from the first store into a second one, as a
75 // receiver would take it from a peer, and then written as a bundle and
76 // read back into a third. Each checks every byte as it comes.
77 let fetched = MemoryStore::new();
78 let started = Instant::now();
79 fetch(&fetched, &root, &mut Responder::new(&store))?;
80 report("fetched", started, data.len());
81
82 let imported = MemoryStore::new();
83 let mut bundle = Vec::with_capacity(data.len() + (1 << 20));
84 let started = Instant::now();
85 export(&store, &root, &mut bundle)?;
86 import(&imported, &root, &mut bundle.as_slice())?;
87 report("bundled", started, data.len());
88
89 if let Some(folder) = std::env::args().nth(2) {
90 let mut best = f64::MAX;
91 let (mut bytes, mut files) = (0, 0);
92 for _ in 0..5 {
93 let store = MemoryStore::new();
94 let started = Instant::now();
95 let root = build(&store, &folder, &BuildOptions::new())?;
96 best = best.min(started.elapsed().as_secs_f64());
97 bytes = record_of(&store, &root)?.top().size;
98 files = store.count(Stored::FileList);
99 }
100 let rate = bytes as f64 / (1 << 20) as f64 / best.max(1e-9);
101 println!("{:<22} {best:>7.3} s {rate:>8.0} MiB/s", "built from disk");
102 println!("{:<22} {files} different files, {bytes} bytes", "");
103 }
104 Ok(())
105}More examples
examples/roundtrip.rs (line 231)
220fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
221 let mut arguments = std::env::args_os().skip(1);
222 let (Some(folder), Some(work)) = (arguments.next(), arguments.next()) else {
223 eprintln!("usage: roundtrip <folder> <work-dir>");
224 std::process::exit(2);
225 };
226 let work = PathBuf::from(work);
227 fs::create_dir(&work)?;
228
229 let source = DirStore::new(work.join("source-store"));
230 let started = Instant::now();
231 let root = build(&source, &folder, &BuildOptions::new())?;
232 let record = record_of(&source, &root)?;
233 let (size, totals) = (record.top().size, record.totals());
234 report("built", started, size);
235 println!("root {root}");
236 println!(
237 "the tree {size} bytes; kept once: {} chunks of {} bytes, and {} bytes of lists",
238 totals.chunks, totals.chunk_bytes, totals.list_bytes
239 );
240
241 let started = Instant::now();
242 let summary = verify(&source, &root)?;
243 report("checked", started, totals.chunk_bytes);
244 println!(
245 " {} different files in {} different folders",
246 summary.files, summary.folders
247 );
248
249 let fetched = DirStore::new(work.join("fetched-store"));
250 let started = Instant::now();
251 fetch(&fetched, &root, &mut Responder::new(&source))?;
252 report("fetched", started, totals.chunk_bytes);
253
254 if let Some(file) = largest_file(&source, record.top())? {
255 // A stretch of 4 KiB is read from 64 places spread over the file, each
256 // asked for on its own, and the time one takes on average is printed:
257 // a single read is shorter than some systems' clocks can tell apart.
258 let length = file.size.min(4096);
259 let places: u64 = 64;
260 let mut holder = Responder::new(&source);
261 let mut remote = Remote::open(&mut holder, &root)?;
262 let mut stretch = Vec::new();
263 let started = Instant::now();
264 for place in 0..places {
265 stretch.clear();
266 let offset = (file.size - length) * place / (places - 1);
267 remote.read(&file, offset, length, &mut stretch)?;
268 }
269 println!(
270 "a stretch {:>8.4} s {} bytes at a time, from {places} places in a file of {} bytes",
271 started.elapsed().as_secs_f64() / places as f64,
272 stretch.len(),
273 file.size
274 );
275 }
276
277 let written = work.join("written");
278 let started = Instant::now();
279 write_to_disk(&fetched, &root, &written)?;
280 report("written out", started, size);
281
282 let again = DirStore::new(work.join("again-store"));
283 let started = Instant::now();
284 let rebuilt = build(&again, &written, &BuildOptions::new())?;
285 report("built again", started, size);
286 if rebuilt != root {
287 return Err(format!("the copy has the root {rebuilt}, and not {root}").into());
288 }
289 println!("the copy has the same root");
290 Ok(())
291}Sourcepub fn chunking(self, chunking: Chunking) -> Self
pub fn chunking(self, chunking: Chunking) -> Self
How files are chunked. Without this they are chunked by content, as every tree outside a test should be.
Sourcepub fn timestamp(self, on: bool) -> Self
pub fn timestamp(self, on: bool) -> Self
Writes the time of building into the record. Two builds of the same folder then have different roots.
Sourcepub fn workers(self, workers: usize) -> Self
pub fn workers(self, workers: usize) -> Self
How many threads build the tree. Zero is one for each core. Files are chunked several at once, and a thread that has no file of its own helps to hash the chunks of a long one.
pub fn links(self, links: Links) -> Self
Trait Implementations§
Source§impl Clone for BuildOptions
impl Clone for BuildOptions
Source§impl Debug for BuildOptions
impl Debug for BuildOptions
Auto Trait Implementations§
impl Forget for BuildOptions
impl Freeze for BuildOptions
impl Move for BuildOptions
impl RefUnwindSafe for BuildOptions
impl Send for BuildOptions
impl Sync for BuildOptions
impl Unpin for BuildOptions
impl UnsafeUnpin for BuildOptions
impl UnwindSafe for BuildOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more