|
| 1 | +use bytes::Bytes; |
| 2 | +use dstore::local::Store; |
| 3 | +use std::io; |
| 4 | +use std::io::{stdin, BufRead, Write}; |
| 5 | + |
| 6 | +pub struct REPL { |
| 7 | + store: Store, |
| 8 | +} |
| 9 | + |
| 10 | +impl REPL { |
| 11 | + async fn new(store: Store) -> Self { |
| 12 | + Self { store } |
| 13 | + } |
| 14 | + |
| 15 | + async fn parse_input(&mut self, cmd: String) -> Result<(), Box<dyn std::error::Error>> { |
| 16 | + // Meta commands start with `.`. |
| 17 | + if cmd.starts_with(".") { |
| 18 | + match MetaCmdResult::run(&cmd) { |
| 19 | + MetaCmdResult::Unrecognized => Ok(println!("db: meta command not found: {}", cmd)), |
| 20 | + MetaCmdResult::Success => Ok(()), |
| 21 | + } |
| 22 | + } else { |
| 23 | + let words = cmd.split(" ").collect::<Vec<&str>>(); |
| 24 | + match words[0].to_lowercase().as_ref() { |
| 25 | + "set" | "put" | "insert" | "in" | "i" => { |
| 26 | + let key = words[1].to_string(); |
| 27 | + let value = Bytes::from(words[2..].join(" ")); |
| 28 | + if let Err(e) = self.store.insert(key, value).await { |
| 29 | + eprintln!("{}", e); |
| 30 | + } |
| 31 | + |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | + "get" | "select" | "output" | "out" | "o" => { |
| 35 | + let key = words[1].to_string(); |
| 36 | + match self.store.get(&key).await { |
| 37 | + Ok(value) => { |
| 38 | + println!("db: {} -> {}", key, String::from_utf8(value.to_vec())?) |
| 39 | + } |
| 40 | + Err(e) => eprintln!("{}", e), |
| 41 | + } |
| 42 | + |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | + "del" | "delete" | "rem" | "remove" | "rm" | "d" => { |
| 46 | + // Removes only from local |
| 47 | + let key = words[1].to_string(); |
| 48 | + Ok(self.store.remove(key)) |
| 49 | + } |
| 50 | + _ => Ok(eprintln!("Unknown command!")), |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +pub enum MetaCmdResult { |
| 56 | + Success, |
| 57 | + Unrecognized, |
| 58 | +} |
| 59 | + |
| 60 | +impl MetaCmdResult { |
| 61 | + /// Execute Meta commands on the REPL. |
| 62 | + pub fn run(cmd: &String) -> Self { |
| 63 | + match cmd.as_ref() { |
| 64 | + ".exit" => std::process::exit(0), |
| 65 | + ".version" => { |
| 66 | + if let Some(ver) = option_env!("CARGO_PKG_VERSION") { |
| 67 | + println!("You are using KVDB v{}", ver); |
| 68 | + } |
| 69 | + Self::Success |
| 70 | + } |
| 71 | + _ => Self::Unrecognized, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::main] |
| 77 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 78 | + let client_addr = "http://127.0.0.1:50051".parse().unwrap(); |
| 79 | + let local_store = Store::new(client_addr).await?; |
| 80 | + let mut repl = REPL::new(local_store).await; |
| 81 | + |
| 82 | + print!("dstore v0.1.0\nThis is an experimental database, do contribute to further developments at https://github.com/vyuham/dstore. \nUse `.exit` to exit the repl\ndb > "); |
| 83 | + io::stdout().flush().expect("Error"); |
| 84 | + |
| 85 | + for cmd in stdin().lock().lines() { |
| 86 | + match cmd { |
| 87 | + Ok(cmd) => { |
| 88 | + repl.parse_input(cmd.trim().to_string()).await?; |
| 89 | + } |
| 90 | + Err(_) => eprint!("Error in reading command, exiting REPL."), |
| 91 | + } |
| 92 | + print!("db > "); |
| 93 | + io::stdout().flush().expect("Error"); |
| 94 | + } |
| 95 | + |
| 96 | + Ok(()) |
| 97 | +} |
0 commit comments