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 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);

View File

@ -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<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() {
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 !");

View File

@ -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<u8>, values: &[u8]) -> fst::Result<MultiMap> {
let map = fst::Map::from_bytes(map)?;
let values = bincode::deserialize(values).unwrap();
Ok(MultiMap { map, values })
}