Adds DB struct and impl to db_handler
This commit is contained in:
parent
5dc2ed2a90
commit
84fa10cbe1
|
@ -5,51 +5,57 @@ use chrono::{naive::Days, DateTime, Local, NaiveDateTime, Utc};
|
|||
use lox_library::{BridgeAuth, BridgeDb};
|
||||
use sled::IVec;
|
||||
|
||||
pub fn write_context_to_db(db: sled::Db, context: lox_context::LoxServerContext) {
|
||||
let date = Local::now().format("context_%Y-%m-%d_%H:%M:%S").to_string();
|
||||
let json_result = serde_json::to_vec(&context).unwrap();
|
||||
println!("Date: {:?}", date);
|
||||
let new_ivec = db.insert(
|
||||
IVec::from(date.as_bytes().to_vec()),
|
||||
IVec::from(json_result.clone()),
|
||||
);
|
||||
assert_eq!(
|
||||
db.get(IVec::from(date.as_bytes().to_vec()))
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
IVec::from(json_result)
|
||||
);
|
||||
println!("New entry key: {:?}", new_ivec);
|
||||
pub struct DB {
|
||||
db: sled::Db,
|
||||
}
|
||||
|
||||
pub fn open_new_or_existing_db(
|
||||
db_config: DbConfig,
|
||||
roll_back_date: Option<String>,
|
||||
) -> Result<(sled::Db, lox_context::LoxServerContext), sled::Error> {
|
||||
let context: lox_context::LoxServerContext;
|
||||
let (lox_db, context) = match sled::open(db_config.db_path) {
|
||||
Ok(lox_db) => {
|
||||
// Check if the lox_db already exists
|
||||
if lox_db.was_recovered() {
|
||||
context = read_lox_context_from_db(lox_db.clone(), roll_back_date);
|
||||
//Otherwise, create a new Lox context
|
||||
} else {
|
||||
let new_db = BridgeDb::new();
|
||||
let new_ba = BridgeAuth::new(new_db.pubkey);
|
||||
context = lox_context::LoxServerContext {
|
||||
db: Arc::new(Mutex::new(new_db)),
|
||||
ba: Arc::new(Mutex::new(new_ba)),
|
||||
extra_bridges: Arc::new(Mutex::new(Vec::new())),
|
||||
to_be_replaced_bridges: Arc::new(Mutex::new(Vec::new())),
|
||||
};
|
||||
impl DB {
|
||||
pub fn write_context(&mut self, context: lox_context::LoxServerContext) {
|
||||
let date = Local::now().format("context_%Y-%m-%d_%H:%M:%S").to_string();
|
||||
let json_result = serde_json::to_vec(&context).unwrap();
|
||||
println!("Writing context to the db with key: {:?}", date);
|
||||
let _new_ivec = self.db.insert(
|
||||
IVec::from(date.as_bytes().to_vec()),
|
||||
IVec::from(json_result.clone()),
|
||||
);
|
||||
assert_eq!(
|
||||
self.db
|
||||
.get(IVec::from(date.as_bytes().to_vec()))
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
IVec::from(json_result)
|
||||
);
|
||||
}
|
||||
|
||||
pub fn open_new_or_existing_db(
|
||||
db_config: DbConfig,
|
||||
roll_back_date: Option<String>,
|
||||
) -> Result<(DB, lox_context::LoxServerContext), sled::Error> {
|
||||
let context: lox_context::LoxServerContext;
|
||||
let (lox_db, context) = match sled::open(db_config.db_path) {
|
||||
Ok(lox_db) => {
|
||||
// Check if the lox_db already exists
|
||||
if lox_db.was_recovered() {
|
||||
context = read_lox_context_from_db(lox_db.clone(), roll_back_date);
|
||||
//Otherwise, create a new Lox context
|
||||
} else {
|
||||
let new_db = BridgeDb::new();
|
||||
let new_ba = BridgeAuth::new(new_db.pubkey);
|
||||
context = lox_context::LoxServerContext {
|
||||
db: Arc::new(Mutex::new(new_db)),
|
||||
ba: Arc::new(Mutex::new(new_ba)),
|
||||
extra_bridges: Arc::new(Mutex::new(Vec::new())),
|
||||
to_be_replaced_bridges: Arc::new(Mutex::new(Vec::new())),
|
||||
};
|
||||
}
|
||||
(DB { db: lox_db }, context)
|
||||
}
|
||||
(lox_db, context)
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Unable to read or create lox database! {:?}", e);
|
||||
}
|
||||
};
|
||||
Ok((lox_db, context))
|
||||
Err(e) => {
|
||||
panic!("Unable to read or create lox database! {:?}", e);
|
||||
}
|
||||
};
|
||||
Ok((lox_db, context))
|
||||
}
|
||||
}
|
||||
|
||||
fn read_lox_context_from_db(
|
||||
|
@ -103,18 +109,12 @@ fn read_lox_context_from_db(
|
|||
fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
|
||||
let parsed_end =
|
||||
NaiveDateTime::parse_from_str(&date_range_end, "context_%Y-%m-%d_%H:%M:%S").unwrap();
|
||||
let dt = DateTime::<Utc>::from_utc(parsed_end, Utc);
|
||||
println!("WHat is datetime here: {:?}", dt);
|
||||
println!(
|
||||
"WHat is datetime - 1 subday here: {:?}",
|
||||
dt.with_timezone(&Utc).checked_sub_days(Days::new(1))
|
||||
);
|
||||
|
||||
let dt = DateTime::<Utc>::from_naive_utc_and_offset(parsed_end, Utc);
|
||||
dt.with_timezone(&Utc).checked_sub_days(Days::new(1))
|
||||
}
|
||||
|
||||
fn use_last_context(db: sled::Db) -> lox_context::LoxServerContext {
|
||||
let ivec_context = db.last().unwrap().unwrap();
|
||||
fn use_last_context(lox_db: sled::Db) -> lox_context::LoxServerContext {
|
||||
let ivec_context = lox_db.last().unwrap().unwrap();
|
||||
let ivec_date: String = serde_json::from_slice(&ivec_context.0).unwrap();
|
||||
println!("Using last context with date: {:?}", ivec_date);
|
||||
serde_json::from_slice(&ivec_context.1).unwrap()
|
||||
|
|
|
@ -17,7 +17,7 @@ use std::{
|
|||
};
|
||||
|
||||
mod db_handler;
|
||||
use db_handler::{open_new_or_existing_db, write_context_to_db};
|
||||
use db_handler::DB;
|
||||
mod lox_context;
|
||||
mod request_handler;
|
||||
use request_handler::handle;
|
||||
|
@ -151,7 +151,7 @@ async fn context_manager(
|
|||
roll_back_date: Option<String>,
|
||||
mut context_rx: mpsc::Receiver<Command>,
|
||||
) {
|
||||
let (lox_db, context) = match open_new_or_existing_db(db_config, roll_back_date) {
|
||||
let (mut lox_db, context) = match DB::open_new_or_existing_db(db_config, roll_back_date) {
|
||||
Ok((lox_db, context)) => (lox_db, context),
|
||||
Err(e) => {
|
||||
panic!("Error: {:?}", e);
|
||||
|
@ -303,7 +303,7 @@ async fn context_manager(
|
|||
*/
|
||||
context.allocate_leftover_bridges();
|
||||
context.encrypt_table();
|
||||
write_context_to_db(lox_db.clone(), context.clone());
|
||||
lox_db.write_context(context.clone());
|
||||
sleep(Duration::from_millis(1)).await;
|
||||
}
|
||||
Request { req, sender } => {
|
||||
|
|
Loading…
Reference in New Issue