Skip to content

Commit

Permalink
docs: guide to launch project
Browse files Browse the repository at this point in the history
* build: parser

* docs: write readme

* docs: more docs
  • Loading branch information
odysa authored Aug 1, 2021
1 parent 1edccc5 commit 3344dae
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
# Yet another kvs store

## Todo:
* compact log file
## run
Launch server first
```
cargo run --bin kvs-server --addr 127.0.0.1:4000
```
then launch client
```
cargo run --bin kvs-client set key value --addr 127.0.0.1:4000
```

available commands:
```
set key value
rm key
get key
```

## build
```
cargo build
```
## benchmark
```
cargo bench
```
## test
```
cargo test
```
2 changes: 1 addition & 1 deletion src/protocol/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::{

use crate::error::Result;

use super::protocol::Frame;
use super::frame::Frame;

struct Connection {
stream: BufWriter<TcpStream>,
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod connection;
mod protocol;
mod frame;
mod parser;
57 changes: 57 additions & 0 deletions src/protocol/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::{str, vec};

use super::frame::Frame;
use crate::error::{Error, Result};
use atoi::atoi;
use bytes::Bytes;

/// Parse a frame to command
pub struct Parser {
contents: vec::IntoIter<Frame>,
}

impl Parser {
pub fn new(&self, frame: Frame) -> Result<Self> {
let array = match frame {
Frame::Array(array) => array,
_ => return Err(Error::from("invalid frame".to_string())),
};

let contents = array.into_iter();

Ok(Parser { contents })
}

pub fn next(&mut self) -> Result<Frame> {
self.contents
.next()
.ok_or(Error::from("frame empty".to_string()))
}

pub fn next_string(&mut self) -> Result<String> {
match self.next()? {
Frame::Simple(v) => Ok(v),
Frame::Bulk(v) => Ok(str::from_utf8(&v)?.to_string()),
_ => Err(Error::from("invalid frame".to_string())),
}
}

pub fn next_int(&mut self) -> Result<u64> {
match self.next()? {
Frame::Simple(v) => {
atoi(&v.as_bytes()).ok_or(Error::from(format!("cannot convert {} to int", v)))
}
Frame::Integers(v) => Ok(v),
Frame::Bulk(v) => atoi(&v).ok_or(Error::from(format!("cannot convert {:?} to int", v))),
_ => Err(Error::from("invalid frame".to_string())),
}
}

pub fn next_bytes(&mut self) -> Result<Bytes> {
match self.next()? {
Frame::Simple(v) => Ok(Bytes::from(v.into_bytes())),
Frame::Bulk(v) => Ok(v),
_ => Err(Error::from("invalid frame".to_string())),
}
}
}

0 comments on commit 3344dae

Please sign in to comment.