Struct rustbreak::Database
[−]
[src]
pub struct Database<T: Serialize + DeserializeOwned + Eq + Hash> { /* fields omitted */ }
The Database structure
Notes
One should create this once for each Database instance. Subsequent tries to open the same file should fail or worse, could break the database.
Example
use rustbreak::Database; let db = Database::open("/tmp/artists").unwrap(); let albums = vec![ ("What you do", "The Queenstons"), ("Experience", "The Prodigy"), ]; for (album, artist) in albums { db.insert(&format!("album_{}",album), artist).unwrap(); } db.flush().unwrap();
Methods
impl<T: Serialize + DeserializeOwned + Eq + Hash> Database<T>
[src]
fn open<P: AsRef<Path>>(path: P) -> Result<Database<T>>
[src]
Opens a new Database
This might fail if the file is non-empty and was not created by RustBreak, or if the file is already being used by another RustBreak instance.
Example
use rustbreak::Database; let db = Database::open("/tmp/more_artists").unwrap(); let albums = vec![ ("What you do", "The Queenstons"), ("Experience", "The Prodigy"), ]; for (album, artist) in albums { db.insert(&format!("album_{}",album), artist).unwrap(); } db.flush().unwrap();
fn insert<S: Serialize, K: ?Sized>(&self, key: &K, obj: S) -> Result<()> where
T: Borrow<K>,
K: Hash + PartialEq + ToOwned<Owned = T>,
[src]
T: Borrow<K>,
K: Hash + PartialEq + ToOwned<Owned = T>,
Insert a given Object into the Database at that key
This will overwrite any existing objects.
The Object has to be serializable.
fn delete<K: ?Sized>(&self, key: &K) -> Result<()> where
T: Borrow<K>,
K: Hash + Eq,
[src]
T: Borrow<K>,
K: Hash + Eq,
Remove an Object at that key
fn retrieve<S: DeserializeOwned, K: ?Sized>(&self, key: &K) -> Result<S> where
T: Borrow<K>,
K: Hash + Eq,
[src]
T: Borrow<K>,
K: Hash + Eq,
Retrieves an Object from the Database
Errors
This will return an Err(BreakError::NotFound)
if there is no key behind the object.
If you tried to request something that can't be serialized to then
Err(BreakError::Deserialize)
will be returned.
Example
use rustbreak::{Database, BreakError}; let db = Database::open("/tmp/stuff").unwrap(); for i in 0..5i64 { db.insert(&format!("num_{}", i), i*i*i).unwrap(); } let num : i64 = db.retrieve::<i64, str>("num_0").unwrap(); assert_eq!(num, 0); match db.retrieve::<usize, str>("non-existent") { Err(BreakError::NotFound) => {}, _ => panic!("Was still found?"), } match db.retrieve::<Vec<String>, str>("num_1") { Err(_) => {}, _ => panic!("Was deserialized?"), }
fn contains_key<S: DeserializeOwned, K: ?Sized>(&self, key: &K) -> Result<bool> where
T: Borrow<K>,
K: Hash + Eq,
[src]
T: Borrow<K>,
K: Hash + Eq,
Checks wether a given key exists in the Database
fn flush(&self) -> Result<()>
[src]
Flushes the Database to disk
fn transaction(&self) -> Transaction<T>
[src]
Starts a transaction
A transaction passes through reads but caches writes. This means that if changes do happen
they are processed at the same time. To run them you have to call run
on the
Transaction
object.
fn lock(&self) -> Result<Lock<T>>
[src]
Locks the Database, making sure only the caller can change it
This write-locks the Database until the Lock
has been dropped.
Panics
If you panic while holding the lock it will get poisoned and subsequent calls to it will fail. You will have to re-open the Database to be able to continue accessing it.