use std::fmt::{Display, Formatter};
use crate::core::codecs::lucene101::lucene101_postings_format::IntBlockTermState;
use crate::core::index::base_terms_enum::BaseTermsEnumTermStateImpl;
use crate::core::index::ord_term_state::OrdTermState;
use crate::core::index::term_state::TermState;
use crate::core::index::term_states::EmptyTermState;
use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::error::lucene_error::Result;
use crate::impl_from_for_enum;
#[derive(Default, Clone)]
pub struct BlockTermState {
pub doc_freq: i32,
pub total_term_freq: i64,
pub term_block_ord: i32,
pub block_file_pointer: i64,
ord: OrdTermState,
}
impl Display for BlockTermState {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} docFreq={} totalTermFreq={} termBlockOrd={} blockFP={}",
self.ord, self.doc_freq, self.total_term_freq, self.term_block_ord, self.block_file_pointer
)
}
}
impl TermState for BlockTermState {
fn copy_from(&mut self, other: &Self) -> Result<()> {
self.doc_freq = other.doc_freq;
self.total_term_freq = other.total_term_freq;
self.term_block_ord = other.term_block_ord;
self.block_file_pointer = other.block_file_pointer;
self.ord = other.ord.clone();
Ok(())
}
}
#[derive(Clone)]
pub enum TermStateEnum {
Int(IntBlockTermState),
Block(BlockTermState),
Empty(EmptyTermState),
Ord(OrdTermState),
BaseTermsEnum(BaseTermsEnumTermStateImpl),
}
impl_from_for_enum!(
TermStateEnum,
IntBlockTermState => Int,
BlockTermState => Block,
EmptyTermState => Empty,
OrdTermState => Ord,
BaseTermsEnumTermStateImpl => BaseTermsEnum,
);
impl TermStateEnum {
#[inline]
pub fn get_block_term_state_mut(&mut self) -> Result<&mut BlockTermState> {
match self {
TermStateEnum::Int(int) => Ok(&mut int.base),
TermStateEnum::Block(block) => Ok(block),
_ => Err(LuceneError::unsupported_operation("")),
}
}
#[inline]
pub fn get_block_term_state(&self) -> Result<&BlockTermState> {
match self {
TermStateEnum::Int(int) => Ok(&int.base),
TermStateEnum::Block(block) => Ok(block),
_ => Err(LuceneError::unsupported_operation("")),
}
}
pub fn ord(&self) -> Result<i64> {
match self {
TermStateEnum::Int(int) => Ok(int.base.ord.ord),
TermStateEnum::Block(block) => Ok(block.ord.ord),
TermStateEnum::Ord(ord) => Ok(ord.ord),
_ => Err(LuceneError::unsupported_operation("")),
}
}
pub fn set_ord(&mut self, ord: i64) -> Result<()> {
match self {
TermStateEnum::Int(v) => v.base.ord.ord = ord,
TermStateEnum::Block(v) => v.ord.ord = ord,
TermStateEnum::Ord(v) => v.ord = ord,
_ => return Err(LuceneError::unsupported_operation("")),
}
Ok(())
}
}
impl Display for TermStateEnum {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
TermStateEnum::Int(v) => v.fmt(f),
TermStateEnum::Block(v) => v.fmt(f),
TermStateEnum::Empty(v) => v.fmt(f),
TermStateEnum::Ord(v) => v.fmt(f),
TermStateEnum::BaseTermsEnum(v) => v.fmt(f),
}
}
}
impl TermState for TermStateEnum {
fn copy_from(&mut self, other: &Self) -> Result<()> {
match (self, other) {
(TermStateEnum::Int(int), TermStateEnum::Int(o)) => int.copy_from(o),
(TermStateEnum::Block(block), TermStateEnum::Block(o)) => block.copy_from(o),
(TermStateEnum::Empty(empty), TermStateEnum::Empty(o)) => empty.copy_from(o),
(TermStateEnum::Ord(ord), TermStateEnum::Ord(o)) => ord.copy_from(o),
(TermStateEnum::BaseTermsEnum(ord), TermStateEnum::BaseTermsEnum(o)) => ord.copy_from(o),
_ => Err(LuceneError::illegal_state(
"TermState variants must match when copying",
)),
}
}
}