Builder

Struct Builder 

Source
pub struct Builder<WK, WV> { /* private fields */ }
Expand description

Serializes a stream of ([u8], [u8]) key-value pairs.

Serialization happens by writing key-value pairs in sorted order. A value is always written before its corresponding key, because the index will map that key to the starting byte offset of the value that was written.

Many calls of append_value_bytes can be made before committing the key –> offset mapping:

use mmap_cache::{Builder, Cache};

let mut index_bytes = Vec::new();
let mut value_bytes = Vec::new();

let mut builder = Builder::new(&mut index_bytes, &mut value_bytes)?;

// Write a value with multiple append calls.
builder.append_value_bytes(&777u32.to_be_bytes())?;
builder.append_value_bytes(&777u32.to_be_bytes())?;
builder.commit_entry(b"hot_garbage")?;

// Or equivalently, use just one insert call.
let mut buf = [0; 8];
buf[0..4].copy_from_slice(&777u32.to_be_bytes());
buf[4..8].copy_from_slice(&777u32.to_be_bytes());
builder.insert(b"lots_of_garbage", &buf)?;

builder.finish()?;

let cache = Cache::new(&index_bytes, &value_bytes)?;
assert_eq!(cache.get_value_offset(b"hot_garbage"), Some(0));
assert_eq!(unsafe { cache.get_transmuted_value(b"hot_garbage") }, Some(&buf));
assert_eq!(cache.get_value_offset(b"lots_of_garbage"), Some(8));
assert_eq!(unsafe { cache.get_transmuted_value(b"lots_of_garbage") }, Some(&buf));

Implementations§

Source§

impl<WK, WV> Builder<WK, WV>
where WK: Write, WV: Write,

Source

pub fn new(index_writer: WK, value_writer: WV) -> Result<Self, Error>

Creates a new Builder for serializing a collection of key-value pairs.

  • index_writer: Writes the serialized fst::Map which stores the value offsets.
  • value_writer: Writes the values pointed to by the byte offsets stored in the fst::Map.
§Warning

This crate has no control over the alignment guarantees provided by the given writers. Be careful to preserve alignment when using memmap2.

Source

pub fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error>

Writes value into the value stream and commits the entry, storing the value’s u64 byte offset along with the key in the fst::Map.

Source

pub fn commit_entry(&mut self, key: &[u8]) -> Result<(), Error>

Finishes writing the current value, associating the starting byte offset of the value with key.

Source

pub fn append_value_bytes(&mut self, value: &[u8]) -> Result<(), Error>

Writes value into the value stream.

The caller may continue appending more value bytes as needed before calling commit_entry to finish the current entry and start a new one.

Source

pub fn finish(self) -> Result<(), Error>

Completes the serialization and flushes any outstanding IO.

Source§

impl Builder<BufWriter<File>, BufWriter<File>>

Source

pub fn create_files( index_path: impl AsRef<Path>, value_path: impl AsRef<Path>, ) -> Result<Self, Error>

Creates a new Builder, using the file at index_path for an index writer and the file at value_path as a value writer.

This always overwrites the given files.

After calling finish, these same files can be used with Cache::map_files.

Auto Trait Implementations§

§

impl<WK, WV> Freeze for Builder<WK, WV>
where WV: Freeze, WK: Freeze,

§

impl<WK, WV> RefUnwindSafe for Builder<WK, WV>

§

impl<WK, WV> Send for Builder<WK, WV>
where WV: Send, WK: Send,

§

impl<WK, WV> Sync for Builder<WK, WV>
where WV: Sync, WK: Sync,

§

impl<WK, WV> Unpin for Builder<WK, WV>
where WV: Unpin, WK: Unpin,

§

impl<WK, WV> UnwindSafe for Builder<WK, WV>
where WV: UnwindSafe, WK: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.