sonic_channel/channels/control.rs
1use super::{ChannelMode, SonicChannel, SonicStream};
2use crate::commands::*;
3use crate::result::Result;
4use std::net::ToSocketAddrs;
5
6/// The Sonic Channel Control mode is used for administration purposes.
7/// Once in this mode, you cannot switch to other modes or gain access
8/// to commands from other modes.
9///
10/// ### Available commands
11///
12/// In this mode you can use `consolidate`, `backup`, `restore`,
13/// `ping` and `quit` commands.
14///
15/// **Note:** This mode requires enabling the `control` feature.
16#[derive(Debug)]
17pub struct ControlChannel(SonicStream);
18
19impl SonicChannel for ControlChannel {
20 type Channel = ControlChannel;
21
22 fn stream(&self) -> &SonicStream {
23 &self.0
24 }
25
26 fn start<A, S>(addr: A, password: S) -> Result<Self::Channel>
27 where
28 A: ToSocketAddrs,
29 S: ToString,
30 {
31 SonicStream::connect_with_start(ChannelMode::Control, addr, password).map(Self)
32 }
33}
34
35impl ControlChannel {
36 init_command!(
37 /// Stop connection.
38 ///
39 /// ```rust,no_run
40 /// # use sonic_channel::*;
41 /// # fn main() -> result::Result<()> {
42 /// let channel = ControlChannel::start(
43 /// "localhost:1491",
44 /// "SecretPassword",
45 /// )?;
46 ///
47 /// channel.quit()?;
48 /// # Ok(())
49 /// # }
50 use QuitCommand for fn quit();
51 );
52
53 init_command!(
54 /// Ping server.
55 ///
56 /// ```rust,no_run
57 /// # use sonic_channel::*;
58 /// # fn main() -> result::Result<()> {
59 /// let channel = ControlChannel::start(
60 /// "localhost:1491",
61 /// "SecretPassword",
62 /// )?;
63 ///
64 /// channel.ping()?;
65 /// # Ok(())
66 /// # }
67 use PingCommand for fn ping();
68 );
69}
70
71impl ControlChannel {
72 init_command!(
73 /// Trigger control action.
74 ///
75 /// Note: This method requires enabling the `control` feature and start connection in
76 /// Control mode
77 ///
78 /// ```rust,no_run
79 /// # use sonic_channel::*;
80 /// # fn main() -> result::Result<()> {
81 /// let control_channel = ControlChannel::start(
82 /// "localhost:1491",
83 /// "SecretPassword",
84 /// )?;
85 ///
86 /// control_channel.trigger(TriggerRequest::Consolidate)?;
87 /// # Ok(())
88 /// # }
89 use TriggerCommand for fn trigger(
90 req: TriggerRequest,
91 )
92 );
93
94 /// Consolidate indexed search data instead of waiting for the next automated
95 /// consolidation tick.
96 ///
97 /// Note: This method requires enabling the `control` feature and start
98 /// connection in Control mode.
99 ///
100 /// ```rust,no_run
101 /// # use sonic_channel::*;
102 /// # fn main() -> result::Result<()> {
103 /// let control_channel = ControlChannel::start(
104 /// "localhost:1491",
105 /// "SecretPassword",
106 /// )?;
107 ///
108 /// control_channel.consolidate()?;
109 /// # Ok(())
110 /// # }
111 /// ```
112 pub fn consolidate(&self) -> Result<()> {
113 self.trigger(TriggerRequest::Consolidate)
114 }
115
116 /// Backup KV + FST to <path>/<BACKUP_{KV/FST}_PATH>
117 /// See [sonic backend source code](https://github.com/valeriansaliou/sonic/blob/master/src/channel/command.rs#L808)
118 /// for more information.
119 ///
120 /// Note: This method requires enabling the `control` feature and start
121 /// connection in Control mode.
122 ///
123 /// ```rust,no_run
124 /// # use sonic_channel::*;
125 /// # fn main() -> result::Result<()> {
126 /// let control_channel = ControlChannel::start(
127 /// "localhost:1491",
128 /// "SecretPassword",
129 /// )?;
130 ///
131 /// control_channel.backup("2020-08-07T23-48")?;
132 /// # Ok(())
133 /// # }
134 /// ```
135 pub fn backup(&self, path: &str) -> Result<()> {
136 self.trigger(TriggerRequest::Backup(path))
137 }
138
139 /// Restore KV + FST from <path> if you already have backup with the same name.
140 ///
141 /// Note: This method requires enabling the `control` feature and start
142 /// connection in Control mode.
143 ///
144 /// ```rust,no_run
145 /// # use sonic_channel::*;
146 /// # fn main() -> result::Result<()> {
147 /// let control_channel = ControlChannel::start(
148 /// "localhost:1491",
149 /// "SecretPassword",
150 /// )?;
151 ///
152 /// let result = control_channel.restore("2020-08-07T23-48")?;
153 /// assert_eq!(result, ());
154 /// # Ok(())
155 /// # }
156 /// ```
157 pub fn restore(&self, path: &str) -> Result<()> {
158 self.trigger(TriggerRequest::Restore(path))
159 }
160}