Accept queries from standard input when not given as argument

This commit is contained in:
Kerollmops 2020-06-20 12:01:15 +02:00
parent d6705d5529
commit c84012d655
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -1,4 +1,5 @@
use std::io::{self, Write};
use std::io::{self, Write, BufRead};
use std::iter::once;
use std::path::PathBuf;
use std::time::Instant;
@ -19,7 +20,7 @@ struct Opt {
database: PathBuf,
/// The query string to search for (doesn't support prefix search yet).
query: String,
query: Option<String>,
}
fn main() -> anyhow::Result<()> {
@ -34,10 +35,19 @@ fn main() -> anyhow::Result<()> {
let index = Index::new(&env)?;
let before = Instant::now();
let rtxn = env.read_txn()?;
let documents_ids = index.search(&rtxn, &opt.query)?;
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 = _>>,
};
for result in lines {
let before = Instant::now();
let query = result?;
let documents_ids = index.search(&rtxn, &query)?;
let headers = match index.headers(&rtxn)? {
Some(headers) => headers,
None => return Ok(()),
@ -53,6 +63,7 @@ fn main() -> anyhow::Result<()> {
}
eprintln!("Took {:.02?} to find {} documents", before.elapsed(), documents_ids.len());
}
Ok(())
}