Skip to content

Commit

Permalink
feat: add stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
care0717 committed Jun 20, 2021
1 parent 1ba9f82 commit 63e4d2e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/avl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ impl<T: Ord + Clone + Sync + Send, U: Clone + Sync + Send> Memtable<T, U> for Av
fn search(&self, key: &T) -> Option<&U> {
self.root.as_ref().map_or(None, |node| node.search(key))
}
fn to_vec(&self) -> Vec<(&T, &U)> {
self.iter().collect()
}
}

impl<'a, T: 'a + Ord + Clone + Sync + Send, U: Clone + Sync + Send> AvlTreeMap<T, U> {
fn iter(&'a self) -> AvlTreeSetIter<'a, T, U> {
impl<T: Ord + Clone + Sync + Send, U: Clone + Sync + Send> AvlTreeMap<T, U> {
fn iter(&self) -> AvlTreeSetIter<'_, T, U> {
AvlTreeSetIter {
prev_nodes: Vec::new(),
current_tree: &self.root,
Expand Down
26 changes: 15 additions & 11 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use crate::command::Command::{Delete, Get, Set};
use crate::command::Command::{Delete, Get, Set, Stats};
use crate::value::Value;

pub enum Command {
Set { key: String, value: Value },
Get { key: String },
Delete { key: String },
Stats {},
}

pub fn new_command_set<'a>(key: String, value: Value) -> Command {
return Set { key, value };
}

pub fn new_command_get(key: String) -> Command {
return Get { key };
}

pub fn new_command_delete(key: String) -> Command {
return Delete { key };
impl Command {
pub fn new_set(key: String, value: Value) -> Self {
Set { key, value }
}
pub fn new_get(key: String) -> Self {
Get { key }
}
pub fn new_delete(key: String) -> Self {
Delete { key }
}
pub fn new_stats() -> Self {
Stats {}
}
}
21 changes: 11 additions & 10 deletions src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::command;
use crate::command::Command;
use crate::value::Value;
use std::io;
use std::io::BufRead;
Expand All @@ -13,7 +13,7 @@ pub fn new<R: io::Read>(reader: R) -> Decoder<R> {
}

impl<R: io::Read> Decoder<R> {
pub fn decode(&mut self) -> Result<command::Command, io::Error> {
pub fn decode(&mut self) -> Result<Command, io::Error> {
let mut buf = String::new();
let nbytes = self.reader.read_line(&mut buf)?;
if nbytes == 0 {
Expand All @@ -29,14 +29,15 @@ impl<R: io::Read> Decoder<R> {
&"set" => self.decode_set(commands),
&"get" => self.decode_get(commands),
&"delete" => self.decode_delete(commands),
&"stats" => Ok(Command::new_stats()),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("unknown command: {}\n", c),
)),
})
}

fn decode_set(&mut self, commands: Vec<&str>) -> Result<command::Command, io::Error> {
fn decode_set(&mut self, commands: Vec<&str>) -> Result<Command, io::Error> {
if commands.len() != 5 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
Expand All @@ -59,27 +60,27 @@ impl<R: io::Read> Decoder<R> {
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "got eof\n"));
}
let value = Value::new(buf.trim().parse().unwrap(), flags, exptime);
Ok(command::new_command_set(key.to_string(), value))
Ok(Command::new_set(key.to_string(), value))
}

fn decode_get(&mut self, commands: Vec<&str>) -> Result<command::Command, io::Error> {
fn decode_get(&self, commands: Vec<&str>) -> Result<Command, io::Error> {
if commands.len() != 2 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"get command length must be 2\n",
));
}
let _key = commands[1];
Ok(command::new_command_get(_key.to_string()))
let key = commands[1];
Ok(Command::new_get(key.to_string()))
}
fn decode_delete(&mut self, commands: Vec<&str>) -> Result<command::Command, io::Error> {
fn decode_delete(&self, commands: Vec<&str>) -> Result<Command, io::Error> {
if commands.len() != 2 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"delete command length must be 2\n",
));
}
let _key = commands[1];
Ok(command::new_command_delete(_key.to_string()))
let key = commands[1];
Ok(Command::new_delete(key.to_string()))
}
}
6 changes: 5 additions & 1 deletion src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Executor {
file.write(&binary)?;
let binary_len = binary.len() as i32;
file.write(&binary_len.to_le_bytes())?;
memtable.write()?.insert(key, value);
memtable.insert(key, value);
Ok("STORED".to_string())
}
Command::Get { key } => {
Expand All @@ -51,6 +51,10 @@ impl Executor {
memtable.delete(&key);
Ok("DELETED".to_string())
}
Command::Stats {} => {
let memtable = self.memtable.read()?;
Ok(format!("STAT curr_items {}", memtable.to_vec().len()))
},
}
}
}
1 change: 1 addition & 0 deletions src/memtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub trait Memtable<T, U>: Sync + Send {
fn insert(&mut self, key: T, value: U);
fn delete(&mut self, key: &T);
fn search(&self, key: &T) -> Option<&U>;
fn to_vec(&self) -> Vec<(&T, &U)>;
}
3 changes: 1 addition & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ impl Value {
let data = String::from_utf8(vec[index..(index + data_len)].to_vec())?;

index -= size_of::<usize>();
let exptime =
usize::from_le_bytes(vec[index..(index + size_of::<usize>())].try_into()?);
let exptime = usize::from_le_bytes(vec[index..(index + size_of::<usize>())].try_into()?);

index -= size_of::<usize>();
let flags = usize::from_le_bytes(vec[index..(index + size_of::<usize>())].try_into()?);
Expand Down

0 comments on commit 63e4d2e

Please # to comment.