feat: implement flush collection command

This commit is contained in:
Dmitriy Pleshevskiy 2020-07-23 14:27:37 +03:00
parent b24ad5a402
commit b73bddf7fc
3 changed files with 58 additions and 0 deletions

View file

@ -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")]

View file

@ -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<flush_count>\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<Self::Response> {
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",
))
}),
}
}
}

View file

@ -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")]