2019-01-01 16:37:15 +01:00
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|
|
|
|
2019-02-03 12:22:50 +01:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-01-06 18:03:47 +01:00
|
|
|
use std::io::{self, BufRead, BufReader};
|
2018-12-10 15:13:25 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::error::Error;
|
2018-12-29 12:26:33 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::fs::File;
|
2018-12-10 15:13:25 +01:00
|
|
|
|
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
use meilidb::database::{Database, Schema, UpdateBuilder};
|
2018-12-10 15:13:25 +01:00
|
|
|
use meilidb::tokenizer::DefaultBuilder;
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
pub struct Opt {
|
2018-12-29 12:26:33 +01:00
|
|
|
/// The destination where the database must be created.
|
2018-12-10 15:13:25 +01:00
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
pub database_path: PathBuf,
|
|
|
|
|
|
|
|
/// The csv file to index.
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
pub csv_data_path: PathBuf,
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
/// The path to the schema.
|
|
|
|
#[structopt(long = "schema", parse(from_os_str))]
|
|
|
|
pub schema_path: PathBuf,
|
2019-01-06 18:03:47 +01:00
|
|
|
|
|
|
|
/// The path to the list of stop words (one by line).
|
|
|
|
#[structopt(long = "stop-words", parse(from_os_str))]
|
|
|
|
pub stop_words_path: Option<PathBuf>,
|
|
|
|
|
|
|
|
#[structopt(long = "update-group-size")]
|
|
|
|
pub update_group_size: Option<usize>,
|
2018-12-10 15:13:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Document<'a> (
|
|
|
|
#[serde(borrow)]
|
|
|
|
HashMap<Cow<'a, str>, Cow<'a, str>>
|
|
|
|
);
|
2018-12-10 15:13:25 +01:00
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
fn index(
|
|
|
|
schema: Schema,
|
|
|
|
database_path: &Path,
|
|
|
|
csv_data_path: &Path,
|
|
|
|
update_group_size: Option<usize>,
|
|
|
|
stop_words: &HashSet<String>,
|
|
|
|
) -> Result<Database, Box<Error>>
|
|
|
|
{
|
2018-12-31 23:20:18 +01:00
|
|
|
let database = Database::create(database_path, &schema)?;
|
2018-12-10 15:13:25 +01:00
|
|
|
|
|
|
|
let mut rdr = csv::Reader::from_path(csv_data_path)?;
|
|
|
|
let mut raw_record = csv::StringRecord::new();
|
|
|
|
let headers = rdr.headers()?.clone();
|
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
let mut i = 0;
|
|
|
|
let mut end_of_file = false;
|
|
|
|
|
|
|
|
while !end_of_file {
|
|
|
|
let tokenizer_builder = DefaultBuilder::new();
|
|
|
|
let update_path = tempfile::NamedTempFile::new()?;
|
|
|
|
let mut update = UpdateBuilder::new(update_path.path().to_path_buf(), schema.clone());
|
|
|
|
|
|
|
|
loop {
|
|
|
|
end_of_file = !rdr.read_record(&mut raw_record)?;
|
|
|
|
if end_of_file { break }
|
|
|
|
|
|
|
|
let document: Document = match raw_record.deserialize(Some(&headers)) {
|
|
|
|
Ok(document) => document,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
update.update_document(&document, &tokenizer_builder, &stop_words)?;
|
|
|
|
|
|
|
|
print!("\rindexing document {}", i);
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if let Some(group_size) = update_group_size {
|
|
|
|
if i % group_size == 0 { break }
|
2018-12-10 15:13:25 +01:00
|
|
|
}
|
2019-01-06 18:03:47 +01:00
|
|
|
}
|
2018-12-10 15:13:25 +01:00
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
println!();
|
2018-12-10 15:13:25 +01:00
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
println!("building update...");
|
|
|
|
let update = update.build()?;
|
|
|
|
println!("ingesting update...");
|
|
|
|
database.ingest_update_file(update)?;
|
|
|
|
}
|
2018-12-10 15:13:25 +01:00
|
|
|
|
|
|
|
Ok(database)
|
|
|
|
}
|
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
fn retrieve_stop_words(path: &Path) -> io::Result<HashSet<String>> {
|
|
|
|
let f = File::open(path)?;
|
|
|
|
let reader = BufReader::new(f);
|
|
|
|
let mut words = HashSet::new();
|
|
|
|
|
|
|
|
for line in reader.lines() {
|
|
|
|
let line = line?;
|
|
|
|
let word = line.trim().to_string();
|
|
|
|
words.insert(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(words)
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:13:25 +01:00
|
|
|
fn main() -> Result<(), Box<Error>> {
|
2019-01-06 15:01:09 +01:00
|
|
|
let _ = env_logger::init();
|
2018-12-10 15:13:25 +01:00
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
let schema = {
|
|
|
|
let file = File::open(&opt.schema_path)?;
|
|
|
|
Schema::from_toml(file)?
|
|
|
|
};
|
2018-12-10 15:13:25 +01:00
|
|
|
|
2019-01-06 18:03:47 +01:00
|
|
|
let stop_words = match opt.stop_words_path {
|
|
|
|
Some(ref path) => retrieve_stop_words(path)?,
|
|
|
|
None => HashSet::new(),
|
|
|
|
};
|
|
|
|
|
2018-12-10 15:13:25 +01:00
|
|
|
let (elapsed, result) = elapsed::measure_time(|| {
|
2019-01-06 18:03:47 +01:00
|
|
|
index(schema, &opt.database_path, &opt.csv_data_path, opt.update_group_size, &stop_words)
|
2018-12-10 15:13:25 +01:00
|
|
|
});
|
|
|
|
|
2018-12-11 12:06:02 +01:00
|
|
|
if let Err(e) = result {
|
|
|
|
return Err(e.into())
|
|
|
|
}
|
2018-12-10 15:13:25 +01:00
|
|
|
|
|
|
|
println!("database created in {} at: {:?}", elapsed, opt.database_path);
|
|
|
|
Ok(())
|
|
|
|
}
|