Skip to content

Commit

Permalink
feat(*): finalize search (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft authored Mar 18, 2024
1 parent a94db99 commit 0e17c88
Show file tree
Hide file tree
Showing 19 changed files with 601 additions and 88 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{command, crate_version, Parser, Subcommand};

use crate::alias::AliasCommands;
use crate::{CompileArgs, DumpArgs, IndexArgs, LexiconArgs, LookupArgs, MergeArgs};
use crate::{CompileArgs, DumpArgs, IndexArgs, LexiconArgs, LookupArgs, MergeArgs, SearchArgs};

#[derive(Debug, Parser)]
#[command(name = "odict", about = "the lighting-fast open-source dictionary compiler", version = crate_version!(), long_about = None)]
Expand Down Expand Up @@ -47,4 +47,8 @@ pub enum Commands {
/// Merge entries from multiple dictionaries into a destination dictionary
#[command(arg_required_else_help = true)]
Merge(MergeArgs),

/// Run a full-text query on a compiled dictionary
#[command(arg_required_else_help = true)]
Search(SearchArgs),
}
10 changes: 10 additions & 0 deletions cli/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ pub enum DumpFormat {
MySQL,
}

impl Display for PrintFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PrintFormat::Print => write!(f, "print"),
PrintFormat::JSON => write!(f, "json"),
PrintFormat::XML => write!(f, "xml"),
}
}
}

impl Display for DumpFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
20 changes: 11 additions & 9 deletions cli/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ use odict::search::{get_default_index_dir, IndexOptions};

use crate::CLIContext;

pub(super) static DEFAULT_INDEX_MEMORY: usize = 15000000;

#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = true)]
#[command(flatten_help = true)]
pub struct IndexArgs {
/// Path to a compiled dictionary
/// Path to a compiled dictionary or an alias
#[arg(required = true)]
dictionary: String,
pub(super) dictionary: String,

/// Custom directory to store the index
#[arg(short)]
directory: Option<PathBuf>,
pub(super) directory: Option<PathBuf>,

/// Whether to overwrite the index if it already exists
#[arg(short = 'f', default_value_t = false)]
overwrite: bool,
pub(super) overwrite: bool,

/// Memory arena per thread in bytes. Must be above 15MB.
#[arg(short, default_value_t = 15000000)]
memory: usize,
#[arg(short, default_value_t = DEFAULT_INDEX_MEMORY)]
pub(super) memory: usize,
}

pub fn index(ctx: &mut CLIContext, args: &IndexArgs) -> Result<(), Box<dyn Error>> {
Expand All @@ -34,9 +36,9 @@ pub fn index(ctx: &mut CLIContext, args: &IndexArgs) -> Result<(), Box<dyn Error

ctx.println("".to_string());

let archive = file.to_archive()?;
let dict = file.to_dictionary()?;

let progress1 = ProgressBar::new(archive.entries.len() as u64).with_style(
let progress1 = ProgressBar::new(dict.entries.len() as u64).with_style(
ProgressStyle::with_template("[{eta_precise}] {bar} {pos}/{len} entries indexed").unwrap(),
);

Expand All @@ -52,7 +54,7 @@ pub fn index(ctx: &mut CLIContext, args: &IndexArgs) -> Result<(), Box<dyn Error
progress2.inc(1);
});

archive.index(&options)?;
dict.index(&options)?;

progress1.finish();

Expand Down
2 changes: 2 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod lexicon;
mod lookup;
mod merge;
mod print;
mod search;
mod utils;

pub use alias::*;
Expand All @@ -21,4 +22,5 @@ pub use lexicon::*;
pub use lookup::*;
pub use merge::*;
pub use print::*;
pub use search::*;
pub use utils::*;
3 changes: 2 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Write;

use clap::Parser;
use cli::{alias, compile, lexicon, lookup, merge, t, CLIContext, Commands, CLI};
use cli::{alias, compile, lexicon, lookup, merge, search, t, CLIContext, Commands, CLI};

fn main() {
let cli = CLI::parse();
Expand All @@ -16,6 +16,7 @@ fn main() {
Commands::Lexicon(ref args) => lexicon(c, args),
Commands::Lookup(ref args) => lookup(c, args),
Commands::Merge(ref args) => merge(c, args),
Commands::Search(ref args) => search(c, args),
},
&mut ctx,
);
Expand Down
57 changes: 57 additions & 0 deletions cli/src/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::error::Error;

use clap::{arg, command, Args};
use odict::search::{get_default_index_dir, SearchOptions};

use crate::{enums::PrintFormat, print_entries, CLIContext, IndexArgs, DEFAULT_INDEX_MEMORY};

#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = true)]
#[command(flatten_help = true)]
pub struct SearchArgs {
/// Path to a compiled dictionary or an alias
#[arg(required = true)]
dictionary: String,

/// Format in which to print the results
#[arg(short, long, default_value_t = PrintFormat::JSON)]
format: PrintFormat,

/// Creates a new index if one doesn't already exist
#[arg(long, default_value_t = false)]
index: bool,

/// Search query
#[arg(required = true)]
query: String,
}

pub fn search(ctx: &mut CLIContext, args: &SearchArgs) -> Result<(), Box<dyn Error>> {
let file = ctx
.reader
.read_from_path_or_alias_with_manager(&args.dictionary, &ctx.alias_manager)?;

let dict = file.to_dictionary()?;

if args.index {
let index_path = get_default_index_dir().join(dict.id.as_str());

if !index_path.exists() {
crate::index(
ctx,
&IndexArgs {
dictionary: args.dictionary.clone(),
directory: None,
overwrite: false,
memory: DEFAULT_INDEX_MEMORY,
},
)?;
}
}

let results = dict.search(args.query.as_str(), SearchOptions::default())?;

print_entries(ctx, vec![results], &args.format)?;

Ok(())
}
Loading

0 comments on commit 0e17c88

Please sign in to comment.