http: Make the map static

This commit is contained in:
Kerollmops 2018-04-22 22:17:01 +02:00
parent c1513bf139
commit b729c76bce
No known key found for this signature in database
GPG Key ID: 016ACC0DC3ADC318
3 changed files with 17 additions and 17 deletions

1
Cargo.lock generated
View File

@ -317,6 +317,7 @@ dependencies = [
"fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fst-levenshtein 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fst-levenshtein 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -9,6 +9,7 @@ env_logger = { version = "0.3", default-features = false }
fst = "0.3" fst = "0.3"
fst-levenshtein = "0.2" fst-levenshtein = "0.2"
futures = "0.1" futures = "0.1"
lazy_static = "1.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

@ -2,6 +2,7 @@ extern crate env_logger;
extern crate fst; extern crate fst;
extern crate fst_levenshtein; extern crate fst_levenshtein;
extern crate futures; extern crate futures;
#[macro_use] extern crate lazy_static;
extern crate raptor; extern crate raptor;
extern crate tokio_minihttp; extern crate tokio_minihttp;
extern crate tokio_proto; extern crate tokio_proto;
@ -22,8 +23,17 @@ use tokio_service::Service;
use raptor::MultiMap; use raptor::MultiMap;
lazy_static! {
static ref MAP: MultiMap = {
let map = read_to_vec("map.fst").unwrap();
let values = read_to_vec("values.vecs").unwrap();
MultiMap::from_bytes(map, &values).unwrap()
};
}
struct MainService { struct MainService {
map: MultiMap, map: &'static MultiMap,
} }
impl Service for MainService { impl Service for MainService {
@ -47,13 +57,15 @@ impl Service for MainService {
let lev = Levenshtein::new(&key, 2).unwrap(); let lev = Levenshtein::new(&key, 2).unwrap();
let mut body = String::new(); let mut body = String::new();
body.push_str("<html><body>");
let mut stream = self.map.search(lev).into_stream(); let mut stream = self.map.search(lev).into_stream();
while let Some((key, values)) = stream.next() { while let Some((key, values)) = stream.next() {
let values = &values[..values.len().min(10)]; let values = &values[..values.len().min(10)];
body.push_str(&format!("{:?} {:?}\n", key, values)); body.push_str(&format!("{:?} {:?}</br>", key, values));
} }
body.push_str("</body></html>");
resp.body(&body); resp.body(&body);
} }
@ -75,19 +87,5 @@ fn main() {
drop(env_logger::init()); drop(env_logger::init());
let addr = "0.0.0.0:8080".parse().unwrap(); let addr = "0.0.0.0:8080".parse().unwrap();
TcpServer::new(Http, addr).serve(|| { TcpServer::new(Http, addr).serve(|| Ok(MainService { map: &MAP }))
// TODO move the MultiMap construction out of this
// closure, make it global.
// It will permit the server to be multithreaded.
let map = read_to_vec("map.fst").unwrap();
let values = read_to_vec("values.vecs").unwrap();
let map = MultiMap::from_bytes(map, &values).unwrap();
println!("Called Fn here !");
Ok(MainService { map })
})
} }