use crate::core::index::index_reader::Identity;
use crate::core::index::index_reader_context::IndexReaderContext;
use crate::core::index::term::Term;
use crate::core::index::terms::Terms;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::multi_term_query::{
ConstantScoreBlendedRewrite, MultiTermQuery, MultiTermQuerySet, RewriteMethod, RewriteMethodEnum,
};
use crate::core::search::query::{Query, QueryBase, QueryRef, QueryWeight};
use crate::core::search::query_visitor::QueryVisitor;
use crate::core::search::score_mode::ScoreMode;
use crate::core::search::similarities_impl::similarities::Similarity;
use crate::core::util::HasIdentity;
use crate::core::util::accountable::Accountable;
use crate::core::util::automation::automaton::Automaton;
use crate::core::util::automation::compiled_automaton::{CompiledAutomaton, CompiledAutomatonTE};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
#[derive(Clone)]
pub struct AutomatonQuery {
pub(crate) compiled: CompiledAutomaton,
pub(crate) term: Term,
#[allow(dead_code)]
automaton_is_binary: bool,
ram_bytes_used: i64,
id: Identity,
pub(crate) rewrite_method: RewriteMethodEnum,
}
impl AutomatonQuery {
pub fn from_automaton<TermInput>(term: TermInput, automaton: Automaton) -> Result<Self>
where
TermInput: Into<Term>,
{
let term = term.into();
Self::from_automaton_with_binary(term, automaton, false)
}
pub fn from_automaton_with_binary<TermInput>(
term: TermInput,
automaton: Automaton,
is_binary: bool,
) -> Result<Self>
where
TermInput: Into<Term>,
{
let term = term.into();
Self::new(term, automaton, is_binary, ConstantScoreBlendedRewrite)
}
pub fn new<T, TermInput>(
term: TermInput,
automaton: Automaton,
is_binary: bool,
rewrite_method: T,
) -> Result<Self>
where
T: Into<RewriteMethodEnum>,
TermInput: Into<Term>,
{
let term = term.into();
let rewrite_method = rewrite_method.into();
let compiled = CompiledAutomaton::with_binary(automaton, false, true, is_binary)?;
let ram_bytes_used = term
.ram_bytes_used()?
.saturating_add(compiled.ram_bytes_used()?);
Ok(Self {
compiled,
term,
#[allow(dead_code)]
automaton_is_binary: is_binary,
ram_bytes_used,
id: Identity::new(),
rewrite_method,
})
}
pub fn get_compiled(&self) -> &CompiledAutomaton {
&self.compiled
}
pub(crate) fn visit_with_query<QV>(&self, visitor: &mut QV, query: QueryRef<'_>) -> Result<()>
where
QV: QueryVisitor,
{
if visitor.accept_field(self.term.field()) {
self.compiled.visit(visitor, query, self.term.field())?;
}
Ok(())
}
}
impl QueryBase for AutomatonQuery {
type Weight<IRC, SIM>
= QueryWeight<IRC, SIM>
where
SIM: Similarity,
IRC: IndexReaderContext + 'static;
fn to_string(&self, field: &str) -> Result<String> {
let text = "AutomatonQuery {\n}";
if self.term.field() == field {
return Ok(text.to_owned());
}
let mut buffer = String::with_capacity(self.term.field().len() + 1 + text.len());
buffer.push_str(self.term.field());
buffer.push(':');
buffer.push_str(text);
Ok(buffer)
}
fn create_weight<IRC, SIM>(
self,
_searcher: &IndexSearcher<IRC, SIM>,
_score_mode: &ScoreMode,
_boost: f32,
) -> Result<Self::Weight<IRC, SIM>>
where
SIM: Similarity,
IRC: IndexReaderContext + 'static,
Self: Sized,
IndexSearcher<IRC, SIM>: Sync,
{
Err(LuceneError::unsupported_operation(""))
}
fn rewrite<IRC, SIM>(&self, searcher: &IndexSearcher<IRC, SIM>) -> Result<Option<Query>>
where
SIM: Similarity,
IRC: IndexReaderContext + 'static,
Self: Sized,
IndexSearcher<IRC, SIM>: Sync,
{
self.rewrite_method.rewrite(searcher, self).map(Some)
}
fn visit<QV>(&self, visitor: &mut QV) -> Result<()>
where
QV: QueryVisitor,
{
self.visit_with_query(visitor, self.into())
}
}
impl Debug for AutomatonQuery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.to_string("") {
Ok(s) => write!(f, "{}", s),
Err(_) => Err(std::fmt::Error),
}
}
}
impl HasIdentity for AutomatonQuery {
fn identity(&self) -> &Identity {
&self.id
}
}
impl Accountable for AutomatonQuery {
fn ram_bytes_used(&self) -> Result<i64> {
Ok(self.ram_bytes_used)
}
}
impl PartialEq for AutomatonQuery {
fn eq(&self, other: &Self) -> bool {
if std::ptr::eq(self, other) {
return true;
}
self.rewrite_method == other.rewrite_method
&& self.compiled == other.compiled
&& self.term == other.term
}
}
impl Eq for AutomatonQuery {}
impl Hash for AutomatonQuery {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.rewrite_method.hash(state);
self.compiled.hash(state);
self.term.hash(state);
}
}
impl MultiTermQuery for AutomatonQuery {
fn get_field(&self) -> &str {
self.term.field()
}
type TermsEnum<T>
= CompiledAutomatonTE<T>
where
T: Terms;
fn get_terms_enum<T>(&self, terms: T) -> Result<Self::TermsEnum<T>>
where
T: Terms + Clone,
{
self.compiled.get_terms_enum(terms)
}
fn to_query(&self) -> Query {
MultiTermQuerySet::from(self.clone()).into()
}
}