Skip to content

Commit

Permalink
feat: decode input
Browse files Browse the repository at this point in the history
  • Loading branch information
care0717 committed Mar 2, 2021
1 parent 55d80f4 commit 933ba0a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# lsm_engine
LSMツリーインデックスベースのKVS
LSMツリーインデックスベースのKVS
memcachedプロトコル準拠
6 changes: 6 additions & 0 deletions src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ fn handler(stream: TcpStream) -> Result<(), Error> {

std::io::stdin().read_line(&mut input)?;

if input.split_whitespace().next().map_or(false, |v| v == "set") {
let mut body = String::new();
std::io::stdin().read_line(&mut body)?;
input = input + &*body;
}

let mut writer = BufWriter::new(&stream);
writer.write(input.as_bytes())?;
writer.flush()?;
Expand Down
31 changes: 19 additions & 12 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::io::{Error, Write};
use std::io::{BufWriter, Write, Error, stdout, ErrorKind};
use std::net::{TcpListener, TcpStream};
use std::io::BufRead;
use std::io::{BufReader, BufWriter};
use std::thread;
use lsm_engine::decoder;

fn main() {
let listener = TcpListener::bind("0.0.0.0:33333").expect("Error. failed to bind.");
Expand All @@ -20,17 +19,25 @@ fn main() {

fn handler(stream: TcpStream) -> Result<(), Error> {
println!("Connection from {}", stream.peer_addr()?);

let mut reader = BufReader::new(&stream);
let mut decoder = decoder::new(&stream);
let mut writer = BufWriter::new(&stream);
loop {
let mut buffer = String::new();
let nbytes = reader.read_line(&mut buffer)?;
if nbytes == 0 {
return Ok(());
let decoded = decoder.decode();
match decoded {
Ok(d) => {
print!("{}", d);
writer.write(d.as_bytes())?;
writer.flush()?;
}
Err(e) => {
print!("{}", e);
if e.kind() == ErrorKind::UnexpectedEof {
return Err(e)
}
writer.write(format!("{}\n", e.to_string()).as_bytes())?;
writer.flush()?;
}
}
print!("{}", buffer);
writer.write(buffer.as_bytes())?;
writer.flush()?;
stdout().flush()?;
}
}
58 changes: 58 additions & 0 deletions src/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::io::{BufRead, Error, ErrorKind, Read, BufReader};

pub struct Decoder<R: Read> {
reader: BufReader<R>
}

pub fn new<R: Read>(reader: R) -> Decoder<R> {
let r = BufReader::new(reader);
Decoder{reader: r}
}

impl<R: Read> Decoder<R> {
pub fn decode(&mut self) -> Result<&str, Error> {
let mut buf = String::new();
let nbytes = self.reader.read_line(&mut buf)?;
if nbytes == 0 {
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n"))
}
let commands: Vec<&str> = buf.trim().split_whitespace().collect();


commands.clone().first()
.ok_or(Error::new(ErrorKind::InvalidInput, "no content\n"))
.and_then(
move |c| match c {
&"set" => self.decode_set(commands),
&"get" => self.decode_get(commands),
_ => Err(Error::new(ErrorKind::InvalidInput, format!("unknown command: {}\n", c)))
}
)
}

fn decode_set(&mut self, commands: Vec<&str>) -> Result<&str, Error> {
if commands.len() != 5 {
return Err(Error::new(ErrorKind::InvalidInput, "set command length must be 5\n"))
}
let _key = commands[1];
let _flag = commands[2];
let _exptime = commands[3];
let _bytes = commands[4].parse::<usize>().map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;

let mut buf = String::new();
let nbytes = self.reader.read_line(&mut buf)?;
if nbytes == 0 {
return Err(Error::new(ErrorKind::UnexpectedEof, "got eof\n"))
}
Ok("STORED\n")
}

fn decode_get(&mut self, commands: Vec<&str>) -> Result<&str, Error> {
if commands.len() != 2 {
return Err(Error::new(ErrorKind::InvalidInput, "get command length must be 2\n"))
}
let _key = commands[1];
Ok("key\n")
}
}

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod decoder;

0 comments on commit 933ba0a

Please # to comment.