Skip to main content

MemoryStore

Struct MemoryStore 

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

A store in memory.

Implementations§

Source§

impl MemoryStore

Source

pub fn new() -> Self

Examples found in repository?
examples/proof_sizes.rs (line 39)
32fn main() -> Result<()> {
33    println!("One entry of a folder, by its name. Names are 16 bytes long.");
34    println!(
35        "{:>10}  {:>12}  {:>8}  {:>10}",
36        "entries", "whole list", "branch", "smaller by"
37    );
38    for count in [10_usize, 1_000, 10_000, 1_000_000] {
39        let store = MemoryStore::new();
40        let mut builder = Builder::new(&store, Chunking::fixed(16)?);
41        let file = builder.file_from_bytes(b"the same small file")?;
42        let entries = (0..count)
43            .map(|number| Entry::new(format!("file-{number:07}.bin"), file, ExtraData::new()))
44            .collect::<Result<Vec<_>>>()?;
45        let folder = builder.folder(entries)?;
46        let list = store
47            .get(Stored::FolderList, &folder.hash)?
48            .map_or(0, |list| list.len());
49        let request = Request::FolderName {
50            folder: folder.hash,
51            name: format!("file-{:07}.bin", count / 2),
52        };
53        row(count, list, answer_bytes(&store, &request)?);
54    }
55
56    println!();
57    println!("One chunk of a file, by its number.");
58    println!(
59        "{:>10}  {:>12}  {:>8}  {:>10}",
60        "chunks", "whole list", "branch", "smaller by"
61    );
62    for count in [10_usize, 1_000, 10_000, 500_000] {
63        // Chunks of one byte, so that a file of many chunks is small.
64        let store = MemoryStore::new();
65        let mut builder = Builder::new(&store, Chunking::fixed(1)?);
66        let bytes: Vec<u8> = (0..count).map(|at| (at % 251) as u8).collect();
67        let file = builder.file_from_bytes(&bytes)?;
68        let list = store
69            .get(Stored::FileList, &file.hash)?
70            .map_or(0, |list| list.len());
71        let request = Request::FileBranch {
72            file: file.hash,
73            start: (count / 2) as u64,
74            count: 1,
75        };
76        row(count, list, answer_bytes(&store, &request)?);
77    }
78    Ok(())
79}
More examples
Hide additional examples
examples/speed.rs (line 62)
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}
Source

pub fn count(&self, kind: Stored) -> usize

How many things of kind are kept.

Examples found in repository?
examples/speed.rs (line 98)
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}
Source

pub fn bytes(&self, kind: Stored) -> u64

How many bytes the things of kind hold together.

Source

pub fn hashes(&self, kind: Stored) -> Vec<Hash>

The hashes of the things of kind, in order.

Trait Implementations§

Source§

impl Default for MemoryStore

Source§

fn default() -> Self

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

impl Store for MemoryStore

Source§

fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()>

Keeps bytes under hash. Keeping what is kept already changes nothing.
Source§

fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>>

What is kept under hash, if anything.
Source§

fn has(&self, kind: Stored, hash: &Hash) -> Result<bool>

Whether anything is kept under hash. Unless a store has its own, this asks how long the thing is, so a store that can say that without reading the bytes is not read.
Source§

fn remove(&self, kind: Stored, hash: &Hash) -> Result<()>

Forgets what is kept under hash.
Source§

fn lend( &self, kind: Stored, hash: &Hash, with: &mut dyn FnMut(&[u8]) -> Result<()>, ) -> Result<bool>

Lends what is kept under hash to with, and says whether anything is kept: with is called once if it is, and not at all if it is not. The bytes are lent for that call only, and an error from with is what this gives back. Read more
Source§

fn read_into( &self, kind: Stored, hash: &Hash, into: &mut [u8], ) -> Result<Option<u64>>

Reads what is kept under hash into into, when it is as long as into is, and says how long it is: None when nothing is kept. A thing of another length is not read, and into is left as it was. Read more
Source§

fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>>

How many bytes are kept under hash.

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> 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, 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.