doc: write examples for ingest commands

This commit is contained in:
Dmitriy Pleshevskiy 2020-08-01 10:52:22 +03:00
parent 18ec91a983
commit 7e19f5b8d1
4 changed files with 158 additions and 37 deletions

View file

@ -21,7 +21,7 @@ regex = { version = "1.3.4", optional = true }
[features] [features]
default = ["search"] default = ["search"]
ingest = ["regex"] ingest = []
search = ["regex"] search = ["regex"]
control = [] control = []

View file

@ -54,26 +54,26 @@ macro_rules! init_commands {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ChannelMode { pub enum ChannelMode {
/// Sonic server search channel mode. /// Sonic server search channel mode.
/// ///
/// In this mode you can use `query`, `suggest`, `ping` and `quit` commands. /// In this mode you can use `query`, `suggest`, `ping` and `quit` commands.
/// ///
/// Note: This mode requires enabling the `search` feature. /// Note: This mode requires enabling the `search` feature.
#[cfg(feature = "search")] #[cfg(feature = "search")]
Search, Search,
/// Sonic server ingest channel mode. /// Sonic server ingest channel mode.
/// ///
/// In this mode you can use `push`, `pop`, `flushc`, `flushb`, `flusho`, /// In this mode you can use `push`, `pop`, `flushc`, `flushb`, `flusho`,
/// `ping` and `quit` commands. /// `ping` and `quit` commands.
/// ///
/// Note: This mode requires enabling the `ingest` feature. /// Note: This mode requires enabling the `ingest` feature.
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
Ingest, Ingest,
/// Sonic server control channel mode. /// Sonic server control channel mode.
/// ///
/// In this mode you can use `ping` and `quit` commands. /// In this mode you can use `ping` and `quit` commands.
/// ///
/// Note: This mode requires enabling the `control` feature. /// Note: This mode requires enabling the `control` feature.
#[cfg(feature = "control")] #[cfg(feature = "control")]
Control, Control,
@ -191,7 +191,7 @@ impl SonicChannel {
/// ///
/// ```rust /// ```rust
/// use sonic_channel::*; /// use sonic_channel::*;
/// ///
/// SonicChannel::connect_with_start(ChannelMode::Search, "localhost:1491", "SecretPassword"); /// SonicChannel::connect_with_start(ChannelMode::Search, "localhost:1491", "SecretPassword");
/// ``` /// ```
pub fn connect_with_start<A, S>(mode: ChannelMode, addr: A, password: S) -> Result<Self> pub fn connect_with_start<A, S>(mode: ChannelMode, addr: A, password: S) -> Result<Self>
@ -215,7 +215,32 @@ impl SonicChannel {
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
init_commands! { init_commands! {
#[doc="Push search data in the index."] #[doc=r#"
Push search data in the index.
Note: This method requires enabling the `ingest` feature and start
connection in Ingest mode.
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let result = ingest_channel.push(
"search",
"default",
"recipe:295",
"Sweet Teriyaki Beef Skewers"
)?;
assert_eq!(result, true);
# Ok(())
# }
```
"#]
use PushCommand for fn push<'a>( use PushCommand for fn push<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -223,7 +248,34 @@ impl SonicChannel {
text: &'a str, text: &'a str,
); );
#[doc="Push search data in the index with locale parameter in ISO 639-3 code."] #[doc=r#"
Push search data in the index with locale parameter in ISO 639-3 code.
Note: This method requires enabling the `ingest` feature and start
connection in Ingest mode.
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let result = ingest_channel.push_with_locale(
"search",
"default",
"recipe:296",
"Гренки с жареным картофелем и сыром",
"rus"
)?;
assert_eq!(result, true);
# Ok(())
# }
```
"#]
#[doc=""]
use PushCommand for fn push_with_locale<'a>( use PushCommand for fn push_with_locale<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -233,11 +285,24 @@ impl SonicChannel {
); );
#[doc=r#" #[doc=r#"
Pop search data from the index. Returns removed words count as u32 type. Pop search data from the index. Returns removed words count as usize type.
```rust Note: This method requires enabling the `ingest` feature and start
let result = ingest_channel.pop("search", "default", "recipe:295", "cake")?; connection in Ingest mode.
dbg!(result);
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let result = ingest_channel.pop("search", "default", "recipe:295", "beef")?;
assert_eq!(result, 1);
# Ok(())
# }
``` ```
"#] "#]
use PopCommand for fn pop<'a>( use PopCommand for fn pop<'a>(
@ -247,18 +312,78 @@ impl SonicChannel {
text: &'a str, text: &'a str,
); );
#[doc="Flush all indexed data from collections."] #[doc=r#"
Flush all indexed data from collections.
Note: This method requires enabling the `ingest` feature and start
connection in Ingest mode.
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let flushc_count = ingest_channel.flushc("search")?;
dbg!(flushc_count);
# Ok(())
# }
```
"#]
use FlushCommand for fn flushc<'a>( use FlushCommand for fn flushc<'a>(
collection: &'a str, collection: &'a str,
); );
#[doc="Flush all indexed data from bucket in a collection."] #[doc=r#"
Flush all indexed data from bucket in a collection.
Note: This method requires enabling the `ingest` feature and start
connection in Ingest mode.
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let flushb_count = ingest_channel.flushb("search", "default")?;
dbg!(flushb_count);
# Ok(())
# }
```
"#]
use FlushCommand for fn flushb<'a>( use FlushCommand for fn flushb<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
); );
#[doc="Flush all indexed data from an object in a bucket in collection."] #[doc=r#"
Flush all indexed data from an object in a bucket in collection.
Note: This method requires enabling the `ingest` feature and start
connection in Ingest mode.
```rust,no_run
# use sonic_channel::*;
# fn main() -> result::Result<()> {
let ingest_channel = SonicChannel::connect_with_start(
ChannelMode::Ingest,
"localhost:1491",
"SecretPassword"
)?;
let flusho_count = ingest_channel.flusho("search", "default", "recipe:296")?;
dbg!(flusho_count);
# Ok(())
# }
```
"#]
use FlushCommand for fn flusho<'a>( use FlushCommand for fn flusho<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),

View file

@ -1,9 +1,7 @@
use super::StreamCommand; use super::StreamCommand;
use crate::result::{Error, ErrorKind, Result}; use crate::result::{Error, ErrorKind, Result};
use regex::Regex;
const RE_QUERY_RECEIVED_MESSAGE: &str = r"^RESULT (?P<flush_count>\d+)\r\n$";
#[doc(hidden)]
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct FlushCommand<'a> { pub struct FlushCommand<'a> {
pub collection: &'a str, pub collection: &'a str,
@ -28,21 +26,17 @@ impl StreamCommand for FlushCommand<'_> {
} }
fn receive(&self, message: String) -> Result<Self::Response> { fn receive(&self, message: String) -> Result<Self::Response> {
lazy_static! { if message.starts_with("RESULT ") {
static ref RE: Regex = Regex::new(RE_QUERY_RECEIVED_MESSAGE).unwrap(); let count = message.split_whitespace().last().unwrap_or_default();
} count.parse().map_err(|_| {
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( Error::new(ErrorKind::QueryResponseError(
"Cannot parse sonic response to uint", "Cannot parse count of flush method response to usize",
)) ))
}), })
} else {
Err(Error::new(ErrorKind::QueryResponseError(
"Sonic response are wrong. Please write issue to github.",
)))
} }
} }
} }

View file

@ -11,7 +11,7 @@ pub struct PopCommand<'a> {
} }
impl StreamCommand for PopCommand<'_> { impl StreamCommand for PopCommand<'_> {
type Response = u32; type Response = usize;
fn message(&self) -> String { fn message(&self) -> String {
let mut message = format!( let mut message = format!(
@ -23,11 +23,13 @@ impl StreamCommand for PopCommand<'_> {
} }
fn receive(&self, message: String) -> Result<Self::Response> { fn receive(&self, message: String) -> Result<Self::Response> {
if message.starts_with("RESULT") { if message.starts_with("RESULT ") {
let count = message.split_whitespace().last().unwrap_or_default(); let count = message.split_whitespace().last().unwrap_or_default();
count count
.parse() .parse()
.map_err(|_| Error::new(ErrorKind::QueryResponseError("Cannot parse pop count"))) .map_err(|_| Error::new(ErrorKind::QueryResponseError(
"Cannot parse count of pop method response to usize",
)))
} else { } else {
Err(Error::new(ErrorKind::QueryResponseError("Cannot parse result"))) Err(Error::new(ErrorKind::QueryResponseError("Cannot parse result")))