diff --git a/Cargo.toml b/Cargo.toml index 4c22edc..9af68e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sonic-channel" -version = "0.2.2" +version = "0.3.0" authors = ["Dmitriy Pleshevskiy "] description = "Rust client for sonic search backend" categories = ["api-bindings"] diff --git a/README.md b/README.md index 94fb540..118b667 100644 --- a/README.md +++ b/README.md @@ -7,18 +7,18 @@ We recommend you start with the [documentation]. ## Installation -Add `sonic-channel = { version = "0.2" }` as a dependency in `Cargo.toml`. +Add `sonic-channel = { version = "0.3" }` as a dependency in `Cargo.toml`. `Cargo.toml` example: ```toml [package] name = "my-crate" -version = "0.2.0" +version = "0.1.0" authors = ["Me "] [dependencies] -sonic-channel = { version = "0.2" } +sonic-channel = { version = "0.3" } ``` @@ -68,6 +68,27 @@ fn main() -> result::Result<()> { } ``` +### Control channel + +Note: This example requires enabling the `control` feature. + +```rust +use sonic_channel::*; + +fn main() -> result::Result<()> { + let mut channel = SonicChannel::connect_with_start( + ChannelMode::Control, + "localhost:1491", + "SecretPassword", + )?; + + let result = channel.consolidate()?; + assert_eq!(result, true); + + Ok(()) +} +``` + ## Available features diff --git a/src/channel.rs b/src/channel.rs index b199306..e6a60ed 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -519,13 +519,13 @@ impl SonicChannel { ```rust,no_run # use sonic_channel::*; # fn main() -> result::Result<()> { - let ingest_channel = SonicChannel::connect_with_start( + let search_channel = SonicChannel::connect_with_start( ChannelMode::Search, "localhost:1491", "SecretPassword", )?; - let result = ingest_channel.query("search", "default", "Beef")?; + let result = search_channel.query("search", "default", "Beef")?; dbg!(result); # Ok(()) # } @@ -547,13 +547,13 @@ impl SonicChannel { ```rust,no_run # use sonic_channel::*; # fn main() -> result::Result<()> { - let ingest_channel = SonicChannel::connect_with_start( + let search_channel = SonicChannel::connect_with_start( ChannelMode::Search, "localhost:1491", "SecretPassword", )?; - let result = ingest_channel.query_with_limit( + let result = search_channel.query_with_limit( "search", "default", "Beef", @@ -581,13 +581,13 @@ impl SonicChannel { ```rust,no_run # use sonic_channel::*; # fn main() -> result::Result<()> { - let ingest_channel = SonicChannel::connect_with_start( + let search_channel = SonicChannel::connect_with_start( ChannelMode::Search, "localhost:1491", "SecretPassword", )?; - let result = ingest_channel.query_with_limit_and_offset( + let result = search_channel.query_with_limit_and_offset( "search", "default", "Beef", @@ -616,13 +616,13 @@ impl SonicChannel { ```rust,no_run # use sonic_channel::*; # fn main() -> result::Result<()> { - let ingest_channel = SonicChannel::connect_with_start( + let search_channel = SonicChannel::connect_with_start( ChannelMode::Search, "localhost:1491", "SecretPassword", )?; - let result = ingest_channel.suggest("search", "default", "Beef")?; + let result = search_channel.suggest("search", "default", "Beef")?; dbg!(result); # Ok(()) # } @@ -634,7 +634,6 @@ impl SonicChannel { word: &'a str, ); - #[doc=r#" Suggest auto-completes words with limit. @@ -644,13 +643,13 @@ impl SonicChannel { ```rust,no_run # use sonic_channel::*; # fn main() -> result::Result<()> { - let ingest_channel = SonicChannel::connect_with_start( + let search_channel = SonicChannel::connect_with_start( ChannelMode::Search, "localhost:1491", "SecretPassword", )?; - let result = ingest_channel.suggest_with_limit("search", "default", "Beef", 5)?; + let result = search_channel.suggest_with_limit("search", "default", "Beef", 5)?; dbg!(result); # Ok(()) # } @@ -665,5 +664,85 @@ impl SonicChannel { } #[cfg(feature = "control")] - init_commands! {} + init_commands! { + #[doc=r#" + Consolidate indexed search data instead of waiting for the next automated + consolidation tick. + + Note: This method requires enabling the `control` feature and start + connection in Control mode. + + ```rust,no_run + # use sonic_channel::*; + # fn main() -> result::Result<()> { + let control_channel = SonicChannel::connect_with_start( + ChannelMode::Control, + "localhost:1491", + "SecretPassword", + )?; + + let result = control_channel.consolidate()?; + assert_eq!(result, true); + # Ok(()) + # } + ``` + "#] + use TriggerCommand for fn consolidate(); + + #[doc=r#" + Backup KV + FST to / + See [sonic backend source code](https://github.com/valeriansaliou/sonic/blob/master/src/channel/command.rs#L808) + for more information. + + Note: This method requires enabling the `control` feature and start + connection in Control mode. + + ```rust,no_run + # use sonic_channel::*; + # fn main() -> result::Result<()> { + let control_channel = SonicChannel::connect_with_start( + ChannelMode::Control, + "localhost:1491", + "SecretPassword", + )?; + + let result = control_channel.backup("2020-08-07T23-48")?; + assert_eq!(result, true); + # Ok(()) + # } + ``` + "#] + use TriggerCommand for fn backup<'a>( + // It's not action, but my macro cannot support alias for custom argument. + // TODO: Add alias to macro and rename argument of this function. + action: &'a str => TriggerAction::Backup(action), + ); + + #[doc=r#" + Restore KV + FST from if you already have backup with the same name. + + Note: This method requires enabling the `control` feature and start + connection in Control mode. + + ```rust,no_run + # use sonic_channel::*; + # fn main() -> result::Result<()> { + let control_channel = SonicChannel::connect_with_start( + ChannelMode::Control, + "localhost:1491", + "SecretPassword", + )?; + + let result = control_channel.restore("2020-08-07T23-48")?; + assert_eq!(result, true); + # Ok(()) + # } + ``` + "#] + use TriggerCommand for fn restore<'a>( + // It's not action, but my macro cannot support alias for custom argument. + // TODO: Add alias to macro and rename argument of this function. + action: &'a str => TriggerAction::Restore(action), + ); + } } diff --git a/src/commands/count.rs b/src/commands/count.rs index e888e60..93744a9 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -1,7 +1,6 @@ use super::StreamCommand; use crate::result::*; -#[doc(hidden)] #[derive(Debug, Default)] pub struct CountCommand<'a> { pub collection: &'a str, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0357493..811320e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -17,6 +17,9 @@ mod query; #[cfg(feature = "search")] mod suggest; +#[cfg(feature = "control")] +mod trigger; + pub(crate) use quit::QuitCommand; pub(crate) use start::StartCommand; @@ -36,6 +39,9 @@ pub(crate) use query::QueryCommand; #[cfg(feature = "search")] pub(crate) use suggest::SuggestCommand; +#[cfg(feature = "control")] +pub(crate) use trigger::{TriggerCommand, TriggerAction}; + use crate::result::Result; pub trait StreamCommand { diff --git a/src/commands/trigger.rs b/src/commands/trigger.rs new file mode 100644 index 0000000..57a8f4e --- /dev/null +++ b/src/commands/trigger.rs @@ -0,0 +1,47 @@ +use super::StreamCommand; +use crate::result::*; +use std::fmt; + +#[derive(Debug)] +pub enum TriggerAction<'a> { + Consolidate, + Backup(&'a str), + Restore(&'a str), +} + +impl Default for TriggerAction<'_> { + fn default() -> Self { + TriggerAction::Consolidate + } +} + +impl fmt::Display for TriggerAction<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> { + match self { + TriggerAction::Consolidate => write!(f, "consolidate"), + TriggerAction::Backup(data) => write!(f, "backup {}", data), + TriggerAction::Restore(data) => write!(f, "restore {}", data), + } + } +} + +#[derive(Debug, Default)] +pub struct TriggerCommand<'a> { + pub action: TriggerAction<'a>, +} + +impl StreamCommand for TriggerCommand<'_> { + type Response = bool; + + fn message(&self) -> String { + format!("TRIGGER {}\r\n", self.action) + } + + fn receive(&self, message: String) -> Result { + if message == "OK\r\n" { + Ok(true) + } else { + Err(Error::new(ErrorKind::WrongSonicResponse)) + } + } +} diff --git a/src/lib.rs b/src/lib.rs index fab0ea4..9ff9d34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,27 @@ //! } //! ``` //! +//! ### Control channel +//! +//! Note: This example requires enabling the `control` feature. +//! +//! ```rust,no_run +//! use sonic_channel::*; +//! +//! fn main() -> result::Result<()> { +//! let mut channel = SonicChannel::connect_with_start( +//! ChannelMode::Control, +//! "localhost:1491", +//! "SecretPassword", +//! )?; +//! +//! let result = channel.consolidate()?; +//! assert_eq!(result, true); +//! +//! Ok(()) +//! } +//! ``` +//! //! [sonic]: https://github.com/valeriansaliou/sonic // Rustc lints.