2021-03-11 00:31:50 +00:00
|
|
|
use crate::error::Result;
|
|
|
|
use std::path::Path;
|
|
|
|
use geoffrey_models::GeoffreyDatabaseModel;
|
2021-05-08 20:42:47 +00:00
|
|
|
use crate::{u64_from_bytes, u64_to_bytes};
|
|
|
|
use sled::Iter;
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
pub struct Database {
|
|
|
|
db: sled::Db,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
pub fn new(db_path: &Path) -> Result<Self> {
|
|
|
|
let db = sled::open(db_path)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
db,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tree<T>(&self) -> Result<sled::Tree> where T: GeoffreyDatabaseModel {
|
|
|
|
Ok(self.db.open_tree::<String>(T::tree())?)
|
|
|
|
}
|
|
|
|
|
2021-03-13 20:42:37 +00:00
|
|
|
pub fn insert<T>(&self, mut model: T) -> Result<T> where T: GeoffreyDatabaseModel {
|
|
|
|
let id = match model.id() {
|
|
|
|
Some(id) => id,
|
|
|
|
None => {
|
|
|
|
let id = self.db.generate_id()?;
|
|
|
|
model.set_id(id);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-11 00:31:50 +00:00
|
|
|
let tree = self.get_tree::<T>()?;
|
|
|
|
let id_bytes = u64_to_bytes(id);
|
|
|
|
|
2021-03-13 20:42:37 +00:00
|
|
|
tree.insert(id_bytes, model.to_bytes()?)?;
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
Ok(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get<T>(&self, id: u64) -> Result<Option<T>> where T: GeoffreyDatabaseModel {
|
|
|
|
let tree = self.get_tree::<T>()?;
|
|
|
|
let id_bytes = u64_to_bytes(id);
|
|
|
|
|
|
|
|
if let Some(bytes) = tree.get(id_bytes)? {
|
2021-03-13 20:42:37 +00:00
|
|
|
Ok(Some(T::try_from_bytes(&bytes)?))
|
2021-03-11 00:31:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-08 20:42:47 +00:00
|
|
|
|
|
|
|
pub fn filter<T, F>(&self, f: F) -> Result<Vec<T>> where T: GeoffreyDatabaseModel, F: Fn(u64, &T) -> bool {
|
|
|
|
let tree = self.db.open_tree(T::tree())?;
|
|
|
|
|
|
|
|
Ok(tree.iter().filter_map(|e| {
|
|
|
|
if let Ok((id, data)) = e {
|
|
|
|
let id = u64_from_bytes(&mut id.clone());
|
|
|
|
let data = T::try_from_bytes(&data).unwrap();
|
|
|
|
|
|
|
|
if f(id, &data) {
|
|
|
|
Some(data)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tree_iter<T>(&self) -> Result<Iter> where T: GeoffreyDatabaseModel {
|
|
|
|
Ok(self.db.open_tree(T::tree()).map(|tree| tree.iter())?)
|
|
|
|
}
|
2021-03-11 00:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::database::{Database};
|
|
|
|
use std::path::Path;
|
|
|
|
use geoffrey_models::models::player::{Player, UserID};
|
|
|
|
use lazy_static::lazy_static;
|
2021-05-08 20:42:47 +00:00
|
|
|
use geoffrey_models::GeoffreyDatabaseModel;
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2021-05-08 20:42:47 +00:00
|
|
|
static ref DB: Database = Database::new(Path::new("../locations")).unwrap();
|
2021-03-11 00:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup() {
|
|
|
|
DB.db.clear().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert() {
|
2021-05-08 20:42:47 +00:00
|
|
|
let player = Player::new("CoolZero123", UserID::DiscordUUID(0u64));
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
let p2 = DB.insert::<Player>(player.clone()).unwrap();
|
|
|
|
|
2021-05-08 20:42:47 +00:00
|
|
|
assert!(p2.id().is_some());
|
2021-03-11 00:31:50 +00:00
|
|
|
assert_eq!(player.name, p2.name);
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get() {
|
2021-05-08 20:42:47 +00:00
|
|
|
let player = Player::new("CoolZero123", UserID::DiscordUUID(0u64));
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
let p2 = DB.insert::<Player>(player.clone()).unwrap();
|
|
|
|
|
2021-05-08 20:42:47 +00:00
|
|
|
let p3 = DB.get::<Player>(p2.id().unwrap()).unwrap();
|
2021-03-11 00:31:50 +00:00
|
|
|
|
|
|
|
assert!(p3.is_some());
|
|
|
|
assert_eq!(p3.unwrap().name, player.name);
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|