use crate::core::index::automaton_terms_enum::AutomatonTermsEnum;
use crate::core::index::filtered_terms_enum::{FilteredTermsEnum, FilteredTermsEnumBase};
use crate::core::index::leaf_reader::LeafReader;
use crate::core::index::terms_enum::{
EmptyTermsEnum, SeekStatus, TermsEnum, TermsEnumEnum2,
TermsEnumWithUnsupportedPostingsAndAttributesWithEmpty,
TermsEnumWithUnsupportedPostingsAndAttributesWithEmptyIntersect,
};
use crate::core::index::{BytesRef, BytesRefBuilder, BytesRefValue};
use crate::core::util::automation::compiled_automaton::CompiledAutomaton;
use crate::core::util::bytes_ref_iterator::BytesRefIterator;
use crate::core::util::error::lucene_error::{LuceneErrorKind, Result};
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
pub trait Terms {
type TermsEnum: TermsEnum;
fn iterator(&self) -> Result<Self::TermsEnum>;
type IntersectIter: TermsEnum<PostingsEnum = <Self::TermsEnum as TermsEnum>::PostingsEnum>;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter>;
fn default_intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<FilteredTermsEnum<Self::TermsEnum, AutomatonTermsEnum>>
where
Self: Sized,
{
let terms_enum = self.iterator()?;
if start_term.is_some() {
AutomatonTermsEnum::with_start_term(terms_enum, compiled, start_term)
} else {
AutomatonTermsEnum::new(terms_enum, compiled)
}
}
fn size(&self) -> Result<i64>;
fn get_sum_total_term_freq(&self) -> Result<i64>;
fn get_sum_doc_freq(&self) -> Result<i64>;
fn get_doc_count(&self) -> Result<i32>;
fn has_freqs(&self) -> bool;
fn has_offsets(&self) -> bool;
fn has_positions(&self) -> bool;
fn has_payloads(&self) -> bool;
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
let mut iterator = self.iterator()?;
match iterator.next()? {
Some(term) => Ok(Some(Cow::Owned(term.into_owned()))),
None => Ok(None),
}
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
let size = self.size()?;
match size.cmp(&0) {
std::cmp::Ordering::Equal => return Ok(None),
std::cmp::Ordering::Greater => {
let seek_result = (|| -> Result<Cow<'_, BytesRef<Vec<u8>>>> {
let mut iterator = self.iterator()?;
iterator.seek_exact_with_ord(size - 1)?;
Ok(Cow::Owned(iterator.term()?.into_owned()))
})();
match seek_result {
Ok(term) => return Ok(Some(term)),
Err(error) if matches!(error.kind(), LuceneErrorKind::UnsupportedOperation(_)) => {},
Err(error) => return Err(error),
}
},
std::cmp::Ordering::Less => {},
}
let mut iterator = self.iterator()?;
let v = iterator.next()?;
if v.is_none() {
return Ok(None);
}
drop(v);
let mut scratch = BytesRefBuilder::new();
scratch.append_byte(0)?;
loop {
let mut low = 0;
let mut high = 256;
while low != high {
let mid = (((low + high) as u32) >> 1) as i32;
scratch.set_byte_at(scratch.length() - 1, mid as u8);
match iterator.seek_ceil(scratch.get_bytes_mut_ref())? {
SeekStatus::End => {
if mid == 0 {
scratch.set_length(scratch.length() - 1);
return Ok(Some(Cow::Owned(scratch.get_bytes_owner())));
}
high = mid;
},
_ => {
if low == mid {
break;
}
low = mid;
},
}
}
scratch.set_length(scratch.length() + 1);
scratch.grow(scratch.length())?;
}
}
fn get_stats(&self) -> Result<String> {
Ok(format!(
"impl={},size={},docCount={},sumTotalTermFreq={},sumDocFreq={}",
std::any::type_name::<Self>(),
self.size()?,
self.get_doc_count()?,
self.get_sum_total_term_freq()?,
self.get_sum_doc_freq()?
))
}
}
impl<T> Terms for &T
where
T: Terms,
{
type TermsEnum = T::TermsEnum;
fn iterator(&self) -> Result<Self::TermsEnum> {
(**self).iterator()
}
type IntersectIter = T::IntersectIter;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter> {
(**self).intersect(compiled, start_term)
}
fn default_intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<FilteredTermsEnum<Self::TermsEnum, AutomatonTermsEnum>>
where
Self: Sized,
{
(**self).default_intersect(compiled, start_term)
}
fn size(&self) -> Result<i64> {
(**self).size()
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
(**self).get_sum_total_term_freq()
}
fn get_sum_doc_freq(&self) -> Result<i64> {
(**self).get_sum_doc_freq()
}
fn get_doc_count(&self) -> Result<i32> {
(**self).get_doc_count()
}
fn has_freqs(&self) -> bool {
(**self).has_freqs()
}
fn has_offsets(&self) -> bool {
(**self).has_offsets()
}
fn has_positions(&self) -> bool {
(**self).has_positions()
}
fn has_payloads(&self) -> bool {
(**self).has_payloads()
}
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_min()
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_max()
}
fn get_stats(&self) -> Result<String> {
(**self).get_stats()
}
}
impl<T> Terms for Rc<T>
where
T: Terms,
{
type TermsEnum = T::TermsEnum;
fn iterator(&self) -> Result<Self::TermsEnum> {
(**self).iterator()
}
type IntersectIter = T::IntersectIter;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter> {
(**self).intersect(compiled, start_term)
}
fn default_intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<FilteredTermsEnum<Self::TermsEnum, AutomatonTermsEnum>>
where
Self: Sized,
{
(**self).default_intersect(compiled, start_term)
}
fn size(&self) -> Result<i64> {
(**self).size()
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
(**self).get_sum_total_term_freq()
}
fn get_sum_doc_freq(&self) -> Result<i64> {
(**self).get_sum_doc_freq()
}
fn get_doc_count(&self) -> Result<i32> {
(**self).get_doc_count()
}
fn has_freqs(&self) -> bool {
(**self).has_freqs()
}
fn has_offsets(&self) -> bool {
(**self).has_offsets()
}
fn has_positions(&self) -> bool {
(**self).has_positions()
}
fn has_payloads(&self) -> bool {
(**self).has_payloads()
}
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_min()
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_max()
}
fn get_stats(&self) -> Result<String> {
(**self).get_stats()
}
}
impl<T> Terms for Arc<T>
where
T: Terms,
{
type TermsEnum = T::TermsEnum;
fn iterator(&self) -> Result<Self::TermsEnum> {
(**self).iterator()
}
type IntersectIter = T::IntersectIter;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter> {
(**self).intersect(compiled, start_term)
}
fn default_intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<FilteredTermsEnum<Self::TermsEnum, AutomatonTermsEnum>>
where
Self: Sized,
{
(**self).default_intersect(compiled, start_term)
}
fn size(&self) -> Result<i64> {
(**self).size()
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
(**self).get_sum_total_term_freq()
}
fn get_sum_doc_freq(&self) -> Result<i64> {
(**self).get_sum_doc_freq()
}
fn get_doc_count(&self) -> Result<i32> {
(**self).get_doc_count()
}
fn has_freqs(&self) -> bool {
(**self).has_freqs()
}
fn has_offsets(&self) -> bool {
(**self).has_offsets()
}
fn has_positions(&self) -> bool {
(**self).has_positions()
}
fn has_payloads(&self) -> bool {
(**self).has_payloads()
}
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_min()
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
(**self).get_max()
}
fn get_stats(&self) -> Result<String> {
(**self).get_stats()
}
}
pub(crate) fn get_terms<LR>(reader: &LR, field: &str) -> Result<TermsEnum2Type<LR::Terms>>
where
LR: LeafReader,
{
let terms = reader.terms(field)?;
match terms {
Some(t) => Ok(TermsWithEmpty::Terms(t)),
None => Ok(TermsWithEmpty::Empty(EmptyTerms)),
}
}
pub type TermsEnum2Type<T> = TermsWithEmpty<T>;
pub enum TermsWithEmpty<T> {
Terms(T),
Empty(EmptyTerms),
}
impl<T> Terms for TermsWithEmpty<T>
where
T: Terms,
{
type TermsEnum = TermsEnumWithUnsupportedPostingsAndAttributesWithEmpty<T::TermsEnum>;
fn iterator(&self) -> Result<Self::TermsEnum> {
match self {
Self::Terms(terms) => terms
.iterator()
.map(TermsEnumWithUnsupportedPostingsAndAttributesWithEmpty::WithPostingsAndAttributes),
Self::Empty(terms) => terms
.iterator()
.map(TermsEnumWithUnsupportedPostingsAndAttributesWithEmpty::WithoutPostingsAndAttributes),
}
}
type IntersectIter =
TermsEnumWithUnsupportedPostingsAndAttributesWithEmptyIntersect<T::IntersectIter>;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter> {
match self {
Self::Terms(terms) => terms
.intersect(compiled, start_term)
.map(
TermsEnumWithUnsupportedPostingsAndAttributesWithEmptyIntersect::WithPostingsAndAttributes,
),
Self::Empty(terms) => terms
.intersect(compiled, start_term)
.map(
TermsEnumWithUnsupportedPostingsAndAttributesWithEmptyIntersect::WithoutPostingsAndAttributes,
),
}
}
fn size(&self) -> Result<i64> {
match self {
Self::Terms(terms) => terms.size(),
Self::Empty(terms) => terms.size(),
}
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
match self {
Self::Terms(terms) => terms.get_sum_total_term_freq(),
Self::Empty(terms) => terms.get_sum_total_term_freq(),
}
}
fn get_sum_doc_freq(&self) -> Result<i64> {
match self {
Self::Terms(terms) => terms.get_sum_doc_freq(),
Self::Empty(terms) => terms.get_sum_doc_freq(),
}
}
fn get_doc_count(&self) -> Result<i32> {
match self {
Self::Terms(terms) => terms.get_doc_count(),
Self::Empty(terms) => terms.get_doc_count(),
}
}
fn has_freqs(&self) -> bool {
match self {
Self::Terms(terms) => terms.has_freqs(),
Self::Empty(terms) => terms.has_freqs(),
}
}
fn has_offsets(&self) -> bool {
match self {
Self::Terms(terms) => terms.has_offsets(),
Self::Empty(terms) => terms.has_offsets(),
}
}
fn has_positions(&self) -> bool {
match self {
Self::Terms(terms) => terms.has_positions(),
Self::Empty(terms) => terms.has_positions(),
}
}
fn has_payloads(&self) -> bool {
match self {
Self::Terms(terms) => terms.has_payloads(),
Self::Empty(terms) => terms.has_payloads(),
}
}
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
match self {
Self::Terms(terms) => terms.get_min(),
Self::Empty(terms) => terms.get_min(),
}
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
match self {
Self::Terms(terms) => terms.get_max(),
Self::Empty(terms) => terms.get_max(),
}
}
fn get_stats(&self) -> Result<String> {
match self {
Self::Terms(terms) => terms.get_stats(),
Self::Empty(terms) => terms.get_stats(),
}
}
}
#[derive(Default)]
pub struct EmptyTerms;
impl Terms for EmptyTerms {
type TermsEnum = EmptyTermsEnum;
fn iterator(&self) -> Result<Self::TermsEnum> {
Ok(EmptyTermsEnum)
}
type IntersectIter
= FilteredTermsEnum<Self::TermsEnum, AutomatonTermsEnum>
where
Self::TermsEnum: BytesRefIterator,
AutomatonTermsEnum: FilteredTermsEnumBase;
fn intersect(
&self,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<Self::IntersectIter> {
self.default_intersect(compiled, start_term)
}
fn size(&self) -> Result<i64> {
Ok(0)
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
Ok(0)
}
fn get_sum_doc_freq(&self) -> Result<i64> {
Ok(0)
}
fn get_doc_count(&self) -> Result<i32> {
Ok(0)
}
fn has_freqs(&self) -> bool {
false
}
fn has_offsets(&self) -> bool {
false
}
fn has_positions(&self) -> bool {
false
}
fn has_payloads(&self) -> bool {
false
}
}
macro_rules! either_terms {
($vis:vis $name:ident => { te: $te:ident, ie: $ie:ident } { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
$vis enum $name<$( $T ),+> {
$( $Variant($T), )+
}
impl<$( $T ),+> Terms for $name<$( $T ),+>
where
$( $T: Terms ),+
{
type TermsEnum = $te<$( <$T as Terms>::TermsEnum ),+>;
type IntersectIter = $ie<$( <$T as Terms>::IntersectIter ),+>;
fn iterator(&self) -> Result<Self::TermsEnum> {
match self {
$(
Self::$Variant(inner) => {
let it = inner.iterator()?;
Ok($te::$Variant(it))
}
),+
}
}
fn intersect(
&self,
ca: &CompiledAutomaton,
start: Option<&BytesRef<Vec<u8>>>
) -> Result<Self::IntersectIter> {
match self {
$(
Self::$Variant(inner) => {
let it = inner.intersect(ca, start)?;
Ok($ie::$Variant(it))
}
),+
}
}
fn size(&self) -> Result<i64> {
match self { $( Self::$Variant(inner) => inner.size(), )+ }
}
fn get_doc_count(&self) -> Result<i32> {
match self { $( Self::$Variant(inner) => inner.get_doc_count(), )+ }
}
fn get_sum_doc_freq(&self) -> Result<i64> {
match self { $( Self::$Variant(inner) => inner.get_sum_doc_freq(), )+ }
}
fn get_sum_total_term_freq(&self) -> Result<i64> {
match self { $( Self::$Variant(inner) => inner.get_sum_total_term_freq(), )+ }
}
fn has_freqs(&self) -> bool {
match self { $( Self::$Variant(inner) => inner.has_freqs(), )+ }
}
fn has_offsets(&self) -> bool {
match self { $( Self::$Variant(inner) => inner.has_offsets(), )+ }
}
fn has_positions(&self) -> bool {
match self { $( Self::$Variant(inner) => inner.has_positions(), )+ }
}
fn has_payloads(&self) -> bool {
match self { $( Self::$Variant(inner) => inner.has_payloads(), )+ }
}
fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
match self { $( Self::$Variant(inner) => inner.get_min(), )+ }
}
fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
match self { $( Self::$Variant(inner) => inner.get_max(), )+ }
}
fn get_stats(&self) -> Result<String> {
match self { $( Self::$Variant(inner) => inner.get_stats(), )+ }
}
}
};
}
either_terms!(
pub TermsEnum2
=> { te: TermsEnumEnum2, ie: TermsEnumEnum2 }
{ A:A,B:B}
);
pub type TermsTE<T> = <T as Terms>::TermsEnum;
pub type TermsIntersect<T> = <T as Terms>::IntersectIter;
pub type TermsPosting<T> = <TermsTE<T> as TermsEnum>::PostingsEnum;
pub type TermsIntersectPosting<T> = <TermsIntersect<T> as TermsEnum>::PostingsEnum;