2020-06-20 18:01:15 +08:00
|
|
|
use std::io::{self, Write, BufRead};
|
|
|
|
use std::iter::once;
|
2020-05-31 22:09:34 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
2020-05-31 23:48:13 +08:00
|
|
|
use heed::EnvOpenOptions;
|
2020-07-12 16:55:09 +08:00
|
|
|
use log::debug;
|
2020-08-07 00:19:10 +08:00
|
|
|
use milli::Index;
|
2020-07-12 16:55:09 +08:00
|
|
|
use structopt::StructOpt;
|
2020-05-31 22:09:34 +08:00
|
|
|
|
2020-06-11 04:05:01 +08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|
|
|
|
2020-05-31 22:09:34 +08:00
|
|
|
#[derive(Debug, StructOpt)]
|
2020-07-12 17:06:45 +08:00
|
|
|
#[structopt(name = "milli-search", about = "A simple search binary for milli project.")]
|
2020-05-31 22:09:34 +08:00
|
|
|
struct Opt {
|
|
|
|
/// The database path where the database is located.
|
|
|
|
/// It is created if it doesn't already exist.
|
|
|
|
#[structopt(long = "db", parse(from_os_str))]
|
|
|
|
database: PathBuf,
|
|
|
|
|
2020-08-10 19:53:53 +08:00
|
|
|
/// The maximum size the database can take on disk. It is recommended to specify
|
|
|
|
/// the whole disk space (value must be a multiple of a page size).
|
|
|
|
#[structopt(long = "db-size", default_value = "107374182400")] // 100 GB
|
|
|
|
database_size: usize,
|
|
|
|
|
2020-07-12 17:04:35 +08:00
|
|
|
/// Verbose mode (-v, -vv, -vvv, etc.)
|
|
|
|
#[structopt(short, long, parse(from_occurrences))]
|
|
|
|
verbose: usize,
|
|
|
|
|
2020-05-31 22:09:34 +08:00
|
|
|
/// The query string to search for (doesn't support prefix search yet).
|
2020-06-20 18:01:15 +08:00
|
|
|
query: Option<String>,
|
2020-05-31 22:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2020-07-12 17:04:35 +08:00
|
|
|
stderrlog::new()
|
|
|
|
.verbosity(opt.verbose)
|
|
|
|
.show_level(false)
|
|
|
|
.timestamp(stderrlog::Timestamp::Off)
|
|
|
|
.init()?;
|
|
|
|
|
2020-05-31 22:09:34 +08:00
|
|
|
std::fs::create_dir_all(&opt.database)?;
|
|
|
|
let env = EnvOpenOptions::new()
|
2020-08-10 19:53:53 +08:00
|
|
|
.map_size(opt.database_size)
|
2020-06-23 01:04:10 +08:00
|
|
|
.max_dbs(10)
|
2020-08-07 19:11:31 +08:00
|
|
|
.open(&opt.database)?;
|
2020-05-31 22:09:34 +08:00
|
|
|
|
2020-08-07 19:11:31 +08:00
|
|
|
// Open the LMDB database.
|
2020-08-10 19:47:19 +08:00
|
|
|
let index = Index::new(&env, opt.database)?;
|
2020-05-31 22:09:34 +08:00
|
|
|
let rtxn = env.read_txn()?;
|
|
|
|
|
2020-06-20 18:01:15 +08:00
|
|
|
let stdin = io::stdin();
|
|
|
|
let lines = match opt.query {
|
|
|
|
Some(query) => Box::new(once(Ok(query.to_string()))),
|
|
|
|
None => Box::new(stdin.lock().lines()) as Box<dyn Iterator<Item = _>>,
|
2020-05-31 23:01:11 +08:00
|
|
|
};
|
|
|
|
|
2020-06-20 18:01:15 +08:00
|
|
|
for result in lines {
|
|
|
|
let before = Instant::now();
|
2020-05-31 22:09:34 +08:00
|
|
|
|
2020-06-20 18:01:15 +08:00
|
|
|
let query = result?;
|
2020-08-13 20:15:05 +08:00
|
|
|
let result = index.search(&rtxn).query(query).execute().unwrap();
|
|
|
|
|
2020-06-20 18:01:15 +08:00
|
|
|
let headers = match index.headers(&rtxn)? {
|
|
|
|
Some(headers) => headers,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2020-08-13 20:15:05 +08:00
|
|
|
let documents = index.documents(result.documents_ids.iter().cloned())?;
|
2020-06-20 18:01:15 +08:00
|
|
|
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
stdout.write_all(&headers)?;
|
|
|
|
|
2020-08-10 19:47:19 +08:00
|
|
|
for (_id, content) in documents {
|
|
|
|
stdout.write_all(&content)?;
|
2020-05-31 22:09:34 +08:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:15:05 +08:00
|
|
|
debug!("Took {:.02?} to find {} documents", before.elapsed(), result.documents_ids.len());
|
2020-06-20 18:01:15 +08:00
|
|
|
}
|
2020-05-31 22:09:34 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|