use crate::core::analysis::analyzer::Analyzer;
use crate::core::analysis::reader::ReaderEnum;
use crate::core::document::field::{Field, FieldBase, FieldDataEnum, Store};
use crate::core::document::field_type::FieldType;
use crate::core::document::int_point::IntPoint;
use crate::core::document::invertable_field::InvertableType;
use crate::core::document::sorted_numeric_doc_values_field::SortedNumericDocValuesField;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::doc_values_type::DocValuesType;
use crate::core::index::indexable_field::{
IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::search::index_or_doc_values_query::IndexOrDocValuesQuery;
use crate::core::search::index_sort_sorted_numeric_doc_values_range_query::IndexSortSortedNumericDocValuesRangeQuery;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::number::Number;
use crate::core::util::numeric_utils::NumericUtils;
use std::borrow::Cow;
use std::fmt;
use std::sync::LazyLock;
pub static FIELD_TYPE: LazyLock<FieldType> = LazyLock::new(|| {
let mut ft = FieldType::new();
expect_invariant!(
ft.set_dimensions(1, BitUtil::INT_BYTES),
"the built-in integer field dimensions are valid"
);
expect_invariant!(
ft.set_doc_values_type(DocValuesType::SortedNumeric),
"the built-in integer field type is mutable and unfrozen"
);
ft.freeze();
ft
});
pub static FIELD_TYPE_STORED: LazyLock<FieldType> = LazyLock::new(|| {
let mut ft = expect_invariant!(
FieldType::from_ref(&*FIELD_TYPE),
"the built-in integer field type can be copied"
);
expect_invariant!(
ft.set_stored(true),
"the copied integer field type is mutable and unfrozen"
);
ft.freeze();
ft
});
pub struct IntField {
parent_field: Field,
stored_value: Option<FieldDataEnum>,
}
impl IntField {
pub fn new<T>(name: T, value: i32, stored: Store) -> Result<IntField>
where
T: Into<Cow<'static, str>>,
{
let stored = stored.into();
let (field_type, stored_value) = if stored {
(FIELD_TYPE_STORED.clone(), Some(value.into()))
} else {
(FIELD_TYPE.clone(), None)
};
let parent_field = Field::new(name, value, field_type);
Ok(IntField {
parent_field,
stored_value,
})
}
pub fn new_exact_query<T>(
field: T,
value: i32,
) -> Result<IndexSortSortedNumericDocValuesRangeQuery>
where
T: Into<String>,
{
Self::new_range_query(field, value, value)
}
pub fn new_range_query<T>(
field: T,
lower_value: i32,
upper_value: i32,
) -> Result<IndexSortSortedNumericDocValuesRangeQuery>
where
T: Into<String>,
{
let field = field.into();
let fallback_query = IndexOrDocValuesQuery::new(
IntPoint::new_range_query(field.clone(), lower_value, upper_value)?,
SortedNumericDocValuesField::new_slow_range_query(
field.clone(),
lower_value as i64,
upper_value as i64,
),
);
Ok(IndexSortSortedNumericDocValuesRangeQuery::new(
field,
lower_value as i64,
upper_value as i64,
fallback_query,
))
}
pub fn new_set_query<T, V>(field: T, values: V) -> Result<IndexOrDocValuesQuery>
where
T: Into<String>,
V: Into<Vec<i32>>,
{
let values = values.into();
let field = field.into();
let point_query = IntPoint::new_set_query(field.clone(), values.as_slice())?;
let dv_query = SortedNumericDocValuesField::new_slow_set_query(
field,
values.into_iter().map(|v| v as i64).collect::<Vec<_>>(),
)?;
Ok(IndexOrDocValuesQuery::new(point_query, dv_query))
}
}
impl FieldBase for IntField {
fn set_int_value(&mut self, value: i32) -> Result<()> {
self.parent_field.set_int_value(value)?;
if self.stored_value.is_some() {
self.stored_value = Some(value.into());
}
Ok(())
}
}
impl IndexableField for IntField {
fn name(&self) -> &str {
self.parent_field.name()
}
type FieldType<'a>
= &'a FieldType
where
Self: 'a;
fn field_type(&self) -> Self::FieldType<'_> {
self.parent_field.field_type()
}
fn token_stream<'a, A>(
&'a mut self,
analyzer: &'a A,
reuse_token_stream: &'a mut Option<ReusedIndexingTokenStream>,
) -> Result<IndexingTokenStream<'a>>
where
A: Analyzer,
{
self.parent_field.token_stream(analyzer, reuse_token_stream)
}
fn binary_value(&self) -> Result<Option<BinaryValueEnum<'_>>> {
match &self.parent_field.fields_data {
FieldDataEnum::Number(Number::I32(v)) => {
let mut bytes = [0u8; BitUtil::LONG_BYTES];
NumericUtils::int_to_sortable_bytes(*v, &mut bytes, 0);
Ok(Some(BinaryValueEnum::Inline {
bytes,
length: BitUtil::INT_BYTES,
}))
},
_ => Err(LuceneError::illegal_state(
"parent_field's fields_data does not contain an i32 value",
)),
}
}
fn take_binary_value(&mut self) -> Result<Option<BytesRef<Vec<u8>>>> {
match &self.parent_field.fields_data {
FieldDataEnum::Number(Number::I32(v)) => {
let mut bytes = vec![0u8; BitUtil::INT_BYTES];
NumericUtils::int_to_sortable_bytes(*v, &mut bytes, 0);
Ok(Some(BytesRef::from_bytes(bytes)))
},
_ => Err(LuceneError::illegal_state(
"parent_field's fields_data does not contain an i32 value",
)),
}
}
fn string_value(&self) -> Result<Option<Cow<'_, str>>> {
self.parent_field.string_value()
}
fn take_string_value(&mut self) -> Result<Option<String>> {
self.parent_field.take_string_value()
}
fn take_reader_value(&mut self) -> Result<Option<ReaderEnum>> {
self.parent_field.take_reader_value()
}
fn numeric_value(&self) -> Result<Option<Number>> {
self.parent_field.numeric_value()
}
type StoredValue<'a>
= &'a FieldDataEnum
where
Self: 'a;
fn stored_value(&self) -> Result<Option<Self::StoredValue<'_>>> {
Ok(self.stored_value.as_ref())
}
fn invertable_type(&self) -> &InvertableType {
self.parent_field.invertable_type()
}
}
impl fmt::Display for IntField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} <{}:{}>",
std::any::type_name::<Self>(),
self.parent_field.name(),
self.parent_field.fields_data
)
}
}