[−][src]Trait caves::Cave
A simple interface for key-value stores.
A Cave
object must have support for the following actions:
- Get a key by name, or return an error if it doesn't exist.
- Store a key by name; update it if it exists or create it if it doesn't.
- Delete a key by name, or return an error if it doesn't exist.
These actions must be able to happen concurrently, from any thread. This means that the objects must not rely on exclusive mutability references in order to update their state.
Usage
Here's an example on how one can use a Cave
object. In this example, we
use a MemoryCave
object, but any other Cave
object can be used in its
place.
use caves::errors::Error; use caves::{MemoryCave, Cave}; // Initialize a MemoryCave object. let b = MemoryCave::new(); // Create a new key with an empty value. b.set("key", b""); // Override the key's value. b.set("key", b"value"); // Retrieve the contents of the key. let res = b.get("key"); assert_eq!(res.unwrap(), b"value"); // Delete the key. b.delete("key"); // Subsequent attempts to retrieve the contents of the key should return an // error. let res = b.get("key"); assert_eq!(res, Err(Error::NotFound("key".to_string())));
Required methods
fn get(&self, name: &str) -> Res
Get a key by its name, and return its contents.
If it does not exist, return an error.
fn set(&self, name: &str, data: &[u8]) -> Res
Create or update a key by its name.
fn delete(&self, name: &str) -> Res
Delete a key by its name.
If it does not exist, return an error.
Provided methods
fn not_found(&self, name: &str) -> Res
A helper method to return an error for keys that could not be found.