2018-05-27 15:23:43 +02:00
|
|
|
extern crate env_logger;
|
2018-07-06 21:26:07 +02:00
|
|
|
extern crate rocksdb;
|
2018-05-27 15:23:43 +02:00
|
|
|
extern crate fst;
|
|
|
|
extern crate raptor;
|
2018-06-24 19:27:24 +02:00
|
|
|
extern crate elapsed;
|
2018-05-27 15:23:43 +02:00
|
|
|
|
2018-06-25 22:26:49 +02:00
|
|
|
use std::env;
|
2018-07-06 21:26:07 +02:00
|
|
|
use std::str::from_utf8_unchecked;
|
2018-06-24 19:27:24 +02:00
|
|
|
use std::io::{self, Write};
|
|
|
|
use elapsed::measure_time;
|
2018-05-27 15:23:43 +02:00
|
|
|
use fst::Streamer;
|
2018-07-06 21:26:07 +02:00
|
|
|
use rocksdb::{DB, DBOptions};
|
2018-06-24 19:27:24 +02:00
|
|
|
use raptor::{load_map, DocIndexMap, RankedStream, LevBuilder};
|
2018-05-27 15:23:43 +02:00
|
|
|
|
2018-07-06 21:26:07 +02:00
|
|
|
fn search(map: &DocIndexMap, lev_builder: &LevBuilder, db: &DB, query: &str) {
|
2018-05-27 15:23:43 +02:00
|
|
|
let mut automatons = Vec::new();
|
|
|
|
for query in query.split_whitespace() {
|
2018-07-06 20:58:06 +02:00
|
|
|
let lev = lev_builder.get_automaton(query);
|
2018-05-27 15:23:43 +02:00
|
|
|
automatons.push(lev);
|
|
|
|
}
|
|
|
|
|
2018-06-24 19:37:38 +02:00
|
|
|
let mut stream = RankedStream::new(&map, map.values(), automatons, 20);
|
2018-07-06 22:05:51 +02:00
|
|
|
while let Some(document) = stream.next() {
|
|
|
|
print!("{:?} ", document.document_id);
|
2018-06-24 19:27:24 +02:00
|
|
|
|
2018-07-06 22:05:51 +02:00
|
|
|
let title_key = format!("{}-title", document.document_id);
|
2018-07-06 21:26:07 +02:00
|
|
|
let title = db.get(title_key.as_bytes()).unwrap().unwrap();
|
|
|
|
let title = unsafe { from_utf8_unchecked(&title) };
|
|
|
|
print!("{:?}", title);
|
2018-06-24 19:27:24 +02:00
|
|
|
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
drop(env_logger::init());
|
|
|
|
|
2018-07-06 20:58:06 +02:00
|
|
|
let (elapsed, map) = measure_time(|| load_map("map.fst", "values.vecs").unwrap());
|
|
|
|
println!("{} to load the map", elapsed);
|
|
|
|
|
|
|
|
let (elapsed, lev_builder) = measure_time(|| LevBuilder::new());
|
|
|
|
println!("{} to load the levenshtein automaton", elapsed);
|
2018-06-24 19:27:24 +02:00
|
|
|
|
2018-07-06 21:26:07 +02:00
|
|
|
let (elapsed, db) = measure_time(|| {
|
|
|
|
let opts = DBOptions::new();
|
|
|
|
let error_if_log_file_exist = false;
|
|
|
|
DB::open_for_read_only(opts, "rocksdb/storage", error_if_log_file_exist).unwrap()
|
|
|
|
});
|
|
|
|
println!("{} to load the rocksdb DB", elapsed);
|
|
|
|
|
2018-06-24 19:27:24 +02:00
|
|
|
match env::args().nth(1) {
|
|
|
|
Some(query) => {
|
|
|
|
println!("Searching for: {:?}", query);
|
|
|
|
let query = query.to_lowercase();
|
2018-07-06 21:26:07 +02:00
|
|
|
let (elapsed, _) = measure_time(|| search(&map, &lev_builder, &db, &query));
|
2018-06-24 19:27:24 +02:00
|
|
|
println!("Finished in {}", elapsed);
|
|
|
|
},
|
|
|
|
None => loop {
|
|
|
|
print!("Searching for: ");
|
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
|
|
|
|
let mut query = String::new();
|
|
|
|
io::stdin().read_line(&mut query).unwrap();
|
|
|
|
let query = query.trim().to_lowercase();
|
|
|
|
|
|
|
|
if query.is_empty() { break }
|
|
|
|
|
2018-07-06 21:26:07 +02:00
|
|
|
let (elapsed, _) = measure_time(|| search(&map, &lev_builder, &db, &query));
|
2018-06-24 19:27:24 +02:00
|
|
|
println!("Finished in {}", elapsed);
|
|
|
|
},
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|