Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to clap 4 #96

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 77 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
persistent = "0.4"
clap = "3"
clap = "4"
ansi_term = "0.12"
urlencoded = "0.6"
mount = "0.4"
Expand Down
8 changes: 4 additions & 4 deletions src/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use tantivy::schema::{Field, Schema};
use tantivy::Index;

pub fn run_bench_cli(matches: &ArgMatches) -> Result<(), String> {
let index_path = PathBuf::from(matches.value_of("index").unwrap());
let queries_path = PathBuf::from(matches.value_of("queries").unwrap()); // the unwrap is safe as long as it is comming from the main cli.
let num_repeat = ArgMatches::value_of_t(matches, "num_repeat")
.map_err(|e| format!("Failed to read num_repeat argument as an integer. {:?}", e))?;
let index_path = PathBuf::from(matches.get_one::<String>("index").unwrap());
let queries_path = PathBuf::from(matches.get_one::<String>("queries").unwrap()); // the unwrap is safe as long as it is comming from the main cli.
let num_repeat: usize = *ArgMatches::get_one(matches, "num_repeat")
.expect("Failed to read num_repeat argument as an integer");
run_bench(&index_path, &queries_path, num_repeat).map_err(From::from)
}

Expand Down
16 changes: 8 additions & 8 deletions src/commands/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ use time::Instant;
use crate::commands::merge::run_merge;

pub fn run_index_cli(argmatch: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(argmatch.value_of("index").unwrap());
let index_directory = PathBuf::from(argmatch.get_one::<String>("index").unwrap());
let document_source = argmatch
.value_of("file")
.get_one::<String>("file")
.map(|path| DocumentSource::FromFile(PathBuf::from(path)))
.unwrap_or(DocumentSource::FromPipe);
let no_merge = argmatch.is_present("nomerge");
let force_merge = argmatch.is_present("forcemerge");
let mut num_threads = ArgMatches::value_of_t(argmatch, "num_threads")
.map_err(|_| format!("Failed to read num_threads argument as an integer."))?;
let no_merge = argmatch.contains_id("nomerge");
let force_merge = argmatch.contains_id("forcemerge");
let mut num_threads: usize = *ArgMatches::get_one(argmatch, "num_threads")
.expect("Failed to read num_threads argument as an integer.");
if num_threads == 0 {
num_threads = 1;
}
let buffer_size: usize = ArgMatches::value_of_t(argmatch, "memory_size")
.map_err(|_| format!("Failed to read the buffer size argument as an integer."))?;
let buffer_size: usize = *ArgMatches::get_one(argmatch, "memory_size")
.expect("Failed to read the buffer size argument as an integer.");
let buffer_size_per_thread = buffer_size / num_threads;
run_index(
index_directory,
Expand Down
29 changes: 22 additions & 7 deletions src/commands/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use clap::ArgMatches;
use tantivy::schema::Schema;
use tantivy::space_usage::PerFieldSpaceUsage;
use std::convert::From;
use std::path::Path;
use std::path::PathBuf;
use tantivy;
use tantivy::schema::Schema;
use tantivy::space_usage::PerFieldSpaceUsage;
use tantivy::Index;

pub fn run_inspect_cli(matches: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(matches.value_of("index").unwrap());
let index_directory = PathBuf::from(matches.get_one::<String>("index").unwrap());
run_inspect(&index_directory).map_err(|e| format!("{:?}", e))
}

Expand All @@ -28,14 +28,25 @@ fn run_inspect(directory: &Path) -> tantivy::Result<()> {
let space_usage = searcher.space_usage()?;
println!("Total bytes: {}", space_usage.total());
println!("");
for (i, (segment_reader, segment_space_usage)) in segments.iter().zip(space_usage.segments().iter()).enumerate() {
for (i, (segment_reader, segment_space_usage)) in segments
.iter()
.zip(space_usage.segments().iter())
.enumerate()
{
let section_count = i + 2;
println!("{}. Space usage for segment: `{}`", section_count, segment_reader.segment_id().uuid_string());
println!(
"{}. Space usage for segment: `{}`",
section_count,
segment_reader.segment_id().uuid_string()
);
println!("==============================================================================");
println!("Num docs: {}", segment_space_usage.num_docs());
println!("Store space usage:");
println!("Total bytes: {}", segment_space_usage.store().total());
println!("Offset bytes: {}", segment_space_usage.store().offsets_usage());
println!(
"Offset bytes: {}",
segment_space_usage.store().offsets_usage()
);
println!("");
println!("{}.1 Term dictionnary space usage", section_count);
println!("--------------------------------");
Expand Down Expand Up @@ -75,6 +86,10 @@ fn print_fields_space_usage(schema: &Schema, per_field_space_usage: &PerFieldSpa
println!("Total bytes: {}", per_field_space_usage.total());
for (field, field_space_usage) in per_field_space_usage.fields() {
let field_name = schema.get_field_name(*field);
println!("Field `{}` bytes: {}", field_name, field_space_usage.total());
println!(
"Field `{}` bytes: {}",
field_name,
field_space_usage.total()
);
}
}
2 changes: 1 addition & 1 deletion src/commands/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn error_msg(err: tantivy::TantivyError) -> String {
}

pub fn run_merge_cli(argmatch: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(argmatch.value_of("index").unwrap());
let index_directory = PathBuf::from(argmatch.get_one::<String>("index").unwrap());
run_merge(index_directory).map_err(error_msg)

// we rollback to force a gc.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tantivy::schema::*;
use tantivy::Index;

pub fn run_new_cli(matches: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(matches.value_of("index").unwrap());
let index_directory = PathBuf::from(matches.get_one::<String>("index").unwrap());
run_new(index_directory).map_err(|e| format!("{:?}", e))
}

Expand Down
8 changes: 4 additions & 4 deletions src/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use tantivy::schema::FieldType;
use tantivy::{Index, TERMINATED};

pub fn run_search_cli(matches: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(matches.value_of("index").unwrap());
let query = matches.value_of("query").unwrap();
let agg = matches.value_of("aggregation");
let index_directory = PathBuf::from(matches.get_one::<String>("index").unwrap());
let query = matches.get_one::<String>("query").unwrap();
let agg = matches.get_one::<String>("aggregation");
run_search(&index_directory, &query, &agg).map_err(|e| format!("{:?}", e))
}

fn run_search(
directory: &Path,
query: &str,
agg: &std::option::Option<&str>,
agg: &std::option::Option<&String>,
) -> tantivy::Result<()> {
let index = Index::open_in_dir(directory)?;
let schema = index.schema();
Expand Down
7 changes: 4 additions & 3 deletions src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ use tantivy::{DocAddress, Score};
use urlencoded::UrlEncodedQuery;

pub fn run_serve_cli(matches: &ArgMatches) -> Result<(), String> {
let index_directory = PathBuf::from(matches.value_of("index").unwrap());
let port = ArgMatches::value_of_t(matches, "port").unwrap_or(3000u16);
let host_str = matches.value_of("host").unwrap_or("localhost");
let index_directory = PathBuf::from(matches.get_one::<String>("index").unwrap());
let port = ArgMatches::get_one(matches, "port").unwrap_or(&3000u16);
let fallback = "localhost".to_string();
let host_str = matches.get_one::<String>("host").unwrap_or(&fallback);
let host = format!("{}:{}", host_str, port);
run_serve(index_directory, &host).map_err(|e| format!("{:?}", e))
}
Expand Down
Loading