Struct BlobStore

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

Struct for interacting with the blob store

Implementations§

Source§

impl BlobStore

Source

pub fn new<P: AsRef<Path>>(path: P) -> Result<BlobStore>

Creates a new instance of the BlobStore struct used to interact with the blob store. If the specified blob store root path does not exists, it tries to create it.

§Errors

It errors if the specified path is not a directory or if it does not exist and cannot be created.

§Examples
use rstr::BlobStore;

let blob_store = BlobStore::new("../tests/test_data_store");
assert!(blob_store.is_ok());

let blob_store = BlobStore::new("../tests/test_file.txt");
assert!(blob_store.is_err());
Source

pub fn hasher() -> Sha256

Returns an instance of the hasher used to compute the blob reference for a file

§Examples
use rstr::BlobStore;
use sha2::{Digest, Sha256};

let mut hasher = BlobStore::hasher();
hasher.update(b"hello world");
let result = hasher.finalize();
assert_eq!(format!("{:x}", result), "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
Source

pub fn add<P: AsRef<Path>>(&self, path: P) -> Result<BlobRef>

Add a file to the blob store given a path.

§Examples
use rstr::{BlobStore, BlobRef};
use std::path::PathBuf;
let blob_store = BlobStore::new("../tests/test_data_store/").unwrap();

let blob_ref: BlobRef = blob_store.add("../tests/test_file.txt").unwrap();
assert!(blob_store.exists(&blob_ref));
assert_eq!(blob_ref.reference(), "f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de");
Source

pub fn add_files<P: AsRef<Path>>( &self, paths: &[P], threads: u8, ) -> (Vec<(PathBuf, BlobRef)>, Vec<(PathBuf, Error)>)

Given a list of paths to files/directories it adds them to the blob store. In the case of a directory it adds all the files in its children recursively.

The function iterates over all paths in parallel and adds each file to the blob store.

It returns two vectors: one containing the paths to the files that were successfully added together with their generated BlobRef and the other containing the list of paths that errored together with the error.

§Examples
use rstr::{BlobStore, BlobRef};

let blob_store = BlobStore::new("../tests/test_data_store").unwrap();

let paths = [Path::new("../tests/test_file.txt")];
let threads: u8 = 8;
let (blob_refs_with_paths, errors) = blob_store.add_files(&paths[..], threads);
let blob_refs: Vec<BlobRef> = blob_refs_with_paths.into_iter().map(|(_, b)| b).collect();

assert_eq!(blob_refs[0].reference(), "f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de")
Source

pub fn get(&self, blob_ref: &BlobRef) -> Result<Vec<u8>>

Given a BlobRef it retrieves the associated file and returns it as a byte-array.

§Examples
use rstr::{BlobStore, BlobRef};

let blob_store = BlobStore::new("../tests/test_data_store").unwrap();
let reference = "f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de";
let blob_ref = BlobRef::new(reference).unwrap();

let content = blob_store.get(&blob_ref).unwrap();

assert_eq!(content, &[
    84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 102, 105, 108,
    101, 46,
]);
Source

pub fn exists(&self, blob_ref: &BlobRef) -> bool

Returns true if there is a file associated with the BlobRef in the blob store

§Examples
use rstr::{BlobStore, BlobRef};

let blob_store = BlobStore::new("../tests/test_data_store/").unwrap();
let blob_ref = BlobRef::new("f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de").unwrap();

assert!(blob_store.exists(&blob_ref))
Source

pub fn delete(&self, blob_ref: &BlobRef) -> Result<()>

Given a BlobRef it deletes the corresponding blob from the blob store

§Examples
let blob_store = BlobStore::new("/path/to/blob/store").unwrap();

let blob_ref = BlobRef::new("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9").unwrap();
assert!(blob_store.exists(&blob_ref));

blob_store.delete(&blob_ref);
assert!(!blob_store.exists(&blob_ref));
§Errors

See fs::remove_dir_all.

Source

pub fn metadata(&self, blob_ref: &BlobRef) -> Result<BlobMetadata>

Given a BlobRef returns the metadata relative to the referenced blob. For more details on the metadata returned see BlobMetadata.

The mime type is inferred from the file’s magic number as a string. It defaults to “application/octet-stream” if it cannot determine the type. We use the tree_magic_mini crate to infer the mime type.

§Errors

Will return an error if the file cannot be found/opened or if std::fs::metadata fails.

Trait Implementations§

Source§

impl Clone for BlobStore

Source§

fn clone(&self) -> BlobStore

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BlobStore

Source§

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

Formats the value using the given formatter. 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> 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 = 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.