pub struct BlobStore { /* private fields */ }
Expand description
Struct for interacting with the blob store
Implementations§
Source§impl BlobStore
impl BlobStore
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<BlobStore>
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());
Sourcepub fn hasher() -> Sha256
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")
Sourcepub fn add<P: AsRef<Path>>(&self, path: P) -> Result<BlobRef>
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");
Sourcepub fn add_files<P: AsRef<Path>>(
&self,
paths: &[P],
threads: u8,
) -> (Vec<(PathBuf, BlobRef)>, Vec<(PathBuf, Error)>)
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")
Sourcepub fn get(&self, blob_ref: &BlobRef) -> Result<Vec<u8>>
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,
]);
Sourcepub fn exists(&self, blob_ref: &BlobRef) -> bool
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))
Sourcepub fn delete(&self, blob_ref: &BlobRef) -> Result<()>
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
.
Sourcepub fn metadata(&self, blob_ref: &BlobRef) -> Result<BlobMetadata>
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.