Skip to content

Commit

Permalink
added js client
Browse files Browse the repository at this point in the history
  • Loading branch information
0b01 committed Oct 8, 2017
1 parent a92902f commit f8ffc33
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Language bindings:

- [x] Rust

- [ ] Python
- [x] Python

- [ ] JavaScript

Expand Down
26 changes: 26 additions & 0 deletions javascript-client/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var net = require('net');



class TectonicDB {

constructor(host, port) {
this.client = new net.Socket();
this.client.connect(9001, '127.0.0.1', function() {
console.log('Connected');
});
}

cmd(command) {
this.client.write(`${commmand}\n`);
}

this.client.on('data', function(data) {
console.log('Received: ' + typeof data);
client.destroy();
});

client.on('close', function() {
console.log('Connection closed');
});
}
11 changes: 3 additions & 8 deletions src/bin/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ struct Cxn {
impl Cxn {
fn cmd(&mut self, command : &str) -> String {
let _ = self.stream.write(command.as_bytes());
let success = self.stream.read_u8().unwrap() == 0x00000001;
if command.starts_with("GET") && success {
let success = self.stream.read_u8().unwrap() == 0x1;
if command.starts_with("GET") && !command.contains("AS JSON") && success {
let vecs = dtf::read_one_batch(&mut self.stream);
format!("[{}]\n", update_vec_to_json(&vecs))
format!("[{}]\n", dtf::update_vec_to_json(&vecs))
} else {
let size = self.stream.read_u64::<BigEndian>().unwrap();
let mut buf = vec![0; size as usize];
Expand All @@ -29,11 +29,6 @@ impl Cxn {
}
}

fn update_vec_to_json(vecs: &Vec<dtf::Update>) -> String {
let objects : Vec<String> = vecs.clone().into_iter().map(|up| up.to_json()).collect();
objects.join(", ")
}

fn main() {
let matches = App::new("tectonic-cli")
.version("0.0.1")
Expand Down
56 changes: 36 additions & 20 deletions src/bin/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ fn gen_response(string : &str, state: &mut State) -> (Option<String>, Option<Vec
state.is_adding = false;
(Some("1\n".to_owned()), None)
},
"GET ALL" => {
match state.get(-1) {
Some(bytes) => (None, Some(bytes)),
None => (None, None)
}
"GET ALL AS JSON" => {
let current_store = state.store.get(&state.current_store_name).unwrap();
let json = dtf::update_vec_to_json(&current_store.v);
let json = format!("[{}]\n", json);
(Some(json), None)
},
"GETALL" => {
"GET ALL" => {
match state.get(-1) {
Some(bytes) => (None, Some(bytes)),
None => (None, None)
Expand All @@ -224,7 +224,7 @@ fn gen_response(string : &str, state: &mut State) -> (Option<String>, Option<Vec
current_store.clear();
(Some("1\n".to_owned()), None)
},
"CLEARALL" => {
"CLEAR ALL" => {
for store in state.store.values_mut() {
store.clear();
}
Expand All @@ -235,7 +235,7 @@ fn gen_response(string : &str, state: &mut State) -> (Option<String>, Option<Vec
current_store.flush();
(Some("1\n".to_owned()), None)
},
"FLUSHALL" => {
"FLUSH ALL" => {
for store in state.store.values() {
store.flush();
}
Expand Down Expand Up @@ -295,11 +295,24 @@ fn gen_response(string : &str, state: &mut State) -> (Option<String>, Option<Vec
// get
if string.starts_with("GET ") {
let num : &str = &string[4..];
let count = num.parse::<i32>().unwrap();

match state.get(count) {
Some(bytes) => (None, Some(bytes)),
None => (None, None)
let count : Vec<&str> = num.split(" ").collect();
let count = count[0].parse::<i32>().unwrap();

if string.contains("AS JSON") {
let current_store = state.store.get(&state.current_store_name).unwrap();

if (current_store.size as i32) <= count || current_store.size == 0 {
(None, None)
} else {
let json = dtf::update_vec_to_json(&current_store.v[..count as usize]);
let json = format!("[{}]\n", json);
(Some(json), None)
}
} else {
match state.get(count) {
Some(bytes) => (None, Some(bytes)),
None => (None, None)
}
}
}

Expand Down Expand Up @@ -337,10 +350,7 @@ fn init_dbs(dtf_folder : &str, state: &mut State) {
}
}

fn handle_client(mut stream: TcpStream, settings : &Settings) {
let dtf_folder = &settings.dtf_folder;
create_dir_if_not_exist(&dtf_folder);

fn init_state(settings: &Settings, dtf_folder: &str) -> State {
let mut state = State {
current_store_name: "default".to_owned(),
is_adding: false,
Expand All @@ -356,7 +366,13 @@ fn handle_client(mut stream: TcpStream, settings : &Settings) {
in_memory: default_in_memory,
folder: dtf_folder.to_owned(),
});
state
}

fn handle_client(mut stream: TcpStream, settings : &Settings) {
let dtf_folder = &settings.dtf_folder;
create_dir_if_not_exist(&dtf_folder);
let mut state = init_state(&settings, &dtf_folder);
init_dbs(&dtf_folder, &mut state);

let mut buf = [0; 2048];
Expand All @@ -368,16 +384,16 @@ fn handle_client(mut stream: TcpStream, settings : &Settings) {
let resp = gen_response(&req, &mut state);
match resp {
(Some(str_resp), None) => {
stream.write_u8(0x00000001).unwrap();
stream.write_u8(0x1).unwrap();
stream.write_u64::<BigEndian>(str_resp.len() as u64).unwrap();
stream.write(str_resp.as_bytes()).unwrap()
},
(None, Some(bytes)) => {
stream.write_u8(0x00000001).unwrap();
stream.write_u8(0x1).unwrap();
stream.write(&bytes).unwrap()
},
(None, None) => {
stream.write_u8(0x00000000).unwrap();
stream.write_u8(0x0).unwrap();
let ret = "ERR.\n";
stream.write_u64::<BigEndian>(ret.len() as u64).unwrap();
stream.write(ret.as_bytes()).unwrap()
Expand Down
5 changes: 5 additions & 0 deletions src/lib/dtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ impl Update {
}
}

pub fn update_vec_to_json(vecs: &[Update]) -> String {
let objects : Vec<String> = vecs.clone().into_iter().map(|up| up.to_json()).collect();
objects.join(", ")
}

impl Ord for Update {
fn cmp(&self, other: &Update) -> Ordering {
return self.partial_cmp(other).unwrap();
Expand Down

0 comments on commit f8ffc33

Please sign in to comment.