sonic_channel/commands/
trigger.rs

1use super::StreamCommand;
2use crate::protocol;
3use crate::result::*;
4use std::path::PathBuf;
5
6/// Parameters for the `trigger` command.
7#[derive(Debug)]
8pub enum TriggerRequest<'a> {
9    /// Consolidate indexed search data instead of waiting for the next automated
10    /// consolidation tick.
11    Consolidate,
12
13    /// Backup KV + FST to <path>/<BACKUP_{KV/FST}_PATH>
14    /// See [sonic backend source code](https://github.com/valeriansaliou/sonic/blob/master/src/channel/command.rs#L808)
15    /// for more information.
16    Backup(&'a str),
17
18    /// Restore KV + FST from <path> if you already have backup with the same name.
19    Restore(&'a str),
20}
21
22#[derive(Debug)]
23pub struct TriggerCommand<'a> {
24    pub(crate) req: TriggerRequest<'a>,
25}
26
27impl StreamCommand for TriggerCommand<'_> {
28    type Response = ();
29
30    fn request(&self) -> protocol::Request {
31        let req = match self.req {
32            TriggerRequest::Consolidate => protocol::TriggerRequest::Consolidate,
33            TriggerRequest::Backup(path) => protocol::TriggerRequest::Backup(PathBuf::from(path)),
34            TriggerRequest::Restore(path) => protocol::TriggerRequest::Restore(PathBuf::from(path)),
35        };
36
37        protocol::Request::Trigger(req)
38    }
39
40    fn receive(&self, res: protocol::Response) -> Result<Self::Response> {
41        if matches!(res, protocol::Response::Ok) {
42            Ok(())
43        } else {
44            Err(Error::WrongResponse)
45        }
46    }
47}