Rename reload/sync to load/save

This commit is contained in:
Marcel Müller
2018-05-09 12:39:03 +02:00
parent a16e3ce84c
commit 2444283498
7 changed files with 27 additions and 25 deletions

View File

@@ -76,7 +76,9 @@ Usage is quite simple:
- Create a `MemoryDatabase` with `MemoryDatabase::memory`
- Create a `Database` with `Database::from_parts`
- `Write`/`Read` data from the Database
- Don't forget to run `sync` periodically
- Don't forget to run `save` periodically, or whenever it makes sense.
- You can save in parallel to using the Database. However you will lock
write acess while it is being written to storage.
```rust
# use std::collections::HashMap;

View File

@@ -24,7 +24,7 @@ lazy_static! {
static ref CONFIG: DB = {
let db = FileDatabase::from_path("/tmp/config.ron", Config::default())
.expect("Create database from path");
db.reload().expect("Config to load");
db.load().expect("Config to load");
db
};
}

View File

@@ -35,10 +35,10 @@ fn do_main() -> Result<(), failure::Error> {
})?;
println!("Syncing Database");
db.sync()?;
db.save()?;
println!("Reloading Database");
db.reload()?;
println!("Loading Database");
db.load()?;
println!("Reading from Database");
db.read(|db| {

View File

@@ -165,7 +165,7 @@ migration_rules! {
fn get_version() -> data::Version {
let db = FileDatabase::<data::Versioning, Ron>::from_path("migration.ron", data::Versioning { version: data::Version::First }).unwrap();
db.reload().unwrap();
db.load().unwrap();
db.read(|ver| ver.version).unwrap()
}

View File

@@ -99,7 +99,7 @@ fn post_register(db: State<DB>, req_user: Form<User>, mut cookies: Cookies) -> R
Cookie::build("user_id", user.username.clone()).http_only(true).finish()
);
});
let _ = db.sync();
let _ = db.save();
Redirect::to("/")
}
@@ -133,7 +133,7 @@ fn post_paste(db: State<DB>, user: User, paste: Form<NewPaste>) -> Redirect {
};
db.pastes.push(paste);
});
let _ = db.sync();
let _ = db.save();
Redirect::to("/")
}
@@ -148,7 +148,7 @@ fn main() {
pastes: vec![],
users: HashMap::new(),
}).unwrap();
let _ = db.reload();
let _ = db.load();
rocket::ignite()

View File

@@ -35,12 +35,12 @@ fn do_main() -> Result<(), failure::Error> {
})?;
println!("Syncing Database");
db.sync()?;
db.save()?;
// Now lets switch it
let db = db.with_deser(Yaml).with_backend(FileBackend::open("test.yml")?);
db.sync()?;
db.save()?;
Ok(())
}

View File

@@ -387,7 +387,7 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
Ok(task(&mut lock))
}
fn load(backend: &mut Back, deser: &DeSer) -> error::Result<Data> {
fn inner_load(backend: &mut Back, deser: &DeSer) -> error::Result<Data> {
let new_data = deser.deserialize(
&backend.get_data().context(error::RustbreakErrorKind::Backend)?[..]
).context(error::RustbreakErrorKind::Deserialization)?;
@@ -395,17 +395,17 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
Ok(new_data)
}
/// Reload the Data from the backend
pub fn reload(&self) -> error::Result<()> {
/// Load the Data from the backend
pub fn load(&self) -> error::Result<()> {
let mut data = self.data.write().map_err(|_| error::RustbreakErrorKind::Poison)?;
let mut backend = self.backend.lock().map_err(|_| error::RustbreakErrorKind::Poison)?;
*data = Self::load(&mut backend, &self.deser).context(error::RustbreakErrorKind::Backend)?;
*data = Self::inner_load(&mut backend, &self.deser).context(error::RustbreakErrorKind::Backend)?;
Ok(())
}
/// Flush the data structure to the backend
pub fn sync(&self) -> error::Result<()> {
pub fn save(&self) -> error::Result<()> {
let mut backend = self.backend.lock().map_err(|_| error::RustbreakErrorKind::Poison)?;
let data = self.data.write().map_err(|_| error::RustbreakErrorKind::Poison)?;
@@ -418,12 +418,12 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
/// Get a clone of the data as it is in memory right now
///
/// To make sure you have the latest data, call this method with `reload` true
pub fn get_data(&self, reload: bool) -> error::Result<Data> {
/// To make sure you have the latest data, call this method with `load` true
pub fn get_data(&self, load: bool) -> error::Result<Data> {
let mut backend = self.backend.lock().map_err(|_| error::RustbreakErrorKind::Poison)?;
let mut data = self.data.write().map_err(|_| error::RustbreakErrorKind::Poison)?;
if reload {
*data = Self::load(&mut backend, &self.deser).context(error::RustbreakErrorKind::Backend)?;
if load {
*data = Self::inner_load(&mut backend, &self.deser).context(error::RustbreakErrorKind::Backend)?;
drop(backend);
}
Ok(data.clone())
@@ -431,11 +431,11 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
/// Puts the data as is into memory
///
/// To sync the data afterwards, call with `sync` true.
pub fn put_data(&self, new_data: Data, sync: bool) -> error::Result<()> {
/// To save the data afterwards, call with `save` true.
pub fn put_data(&self, new_data: Data, save: bool) -> error::Result<()> {
let mut backend = self.backend.lock().map_err(|_| error::RustbreakErrorKind::Poison)?;
let mut data = self.data.write().map_err(|_| error::RustbreakErrorKind::Poison)?;
if sync {
if save {
// TODO: Spin this into its own method
let ser = self.deser.serialize(&*data)
.context(error::RustbreakErrorKind::Serialization)?;
@@ -493,7 +493,7 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
/// db.level = 42;
/// })?;
///
/// db.sync()?;
/// db.save()?;
///
/// let other_db = db.try_clone()?;
///
@@ -587,7 +587,7 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer> {
impl<Data, Back, DeSer> Database<Data, Back, DeSer> {
/// Exchanges the Backend with the new one
///
/// The new backend does not necessarily have the latest data saved to it, so a `.sync` should
/// The new backend does not necessarily have the latest data saved to it, so a `.save` should
/// be called to make sure that it is saved.
pub fn with_backend<T>(self, backend: T) -> Database<Data, T, DeSer>
{