mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-15 23:08:55 +00:00
DB now only writes on borrow_mut
This commit is contained in:
parent
f179b86e12
commit
0273237b76
1 changed files with 18 additions and 10 deletions
|
@ -2,9 +2,7 @@ use rustbreak::{deser::Yaml as Ron, FileDatabase};
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*};
|
use serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::{cell::Cell, path::Path, sync::Arc};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
/// GuildMap defines the guild-map type.
|
/// GuildMap defines the guild-map type.
|
||||||
/// It is basically a HashMap from a GuildId to a data structure.
|
/// It is basically a HashMap from a GuildId to a data structure.
|
||||||
pub type GuildMap<V> = HashMap<GuildId, V>;
|
pub type GuildMap<V> = HashMap<GuildId, V>;
|
||||||
|
@ -38,16 +36,23 @@ where
|
||||||
/// The write guard for our FileDatabase.
|
/// The write guard for our FileDatabase.
|
||||||
/// It wraps the FileDatabase in a write-on-drop lock.
|
/// It wraps the FileDatabase in a write-on-drop lock.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DBWriteGuard<T>(Arc<FileDatabase<T, Ron>>)
|
pub struct DBWriteGuard<T>
|
||||||
where
|
where
|
||||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned;
|
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||||
|
{
|
||||||
|
db: Arc<FileDatabase<T, Ron>>,
|
||||||
|
needs_save: Cell<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> From<Arc<FileDatabase<T, Ron>>> for DBWriteGuard<T>
|
impl<T> From<Arc<FileDatabase<T, Ron>>> for DBWriteGuard<T>
|
||||||
where
|
where
|
||||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||||
{
|
{
|
||||||
fn from(v: Arc<FileDatabase<T, Ron>>) -> Self {
|
fn from(v: Arc<FileDatabase<T, Ron>>) -> Self {
|
||||||
DBWriteGuard(v)
|
DBWriteGuard {
|
||||||
|
db: v,
|
||||||
|
needs_save: Cell::new(false),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,11 +62,12 @@ where
|
||||||
{
|
{
|
||||||
/// Borrows the FileDatabase.
|
/// Borrows the FileDatabase.
|
||||||
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
|
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
|
||||||
(*self).0.borrow_data()
|
(*self).db.borrow_data()
|
||||||
}
|
}
|
||||||
/// Borrows the FileDatabase for writing.
|
/// Borrows the FileDatabase for writing.
|
||||||
pub fn borrow_mut(&self) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
|
pub fn borrow_mut(&self) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
|
||||||
(*self).0.borrow_data_mut()
|
self.needs_save.set(true);
|
||||||
|
(*self).db.borrow_data_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,8 +76,10 @@ where
|
||||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||||
{
|
{
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Err(e) = self.0.save() {
|
if self.needs_save.get() {
|
||||||
dbg!(e);
|
if let Err(e) = self.db.save() {
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue