Skip to content

Commit

Permalink
Implement RECENT
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 4, 2019
1 parent 033e0b1 commit 9b7b56d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
21 changes: 13 additions & 8 deletions src/lib/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub enum Request {
Push { channel_id: String, value: String },
// Retrieve { channel_id: String, value: String },
// Update { channel_id: String, value: String },
Recent { channel_id: String, },
Recent { channel_id: String, count: usize},
}

pub enum Response {
Push {
message: Message,
},
Recent {
channel_id: String,
messages: Vec<Message>,
},
Error {
msg: String,
Expand All @@ -21,12 +21,12 @@ pub enum Response {

impl Request {
pub fn parse(input: &str) -> Result<Request, String> {
println!("Incoming: {:?}", &input);
let mut parts = input.splitn(3, " ");
let channel_id = match parts.next() {
Some(channel_id) => channel_id,
None => return Err(format!("PUSH needs a channel_id")),
};
println!("Incoming on {:?}", channel_id);
match parts.next() {
// Some("RETRIEVE") => {
// let key = match parts.next() {
Expand All @@ -45,16 +45,21 @@ impl Request {
Some(value) => value,
None => return Err(format!("PUSH needs a value")),
};
println!("DOING PUSH on {} with {}", channel_id, value);
Ok(Request::Push {
channel_id: channel_id.to_string(),
value: value.to_string(),
})
}
Some("RECENT") => {
println!("DOING RECENT on {}", channel_id);
let count = match parts.next() {
Some("") => "5",
Some(count) => count,
_ => "5",
};
println!("count {:?}", count);
Ok(Request::Recent {
channel_id: channel_id.to_string(),
count: count.parse::<usize>().unwrap()
})
}
Some(cmd) => Err(format!("unknown command: {}", cmd)),
Expand All @@ -69,9 +74,9 @@ impl Response {
Response::Push { ref message } => {
format!("Pushed Id {}", message.uuid)
},
Response::Recent { ref channel_id } => {
println!("recent {:?}", channel_id);
format!("Recent on {}", channel_id)
Response::Recent { ref messages } => {
println!("recent {:?}", messages);
format!("Recent messages {:?}", messages.len())
},
// Response::Value { ref key, ref value } => format!("{} = {}", key, value),
// Response::Set {
Expand Down
26 changes: 18 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::net::SocketAddr;
use std::collections::HashMap;
use chrono::{Utc};
use lib::{state, types};

use uuid::Uuid;
use std::cmp;

const MAXIMUM: usize = 10;

fn main() -> Result<(), Box<std::error::Error>> {
let addr = env::args().nth(1).unwrap_or("127.0.0.1:6363".to_string());
Expand Down Expand Up @@ -46,7 +48,7 @@ fn main() -> Result<(), Box<std::error::Error>> {
};

match request {
types::Request::Push { channel_id, value: _ } => {
types::Request::Push { channel_id, value } => {
let mut channels = db.channels.lock().unwrap();
if !channels.contains_key(&channel_id) {
channels.insert(channel_id.clone(), state::Channel {
Expand All @@ -66,7 +68,7 @@ fn main() -> Result<(), Box<std::error::Error>> {
let message = state::Message {
uuid: uuid.clone(),
created: now,
value: String::new(),
value,
};
let length = data.len();
data.push(message.clone());
Expand All @@ -75,16 +77,24 @@ fn main() -> Result<(), Box<std::error::Error>> {
message,
}
}
types::Request::Recent { channel_id } => {
types::Request::Recent { channel_id, count } => {
let channels = db.channels.lock().unwrap();
let _channel = channels.get(&channel_id);
let channel = _channel.unwrap();
let data = channel.data.lock().unwrap();
let index: usize = data.len() - 1;
let recent = &data[index..];
println!("{:?}", recent);
let index: usize = {
if data.len() < cmp::min(count, MAXIMUM) {
0
} else {
data.len() - cmp::min(count, MAXIMUM)
}
};
// TODO:
// - Make sure that index is not greater than a const MAXIMUM
println!("index {:?}", index);
let messages = &data[index..];
types::Response::Recent {
channel_id,
messages: messages.to_vec(),
}
}
}
Expand Down

0 comments on commit 9b7b56d

Please sign in to comment.