From b73bddf7fc54b75922c986e7c9f1ef97d548148e Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 23 Jul 2020 14:27:37 +0300 Subject: [PATCH] feat: implement flush collection command --- src/channel.rs | 4 ++++ src/commands/flush.rs | 47 +++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 7 +++++++ 3 files changed, 58 insertions(+) diff --git a/src/channel.rs b/src/channel.rs index 50b156d..0987e2a 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -180,6 +180,10 @@ impl SonicChannel { text: &'a str, locale: Option<&'a str>, ); + + use FlushCommand for fn flushc<'a>( + collection: &'a str, + ); } #[cfg(feature = "search")] diff --git a/src/commands/flush.rs b/src/commands/flush.rs index e69de29..ef6ef63 100644 --- a/src/commands/flush.rs +++ b/src/commands/flush.rs @@ -0,0 +1,47 @@ +use super::StreamCommand; +use crate::result::{Error, ErrorKind, Result}; +use regex::Regex; + +const RE_QUERY_RECEIVED_MESSAGE: &str = r"^RESULT (?P\d+)\r\n$"; +#[derive(Debug, Default)] +pub struct FlushCommand<'a> { + pub collection: &'a str, + pub bucket: Option<&'a str>, + pub object: Option<&'a str>, +} + +impl StreamCommand for FlushCommand<'_> { + type Response = usize; + + fn message(&self) -> String { + let mut message = match (self.bucket, self.object) { + (Some(bucket), Some(object)) => { + format!("FLUSHO {} {} {}", self.collection, bucket, object) + } + (Some(bucket), None) => format!("FLUSHB {} {}", self.collection, bucket), + (None, None) => format!("FLUSHC {}", self.collection), + _ => panic!("Invalid flush command"), + }; + message.push_str("\r\n"); + message + } + + fn receive(&self, message: String) -> Result { + lazy_static! { + static ref RE: Regex = Regex::new(RE_QUERY_RECEIVED_MESSAGE).unwrap(); + } + + dbg!(&message); + + match RE.captures(&message) { + None => Err(Error::new(ErrorKind::QueryResponseError( + "Sonic response are wrong. Please write issue to github.", + ))), + Some(caps) => caps["flush_count"].parse().map_err(|_| { + Error::new(ErrorKind::QueryResponseError( + "Cannot parse sonic response to uint", + )) + }), + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4c82d7d..0c81332 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,8 +3,11 @@ mod start; mod ping; +#[cfg(feature = "ingest")] +mod flush; #[cfg(feature = "ingest")] mod push; + #[cfg(feature = "search")] mod query; #[cfg(feature = "search")] @@ -14,8 +17,12 @@ pub use quit::QuitCommand; pub use start::StartCommand; pub use ping::PingCommand; + +#[cfg(feature = "ingest")] +pub use flush::FlushCommand; #[cfg(feature = "ingest")] pub use push::PushCommand; + #[cfg(feature = "search")] pub use query::QueryCommand; #[cfg(feature = "search")]