<commit_before>/*
 * An example redis server accepting GET and SET requests
 * listening on 127.0.0.1:8000.
 *
 * You can test it with "redis-cli -p 8000".
 *
 * Copyright (c) 2014 by <NAME>
 *
 */

extern crate redis = "redis#0.1";
extern crate sync = "sync#0.10-pre";

use std::io::net::ip::SocketAddr;
use std::io::net::tcp::{TcpListener,TcpStream};
use std::io::{Listener,Acceptor,Writer};
use std::io::BufferedStream;
use std::task;
use std::hashmap::HashMap;
use sync::RWArc;

fn handle_connection(conn: TcpStream, shared_ht: RWArc<HashMap<~[u8],~[u8]>>) {
    debug!("Got connection");

    let mut io = BufferedStream::new(conn);

    loop {
        match redis::parse(&mut io).unwrap() {
            redis::List([redis::Data(/*GET*/[71, 69, 84]), redis::Data(key)]) => {
                debug!("GET: {:s}", std::str::from_utf8(key).unwrap());
                let mut cwr = redis::CommandWriter::new();
                shared_ht.read(|ht| {
                    match ht.find(&key) {
                        Some(val) => {
                            cwr.args(1);
                            cwr.arg_bin(*val);
                        }
                        None => {
                            cwr.nil();
                        }
                    }
                });
                cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
            }
            redis::List([redis::Data(/*SET*/[83, 69, 84]), redis::Data(key),
                         redis::Data(val)]) => {
                debug!("SET: {:s} {:?}", std::str::from_utf8(key).unwrap(), val);

                shared_ht.write(|ht| ht.insert(key.clone(), val.clone()));
                let mut cwr = redis::CommandWriter::new();
                cwr.status("OK");
                cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
            }
            _ => {
                let mut cwr = redis::CommandWriter::new();
                cwr.error("Invalid Command");
                cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
            }
        }
    }
}

fn main() {
    let addr: SocketAddr = from_str("127.0.0.1:8000").unwrap();
    let shared_ht = RWArc::new(HashMap::new());

    match TcpListener::bind(addr) {
        Ok(listener) => {
            match listener.listen() {
                Ok(ref mut acceptor) => {
                    loop {
                        match acceptor.accept() {
                            Ok(conn) => {
                                let ht = shared_ht.clone();
                                task::spawn(proc() {
                                    handle_connection(conn, ht)
                                });
                            }
                            Err(_) => {}
                        }
                    }
                }
                Err(_) => {}
            }
        }
        Err(_) => {}
    }
}
<commit_msg>Get rid of vector patterns<commit_after>/*
 * An example redis server accepting GET and SET requests
 * listening on 127.0.0.1:8000.
 *
 * You can test it with "redis-cli -p 8000".
 *
 * Copyright (c) 2014 by <NAME>
 *
 */

extern crate redis = "redis#0.1";
extern crate sync = "sync#0.10-pre";

use std::io::net::ip::SocketAddr;
use std::io::net::tcp::{TcpListener,TcpStream};
use std::io::{Listener,Acceptor,Writer};
use std::io::BufferedStream;
use std::task;
use std::hashmap::HashMap;
use sync::RWArc;

fn handle_connection(conn: TcpStream, shared_ht: RWArc<HashMap<~[u8],~[u8]>>) {
    debug!("Got connection");

    let mut io = BufferedStream::new(conn);

    loop {
        match redis::parse(&mut io).unwrap() {
            redis::List(ref lst) => {
                match (lst.len(), lst.get(0), lst.get(1), lst.get(2)) {
                    (2, Some(redis::Data(/*GET*/[71, 69, 84])), Some(redis::Data(key)))  => {
                        debug!("GET: {:s}", std::str::from_utf8(key).unwrap());
                        let mut cwr = redis::CommandWriter::new();
                        shared_ht.read(|ht| {
                            match ht.find(&key) {
                                Some(val) => {
                                    cwr.args(1);
                                    cwr.arg_bin(*val);
                                }
                                None => {
                                    cwr.nil();
                                }
                            }
                        });
                        cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
                        continue;
                    }

                    (3, redis::Data(/*SET*/[83, 69, 84]), redis::Data(key),
                                                          redis::Data(val)) => {

                        debug!("SET: {:s} {:?}", std::str::from_utf8(key).unwrap(), val);
                        shared_ht.write(|ht| ht.insert(key.clone(), val.clone()));
                        let mut cwr = redis::CommandWriter::new();
                        cwr.status("OK");
                        cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
                        continue;
                    }

                    _ => { /* fallthrough: error */ }
                }
            }
            _ => { /* fallthrough: error */ }
        }

        /* error */
        let mut cwr = redis::CommandWriter::new();
        cwr.error("Invalid Command");
        cwr.with_buf(|bytes| {io.write(bytes); io.flush()});
    }
}

fn main() {
    let addr: SocketAddr = from_str("127.0.0.1:8000").unwrap();
    let shared_ht = RWArc::new(HashMap::new());

    match TcpListener::bind(addr) {
        Ok(listener) => {
            match listener.listen() {
                Ok(ref mut acceptor) => {
                    loop {
                        match acceptor.accept() {
                            Ok(conn) => {
                                let ht = shared_ht.clone();
                                task::spawn(proc() {
                                    handle_connection(conn, ht)
                                });
                            }
                            Err(_) => {}
                        }
                    }
                }
                Err(_) => {}
            }
        }
        Err(_) => {}
    }
}

{'fraction_non_alphanumeric': 0.15717249082495613, 'fraction_numerical': 0.01675442795595979, 'mean_word_length': 1.1893119105833043, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2616511', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1755, 'n_words': 308}