feat: add pop command to ingest mode

This commit is contained in:
Dmitriy Pleshevskiy 2020-07-28 22:38:02 +03:00
parent 31053815d7
commit 04f52bb076
3 changed files with 55 additions and 0 deletions

View file

@ -220,6 +220,21 @@ impl SonicChannel {
locale: &'a str => Some(locale), locale: &'a str => Some(locale),
); );
#[doc=r#"
Pop search data from the index. Returns removed words count as u32 type.
```rust
let result = ingest_channel.pop("search", "default", "recipe:295", "cake")?;
dbg!(result);
```
"#]
use PopCommand for fn pop<'a>(
collection: &'a str,
bucket: &'a str,
object: &'a str,
text: &'a str,
);
#[doc="Flush all indexed data from collections."] #[doc="Flush all indexed data from collections."]
use FlushCommand for fn flushc<'a>( use FlushCommand for fn flushc<'a>(
collection: &'a str, collection: &'a str,

View file

@ -6,6 +6,8 @@ mod ping;
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
mod flush; mod flush;
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
mod pop;
#[cfg(feature = "ingest")]
mod push; mod push;
#[cfg(feature = "search")] #[cfg(feature = "search")]
@ -22,6 +24,8 @@ pub use ping::PingCommand;
pub use flush::FlushCommand; pub use flush::FlushCommand;
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
pub use push::PushCommand; pub use push::PushCommand;
#[cfg(feature = "ingest")]
pub use pop::PopCommand;
#[cfg(feature = "search")] #[cfg(feature = "search")]
pub use query::QueryCommand; pub use query::QueryCommand;

36
src/commands/pop.rs Normal file
View file

@ -0,0 +1,36 @@
use super::StreamCommand;
use crate::result::{Error, ErrorKind, Result};
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct PopCommand<'a> {
pub collection: &'a str,
pub bucket: &'a str,
pub object: &'a str,
pub text: &'a str,
}
impl StreamCommand for PopCommand<'_> {
type Response = u32;
fn message(&self) -> String {
let mut message = format!(
r#"POP {} {} {} "{}""#,
self.collection, self.bucket, self.object, self.text
);
message.push_str("\r\n");
message
}
fn receive(&self, message: String) -> Result<Self::Response> {
if message.starts_with("RESULT") {
let count = message.split_whitespace().last().unwrap_or_default();
count
.parse()
.map_err(|_| Error::new(ErrorKind::QueryResponseError("Cannot parse pop count")))
} else {
Err(Error::new(ErrorKind::QueryResponseError("Cannot parse result")))
}
}
}