map: Update the map creation and loading

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

View File

@ -53,8 +53,8 @@ fn main() {
let description = product.ft.split_whitespace().filter(|&s| s != "Description"); let description = product.ft.split_whitespace().filter(|&s| s != "Description");
let words = title.chain(description) let words = title.chain(description)
.filter(|&s| s.chars().any(|c| c.is_alphabetic())) // remove that ? .filter(|&s| s.chars().any(|c| c.is_alphabetic())) // remove that ?
.filter(|&s| !common_words.contains(s)) .map(|s| s.trim_matches(|c: char| !c.is_alphabetic()).to_lowercase())
.map(|s| s.to_lowercase()); .filter(|s| !common_words.contains(s));
for word in words { for word in words {
builder.insert(word, product.product_id); builder.insert(word, product.product_id);

View File

@ -9,6 +9,9 @@ extern crate tokio_service;
extern crate url; extern crate url;
use std::io; use std::io;
use std::path::Path;
use std::fs::File;
use std::io::{Read, BufReader};
use fst_levenshtein::Levenshtein; use fst_levenshtein::Levenshtein;
use fst::{IntoStreamer, Streamer}; use fst::{IntoStreamer, Streamer};
@ -35,6 +38,8 @@ impl Service for MainService {
let url = url::Url::parse(&url).unwrap(); let url = url::Url::parse(&url).unwrap();
let mut resp = Response::new(); 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") { if let Some((_, key)) = url.query_pairs().find(|&(ref k, _)| k == "q") {
let key = key.to_lowercase(); let key = key.to_lowercase();
@ -56,6 +61,16 @@ impl Service for MainService {
} }
} }
fn read_to_vec<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
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() { 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();
@ -66,7 +81,10 @@ fn main() {
// closure, make it global. // closure, make it global.
// It will permit the server to be multithreaded. // 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 !"); println!("Called Fn here !");

View File

@ -1,12 +1,9 @@
extern crate bincode; extern crate bincode;
extern crate fst; extern crate fst;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate smallvec; extern crate smallvec;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::io::Write; use std::io::{Write, BufReader};
use std::fs::File; use std::fs::File;
use std::path::Path; use std::path::Path;
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
@ -30,9 +27,17 @@ impl MultiMap {
{ {
let map = fst::Map::from_path(map)?; let map = fst::Map::from_path(map)?;
// TODO handle error !!! // TODO handle errors !!!
let values_file = File::open(values).unwrap(); let values = File::open(values).unwrap();
let values = bincode::deserialize_from(values_file).unwrap(); let values = BufReader::new(values);
let values = bincode::deserialize_from(values).unwrap();
Ok(MultiMap { map, values })
}
pub fn from_bytes(map: Vec<u8>, values: &[u8]) -> fst::Result<MultiMap> {
let map = fst::Map::from_bytes(map)?;
let values = bincode::deserialize(values).unwrap();
Ok(MultiMap { map, values }) Ok(MultiMap { map, values })
} }