DB: No more Arcs!

This commit is contained in:
Natsu Kagami 2020-09-02 17:24:49 -04:00
parent 3ce86bcd19
commit 830faf7424
No known key found for this signature in database
GPG key ID: F17543D4B9424B94

View file

@ -1,15 +1,19 @@
use rustbreak::{deser::Yaml as Ron, FileDatabase}; use rustbreak::{deser::Yaml, 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, path::Path};
use std::{cell::Cell, path::Path, 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>;
/// The generic DB type we will be using. /// The generic DB type we will be using.
pub struct DB<T>(std::marker::PhantomData<T>); pub struct DB<T>(std::marker::PhantomData<T>);
impl<T: std::any::Any> serenity::prelude::TypeMapKey for DB<T> {
type Value = Arc<FileDatabase<T, Ron>>; /// A short type abbreviation for a FileDatabase.
type Database<T> = FileDatabase<T, Yaml>;
impl<T: std::any::Any + Send + Sync> serenity::prelude::TypeMapKey for DB<T> {
type Value = Database<T>;
} }
impl<T: std::any::Any + Default + Send + Sync + Clone + Serialize + std::fmt::Debug> DB<T> impl<T: std::any::Any + Default + Send + Sync + Clone + Serialize + std::fmt::Debug> DB<T>
@ -17,66 +21,64 @@ where
for<'de> T: Deserialize<'de>, for<'de> T: Deserialize<'de>,
{ {
/// Insert into a ShareMap. /// Insert into a ShareMap.
pub fn insert_into(data: &mut ShareMap, path: impl AsRef<Path>) -> Result<(), Error> { pub fn insert_into(data: &mut TypeMap, path: impl AsRef<Path>) -> Result<(), Error> {
let db = FileDatabase::<T, Ron>::from_path(path, T::default())?; let db = Database::<T>::load_from_path_or_default(path)?;
db.load().or_else(|e| { data.insert::<DB<T>>(db);
dbg!(e);
db.save()
})?;
data.insert::<DB<T>>(Arc::new(db));
Ok(()) Ok(())
} }
/// Open a previously inserted DB. /// Open a previously inserted DB.
pub fn open(data: &ShareMap) -> DBWriteGuard<T> { pub fn open(data: &TypeMap) -> DBWriteGuard<T> {
data.get::<Self>().expect("DB initialized").clone().into() data.get::<Self>().expect("DB initialized").into()
} }
} }
/// 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> pub struct DBWriteGuard<'a, 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>>, db: &'a Database<T>,
needs_save: Cell<bool>, needs_save: bool,
} }
impl<T> From<Arc<FileDatabase<T, Ron>>> for DBWriteGuard<T> impl<'a, T> From<&'a Database<T>> for DBWriteGuard<'a, 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: &'a Database<T>) -> Self {
DBWriteGuard { DBWriteGuard {
db: v, db: v,
needs_save: Cell::new(false), needs_save: false,
} }
} }
} }
impl<T> DBWriteGuard<T> impl<'a, T> DBWriteGuard<'a, T>
where where
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{ {
/// Borrows the FileDatabase. /// Borrows the FileDatabase.
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> { pub fn borrow(&'a self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
(*self).db.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.needs_save.set(true); &'a mut self,
(*self).db.borrow_data_mut() ) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
self.needs_save = true;
self.db.borrow_data_mut()
} }
} }
impl<T> Drop for DBWriteGuard<T> impl<'a, T> Drop for DBWriteGuard<'a, T>
where 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 self.needs_save.get() { if self.needs_save {
if let Err(e) = self.db.save() { if let Err(e) = self.db.save() {
dbg!(e); dbg!(e);
} }