From 6b7acbe520039d741ab1d5ded315815569154d2d Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 18 Oct 2020 15:13:34 +0000 Subject: [PATCH] Open trees for rooms and variables, but not yet use them. --- src/bin/dicebot-cmd.rs | 2 +- src/bin/dicebot.rs | 2 +- src/db.rs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index 645046a..cdeaf26 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -5,7 +5,7 @@ use chronicle_dicebot::error::BotError; #[tokio::main] async fn main() -> Result<(), BotError> { - let db = Database::new(&sled::open("test-db")?); + let db = Database::new("test-db")?; let input = std::env::args().skip(1).collect::>().join(" "); let command = match commands::parse(&input) { Ok(command) => command, diff --git a/src/bin/dicebot.rs b/src/bin/dicebot.rs index df886c1..718c71b 100644 --- a/src/bin/dicebot.rs +++ b/src/bin/dicebot.rs @@ -26,7 +26,7 @@ async fn run() -> Result<(), BotError> { .expect("Need a config as an argument"); let cfg = Arc::new(read_config(config_path)?); - let db = Database::new(&sled::open(cfg.database_path())?); + let db = Database::new(&cfg.database_path())?; let state = Arc::new(RwLock::new(DiceBotState::new(&cfg))); match DiceBot::new(&cfg, &state, &db) { diff --git a/src/db.rs b/src/db.rs index 9d9f687..f714675 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ use byteorder::LittleEndian; -use sled::{Db, IVec}; +use sled::{Db, IVec, Tree}; use std::collections::HashMap; use thiserror::Error; use zerocopy::byteorder::I32; @@ -13,6 +13,8 @@ type LittleEndianI32Layout<'a> = LayoutVerified<&'a [u8], I32>; #[derive(Clone)] pub struct Database { db: Db, + variables: Tree, + rooms: Tree, } //TODO better combining of key and value in certain errors (namely @@ -59,8 +61,16 @@ fn convert(raw_value: &[u8]) -> Result { } impl Database { - pub fn new(db: &Db) -> Database { - Database { db: db.clone() } + pub fn new>(path: P) -> Result { + let db = sled::open(path)?; + let variables = db.open_tree("variables")?; + let rooms = db.open_tree("rooms")?; + + Ok(Database { + db: db.clone(), + variables: variables, + rooms: rooms, + }) } pub async fn get_user_variables(