#![forbid(unsafe_code)]
#![warn(missing_docs)]
pub use kevy_resp::Reply;
pub mod backup;
pub mod migrate;
pub(crate) const TOOLS_HELP: &str = "\
KEVY TOOLS: kevy-cli [connection options] --kevy <tool> [args]
(in the REPL: \\<tool> [args]; a bare word is always a server command)
catalog
tables [pattern] TABLE.LIST as rows
indexes [table|pattern] IDX.LIST, with each index's table
views [pattern] VIEW.LIST
describe[+] <table|index|view> columns and access paths, an
index's fields, a view's tree
(+ runs its VERIFY)
show-create <name> [--as kevy|sql] the declaration that recreates it
query
query [--all] [--max-rows n] <IDX.QUERY|VIEW.QUERY …>
rows, following the cursor
explain <index> <shape…> | view <name> the path a query takes
explain --analyze <query…> run it; measured by this client
advise paths refused queries asked for
sql run [--max-rows n] 'SELECT …' one SELECT over declared paths,
sent as the IDX.QUERY for it
sql compile <file.sql> [--apply] CREATE TABLE/INDEX/VIEW as
TABLE.DECLARE / VIEW.CREATE and
query cards (--apply declares)
sql plan <file.sql> what becomes of every query
sql eval '<SELECT …>' [--at <ts>] fold a table-free SELECT
data, one level per pair
backup --data-dir <d> --to <f> a data directory, offline
restore --from <f> --to <d>
export [--prefix p] <file> the keyspace as RESP, online
import [--resume] [--strict] <file>
dump --schema [--table t]… [--as kevy|sql] declarations as a script
dump --all <dir> + each table's declared columns
load <dir> rows, declarations, wait-ready,
doctor
export-csv (--prefix p --columns a,b | --table t) [--via \"IDX.QUERY …\"] <f|->
import-csv <f> (--prefix p --pk c | --table t | --key-column c)
(--header|--columns a,b)
copy-prefix [--rate n] <from> <to>
delete-prefix [--rate n] [--dry-run] <prefix>
checks
digest <prefix> hash a prefix
diff <other host:port|redis://…> <prefix>… compare it with another server
inspect <prefix> sample keys: types and sizes
doctor [--warn-is-failure] [--indexes] [--views]
VERIFY every table (and bare
index, view); exit code answers
lint overlap --prefix <p:> a name under more than one owner?
lint columns <table> column pairs that agree on most rows
backfill-keys --from-index <k> --from-prefix <p:> --from-file <f>
the union of every source
shadow --old <cmd> --new <cmd> old and new read paths, compared
in membership and order
wait-ready [--index n|--table t|--all] [--timeout s]
status server, role, keys, catalogs
running
run [-f file]… [-c cmd]… [--force] [--echo] [--atomic]
exit 3 on an error reply, 2 on a
lost link
watch <seconds> [count] <command…>
feed follow [--prefix p]… [--shard n|all] [--from tail|gen:off]
[--checkpoint f] [--as json|resp] [--on-resync stop|jump]
Rows: --format table|tsv|csv|json, --no-header, --null s, --expanded,
--timing (a table on a terminal, tsv when piped).
EXAMPLES:
kevy-cli # REPL against 127.0.0.1:6379
kevy-cli -p 6004 # REPL against kevy default port
kevy-cli -h prod.internal ping # one-shot PING
kevy-cli -p 6004 set greet hello # one-shot SET, exits 0
# move a keyspace, and prove it arrived
kevy-cli -p 6379 --kevy export --prefix user: dump.resp
kevy-cli -p 6380 --kevy import --strict dump.resp
kevy-cli -p 6379 --kevy diff 127.0.0.1:6380 user:
Docs: https://github.com/goliajp/kevy
";
pub mod rcli;
pub const DEFAULT_HOST: &str = "127.0.0.1";
pub const DEFAULT_PORT: u16 = 6379;
pub mod bulk;
pub mod shadow;
pub mod backfill_keys;
pub(crate) mod collections;
pub mod doctor;
pub mod link;
pub mod lint;
mod tools;
pub fn format_reply(reply: &Reply, indent: usize) -> String {
match reply {
Reply::Simple(s) => String::from_utf8_lossy(s).into_owned(),
Reply::Error(s) | Reply::BlobError(s) => {
format!("(error) {}", String::from_utf8_lossy(s))
}
Reply::Int(n) => format!("(integer) {n}"),
Reply::Bulk(b) => format!("\"{}\"", String::from_utf8_lossy(b)),
Reply::Nil | Reply::Null => "(nil)".to_string(),
Reply::Array(items) if items.is_empty() => "(empty array)".to_string(),
Reply::Array(items) | Reply::Set(items) | Reply::Push(items) => {
let pad = " ".repeat(indent);
items
.iter()
.enumerate()
.map(|(i, it)| format!("{pad}{}) {}", i + 1, format_reply(it, indent + 1)))
.collect::<Vec<_>>()
.join("\n")
}
Reply::Map(pairs) if pairs.is_empty() => "(empty map)".to_string(),
Reply::Map(pairs) => {
let pad = " ".repeat(indent);
pairs
.iter()
.enumerate()
.map(|(i, (k, v))| {
format!(
"{pad}{}) {} => {}",
i + 1,
format_reply(k, indent + 1),
format_reply(v, indent + 1)
)
})
.collect::<Vec<_>>()
.join("\n")
}
Reply::Double(v) => format!("(double) {v}"),
Reply::Boolean(b) => format!("(boolean) {}", if *b { "t" } else { "f" }),
Reply::Verbatim { fmt, data } => format!(
"(verbatim/{}) \"{}\"",
String::from_utf8_lossy(fmt),
String::from_utf8_lossy(data)
),
Reply::BigNumber(s) => format!("(bignum) {}", String::from_utf8_lossy(s)),
}
}
const _: () = {
const fn send_sync<T: Send + Sync>() {}
send_sync::<backfill_keys::Source>();
send_sync::<backfill_keys::SourceReport>();
send_sync::<backfill_keys::Union>();
send_sync::<bulk::DeleteMode>();
send_sync::<bulk::RateLimiter>();
send_sync::<doctor::Health>();
send_sync::<doctor::OnWarning>();
send_sync::<doctor::Scope>();
send_sync::<doctor::TableHealth>();
send_sync::<lint::Coincidence>();
send_sync::<lint::Overlap>();
send_sync::<migrate::Export>();
send_sync::<migrate::ImportReport>();
send_sync::<migrate::ImportStart>();
send_sync::<migrate::OnErrorReply>();
send_sync::<shadow::Compared>();
send_sync::<shadow::Divergence>();
send_sync::<shadow::ShadowReport>();
send_sync::<shadow::Shape>();
};
#[cfg(test)]
mod format_reply_tests {
use super::format_reply;
use kevy_resp::Reply;
fn f(r: &Reply) -> String {
format_reply(r, 0)
}
#[test]
fn every_reply_variant_renders() {
assert_eq!(f(&Reply::Simple(b"OK".to_vec())), "OK");
assert_eq!(f(&Reply::Error(b"ERR nope".to_vec())), "(error) ERR nope");
assert_eq!(f(&Reply::Int(-7)), "(integer) -7");
assert_eq!(f(&Reply::Bulk(b"hi".to_vec())), "\"hi\"");
assert_eq!(f(&Reply::Nil), "(nil)");
assert_eq!(f(&Reply::Array(vec![])), "(empty array)");
assert_eq!(f(&Reply::Double(1.5)), "(double) 1.5");
assert_eq!(f(&Reply::Boolean(true)), "(boolean) t");
assert_eq!(f(&Reply::Boolean(false)), "(boolean) f");
assert_eq!(
f(&Reply::BigNumber(b"123456789012345678901".to_vec())),
"(bignum) 123456789012345678901"
);
assert_eq!(f(&Reply::Null), "(nil)");
assert_eq!(f(&Reply::BlobError(b"ERR nope".to_vec())), "(error) ERR nope");
assert_eq!(
f(&Reply::Verbatim { fmt: *b"txt", data: b"hello".to_vec() }),
"(verbatim/txt) \"hello\""
);
assert_eq!(f(&Reply::Set(vec![Reply::Int(4)])), "1) (integer) 4");
assert_eq!(f(&Reply::Push(vec![Reply::Bulk(b"message".to_vec())])), "1) \"message\"");
}
#[test]
fn arrays_number_from_one_and_nest() {
let flat = Reply::Array(vec![Reply::Int(1), Reply::Bulk(b"x".to_vec())]);
assert_eq!(f(&flat), "1) (integer) 1\n2) \"x\"");
let nested = Reply::Array(vec![Reply::Array(vec![Reply::Int(9)])]);
assert_eq!(f(&nested), "1) 1) (integer) 9");
}
#[test]
fn maps_render_as_pairs() {
assert_eq!(f(&Reply::Map(vec![])), "(empty map)");
let m = Reply::Map(vec![(Reply::Bulk(b"k".to_vec()), Reply::Int(1))]);
assert_eq!(f(&m), "1) \"k\" => (integer) 1");
}
}