From c1513bf139a455a3c6618482c26df648edd66978 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Sun, 22 Apr 2018 22:05:01 +0200 Subject: [PATCH] map: Update the map creation and loading --- src/bin/raptor-cli.rs | 4 ++-- src/bin/raptor.rs | 20 +++++++++++++++++++- src/lib.rs | 19 ++++++++++++------- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/bin/raptor-cli.rs b/src/bin/raptor-cli.rs index d7725b17b..6082243b3 100644 --- a/src/bin/raptor-cli.rs +++ b/src/bin/raptor-cli.rs @@ -53,8 +53,8 @@ fn main() { let description = product.ft.split_whitespace().filter(|&s| s != "Description"); let words = title.chain(description) .filter(|&s| s.chars().any(|c| c.is_alphabetic())) // remove that ? - .filter(|&s| !common_words.contains(s)) - .map(|s| s.to_lowercase()); + .map(|s| s.trim_matches(|c: char| !c.is_alphabetic()).to_lowercase()) + .filter(|s| !common_words.contains(s)); for word in words { builder.insert(word, product.product_id); diff --git a/src/bin/raptor.rs b/src/bin/raptor.rs index bf6da7357..99e07af5c 100644 --- a/src/bin/raptor.rs +++ b/src/bin/raptor.rs @@ -9,6 +9,9 @@ extern crate tokio_service; extern crate url; use std::io; +use std::path::Path; +use std::fs::File; +use std::io::{Read, BufReader}; use fst_levenshtein::Levenshtein; use fst::{IntoStreamer, Streamer}; @@ -35,6 +38,8 @@ impl Service for MainService { let url = url::Url::parse(&url).unwrap(); let mut resp = Response::new(); + resp.header("Content-Type", "text/html"); + resp.header("charset", "utf-8"); if let Some((_, key)) = url.query_pairs().find(|&(ref k, _)| k == "q") { let key = key.to_lowercase(); @@ -56,6 +61,16 @@ impl Service for MainService { } } +fn read_to_vec>(path: P) -> io::Result> { + let file = File::open(path)?; + let mut file = BufReader::new(file); + + let mut vec = Vec::new(); + file.read_to_end(&mut vec)?; + + Ok(vec) +} + fn main() { drop(env_logger::init()); let addr = "0.0.0.0:8080".parse().unwrap(); @@ -66,7 +81,10 @@ fn main() { // closure, make it global. // It will permit the server to be multithreaded. - let map = unsafe { MultiMap::from_paths("map.fst", "values.vecs").unwrap() }; + 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 !"); diff --git a/src/lib.rs b/src/lib.rs index 9d494c80e..78e2a8d80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,9 @@ extern crate bincode; extern crate fst; -extern crate serde; -extern crate serde_json; -#[macro_use] extern crate serde_derive; extern crate smallvec; use std::ops::{Deref, DerefMut}; -use std::io::Write; +use std::io::{Write, BufReader}; use std::fs::File; use std::path::Path; use std::str::from_utf8_unchecked; @@ -30,9 +27,17 @@ impl MultiMap { { let map = fst::Map::from_path(map)?; - // TODO handle error !!! - let values_file = File::open(values).unwrap(); - let values = bincode::deserialize_from(values_file).unwrap(); + // TODO handle errors !!! + let values = File::open(values).unwrap(); + let values = BufReader::new(values); + let values = bincode::deserialize_from(values).unwrap(); + + Ok(MultiMap { map, values }) + } + + pub fn from_bytes(map: Vec, values: &[u8]) -> fst::Result { + let map = fst::Map::from_bytes(map)?; + let values = bincode::deserialize(values).unwrap(); Ok(MultiMap { map, values }) }