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