From 6f435481aebd8e6952c45a22ad470249629addb5 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Sat, 5 Feb 2022 20:16:03 +0800 Subject: [PATCH 01/18] shortcut commands for developer --- createdb.sh | 1 + debug.sh | 2 ++ src/lib/db_ops.rs | 1 + 3 files changed, 4 insertions(+) create mode 100755 createdb.sh create mode 100755 debug.sh diff --git a/createdb.sh b/createdb.sh new file mode 100755 index 0000000..4a35c3a --- /dev/null +++ b/createdb.sh @@ -0,0 +1 @@ +cargo run -- -u mateus -p mateus create-db -d todos -t tokenxyz \ No newline at end of file diff --git a/debug.sh b/debug.sh new file mode 100755 index 0000000..e533036 --- /dev/null +++ b/debug.sh @@ -0,0 +1,2 @@ +export NUN_DBS_DIR=/tmp/data/nun_db +cargo run -- -u mateus -p mateus start --http-address 0.0.0.0:3013 --tcp-address 0.0.0.0:3014 --ws-address 0.0.0.0:3012 diff --git a/src/lib/db_ops.rs b/src/lib/db_ops.rs index da5f2f2..738cc78 100644 --- a/src/lib/db_ops.rs +++ b/src/lib/db_ops.rs @@ -94,6 +94,7 @@ pub fn create_db(name: &String, token: &String, dbs: &Arc, client: &C } } } + pub fn snapshot_db(db: &Database, dbs: &Databases) -> Response { let name = db.name.clone(); { From 9b741985218e9645d51a2273237ae39b64b26f16 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Mon, 7 Feb 2022 23:50:04 +0800 Subject: [PATCH 02/18] shortcut commands for developer --- snapshot.sh | 1 + 1 file changed, 1 insertion(+) create mode 100755 snapshot.sh diff --git a/snapshot.sh b/snapshot.sh new file mode 100755 index 0000000..507d9f4 --- /dev/null +++ b/snapshot.sh @@ -0,0 +1 @@ +cargo run -- -u mateus -p mateus exec "use-db todos tokenxyz; snapshot;" \ No newline at end of file From 8f77f2707c7648241e2d2c68815153530fa1de66 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Mon, 7 Feb 2022 23:51:34 +0800 Subject: [PATCH 03/18] How-to snapshot --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c943154..9cff8fe 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ Done you now have nun-db running in your docker and exposing all the ports to be * [How to create your simple version of google analytics real-time using Nun-db](https://mateusfreira.github.io/@mateusfreira-create-a-simple-verison-of-google-analytics-realtime-using-nun-db/) * [NunDb How to backup one or all databases](https://mateusfreira.github.io/@mateusfreira-nundb-how-to-backups-all-databases-with-one-command/) - +* [To snapshot the database from memory to disk] +nun-db --user $NUN_USER -p $NUN_PWD --host "https://http.nundb.org" exec "use-db $DB_NAME $DB_TOKEN; snapshot" ## Technical documentations From 689839a92e43ef4fc6204fdf26521d17eaf21be2 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 01:09:05 +0800 Subject: [PATCH 04/18] configuration --- src/lib/commad_line/commands.rs | 4 +-- src/lib/configuration.rs | 43 +++++++++++++++++++++++++++++++++ src/lib/disk_ops.rs | 11 +++++---- src/lib/mod.rs | 1 + src/main.rs | 28 ++++++++++++--------- 5 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 src/lib/configuration.rs diff --git a/src/lib/commad_line/commands.rs b/src/lib/commad_line/commands.rs index 605994a..302af10 100644 --- a/src/lib/commad_line/commands.rs +++ b/src/lib/commad_line/commands.rs @@ -10,14 +10,14 @@ pub fn prepare_args<'a>() -> ArgMatches<'static> { Arg::with_name("user") .short("u") .long("user") - .required(true) + //.required(false) .takes_value(true) .help("Admin username"), ) .arg( Arg::with_name("pwd") .short("p") - .required(true) + //.required(false) .takes_value(true) .help("Admin password"), ) diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs new file mode 100644 index 0000000..91f0f63 --- /dev/null +++ b/src/lib/configuration.rs @@ -0,0 +1,43 @@ +use std::env; + +#[derive(Debug)] +pub struct Configuration { + pub nun_user: String, + pub nun_pwd: String, + pub nun_dbs_dir: String, + pub nun_ws_addr: String, + pub nun_http_addr: String, + pub nun_tcp_addr: String, + pub nun_replicate_addr: String, + pub nun_log_level: String, + pub nun_log_dir: String, + pub nun_log_to_file: String, +} + +#[cfg(debug_assertions)] +fn expect_env_var(name: &str, default: &str) -> String { + return env::var(name).unwrap_or(String::from(default)); +} + +#[cfg(not(debug_assertions))] +fn expect_env_var(name: &str, _default: &str) -> String { + return env::var(name).expect(&format!( + "Environment variable {name} is not defined", + name = name + )); +} + +pub fn get_configuration() -> Configuration { + Configuration { + nun_user: expect_env_var("NUN_USER", "mateus"), + nun_pwd: expect_env_var("NUN_PWD", "mateus"), + nun_dbs_dir: expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db"), + nun_ws_addr: expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012"), + nun_http_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013"), + nun_tcp_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3014"), + nun_replicate_addr: expect_env_var("NUN_REPLICATE_ADDR", ""), + nun_log_level: expect_env_var("NUN_LOG_LEVEL", "Info"), //(Off, Error, Warn, Info, Debug, Trace) + nun_log_dir: expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log"), + nun_log_to_file: expect_env_var("NUN_LOG_TO_FILE", "true"), //true or false + } +} \ No newline at end of file diff --git a/src/lib/disk_ops.rs b/src/lib/disk_ops.rs index 7a36488..c250ad2 100644 --- a/src/lib/disk_ops.rs +++ b/src/lib/disk_ops.rs @@ -17,10 +17,11 @@ use std::time::Instant; use crate::bo::*; +use super::configuration::get_configuration; + const SNAPSHOT_TIME: i64 = 3000; // 30 secounds const FILE_NAME: &'static str = "-nun.data"; const META_FILE_NAME: &'static str = "-nun.madadata"; -const DIR_NAME: &'static str = "dbs"; const KEYS_FILE: &'static str = "keys-nun.keys"; @@ -34,10 +35,7 @@ const OP_OP_SIZE: usize = 1; const OP_RECORD_SIZE: usize = OP_TIME_SIZE + OP_DB_ID_SIZE + OP_KEY_SIZE + OP_OP_SIZE; pub fn get_dir_name() -> String { - match env::var_os("NUN_DBS_DIR") { - Some(dir_name) => dir_name.into_string().unwrap(), - None => DIR_NAME.to_string(), - } + get_configuration().nun_dbs_dir } pub fn load_keys_map_from_disk() -> HashMap { @@ -204,7 +202,10 @@ pub fn start_snap_shot_timer(timer: timer::Timer, dbs: Arc) { pub fn snapshot_all_pendding_dbs(dbs: &Arc) { let queue_len = { dbs.to_snapshot.read().unwrap().len() }; + println!("called snapshot_all_pendding_dbs"); + println!("queue_len > 0 == {}", queue_len); if queue_len > 0 { + println!("inside queue_len"); snapshot_keys(&dbs); let mut dbs_to_snapshot = { let dbs = dbs.to_snapshot.write().unwrap(); diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 6d1e9b0..7c81d07 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -1,3 +1,4 @@ +pub mod configuration; pub mod bo; pub mod commad_line; pub mod db_ops; diff --git a/src/main.rs b/src/main.rs index a362ec0..71c7f07 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use futures::channel::mpsc::{channel, Receiver, Sender}; use futures::executor::block_on; use futures::join; use lib::*; +use lib::configuration::Configuration; use log; use signal_hook::{consts::SIGINT, iterator::Signals}; use std::thread; @@ -13,8 +14,8 @@ use std::sync::Arc; use clap::ArgMatches; use env_logger::{Builder, Env, Target}; -fn init_logger() { - let env = Env::default().filter_or("NUN_LOG_LEVEL", "info"); +fn init_logger(config: &Configuration) { + let env = Env::default().filter_or(config.nun_log_level.as_str(), "info"); Builder::from_env(env) .format_level(false) .target(Target::Stdout) @@ -23,21 +24,23 @@ fn init_logger() { } fn main() -> Result<(), String> { - init_logger(); + let config = configuration::get_configuration(); + + init_logger(&config); log::info!("nundb starting!"); let matches: ArgMatches<'_> = lib::commad_line::commands::prepare_args(); if let Some(start_match) = matches.subcommand_matches("start") { return start_db( - matches.value_of("user").unwrap(), - matches.value_of("pwd").unwrap(), - start_match - .value_of("tcp-address") - .unwrap_or("0.0.0.0:3014"), - start_match.value_of("ws-address").unwrap_or("0.0.0.0:3012"), + matches.value_of("user").unwrap_or(config.nun_user.as_str()), + matches.value_of("pwd").unwrap_or(config.nun_pwd.as_str()), + start_match.value_of("ws-address").unwrap_or(config.nun_ws_addr.as_str()), start_match .value_of("http-address") - .unwrap_or("0.0.0.0:3013"), - start_match.value_of("replicate-address").unwrap_or(""), + .unwrap_or(config.nun_http_addr.as_str()), + start_match + .value_of("tcp-address") + .unwrap_or(config.nun_tcp_addr.as_str()), + start_match.value_of("replicate-address").unwrap_or(config.nun_replicate_addr.as_str()), ); } else { return lib::commad_line::commands::exec_command(&matches); @@ -47,11 +50,12 @@ fn main() -> Result<(), String> { fn start_db( user: &str, pwd: &str, - tcp_address: &str, ws_address: &str, http_address: &str, + tcp_address: &str, replicate_address: &str, ) -> Result<(), String> { + let (replication_sender, replication_receiver): (Sender, Receiver) = channel(100); From 1b4eaab11f5f67b6567f35c82e74beede13b18a2 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 07:32:17 +0800 Subject: [PATCH 05/18] Update commands.rs --- src/lib/commad_line/commands.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/commad_line/commands.rs b/src/lib/commad_line/commands.rs index 302af10..1e6960c 100644 --- a/src/lib/commad_line/commands.rs +++ b/src/lib/commad_line/commands.rs @@ -10,14 +10,12 @@ pub fn prepare_args<'a>() -> ArgMatches<'static> { Arg::with_name("user") .short("u") .long("user") - //.required(false) .takes_value(true) .help("Admin username"), ) .arg( Arg::with_name("pwd") .short("p") - //.required(false) .takes_value(true) .help("Admin password"), ) From 910bf5bb4ac03da21f32d5d173d0d3d76c6ca030 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 07:41:10 +0800 Subject: [PATCH 06/18] Update disk_ops.rs --- src/lib/disk_ops.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/disk_ops.rs b/src/lib/disk_ops.rs index 6fb6867..ea128e0 100644 --- a/src/lib/disk_ops.rs +++ b/src/lib/disk_ops.rs @@ -202,10 +202,8 @@ pub fn start_snap_shot_timer(timer: timer::Timer, dbs: Arc) { pub fn snapshot_all_pendding_dbs(dbs: &Arc) { let queue_len = { dbs.to_snapshot.read().unwrap().len() }; - println!("called snapshot_all_pendding_dbs"); - println!("queue_len > 0 == {}", queue_len); + log::debug!("snapshot_all_pendding_dbs | queue_len == {}", queue_len); if queue_len > 0 { - println!("inside queue_len"); snapshot_keys(&dbs); let mut dbs_to_snapshot = { let dbs = dbs.to_snapshot.write().unwrap(); From 7303c9435764066a1f3c1de4b205944000f03258 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 07:43:28 +0800 Subject: [PATCH 07/18] Update main.rs --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71c7f07..cade83a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -12,11 +12,10 @@ use std::thread; use std::sync::Arc; use clap::ArgMatches; -use env_logger::{Builder, Env, Target}; +use env_logger::{Builder, Target}; fn init_logger(config: &Configuration) { - let env = Env::default().filter_or(config.nun_log_level.as_str(), "info"); - Builder::from_env(env) + Builder::from_env(config.nun_log_level.as_str()) .format_level(false) .target(Target::Stdout) .format_timestamp_nanos() From d8ea40ec90e0829c5dc56036ac9b3c0baa944023 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 08:29:23 +0800 Subject: [PATCH 08/18] test added --- src/lib/configuration.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs index 91f0f63..8e3ffde 100644 --- a/src/lib/configuration.rs +++ b/src/lib/configuration.rs @@ -40,4 +40,22 @@ pub fn get_configuration() -> Configuration { nun_log_dir: expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log"), nun_log_to_file: expect_env_var("NUN_LOG_TO_FILE", "true"), //true or false } -} \ No newline at end of file +} + + +#[cfg(test)] +mod tests { + use crate::lib::configuration::{get_configuration}; + + #[test] + fn run_mode_should_get_empty_but_debug_mode_got_value() { + let config = get_configuration(); + + #[cfg(debug_assertions)] + assert_eq!(config.nun_user, "mateus".to_string()); + + #[cfg(not(debug_assertions))] + assert_eq!(config.nun_user, "".to_string()) + } + +} From 5767cd95f72892a86a98c8bd6857dee212fedea0 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 08:50:01 +0800 Subject: [PATCH 09/18] Update disk_ops.rs --- src/lib/disk_ops.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/disk_ops.rs b/src/lib/disk_ops.rs index ea128e0..643b4b8 100644 --- a/src/lib/disk_ops.rs +++ b/src/lib/disk_ops.rs @@ -1,6 +1,5 @@ use log; use std::collections::HashMap; -use std::env; use std::fs; use std::fs::OpenOptions; use std::fs::{create_dir_all, read_dir, File}; From a2de2f5f96c01ebb1530006d8e20319e538f4639 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 09:09:42 +0800 Subject: [PATCH 10/18] Update main.rs --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index cade83a..e38dae2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,8 @@ use clap::ArgMatches; use env_logger::{Builder, Target}; fn init_logger(config: &Configuration) { - Builder::from_env(config.nun_log_level.as_str()) + let env = Env::default().filter_or("NUN_LOG_LEVEL", config.nun_log_level.as_str()); + Builder::from_env(env) .format_level(false) .target(Target::Stdout) .format_timestamp_nanos() From 7d9d2cfb80bedc37c893212b095cd519eb9d042a Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 09:13:49 +0800 Subject: [PATCH 11/18] Update main.rs --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e38dae2..88aa507 100755 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use std::thread; use std::sync::Arc; use clap::ArgMatches; -use env_logger::{Builder, Target}; +use env_logger::{Builder, Env, Target}; fn init_logger(config: &Configuration) { let env = Env::default().filter_or("NUN_LOG_LEVEL", config.nun_log_level.as_str()); From 1abcff7d3d9187c39a7284738666b0b4a7e51053 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 09:18:40 +0800 Subject: [PATCH 12/18] added NUN_USER and NUN_PWD conditional checking will exit if not provided. --- src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main.rs b/src/main.rs index 88aa507..c9c4b5c 100755 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,11 @@ fn init_logger(config: &Configuration) { fn main() -> Result<(), String> { let config = configuration::get_configuration(); + if config.nun_user.as_str() == "" || config.nun_pwd == "" { + println!("NUN_USER and NUN_PWD must be provided."); + std::process::exit(0); + } + init_logger(&config); log::info!("nundb starting!"); let matches: ArgMatches<'_> = lib::commad_line::commands::prepare_args(); From 200a86eebe4d464865acaecbe1facd15cbe3c90b Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 19:32:46 +0800 Subject: [PATCH 13/18] move to test folder --- createdb.sh => tests/createdb.sh | 0 debug.sh => tests/debug.sh | 0 snapshot.sh => tests/snapshot.sh | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename createdb.sh => tests/createdb.sh (100%) rename debug.sh => tests/debug.sh (100%) rename snapshot.sh => tests/snapshot.sh (100%) diff --git a/createdb.sh b/tests/createdb.sh similarity index 100% rename from createdb.sh rename to tests/createdb.sh diff --git a/debug.sh b/tests/debug.sh similarity index 100% rename from debug.sh rename to tests/debug.sh diff --git a/snapshot.sh b/tests/snapshot.sh similarity index 100% rename from snapshot.sh rename to tests/snapshot.sh From ea50105f1a1f7c9770653971ce9c716910c1467e Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 19:34:51 +0800 Subject: [PATCH 14/18] NUN_TCP_ADDR fix --- src/lib/configuration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs index 8e3ffde..6a17259 100644 --- a/src/lib/configuration.rs +++ b/src/lib/configuration.rs @@ -34,7 +34,7 @@ pub fn get_configuration() -> Configuration { nun_dbs_dir: expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db"), nun_ws_addr: expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012"), nun_http_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013"), - nun_tcp_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3014"), + nun_tcp_addr: expect_env_var("NUN_TCP_ADDR", "0.0.0.0:3014"), nun_replicate_addr: expect_env_var("NUN_REPLICATE_ADDR", ""), nun_log_level: expect_env_var("NUN_LOG_LEVEL", "Info"), //(Off, Error, Warn, Info, Debug, Trace) nun_log_dir: expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log"), From 0f18872cdd89ccf147a0f2eafeb6d4970eec298b Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Tue, 8 Feb 2022 22:12:25 +0800 Subject: [PATCH 15/18] changed to use lazy_static --- .vscode/launch.json | 16 +++++++++++ Cargo.toml | 1 + src/lib/configuration.rs | 62 ++++++++++++++++------------------------ src/lib/disk_ops.rs | 7 +++-- src/main.rs | 28 ++++++++---------- 5 files changed, 58 insertions(+), 56 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..224af64 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 50d56c2..69d8413 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ clap = "2.34.0" reqwest = { version="*" , features = ["blocking"]} signal-hook = "0.3.9" atomic_float = "0.1.0" +lazy_static = "1.4.0" [dev-dependencies] tokio-test = "*" diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs index 6a17259..fb853d3 100644 --- a/src/lib/configuration.rs +++ b/src/lib/configuration.rs @@ -1,61 +1,49 @@ use std::env; - -#[derive(Debug)] -pub struct Configuration { - pub nun_user: String, - pub nun_pwd: String, - pub nun_dbs_dir: String, - pub nun_ws_addr: String, - pub nun_http_addr: String, - pub nun_tcp_addr: String, - pub nun_replicate_addr: String, - pub nun_log_level: String, - pub nun_log_dir: String, - pub nun_log_to_file: String, +use lazy_static::lazy_static; + +lazy_static! { + pub static ref NUN_USER: String = expect_env_var("NUN_USER", "mateus", false); + pub static ref NUN_PWD: String = expect_env_var("NUN_PWD", "mateus", false); + pub static ref NUN_DBS_DIR: String = expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db", false); + pub static ref NUN_WS_ADDR: String = expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012", false); + pub static ref NUN_HTTP_ADDR: String = expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013", false); + pub static ref NUN_TCP_ADDR: String = expect_env_var("NUN_TCP_ADDR", "0.0.0.0:3014", false); + pub static ref NUN_REPLICATE_ADDR: String = expect_env_var("NUN_REPLICATE_ADDR", "", false); + pub static ref NUN_LOG_LEVEL: String = expect_env_var("NUN_LOG_LEVEL", "Info", false); //(Off, Error, Warn, Info, Debug, Trace) + pub static ref NUN_LOG_DIR: String = expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log", false); + pub static ref NUN_LOG_TO_FILE: String = expect_env_var("NUN_LOG_TO_FILE", "true", false); //true or false } #[cfg(debug_assertions)] -fn expect_env_var(name: &str, default: &str) -> String { +fn expect_env_var(name: &str, default: &str, _required: bool) -> String { return env::var(name).unwrap_or(String::from(default)); } #[cfg(not(debug_assertions))] -fn expect_env_var(name: &str, _default: &str) -> String { - return env::var(name).expect(&format!( - "Environment variable {name} is not defined", - name = name - )); -} - -pub fn get_configuration() -> Configuration { - Configuration { - nun_user: expect_env_var("NUN_USER", "mateus"), - nun_pwd: expect_env_var("NUN_PWD", "mateus"), - nun_dbs_dir: expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db"), - nun_ws_addr: expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012"), - nun_http_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013"), - nun_tcp_addr: expect_env_var("NUN_TCP_ADDR", "0.0.0.0:3014"), - nun_replicate_addr: expect_env_var("NUN_REPLICATE_ADDR", ""), - nun_log_level: expect_env_var("NUN_LOG_LEVEL", "Info"), //(Off, Error, Warn, Info, Debug, Trace) - nun_log_dir: expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log"), - nun_log_to_file: expect_env_var("NUN_LOG_TO_FILE", "true"), //true or false +fn expect_env_var(name: &str, _default: &str, required: bool) -> String { + if required { + return env::var(name).expect(&format!( + "Environment variable {name} is not defined", + name = name + )); + } else { + return env::var(name).unwrap_or(""); } } #[cfg(test)] mod tests { - use crate::lib::configuration::{get_configuration}; + use crate::lib::configuration::expect_env_var; #[test] fn run_mode_should_get_empty_but_debug_mode_got_value() { - let config = get_configuration(); #[cfg(debug_assertions)] - assert_eq!(config.nun_user, "mateus".to_string()); + assert_eq!(expect_env_var("NUN_USER", "mateus", false), "mateus".to_string()); #[cfg(not(debug_assertions))] - assert_eq!(config.nun_user, "".to_string()) + assert_eq!(expect_env_var("NUN_USER", "", false), "".to_string()) } } diff --git a/src/lib/disk_ops.rs b/src/lib/disk_ops.rs index 643b4b8..97b4e3b 100644 --- a/src/lib/disk_ops.rs +++ b/src/lib/disk_ops.rs @@ -16,8 +16,6 @@ use std::time::Instant; use crate::bo::*; -use super::configuration::get_configuration; - const SNAPSHOT_TIME: i64 = 3000; // 30 secounds const FILE_NAME: &'static str = "-nun.data"; const META_FILE_NAME: &'static str = "-nun.madadata"; @@ -33,8 +31,11 @@ const OP_TIME_SIZE: usize = 8; const OP_OP_SIZE: usize = 1; const OP_RECORD_SIZE: usize = OP_TIME_SIZE + OP_DB_ID_SIZE + OP_KEY_SIZE + OP_OP_SIZE; + +use crate::lib::configuration::{NUN_DBS_DIR}; + pub fn get_dir_name() -> String { - get_configuration().nun_dbs_dir + NUN_DBS_DIR.to_string() } pub fn load_keys_map_from_disk() -> HashMap { diff --git a/src/main.rs b/src/main.rs index c9c4b5c..14f47c8 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use futures::channel::mpsc::{channel, Receiver, Sender}; use futures::executor::block_on; use futures::join; use lib::*; -use lib::configuration::Configuration; use log; use signal_hook::{consts::SIGINT, iterator::Signals}; use std::thread; @@ -14,8 +13,11 @@ use std::sync::Arc; use clap::ArgMatches; use env_logger::{Builder, Env, Target}; -fn init_logger(config: &Configuration) { - let env = Env::default().filter_or("NUN_LOG_LEVEL", config.nun_log_level.as_str()); +use crate::lib::configuration::{NUN_LOG_LEVEL, NUN_USER, NUN_PWD, NUN_WS_ADDR, NUN_HTTP_ADDR, NUN_TCP_ADDR, NUN_REPLICATE_ADDR}; + +fn init_logger() { + + let env = Env::default().filter_or("NUN_LOG_LEVEL", NUN_LOG_LEVEL.as_str()); Builder::from_env(env) .format_level(false) .target(Target::Stdout) @@ -24,28 +26,22 @@ fn init_logger(config: &Configuration) { } fn main() -> Result<(), String> { - let config = configuration::get_configuration(); - - if config.nun_user.as_str() == "" || config.nun_pwd == "" { - println!("NUN_USER and NUN_PWD must be provided."); - std::process::exit(0); - } - init_logger(&config); + init_logger(); log::info!("nundb starting!"); let matches: ArgMatches<'_> = lib::commad_line::commands::prepare_args(); if let Some(start_match) = matches.subcommand_matches("start") { return start_db( - matches.value_of("user").unwrap_or(config.nun_user.as_str()), - matches.value_of("pwd").unwrap_or(config.nun_pwd.as_str()), - start_match.value_of("ws-address").unwrap_or(config.nun_ws_addr.as_str()), + matches.value_of("user").unwrap_or(NUN_USER.as_str()), + matches.value_of("pwd").unwrap_or(NUN_PWD.as_str()), + start_match.value_of("ws-address").unwrap_or(NUN_WS_ADDR.as_str()), start_match .value_of("http-address") - .unwrap_or(config.nun_http_addr.as_str()), + .unwrap_or(NUN_HTTP_ADDR.as_str()), start_match .value_of("tcp-address") - .unwrap_or(config.nun_tcp_addr.as_str()), - start_match.value_of("replicate-address").unwrap_or(config.nun_replicate_addr.as_str()), + .unwrap_or(NUN_TCP_ADDR.as_str()), + start_match.value_of("replicate-address").unwrap_or(NUN_REPLICATE_ADDR.as_str()), ); } else { return lib::commad_line::commands::exec_command(&matches); From ebbeb96f2c1e6a2d36e5dd1272d9bceec2fa93cf Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Wed, 9 Feb 2022 11:24:01 +0800 Subject: [PATCH 16/18] missing to_string() --- src/lib/configuration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs index fb853d3..307a69e 100644 --- a/src/lib/configuration.rs +++ b/src/lib/configuration.rs @@ -27,7 +27,7 @@ fn expect_env_var(name: &str, _default: &str, required: bool) -> String { name = name )); } else { - return env::var(name).unwrap_or(""); + return env::var(name).unwrap_or("".to_string()); } } From 0ebc689a0fb0cb310625b17beaf4c78c0763a699 Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Thu, 10 Feb 2022 18:33:39 +0800 Subject: [PATCH 17/18] remove launch.json --- .vscode/launch.json | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 224af64..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Debug", - "program": "${workspaceFolder}/", - "args": [], - "cwd": "${workspaceFolder}" - } - ] -} \ No newline at end of file From b351687612897cfc706a8518b035ca2394e9e5fa Mon Sep 17 00:00:00 2001 From: Ken Chong Date: Thu, 10 Feb 2022 18:47:45 +0800 Subject: [PATCH 18/18] change to dbs --- src/lib/configuration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/configuration.rs b/src/lib/configuration.rs index 307a69e..3a2c383 100644 --- a/src/lib/configuration.rs +++ b/src/lib/configuration.rs @@ -4,7 +4,7 @@ use lazy_static::lazy_static; lazy_static! { pub static ref NUN_USER: String = expect_env_var("NUN_USER", "mateus", false); pub static ref NUN_PWD: String = expect_env_var("NUN_PWD", "mateus", false); - pub static ref NUN_DBS_DIR: String = expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db", false); + pub static ref NUN_DBS_DIR: String = expect_env_var("NUN_DBS_DIR", "dbs", false); pub static ref NUN_WS_ADDR: String = expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012", false); pub static ref NUN_HTTP_ADDR: String = expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013", false); pub static ref NUN_TCP_ADDR: String = expect_env_var("NUN_TCP_ADDR", "0.0.0.0:3014", false);