From b431d82367486d930b8ff7c4de3b4c03eb345427 Mon Sep 17 00:00:00 2001 From: Ricky Han Date: Sun, 17 Dec 2017 03:30:57 -0500 Subject: [PATCH] refactor: move range parser to parser module --- src/bin/server/handler.rs | 24 ++---------------------- src/bin/server/parser.rs | 22 ++++++++++++++++++++++ src/bin/server/state.rs | 8 +++++++- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/bin/server/handler.rs b/src/bin/server/handler.rs index cf79263a..47281366 100644 --- a/src/bin/server/handler.rs +++ b/src/bin/server/handler.rs @@ -101,7 +101,7 @@ pub fn gen_response (string : &str, state: &mut State) -> ReturnType { Exists(dbname.to_owned()) } else - if string.starts_with("ADD ") { + if string.starts_with("ADD ") || string.starts_with("INSERT ") { let parsed = if string.contains(" INTO ") { parser::parse_add_into(&string) } else { @@ -122,27 +122,7 @@ pub fn gen_response (string : &str, state: &mut State) -> ReturnType { let count : Vec<&str> = count.split(" ").collect(); let count = count[0].parse::().unwrap(); - let ranged = string.contains(" FROM "); - let range = if ranged { - // range to query - let from_epoch = string.clone()[(string.find(" FROM ").unwrap()+6)..] - .split(" ") - .collect::>() - [0] - .parse::() - .unwrap() - * 1000; - let to_epoch = string.clone()[(string.find(" TO ").unwrap()+4)..] - .split(" ") - .collect::>() - [0] - .parse::() - .unwrap() - * 1000; - Some((from_epoch, to_epoch)) - } else { - None - }; + let range = parser::parse_get_range(string); // test if json let format = if string.contains(" AS JSON") { GetFormat::JSON } else { GetFormat::DTF }; diff --git a/src/bin/server/parser.rs b/src/bin/server/parser.rs index 1ee961de..8a451b21 100644 --- a/src/bin/server/parser.rs +++ b/src/bin/server/parser.rs @@ -61,6 +61,28 @@ pub fn parse_add_into(string: &str) -> (Option, Option) { } } +pub fn parse_get_range(string: &str) -> Option<(u64, u64)> { + if string.contains(" FROM ") { + // range to query + let from_epoch = string.clone()[(string.find(" FROM ").unwrap()+6)..] + .split(" ") + .collect::>() + [0] + .parse::() + .unwrap() + * 1000; + let to_epoch = string.clone()[(string.find(" TO ").unwrap()+4)..] + .split(" ") + .collect::>() + [0] + .parse::() + .unwrap() + * 1000; + Some((from_epoch, to_epoch)) + } else { + None + } +} #[cfg(test)] mod tests { diff --git a/src/bin/server/state.rs b/src/bin/server/state.rs index c6a2ab9e..4543c63a 100644 --- a/src/bin/server/state.rs +++ b/src/bin/server/state.rs @@ -403,7 +403,13 @@ impl State { let result = ups_from_fs; match count { - ReqCount::Count(c) => return self._return_aux(&result[..c as usize], format), + ReqCount::Count(c) => { + if result.len() >= c as usize { + return self._return_aux(&result[..(c as usize -1)], format); + } else { + return Some(ReturnType::Error(format!("Requested {} but only have {}.", c, result.len()))) + } + } ReqCount::All => self._return_aux(&result, format), } }