sonic_channel/channels/ingest.rs
1use super::{ChannelMode, SonicChannel, SonicStream};
2use crate::commands::*;
3use crate::result::Result;
4use std::net::ToSocketAddrs;
5
6/// The Sonic Channel Ingest mode is used for altering the search index
7/// (push, pop and flush). Once in this mode, you cannot switch to other
8/// modes or gain access to commands from other modes.
9///
10/// ### Available commands
11///
12/// In this mode you can use `push`, `pop`, `flushc`, `flushb`, `flusho`,
13/// `bucket_count`, `object_count`, `word_count`, `ping` and `quit` commands.
14///
15/// **Note:** This mode requires enabling the `ingest` feature.
16#[derive(Debug)]
17pub struct IngestChannel(SonicStream);
18
19impl SonicChannel for IngestChannel {
20 type Channel = IngestChannel;
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::Ingest, addr, password).map(Self)
32 }
33}
34
35impl IngestChannel {
36 init_command!(
37 /// Stop connection.
38 ///
39 /// ```rust,no_run
40 /// # use sonic_channel::*;
41 /// # fn main() -> result::Result<()> {
42 /// let channel = IngestChannel::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 = IngestChannel::start(
60 /// "localhost:1491",
61 /// "SecretPassword",
62 /// )?;
63 ///
64 /// channel.ping()?;
65 /// # Ok(())
66 /// # }
67 use PingCommand for fn ping();
68 );
69}
70
71impl IngestChannel {
72 init_command!(
73 /// Push search data in the index.
74 ///
75 /// Note: This method requires enabling the `ingest` feature and start
76 /// connection in Ingest mode.
77 ///
78 /// ```rust,no_run
79 /// # use sonic_channel::*;
80 /// # fn main() -> result::Result<()> {
81 /// let ingest_channel = IngestChannel::start(
82 /// "localhost:1491",
83 /// "SecretPassword",
84 /// )?;
85 ///
86 /// let result = ingest_channel.push(PushRequest::new(
87 /// Dest::col("search").obj("recipe:295"),
88 /// "Sweet Teriyaki Beef Skewers"
89 /// ))?;
90 /// assert_eq!(result, ());
91 /// # Ok(())
92 /// # }
93 /// ```
94 use PushCommand for fn push<'a>(
95 req: PushRequest,
96 );
97 );
98
99 init_command!(
100 /// Pop search data from the index. Returns removed words count as usize type.
101 ///
102 /// Note: This method requires enabling the `ingest` feature and start
103 /// connection in Ingest mode.
104 ///
105 /// ```rust,no_run
106 /// # use sonic_channel::*;
107 /// # fn main() -> result::Result<()> {
108 /// let ingest_channel = IngestChannel::start(
109 /// "localhost:1491",
110 /// "SecretPassword",
111 /// )?;
112 ///
113 /// let dest = Dest::col("search").obj("recipe:295");
114 /// let result = ingest_channel.pop(PopRequest::new(dest, "beef"))?;
115 /// assert_eq!(result, 1);
116 /// # Ok(())
117 /// # }
118 /// ```
119 use PopCommand for fn pop(
120 req: PopRequest,
121 );
122 );
123
124 init_command!(
125 /// Flush all indexed data from collections.
126 ///
127 /// Note: This method requires enabling the `ingest` feature and start
128 /// connection in Ingest mode.
129 ///
130 /// ```rust,no_run
131 /// # use sonic_channel::*;
132 /// # fn main() -> result::Result<()> {
133 /// let ingest_channel = IngestChannel::start(
134 /// "localhost:1491",
135 /// "SecretPassword",
136 /// )?;
137 ///
138 /// let flushc_count = ingest_channel.flush(FlushRequest::collection("search"))?;
139 /// dbg!(flushc_count);
140 /// let flushb_count = ingest_channel.flush(FlushRequest::bucket("search", "default"))?;
141 /// dbg!(flushb_count);
142 /// let flusho_count = ingest_channel.flush(
143 /// FlushRequest::object("search", "default", "recipe:295")
144 /// )?;
145 /// dbg!(flusho_count);
146 /// # Ok(())
147 /// # }
148 /// ```
149 use FlushCommand for fn flush(
150 req: FlushRequest,
151 );
152 );
153
154 init_command!(
155 /// Count indexed search data of your collection.
156 ///
157 /// Note: This method requires enabling the `ingest` feature and start
158 /// connection in Ingest mode.
159 ///
160 /// ```rust,no_run
161 /// # use sonic_channel::*;
162 /// # fn main() -> result::Result<()> {
163 /// let ingest_channel = IngestChannel::start(
164 /// "localhost:1491",
165 /// "SecretPassword",
166 /// )?;
167 ///
168 /// let bucket_count = ingest_channel.count(CountRequest::buckets("search"))?;
169 /// dbg!(bucket_count);
170 /// let object_count = ingest_channel.count(CountRequest::objects("search", "default"))?;
171 /// dbg!(object_count);
172 /// let word_count = ingest_channel.count(
173 /// CountRequest::words("search", "default", "recipe:256")
174 /// )?;
175 /// dbg!(object_count);
176 /// # Ok(())
177 /// # }
178 /// ```
179 use CountCommand for fn count(
180 req: CountRequest,
181 );
182 );
183}