1#![forbid(unsafe_code)]
20#![warn(missing_docs)]
21
22pub use kevy_resp::Reply;
23
24pub mod backup;
27
28pub mod migrate;
31
32pub(crate) const TOOLS_HELP: &str = "\
34KEVY TOOLS: kevy-cli [connection options] --kevy <tool> [args]
35 (in the REPL: \\<tool> [args]; a bare word is always a server command)
36
37 catalog
38 tables [pattern] TABLE.LIST as rows
39 indexes [table|pattern] IDX.LIST, with each index's table
40 views [pattern] VIEW.LIST
41 describe[+] <table|index|view> columns and access paths, an
42 index's fields, a view's tree
43 (+ runs its VERIFY)
44 show-create <name> [--as kevy|sql] the declaration that recreates it
45
46 query
47 query [--all] [--max-rows n] <IDX.QUERY|VIEW.QUERY …>
48 rows, following the cursor
49 explain <index> <shape…> | view <name> the path a query takes
50 explain --analyze <query…> run it; measured by this client
51 advise paths refused queries asked for
52 sql run [--max-rows n] 'SELECT …' one SELECT over declared paths,
53 sent as the IDX.QUERY for it
54 sql compile <file.sql> [--apply] CREATE TABLE/INDEX/VIEW as
55 TABLE.DECLARE / VIEW.CREATE and
56 query cards (--apply declares)
57 sql plan <file.sql> what becomes of every query
58 sql eval '<SELECT …>' [--at <ts>] fold a table-free SELECT
59
60 data, one level per pair
61 backup --data-dir <d> --to <f> a data directory, offline
62 restore --from <f> --to <d>
63 export [--prefix p] <file> the keyspace as RESP, online
64 import [--resume] [--strict] <file>
65 dump --schema [--table t]… [--as kevy|sql] declarations as a script
66 dump --all <dir> + each table's declared columns
67 load <dir> rows, declarations, wait-ready,
68 doctor
69 export-csv (--prefix p --columns a,b | --table t) [--via \"IDX.QUERY …\"] <f|->
70 import-csv <f> (--prefix p --pk c | --table t | --key-column c)
71 (--header|--columns a,b)
72 copy-prefix [--rate n] <from> <to>
73 delete-prefix [--rate n] [--dry-run] <prefix>
74
75 checks
76 digest <prefix> hash a prefix
77 diff <other host:port|redis://…> <prefix>… compare it with another server
78 inspect <prefix> sample keys: types and sizes
79 doctor [--warn-is-failure] [--indexes] [--views]
80 VERIFY every table (and bare
81 index, view); exit code answers
82 lint overlap --prefix <p:> a name under more than one owner?
83 lint columns <table> column pairs that agree on most rows
84 backfill-keys --from-index <k> --from-prefix <p:> --from-file <f>
85 the union of every source
86 shadow --old <cmd> --new <cmd> old and new read paths, compared
87 in membership and order
88 wait-ready [--index n|--table t|--all] [--timeout s]
89 status server, role, keys, catalogs
90
91 running
92 run [-f file]… [-c cmd]… [--force] [--echo] [--atomic]
93 exit 3 on an error reply, 2 on a
94 lost link
95 watch <seconds> [count] <command…>
96 feed follow [--prefix p]… [--shard n|all] [--from tail|gen:off]
97 [--checkpoint f] [--as json|resp] [--on-resync stop|jump]
98
99 Rows: --format table|tsv|csv|json, --no-header, --null s, --expanded,
100 --timing (a table on a terminal, tsv when piped).
101
102EXAMPLES:
103 kevy-cli # REPL against 127.0.0.1:6379
104 kevy-cli -p 6004 # REPL against kevy default port
105 kevy-cli -h prod.internal ping # one-shot PING
106 kevy-cli -p 6004 set greet hello # one-shot SET, exits 0
107
108 # move a keyspace, and prove it arrived
109 kevy-cli -p 6379 --kevy export --prefix user: dump.resp
110 kevy-cli -p 6380 --kevy import --strict dump.resp
111 kevy-cli -p 6379 --kevy diff 127.0.0.1:6380 user:
112
113Docs: https://github.com/goliajp/kevy
114";
115
116pub mod rcli;
125
126pub const DEFAULT_HOST: &str = "127.0.0.1";
138pub const DEFAULT_PORT: u16 = 6379;
146
147pub mod bulk;
150
151pub mod shadow;
169
170pub mod backfill_keys;
171pub(crate) mod collections;
172pub mod doctor;
175pub mod link;
176pub mod lint;
177mod tools;
178
179pub fn format_reply(reply: &Reply, indent: usize) -> String {
190 match reply {
191 Reply::Simple(s) => String::from_utf8_lossy(s).into_owned(),
192 Reply::Error(s) | Reply::BlobError(s) => {
193 format!("(error) {}", String::from_utf8_lossy(s))
194 }
195 Reply::Int(n) => format!("(integer) {n}"),
196 Reply::Bulk(b) => format!("\"{}\"", String::from_utf8_lossy(b)),
197 Reply::Nil | Reply::Null => "(nil)".to_string(),
198 Reply::Array(items) if items.is_empty() => "(empty array)".to_string(),
199 Reply::Array(items) | Reply::Set(items) | Reply::Push(items) => {
200 let pad = " ".repeat(indent);
201 items
202 .iter()
203 .enumerate()
204 .map(|(i, it)| format!("{pad}{}) {}", i + 1, format_reply(it, indent + 1)))
205 .collect::<Vec<_>>()
206 .join("\n")
207 }
208 Reply::Map(pairs) if pairs.is_empty() => "(empty map)".to_string(),
210 Reply::Map(pairs) => {
211 let pad = " ".repeat(indent);
212 pairs
213 .iter()
214 .enumerate()
215 .map(|(i, (k, v))| {
216 format!(
217 "{pad}{}) {} => {}",
218 i + 1,
219 format_reply(k, indent + 1),
220 format_reply(v, indent + 1)
221 )
222 })
223 .collect::<Vec<_>>()
224 .join("\n")
225 }
226 Reply::Double(v) => format!("(double) {v}"),
227 Reply::Boolean(b) => format!("(boolean) {}", if *b { "t" } else { "f" }),
228 Reply::Verbatim { fmt, data } => format!(
229 "(verbatim/{}) \"{}\"",
230 String::from_utf8_lossy(fmt),
231 String::from_utf8_lossy(data)
232 ),
233 Reply::BigNumber(s) => format!("(bignum) {}", String::from_utf8_lossy(s)),
234 }
235}
236
237const _: () = {
240 const fn send_sync<T: Send + Sync>() {}
241 send_sync::<backfill_keys::Source>();
242 send_sync::<backfill_keys::SourceReport>();
243 send_sync::<backfill_keys::Union>();
244 send_sync::<bulk::DeleteMode>();
245 send_sync::<bulk::RateLimiter>();
246 send_sync::<doctor::Health>();
247 send_sync::<doctor::OnWarning>();
248 send_sync::<doctor::Scope>();
249 send_sync::<doctor::TableHealth>();
250 send_sync::<lint::Coincidence>();
251 send_sync::<lint::Overlap>();
252 send_sync::<migrate::Export>();
253 send_sync::<migrate::ImportReport>();
254 send_sync::<migrate::ImportStart>();
255 send_sync::<migrate::OnErrorReply>();
256 send_sync::<shadow::Compared>();
257 send_sync::<shadow::Divergence>();
258 send_sync::<shadow::ShadowReport>();
259 send_sync::<shadow::Shape>();
260};
261
262#[cfg(test)]
263mod format_reply_tests {
264 use super::format_reply;
265 use kevy_resp::Reply;
266
267 fn f(r: &Reply) -> String {
268 format_reply(r, 0)
269 }
270
271 #[test]
277 fn every_reply_variant_renders() {
278 assert_eq!(f(&Reply::Simple(b"OK".to_vec())), "OK");
279 assert_eq!(f(&Reply::Error(b"ERR nope".to_vec())), "(error) ERR nope");
280 assert_eq!(f(&Reply::Int(-7)), "(integer) -7");
281 assert_eq!(f(&Reply::Bulk(b"hi".to_vec())), "\"hi\"");
282 assert_eq!(f(&Reply::Nil), "(nil)");
283 assert_eq!(f(&Reply::Array(vec![])), "(empty array)");
284 assert_eq!(f(&Reply::Double(1.5)), "(double) 1.5");
285 assert_eq!(f(&Reply::Boolean(true)), "(boolean) t");
286 assert_eq!(f(&Reply::Boolean(false)), "(boolean) f");
287 assert_eq!(
288 f(&Reply::BigNumber(b"123456789012345678901".to_vec())),
289 "(bignum) 123456789012345678901"
290 );
291
292 assert_eq!(f(&Reply::Null), "(nil)");
296 assert_eq!(f(&Reply::BlobError(b"ERR nope".to_vec())), "(error) ERR nope");
297
298 assert_eq!(
299 f(&Reply::Verbatim { fmt: *b"txt", data: b"hello".to_vec() }),
300 "(verbatim/txt) \"hello\""
301 );
302
303 assert_eq!(f(&Reply::Set(vec![Reply::Int(4)])), "1) (integer) 4");
305 assert_eq!(f(&Reply::Push(vec![Reply::Bulk(b"message".to_vec())])), "1) \"message\"");
306 }
307
308 #[test]
311 fn arrays_number_from_one_and_nest() {
312 let flat = Reply::Array(vec![Reply::Int(1), Reply::Bulk(b"x".to_vec())]);
313 assert_eq!(f(&flat), "1) (integer) 1\n2) \"x\"");
314
315 let nested = Reply::Array(vec![Reply::Array(vec![Reply::Int(9)])]);
316 assert_eq!(f(&nested), "1) 1) (integer) 9");
318 }
319
320 #[test]
323 fn maps_render_as_pairs() {
324 assert_eq!(f(&Reply::Map(vec![])), "(empty map)");
325 let m = Reply::Map(vec![(Reply::Bulk(b"k".to_vec()), Reply::Int(1))]);
326 assert_eq!(f(&m), "1) \"k\" => (integer) 1");
327 }
328}