Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Apr 7, 2024
1 parent 03c44d9 commit 534719f
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 390 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async-channel = "~2.2.0"
bincode = "~1.3.3"
clap = { version = "~4.5.4", features = ["cargo", "derive"] }
config = "~0.14.0"
crossbeam = { version = "0.8.4", features = ["crossbeam-channel"] }
derivative = "~2.2.0"
fs4 = "~0.8.1"
futures = "~0.3.15"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/toydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn main() -> Result<()> {
let srv = Server::new(cfg.id, cfg.peers, raft_log, raft_state)?;

let raft_listener = TcpListener::bind(&cfg.listen_raft).await?;
let sql_listener = TcpListener::bind(&cfg.listen_sql).await?;
let sql_listener = std::net::TcpListener::bind(&cfg.listen_sql)?;

srv.serve(raft_listener, sql_listener).await
}
Expand Down
38 changes: 18 additions & 20 deletions src/bin/toysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use toydb::sql::execution::ResultSet;
use toydb::sql::parser::{Lexer, Token};
use toydb::Client;

#[tokio::main]
async fn main() -> Result<()> {
fn main() -> Result<()> {
let opts = clap::command!()
.name("toysql")
.about("A ToyDB client.")
Expand All @@ -36,13 +35,12 @@ async fn main() -> Result<()> {
.get_matches();

let mut toysql =
ToySQL::new(opts.get_one::<String>("host").unwrap(), *opts.get_one("port").unwrap())
.await?;
ToySQL::new(opts.get_one::<String>("host").unwrap(), *opts.get_one("port").unwrap())?;

if let Some(command) = opts.get_one::<&str>("command") {
toysql.execute(command).await
toysql.execute(command)
} else {
toysql.run().await
toysql.run()
}
}

Expand All @@ -56,9 +54,9 @@ struct ToySQL {

impl ToySQL {
/// Creates a new ToySQL REPL for the given server host and port
async fn new(host: &str, port: u16) -> Result<Self> {
fn new(host: &str, port: u16) -> Result<Self> {
Ok(Self {
client: Client::new((host, port)).await?,
client: Client::new((host, port))?,
editor: Editor::new()?,
history_path: std::env::var_os("HOME")
.map(|home| std::path::Path::new(&home).join(".toysql.history")),
Expand All @@ -67,18 +65,18 @@ impl ToySQL {
}

/// Executes a line of input
async fn execute(&mut self, input: &str) -> Result<()> {
fn execute(&mut self, input: &str) -> Result<()> {
if input.starts_with('!') {
self.execute_command(input).await
self.execute_command(input)
} else if !input.is_empty() {
self.execute_query(input).await
self.execute_query(input)
} else {
Ok(())
}
}

/// Handles a REPL command (prefixed by !, e.g. !help)
async fn execute_command(&mut self, input: &str) -> Result<()> {
fn execute_command(&mut self, input: &str) -> Result<()> {
let mut input = input.split_ascii_whitespace();
let command = input.next().ok_or_else(|| Error::Parse("Expected command.".to_string()))?;

Expand Down Expand Up @@ -116,7 +114,7 @@ The following commands are also available:
"#
),
"!status" => {
let status = self.client.status().await?;
let status = self.client.status()?;
let mut node_logs = status
.raft
.last_index
Expand Down Expand Up @@ -166,11 +164,11 @@ Storage: {keys} keys, {logical_size} MB logical, {nodes}x {disk_size} MB disk,
}
"!table" => {
let args = getargs(1)?;
println!("{}", self.client.get_table(args[0]).await?);
println!("{}", self.client.get_table(args[0])?);
}
"!tables" => {
getargs(0)?;
for table in self.client.list_tables().await? {
for table in self.client.list_tables()? {
println!("{}", table)
}
}
Expand All @@ -180,8 +178,8 @@ Storage: {keys} keys, {logical_size} MB logical, {nodes}x {disk_size} MB disk,
}

/// Runs a query and displays the results
async fn execute_query(&mut self, query: &str) -> Result<()> {
match self.client.execute(query).await? {
fn execute_query(&mut self, query: &str) -> Result<()> {
match self.client.execute(query)? {
ResultSet::Begin { version, read_only } => match read_only {
false => println!("Began transaction at new version {}", version),
true => println!("Began read-only transaction at version {}", version),
Expand Down Expand Up @@ -237,7 +235,7 @@ Storage: {keys} keys, {logical_size} MB logical, {nodes}x {disk_size} MB disk,
}

/// Runs the ToySQL REPL
async fn run(&mut self) -> Result<()> {
fn run(&mut self) -> Result<()> {
if let Some(path) = &self.history_path {
match self.editor.load_history(path) {
Ok(_) => {}
Expand All @@ -252,14 +250,14 @@ Storage: {keys} keys, {logical_size} MB logical, {nodes}x {disk_size} MB disk,
rustyline::Cmd::Noop,
);

let status = self.client.status().await?;
let status = self.client.status()?;
println!(
"Connected to toyDB node \"{}\". Enter !help for instructions.",
status.raft.server
);

while let Some(input) = self.prompt()? {
match self.execute(&input).await {
match self.execute(&input) {
Ok(()) => {}
error @ Err(Error::Internal(_)) => return error,
Err(error) => println!("Error: {}", error),
Expand Down
Loading

0 comments on commit 534719f

Please sign in to comment.