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 serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*};
use std::collections::HashMap;
use std::{cell::Cell, path::Path, sync::Arc};
use std::{collections::HashMap, path::Path};
/// GuildMap defines the guild-map type.
/// It is basically a HashMap from a GuildId to a data structure.
pub type GuildMap<V> = HashMap<GuildId, V>;
/// The generic DB type we will be using.
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>
@ -17,66 +21,64 @@ where
for<'de> T: Deserialize<'de>,
{
/// Insert into a ShareMap.
pub fn insert_into(data: &mut ShareMap, path: impl AsRef<Path>) -> Result<(), Error> {
let db = FileDatabase::<T, Ron>::from_path(path, T::default())?;
db.load().or_else(|e| {
dbg!(e);
db.save()
})?;
data.insert::<DB<T>>(Arc::new(db));
pub fn insert_into(data: &mut TypeMap, path: impl AsRef<Path>) -> Result<(), Error> {
let db = Database::<T>::load_from_path_or_default(path)?;
data.insert::<DB<T>>(db);
Ok(())
}
/// Open a previously inserted DB.
pub fn open(data: &ShareMap) -> DBWriteGuard<T> {
data.get::<Self>().expect("DB initialized").clone().into()
pub fn open(data: &TypeMap) -> DBWriteGuard<T> {
data.get::<Self>().expect("DB initialized").into()
}
}
/// The write guard for our FileDatabase.
/// It wraps the FileDatabase in a write-on-drop lock.
#[derive(Debug)]
pub struct DBWriteGuard<T>
pub struct DBWriteGuard<'a, T>
where
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{
db: Arc<FileDatabase<T, Ron>>,
needs_save: Cell<bool>,
db: &'a Database<T>,
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
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 {
db: v,
needs_save: Cell::new(false),
needs_save: false,
}
}
}
impl<T> DBWriteGuard<T>
impl<'a, T> DBWriteGuard<'a, T>
where
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{
/// Borrows the FileDatabase.
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
(*self).db.borrow_data()
pub fn borrow(&'a self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
self.db.borrow_data()
}
/// Borrows the FileDatabase for writing.
pub fn borrow_mut(&self) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
self.needs_save.set(true);
(*self).db.borrow_data_mut()
pub fn borrow_mut(
&'a mut self,
) -> 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
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{
fn drop(&mut self) {
if self.needs_save.get() {
if self.needs_save {
if let Err(e) = self.db.save() {
dbg!(e);
}