use crate::core::analysis::analyzer::Analyzer;
use crate::core::analysis::reader::ReaderEnum;
use crate::core::document::field::{Field, FieldBase, FieldDataEnum};
use crate::core::document::field_type::FieldType;
use crate::core::document::invertable_field::InvertableType;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::indexable_field::{
IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::index::indexable_field_type::IndexableFieldType;
use crate::core::search::point_in_set_query::{PointInSetBase, PointInSetQuery};
#[cfg(debug_assertions)]
use crate::core::search::point_range_query::check_args;
use crate::core::search::point_range_query::{PointRangeBase, PointRangeQuery};
use crate::core::util::CoreHelper;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::bytes_ref_iterator::BytesRefIterator;
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;
pub struct FloatPoint {
parent_field: Field,
}
impl FloatPoint {
pub fn new<T, P>(name: T, point: P) -> Result<FloatPoint>
where
T: Into<Cow<'static, str>>,
P: AsRef<[f32]>,
{
let point = point.as_ref();
let value = Self::pack(point)?;
let field_type = Self::get_type(point.len())?;
let parent_field = Field::from_bytes_ref(name, value, field_type)?;
Ok(FloatPoint { parent_field })
}
pub fn next_up(f: f32) -> f32 {
if f.to_bits() == 0x8000_0000u32 {
0.0
} else {
f.next_up()
}
}
pub fn next_down(f: f32) -> f32 {
if f.to_bits() == 0u32 {
-0.0
} else {
f.next_down()
}
}
fn get_type(num_dims: usize) -> Result<FieldType> {
let mut field_type = FieldType::new();
field_type.set_dimensions(num_dims, BitUtil::FLOAT_BYTES)?;
field_type.freeze();
Ok(field_type)
}
pub fn set_float_values<P>(&mut self, point: P) -> Result<()>
where
P: AsRef<[f32]>,
{
let point = point.as_ref();
if self.parent_field.field_type().point_dimension_count() != point.len() {
return Err(LuceneError::illegal_argument(format!(
"this field (name={}) uses {} dimensions; cannot change to (incoming) {} dimensions",
self.parent_field.name(),
self.parent_field.field_type().point_dimension_count(),
point.len()
)));
}
if let FieldDataEnum::Binary(value) = &mut self.parent_field.fields_data {
for (i, &dim) in point.iter().enumerate() {
Self::encode_dimension(dim, &mut value.bytes, i * BitUtil::FLOAT_BYTES);
}
} else {
self.parent_field.fields_data = Self::pack(point)?.into();
}
Ok(())
}
pub fn pack<P>(point: P) -> Result<BytesRef<Vec<u8>>>
where
P: AsRef<[f32]>,
{
let point = point.as_ref();
if point.is_empty() {
return Err(LuceneError::illegal_argument(
"point must not be 0 dimensions".to_string(),
));
}
let mut packed = Vec::with_capacity(point.len() * BitUtil::FLOAT_BYTES);
let mut encoded = [0; BitUtil::FLOAT_BYTES];
for &dim in point {
Self::encode_dimension(dim, &mut encoded, 0);
packed.extend_from_slice(&encoded);
}
Ok(BytesRef::from_bytes(packed))
}
pub fn encode_dimension(value: f32, dest: &mut [u8], offset: usize) {
let sortable = NumericUtils::float_to_sortable_int(value);
NumericUtils::int_to_sortable_bytes(sortable, dest, offset);
}
pub fn decode_dimension(value: &[u8], offset: usize) -> f32 {
let int_val = NumericUtils::sortable_bytes_to_int(value, offset);
NumericUtils::sortable_int_to_float(int_val)
}
pub fn new_exact_query<T>(field: T, value: f32) -> Result<PointRangeQuery>
where
T: Into<String>,
{
Self::new_range_query(field, value, value)
}
pub fn new_range_query<T>(field: T, lower_value: f32, upper_value: f32) -> Result<PointRangeQuery>
where
T: Into<String>,
{
Self::new_range_query_n(field, [lower_value], [upper_value])
}
pub fn new_set_query<T, V>(field: T, values: V) -> Result<PointInSetQuery>
where
T: Into<String>,
V: Into<Vec<f32>>,
{
let mut sorted_values = values.into();
sorted_values.sort_unstable_by(|a, b| CoreHelper::compare_f32(*a, *b));
PointInSetQuery::new(
field.into(),
1,
BitUtil::FLOAT_BYTES,
FloatPointSetBytesRefIterator::new(sorted_values),
FloatPointInSetQuery,
)
}
pub fn new_range_query_n<T, L, U>(
field: T,
lower_value: L,
upper_value: U,
) -> Result<PointRangeQuery>
where
T: Into<String>,
L: AsRef<[f32]>,
U: AsRef<[f32]>,
{
let field = field.into();
let len = lower_value.as_ref().len();
let mut lower_point = FloatPoint::pack(lower_value.as_ref())?;
let mut upper_point = FloatPoint::pack(upper_value.as_ref())?;
#[cfg(debug_assertions)]
check_args(&field, &lower_point.bytes, &upper_point.bytes)?;
PointRangeQuery::new(
field,
lower_point.take_bytes(),
upper_point.take_bytes(),
len,
FloatPointRangeQuery,
)
}
}
struct FloatPointSetBytesRefIterator {
sorted_values: Vec<f32>,
upto: usize,
encoded: BytesRef<[u8; BitUtil::FLOAT_BYTES]>,
}
impl FloatPointSetBytesRefIterator {
fn new(sorted_values: Vec<f32>) -> Self {
Self {
sorted_values,
upto: 0,
encoded: BytesRef {
bytes: [0u8; BitUtil::FLOAT_BYTES],
offset: 0,
length: BitUtil::FLOAT_BYTES,
},
}
}
}
impl BytesRefIterator for FloatPointSetBytesRefIterator {
type Value<'a>
= &'a BytesRef<[u8; BitUtil::FLOAT_BYTES]>
where
Self: 'a;
#[inline]
fn next(&mut self) -> Result<Option<Self::Value<'_>>> {
if self.upto == self.sorted_values.len() {
Ok(None)
} else {
FloatPoint::encode_dimension(self.sorted_values[self.upto], &mut self.encoded.bytes, 0);
self.upto += 1;
Ok(Some(&self.encoded))
}
}
}
impl FieldBase for FloatPoint {
fn set_bytes_value<'a, B>(&mut self, _value: B) -> Result<()>
where
B: Into<BinaryValueEnum<'a>>,
{
Err(LuceneError::illegal_argument(
"cannot change value type from float to BytesRef".to_string(),
))
}
fn set_float_value(&mut self, value: f32) -> Result<()> {
self.set_float_values([value])
}
}
impl IndexableField for FloatPoint {
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<'_>>> {
self.parent_field.binary_value()
}
fn take_binary_value(&mut self) -> Result<Option<BytesRef<Vec<u8>>>> {
self.parent_field.take_binary_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>> {
todo!()
}
fn numeric_value(&self) -> Result<Option<Number>> {
if self.parent_field.field_type().point_dimension_count() != 1 {
return Err(LuceneError::illegal_state(format!(
"this field (name={}) uses {} dimensions; cannot convert to a single numeric value",
self.parent_field.name(),
self.parent_field.field_type().point_dimension_count()
)));
}
match &self.parent_field.fields_data {
FieldDataEnum::Binary(bytes) => {
debug_assert!(bytes.length == BitUtil::FLOAT_BYTES);
let value = Self::decode_dimension(&bytes.bytes, bytes.offset);
Ok(Some(Number::F32(value)))
},
_ => Err(LuceneError::illegal_argument(
"Unsupported FieldDataEnum variant",
)),
}
}
type StoredValue<'a>
= &'a FieldDataEnum
where
Self: 'a;
fn stored_value(&self) -> Result<Option<Self::StoredValue<'_>>> {
self.parent_field.stored_value()
}
fn invertable_type(&self) -> &InvertableType {
self.parent_field.invertable_type()
}
}
impl fmt::Display for FloatPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} <{}:",
std::any::type_name::<Self>(),
self.parent_field.name()
)?;
match &self.parent_field.fields_data {
FieldDataEnum::Binary(bytes) => {
let dim_count = self.parent_field.field_type().point_dimension_count();
for dim in 0..dim_count {
if dim > 0 {
write!(f, ",")?;
}
let value =
Self::decode_dimension(&bytes.bytes, bytes.offset + dim * BitUtil::FLOAT_BYTES);
write!(f, "{value}")?;
}
},
_ => {
debug_assert!(false, "no possible here");
write!(f, "Unsupported FieldDataEnum variant")?;
},
}
write!(f, ">")
}
}
#[derive(Debug, Clone)]
pub struct FloatPointRangeQuery;
impl PointRangeBase for FloatPointRangeQuery {
fn to_string(&self, _dimension: usize, value: &[u8]) -> Result<String> {
Ok(FloatPoint::decode_dimension(value, 0).to_string())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FloatPointInSetQuery;
impl PointInSetBase for FloatPointInSetQuery {
fn to_string(&self, value: &[u8]) -> Result<String> {
debug_assert!(value.len() == BitUtil::FLOAT_BYTES);
Ok(FloatPoint::decode_dimension(value, 0).to_string())
}
}