Struct pearl::Storage[][src]

pub struct Storage<K: Key> { /* fields omitted */ }
Expand description

A main storage struct.

This type is clonable, cloning it will only create a new reference, not a new storage. Storage has a type parameter K. To perform read/write operations K must implement Key trait.

Examples

use pearl::{Storage, Builder, Key};

#[tokio::main]
async fn main() {
    let mut storage: Storage<String> = Builder::new()
        .work_dir("/tmp/pearl/")
        .max_blob_size(1_000_000)
        .max_data_in_blob(1_000_000_000)
        .blob_file_name_prefix("pearl-test")
        .build()
        .unwrap();
    storage.init().await.unwrap();
}

Implementations

init() used to prepare all environment to further work.

Storage works in directory provided to builder. If directory don’t exist, storage creates it, otherwise tries to init existing storage.

Errors

Returns error in case of failures with IO operations or if some of the required params are missed.

Writes data to active blob asyncronously. If active blob reaches it limit, creates new and closes old.

Examples

async fn write_data() {
    let key = 42u64.to_be_bytes().to_vec();
    let data = b"async written to blob".to_vec();
    storage.write(key, data).await
}

Errors

Fails with the same errors as write_with

Similar to write but with metadata

Examples

async fn write_data() {
    let key = 42u64.to_be_bytes().to_vec();
    let data = b"async written to blob".to_vec();
    let meta = Meta::new();
    meta.insert("version".to_string(), b"1.0".to_vec());
    storage.write_with(&key, data, meta).await
}

Errors

Fails if duplicates are not allowed and record already exists.

Reads the first found data matching given key.

Examples

async fn read_data() {
    let key = 42u64.to_be_bytes().to_vec();
    let data = storage.read(key).await;
}

Errors

Same as read_with

Reads data matching given key and metadata

Examples

async fn read_data() {
    let key = 42u64.to_be_bytes().to_vec();
    let meta = Meta::new();
    meta.insert("version".to_string(), b"1.0".to_vec());
    let data = storage.read(&key, &meta).await;
}

Errors

Return error if record is not found.

Returns entries with matching key

Errors

Fails after any disk IO errors.

Stop blob updater and release lock file

Errors

Fails because of any IO errors

blob_count returns exact number of closed blobs plus one active, if there is some. It locks on inner structure, so it much slower than next_blob_id.

Examples

use pearl::Builder;

let mut storage = Builder::new().work_dir("/tmp/pearl/").build::<f64>();
storage.init().await;
assert_eq!(storage.blobs_count(), 1);

index_memory returns the amount of memory used by blob to store indices

Returns next blob ID. If pearl dir structure wasn’t changed from the outside, returned number is equal to blobs_count. But this method doesn’t require lock. So it is much faster than blobs_count.

contains is used to check whether a key is in storage. Slower than check_bloom, because doesn’t prevent disk IO operations. contains returns either “definitely in storage” or “definitely not”.

Errors

Fails because of any IO errors

check_bloom is used to check whether a key is in storage. If bloom filter opt out, returns None. Uses bloom filter under the hood, so false positive results are possible, but false negatives are not. In other words, check_bloom returns either “possibly in storage” or “definitely not”.

Total records count in storage.

Records count per blob. Format: (blob_id, count). Last value is from active blob.

Records count in active blob. Returns None if active blob not set or any IO error occured.

Syncronizes data and metadata of the active blob with the filesystem. Like tokio::std::fs::File::sync_data, this function will attempt to ensure that in-core data reaches the filesystem before returning. May not syncronize file metadata to the file system.

Errors

Fails because of any IO errors.

Force closes active blob to dump index on disk and free RAM. Creates new active blob.

Errors

Fails because of any IO errors. Or if there are any problems with syncronization.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Performs the conversion.

Performs the conversion.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Attempts to convert self into T using TryInto<T>. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.