Struct microkv::kv::MicroKV

source ·
pub struct MicroKV { /* private fields */ }
Expand description

Defines the main interface structure to represent the most recent state of the data store.

Implementations§

New MicroKV store with store to base path

Initializes a new empty and unencrypted MicroKV store with an identifying database name. This is the bare minimum that can operate as a key-value store, and can be configured using other builder methods.

Open with base path

Opens a previously instantiated and encrypted MicroKV, given a db name. The public nonce generated from a previous session is also retrieved in order to do authenticated encryption later on.

Helper that forms an absolute path from a given database name and the default workspace path.

with base path

Builds up the MicroKV with a cleartext password, which is hashed using the defaultly supported SHA-256 by sodiumoxide, in order to instantiate a 32-byte hash.

Use if the password to encrypt is not naturally pseudorandom and secured in-memory, and is instead read elsewhere, like a file or stdin (developer should guarantee security when implementing such methods, as MicroKV only guarantees hashing and secure storage).

Builds up the MicroKV with a hashed buffer, which is then locked securely `for later use.

Use if the password to encrypt is generated as a pseudorandom value, or previously hashed by another preferred one-way function within or outside the application.

Set is auto commit

unsafe get, may this api can change name to get_unwrap

Decrypts and retrieves a value. Can return errors if lock is poisoned, ciphertext decryption doesn’t work, and if parsing bytes fail.

Encrypts and adds a new key-value pair to storage.

Delete removes an entry in the key value store.

Arbitrary read-lock that encapsulates a read-only closure. Multiple concurrent readers can hold a lock and parse out data.

use microkv::MicroKV;
use microkv::namespace::ExtendedIndexMap;

let kv = MicroKV::new("example").with_pwd_clear("p@ssw0rd".to_string());
let value = String::from("my value");
kv.namespace("a").put("user", &value).expect("cannot insert user");
kv.namespace("b").put("user", &value).expect("cannot insert user");

kv.lock_read(|c| {
    let user_namespace_1: String = c.kv_get(&kv, "a", "user").expect("cannot read user").expect("key not found");
    let user_namespace_2: String = c.kv_get(&kv, "b", "user").expect("cannot read user").expect("key not found");
    assert_eq!(user_namespace_1, user_namespace_2);
}).expect("cannot get lock")

Arbitrary write-lock that encapsulates a write-only closure Single writer can hold a lock and mutate data, blocking any other readers/writers before the lock is released.

use microkv::MicroKV;
use microkv::namespace::ExtendedIndexMap;

let kv = MicroKV::new("example").with_pwd_clear("p@ssw0rd".to_string());
let value: u32 = 123;
kv.put("number", &value).expect("cannot insert number");

kv.lock_write(|c| {
    let current_value: u32 = c.kv_get_unwrap(&kv, "", "number").expect("cannot read number");
    println!("Current value is: {current_value}");
    c.kv_put(&kv, "", "number", &(current_value + 1));
    let current_value: u32 = c.kv_get_unwrap(&kv, "", "number").expect("cannot read number");
    println!("Now the value is: {current_value}");
}).expect("cannot get lock")

Helper routine that acquires a reader lock and checks if a key exists.

Safely consumes an iterator over the keys in the IndexMap and returns a Vec<String> for further use.

Note that key iteration, not value iteration, is only supported in order to preserve security guarantees.

Safely consumes an iterator over a copy of in-place sorted keys in the IndexMap and returns a Vec<String> for further use.

Note that key iteration, not value iteration, is only supported in order to preserve security guarantees.

Empties out the entire underlying IndexMap in O(n) time, but does not delete the persistent storage file from disk. The IndexMap remains, and its capacity is kept the same.

Writes the IndexMap to persistent storage after encrypting with secure crypto construction.

Clears the underlying data structure for the key-value store, and deletes the database file to remove all traces.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Deserialize this value from the given Serde deserializer. Read more
Executes the destructor for this type. Read more
Serialize this value into the given Serde serializer. 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. 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.