Enable bgsave config to be read from the cfg file

Now, the `bgsave` key can be read and parsed from the config file
next
Sayan Nandan 4 years ago
parent 20d0b3bf1f
commit f2f0d2d4bd
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -0,0 +1,8 @@
# This is a bad toml file since it contains an invalid value of `every` in BGSAVE
[server]
host = "127.0.0.1"
port = 2003
[bgsave]
every = 0.5 # Not possible!
enabled = true

@ -12,8 +12,9 @@ port = 2003 # The port to which you want TDB to bind to
# Set `noart` to true if you want to disable terminal artwork
noart = false
# This key is *REQUIRED*
# This key is *OPTIONAL*, but will be required post 0.5.0
[bgsave]
# Run `BGSAVE` `every` seconds. For example, setting this to 60 will cause BGSAVE to run
# every 1 minute
enabled = true
every = 120

@ -35,6 +35,26 @@ use toml;
pub struct Config {
/// The `server` key
server: ServerConfig,
/// The `bgsave` key
bgsave: Option<BGSave>,
}
/// This struct represents the `bgsave` key in the TOML file
///
/* TODO(@ohsayan): As of now, we will keep this optional, but post 0.5.0,
* we will make it compulsory (so that we don't break semver)
* See the link below for more details:
* https://github.com/terrabasedb/terrabasedb/issues/21#issuecomment-693217709
*/
#[derive(Deserialize, Debug, PartialEq)]
pub struct BGSave {
/// Whether BGSAVE is enabled or not
///
/// If `enabled` is set to true, and an `every` field is also present, we will
/// display a warning, that the `every` key is unused
enabled: bool,
/// Every 'n' seconds
every: u64,
}
/// This struct represents the `server` key in the TOML file
@ -60,6 +80,10 @@ pub struct ParsedConfig {
port: u16,
/// If `noart` is set to true, no terminal artwork should be displayed
noart: bool,
/// Whether BGSAVE is enabled or not
bgsave_enabled: bool,
/// Run `BGSAVE` every _n_ seconds
bgsave_duration: u64,
}
impl ParsedConfig {
@ -77,6 +101,11 @@ impl ParsedConfig {
/// Create a `ParsedConfig` instance from a `Config` object, which is a parsed
/// TOML file (represented as an object)
const fn from_config(cfg: Config) -> Self {
let (bgsave_enabled, bgsave_duration) = if let Some(bgsave) = cfg.bgsave {
(bgsave.enabled, bgsave.every)
} else {
(true, 120)
};
ParsedConfig {
host: cfg.server.host,
port: cfg.server.port,
@ -85,6 +114,8 @@ impl ParsedConfig {
} else {
false
},
bgsave_enabled,
bgsave_duration,
}
}
/// Create a new file
@ -98,26 +129,44 @@ impl ParsedConfig {
host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
port,
noart: false,
bgsave_enabled: true,
bgsave_duration: 120,
}
}
/// Create a new `ParsedConfig` with the default `port` and `noart` settngs
/// and a supplied `host`
pub const fn default_with_host(host: IpAddr) -> Self {
ParsedConfig::new(host, 2003, false)
ParsedConfig::new(host, 2003, false, true, 120)
}
/// Create a new `ParsedConfig` with all the fields
pub const fn new(host: IpAddr, port: u16, noart: bool) -> Self {
ParsedConfig { host, port, noart }
pub const fn new(
host: IpAddr,
port: u16,
noart: bool,
bgsave_enabled: bool,
bgsave_duration: u64,
) -> Self {
ParsedConfig {
host,
port,
noart,
bgsave_enabled,
bgsave_duration,
}
}
/// Create a default `ParsedConfig` with:
/// Create a default `ParsedConfig` with the following setup defaults:
/// - `host`: 127.0.0.1
/// - `port` : 2003
/// - `noart` : false
/// - `bgsave_enabled` : true
/// - `bgsave_duration` : 120
pub const fn default() -> Self {
ParsedConfig {
host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
port: 2003,
noart: false,
bgsave_enabled: true,
bgsave_duration: 120,
}
}
/// Return a (host, port) tuple which can be bound to with `TcpListener`
@ -140,14 +189,7 @@ fn test_config_toml_okayport() {
"#
.to_owned();
let cfg = ParsedConfig::new_from_toml_str(file).unwrap();
assert_eq!(
cfg,
ParsedConfig {
port: 2003,
host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
noart: false,
}
);
assert_eq!(cfg, ParsedConfig::default(),);
}
/// Gets a `toml` file from `WORKSPACEROOT/examples/config-files`
#[cfg(test)]
@ -258,6 +300,8 @@ fn test_config_file_noart() {
port: 2003,
host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
noart: true,
bgsave_enabled: true,
bgsave_duration: 120,
}
);
}
@ -273,6 +317,24 @@ fn test_config_file_ipv6() {
port: 2003,
host: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)),
noart: false,
bgsave_enabled: true,
bgsave_duration: 120,
}
);
}
#[test]
#[cfg(test)]
fn test_config_file_template() {
let file = get_toml_from_examples_dir("template.toml".to_owned()).unwrap();
let cfg = ParsedConfig::new_from_toml_str(file).unwrap();
assert_eq!(cfg, ParsedConfig::default());
}
#[test]
#[cfg(test)]
fn test_config_file_bad_bgsave_section() {
let file = get_toml_from_examples_dir("badcfg2.toml".to_owned()).unwrap();
let cfg = ParsedConfig::new_from_toml_str(file);
assert!(cfg.is_err());
}

Loading…
Cancel
Save