Struct rustbreak::Database [] [src]

pub struct Database<T: Serialize + Deserialize + 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 + Deserialize + Eq + Hash> 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();

Insert a given Object into the Database at that key

This will overwrite any existing objects.

The Object has to be serializable.

Remove an Object at that key

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?"),
}

Checks wether a given key exists in the Database

Flushes the Database to disk

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.

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.

Trait Implementations

impl<T: Debug + Serialize + Deserialize + Eq + Hash> Debug for Database<T>
[src]

Formats the value using the given formatter.