2019-03-20 17:56:17 +01:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-12-02 17:37:51 +01:00
|
|
|
|
2019-04-29 16:30:20 +02:00
|
|
|
use plume_models::{search::Searcher, Connection, CONFIG};
|
2018-12-02 17:37:51 +01:00
|
|
|
use std::fs::{read_dir, remove_file};
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
pub fn command<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("search")
|
|
|
|
.about("Manage search index")
|
2019-03-20 17:56:17 +01:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("init")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("path")
|
|
|
|
.short("p")
|
|
|
|
.long("path")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(false)
|
|
|
|
.help("Path to Plume's working directory"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("force")
|
|
|
|
.short("f")
|
|
|
|
.long("force")
|
|
|
|
.help("Ignore already using directory"),
|
|
|
|
)
|
|
|
|
.about("Initialize Plume's internal search engine"),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("refill")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("path")
|
|
|
|
.short("p")
|
|
|
|
.long("path")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(false)
|
|
|
|
.help("Path to Plume's working directory"),
|
|
|
|
)
|
|
|
|
.about("Regenerate Plume's search index"),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("unlock")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("path")
|
|
|
|
.short("p")
|
|
|
|
.long("path")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(false)
|
|
|
|
.help("Path to Plume's working directory"),
|
|
|
|
)
|
|
|
|
.about("Release lock on search directory"),
|
|
|
|
)
|
2018-12-02 17:37:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
|
|
|
let conn = conn;
|
|
|
|
match args.subcommand() {
|
|
|
|
("init", Some(x)) => init(x, conn),
|
|
|
|
("refill", Some(x)) => refill(x, conn, None),
|
|
|
|
("unlock", Some(x)) => unlock(x),
|
2019-03-21 23:25:22 +01:00
|
|
|
("", None) => command().print_help().unwrap(),
|
2018-12-02 17:37:51 +01:00
|
|
|
_ => println!("Unknown subcommand"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2019-03-22 19:51:36 +01:00
|
|
|
let path = args
|
|
|
|
.value_of("path")
|
|
|
|
.map(|p| Path::new(p).join("search_index"))
|
2019-03-21 10:30:33 +01:00
|
|
|
.unwrap_or_else(|| Path::new(&CONFIG.search_index).to_path_buf());
|
2018-12-02 17:37:51 +01:00
|
|
|
let force = args.is_present("force");
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
let can_do = match read_dir(path.clone()) {
|
|
|
|
// try to read the directory specified
|
|
|
|
Ok(mut contents) => contents.next().is_none(),
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == ErrorKind::NotFound {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
panic!("Error while initialising search index : {}", e);
|
|
|
|
}
|
2018-12-02 17:37:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if can_do || force {
|
2020-06-17 16:57:28 +02:00
|
|
|
let searcher = Searcher::create(&path, &CONFIG.search_tokenizers).unwrap();
|
2018-12-02 17:37:51 +01:00
|
|
|
refill(args, conn, Some(searcher));
|
|
|
|
} else {
|
2019-03-20 17:56:17 +01:00
|
|
|
eprintln!(
|
|
|
|
"Can't create new index, {} exist and is not empty",
|
|
|
|
path.to_str().unwrap()
|
|
|
|
);
|
2018-12-02 17:37:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refill<'a>(args: &ArgMatches<'a>, conn: &Connection, searcher: Option<Searcher>) {
|
2019-11-03 17:51:10 +01:00
|
|
|
let path = args.value_of("path");
|
|
|
|
let path = match path {
|
|
|
|
Some(path) => Path::new(path).join("search_index"),
|
|
|
|
None => Path::new(&CONFIG.search_index).to_path_buf(),
|
|
|
|
};
|
2020-06-17 16:57:28 +02:00
|
|
|
let searcher =
|
|
|
|
searcher.unwrap_or_else(|| Searcher::open(&path, &CONFIG.search_tokenizers).unwrap());
|
2018-12-02 17:37:51 +01:00
|
|
|
|
2019-04-29 16:30:20 +02:00
|
|
|
searcher.fill(conn).expect("Couldn't import post");
|
2018-12-02 17:37:51 +01:00
|
|
|
println!("Commiting result");
|
|
|
|
searcher.commit();
|
|
|
|
}
|
|
|
|
|
2021-01-16 04:21:49 +01:00
|
|
|
fn unlock(args: &ArgMatches) {
|
2019-11-03 17:51:10 +01:00
|
|
|
let path = match args.value_of("path") {
|
|
|
|
None => Path::new(&CONFIG.search_index),
|
|
|
|
Some(x) => Path::new(x),
|
|
|
|
};
|
|
|
|
let meta = Path::new(path).join(".tantivy-meta.lock");
|
2019-04-29 19:08:32 +02:00
|
|
|
remove_file(meta).unwrap();
|
2019-11-03 17:51:10 +01:00
|
|
|
let writer = Path::new(path).join(".tantivy-writer.lock");
|
2019-04-29 19:08:32 +02:00
|
|
|
remove_file(writer).unwrap();
|
2018-12-02 17:37:51 +01:00
|
|
|
}
|