use crate::core::index::BytesRef;
use crate::core::index::doc_values_type::DocValuesType;
use crate::core::index::term::Term;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use std::fmt::Display;
use std::sync::Arc;
pub struct DocValuesUpdate {
pub(crate) doc_values_type: DocValuesType,
pub term: Option<Arc<Term>>,
pub field: String,
pub doc_id_upto: i32,
pub has_value: bool,
pub sub_update: DocValuesUpdateEnum,
}
impl DocValuesUpdate {
pub fn new<T, F>(
doc_values_type: DocValuesType,
term: F,
field: T,
doc_id_upto: i32,
sub_update: DocValuesUpdateEnum,
) -> Self
where
T: Into<String>,
F: Into<Arc<Term>>,
{
Self::new_with_optional_term(
doc_values_type,
Some(term.into()),
field,
doc_id_upto,
sub_update,
)
}
pub(crate) fn new_with_optional_term<T: Into<String>>(
doc_values_type: DocValuesType,
term: Option<Arc<Term>>,
field: T,
doc_id_upto: i32,
sub_update: DocValuesUpdateEnum,
) -> Self {
let field = field.into();
debug_assert!(doc_id_upto >= 0, "{doc_id_upto} must be >= 0");
let has_value = sub_update.has_value();
DocValuesUpdate {
doc_values_type,
term,
field,
doc_id_upto,
has_value,
sub_update,
}
}
pub(crate) fn buffered_term(&self) -> Result<&Term> {
self
.term
.as_deref()
.ok_or_else(|| LuceneError::illegal_state("buffered DocValues update requires a term"))
}
pub(crate) fn has_value(&self) -> bool {
self.has_value
}
#[cfg(test)]
pub fn prepare_for_apply(&mut self, doc_id_upto: i32) -> Option<DocValuesUpdate> {
if doc_id_upto == self.doc_id_upto {
return None;
}
let sub_update = self.sub_update.prepare_for_apply();
Some(DocValuesUpdate::new_with_optional_term(
self.doc_values_type,
self.term.clone(),
self.field.clone(),
doc_id_upto,
sub_update,
))
}
}
impl Display for DocValuesUpdate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.term {
Some(term) => write!(f, "term={term}")?,
None => write!(f, "term=null")?,
}
write!(
f,
", field={}, value={}, docIDUpTo={}",
self.field,
self.sub_update.value_to_string(),
self.doc_id_upto
)
}
}
pub trait DocValuesUpdateBase {
fn value_to_string(&self) -> String;
fn has_value(&self) -> bool;
#[cfg(test)]
fn prepare_for_apply(&mut self) -> DocValuesUpdateEnum;
}
pub struct BinaryDocValuesUpdate {
value: Option<BytesRef<Vec<u8>>>,
}
impl BinaryDocValuesUpdate {
pub fn new(value: Option<BytesRef<Vec<u8>>>) -> Self {
BinaryDocValuesUpdate { value }
}
pub fn get_value(&self) -> Result<&BytesRef<Vec<u8>>> {
self
.value
.as_ref()
.ok_or_else(|| LuceneError::illegal_state("getValue requires an update value"))
}
}
impl DocValuesUpdateBase for BinaryDocValuesUpdate {
fn value_to_string(&self) -> String {
match &self.value {
Some(v) => v.to_string(),
None => "null".to_string(),
}
}
fn has_value(&self) -> bool {
self.value.is_some()
}
#[cfg(test)]
fn prepare_for_apply(&mut self) -> DocValuesUpdateEnum {
DocValuesUpdateEnum::Binary(BinaryDocValuesUpdate::new(self.value.clone()))
}
}
#[derive(Clone)]
pub struct NumericDocValuesUpdate {
value: Option<i64>,
}
impl NumericDocValuesUpdate {
pub fn new(value: Option<i64>) -> Self {
NumericDocValuesUpdate { value }
}
pub fn get_value(&self) -> Result<i64> {
self
.value
.ok_or_else(|| LuceneError::illegal_state("getValue requires an update value"))
}
}
impl DocValuesUpdateBase for NumericDocValuesUpdate {
fn value_to_string(&self) -> String {
match self.value {
Some(v) => v.to_string(),
None => "null".to_string(),
}
}
fn has_value(&self) -> bool {
self.value.is_some()
}
#[cfg(test)]
fn prepare_for_apply(&mut self) -> DocValuesUpdateEnum {
DocValuesUpdateEnum::Numeric(NumericDocValuesUpdate::new(self.value))
}
}
pub enum DocValuesUpdateEnum {
Binary(BinaryDocValuesUpdate),
Numeric(NumericDocValuesUpdate),
}
impl DocValuesUpdateEnum {
pub fn get_binary(&self) -> Option<&BinaryDocValuesUpdate> {
debug_assert!(matches!(self, DocValuesUpdateEnum::Binary(_)));
match self {
DocValuesUpdateEnum::Binary(b) => Some(b),
_ => None,
}
}
pub fn get_numeric(&self) -> Option<&NumericDocValuesUpdate> {
debug_assert!(matches!(self, DocValuesUpdateEnum::Numeric(_)));
match self {
DocValuesUpdateEnum::Numeric(n) => Some(n),
_ => None,
}
}
}
impl DocValuesUpdateBase for DocValuesUpdateEnum {
fn value_to_string(&self) -> String {
match self {
DocValuesUpdateEnum::Binary(b) => b.value_to_string(),
DocValuesUpdateEnum::Numeric(n) => n.value_to_string(),
}
}
fn has_value(&self) -> bool {
match self {
DocValuesUpdateEnum::Binary(b) => b.has_value(),
DocValuesUpdateEnum::Numeric(n) => n.has_value(),
}
}
#[cfg(test)]
fn prepare_for_apply(&mut self) -> DocValuesUpdateEnum {
match self {
DocValuesUpdateEnum::Binary(b) => b.prepare_for_apply(),
DocValuesUpdateEnum::Numeric(n) => n.prepare_for_apply(),
}
}
}