Skip to main content

BuildOptions

Struct BuildOptions 

Source
pub struct BuildOptions { /* private fields */ }
Expand description

How a tree is built from disk.

Implementations§

Source§

impl BuildOptions

Source

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
Hide additional 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}
Source

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.

Source

pub fn extra(self, extra: ExtraData) -> Self

Extra data for the tree’s record.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for BuildOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BuildOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BuildOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.