Skip to content

Commit

Permalink
Begin retrieve command
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 5, 2019
1 parent 9b7b56d commit 5ea001e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 9 deletions.
81 changes: 81 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2018"
[dependencies]
tokio = "0.1"
uuid = { version = "0.7", features = ["v5"] }
chrono = "0.4"
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
4 changes: 2 additions & 2 deletions src/lib/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use chrono::{DateTime, Utc};
use serde::Serialize;

pub struct Channel {
pub index: Mutex<HashMap<String, usize>>,
Expand All @@ -11,8 +12,7 @@ pub struct Database {
pub channels: Arc<Mutex<HashMap<String, Channel>>>,
}

#[derive(Debug)]
#[derive(Clone)]
#[derive(Serialize, Debug, Clone)]
pub struct Message {
pub uuid: String,
pub created: DateTime<Utc>,
Expand Down
31 changes: 27 additions & 4 deletions src/lib/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::lib::state::Message;

pub enum Request {
Push { channel_id: String, value: String },
// Retrieve { channel_id: String, value: String },
Retrieve { channel_id: String, uuid: String },
// Update { channel_id: String, value: String },
Recent { channel_id: String, count: usize},
}
Expand All @@ -14,6 +14,12 @@ pub enum Response {
Recent {
messages: Vec<Message>,
},
Retrieve {
message: Message,
},
Foo {
message: String,
},
Error {
msg: String,
},
Expand Down Expand Up @@ -62,6 +68,16 @@ impl Request {
count: count.parse::<usize>().unwrap()
})
}
Some("RETRIEVE") => {
let uuid = match parts.next() {
Some(uuid) => uuid,
None => return Err(format!("RETRIEVE needs a uuid")),
};
Ok(Request::Retrieve {
channel_id: channel_id.to_string(),
uuid: uuid.to_string(),
})
}
Some(cmd) => Err(format!("unknown command: {}", cmd)),
None => Err(format!("empty input")),
}
Expand All @@ -71,12 +87,19 @@ impl Request {
impl Response {
pub fn serialize(&self) -> String {
match *self {
Response::Foo { ref message } => {
format!("foo {}", message)
},
Response::Push { ref message } => {
format!("Pushed Id {}", message.uuid)
format!("OK {}", message.uuid)
},
Response::Recent { ref messages } => {
println!("recent {:?}", messages);
format!("Recent messages {:?}", messages.len())
let serialized = serde_json::to_string(messages).unwrap();
format!("OK {}", serialized)
},
Response::Retrieve { ref message } => {
let serialized = serde_json::to_string(message).unwrap();
format!("OK {}", serialized)
},
// Response::Value { ref key, ref value } => format!("{} = {}", key, value),
// Response::Set {
Expand Down
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,26 @@ fn main() -> Result<(), Box<std::error::Error>> {
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 {
messages: messages.to_vec(),
}
}
types::Request::Retrieve { channel_id, uuid } => {
// let channels = db.channels.lock().unwrap();
// let _channel = channels.get(&channel_id);
// let channel = _channel.unwrap();
// let data = channel.data.lock().unwrap();
// let index = channel.index.lock().unwrap();
// let message_index = &index.get(&uuid).unwrap();
// println!("index {:?}", message_index);
// let message = &data[message_index];
types::Response::Foo {
message: "bar".to_string(),
}
}
}
});
let writes = responses.fold(writer, |writer, response| {
Expand Down

0 comments on commit 5ea001e

Please sign in to comment.