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::document::xy_point_in_geometry_query::XYPointInGeometryQuery;
use crate::core::geo::xy_circle::XYCircle;
use crate::core::geo::xy_encoding_utils::XYEncodingUtils;
use crate::core::geo::xy_geometry::{XYGeometry, XYGeometryEnum};
use crate::core::geo::xy_polygon::XYPolygon;
use crate::core::geo::xy_rectangle::XYRectangle;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::field_info::FieldInfo;
use crate::core::index::indexable_field::{
IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::index::indexable_field_type::IndexableFieldType;
use crate::core::search::query::Query;
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::{Display, Formatter};
use std::sync::LazyLock;
pub const BYTES: usize = BitUtil::INT_BYTES;
pub(crate) static TYPE: LazyLock<FieldType> = LazyLock::new(|| {
let mut ft = FieldType::new();
expect_invariant!(
ft.set_dimensions(2, BitUtil::INT_BYTES),
"the built-in XY point dimensions are valid"
);
ft.freeze();
ft
});
pub struct XYPointField {
parent_field: Field,
}
impl XYPointField {
pub fn set_location_value(&mut self, x: f32, y: f32) -> Result<()> {
let x_encoded = XYEncodingUtils::encode(x)?;
let y_encoded = XYEncodingUtils::encode(y)?;
match &mut self.parent_field.fields_data {
FieldDataEnum::Binary(bytes) => {
let encoded = &mut bytes.bytes[..2 * BitUtil::INT_BYTES];
NumericUtils::int_to_sortable_bytes(x_encoded, encoded, 0);
NumericUtils::int_to_sortable_bytes(y_encoded, encoded, BitUtil::INT_BYTES);
bytes.offset = 0;
bytes.length = bytes.bytes.len();
},
_ => {
let mut encoded = vec![0u8; 2 * BitUtil::INT_BYTES];
NumericUtils::int_to_sortable_bytes(x_encoded, &mut encoded, 0);
NumericUtils::int_to_sortable_bytes(y_encoded, &mut encoded, BitUtil::INT_BYTES);
self.parent_field.fields_data = BytesRef::from_bytes(encoded).into();
},
}
Ok(())
}
pub fn new<T>(name: T, x: f32, y: f32) -> Result<Self>
where
T: Into<Cow<'static, str>>,
{
let mut field = Self {
parent_field: Field::new(
name.into(),
BytesRef::from_bytes(vec![0u8; 2 * BitUtil::INT_BYTES]),
TYPE.clone(),
),
};
field.set_location_value(x, y)?;
Ok(field)
}
pub(crate) fn check_compatible(field_info: &FieldInfo) -> Result<()> {
if field_info.get_point_dimension_count() != 0
&& field_info.get_point_dimension_count() != TYPE.point_dimension_count()
{
return Err(LuceneError::illegal_argument(format!(
"field=\"{}\" was indexed with numDims={} but this point type has numDims={}, is the field really a XYPoint?",
field_info.name,
field_info.get_point_dimension_count(),
TYPE.point_dimension_count()
)));
}
if field_info.get_point_num_bytes() != 0
&& field_info.get_point_num_bytes() != TYPE.point_num_bytes()
{
return Err(LuceneError::illegal_argument(format!(
"field=\"{}\" was indexed with bytesPerDim={} but this point type has bytesPerDim={}, is the field really a XYPoint?",
field_info.name,
field_info.get_point_num_bytes(),
TYPE.point_num_bytes()
)));
}
Ok(())
}
pub fn new_box_query<T>(field: T, min_x: f32, max_x: f32, min_y: f32, max_y: f32) -> Result<Query>
where
T: Into<String>,
{
let rectangle = XYRectangle::new(min_x, max_x, min_y, max_y)?;
Ok(XYPointInGeometryQuery::new(field.into(), vec![rectangle.into()])?.into())
}
pub fn new_distance_query<T>(field: T, x: f32, y: f32, radius: f32) -> Result<Query>
where
T: Into<String>,
{
let circle = XYCircle::new(x, y, radius)?;
Ok(XYPointInGeometryQuery::new(field.into(), vec![circle.into()])?.into())
}
pub fn new_polygon_query<T, I>(field: T, polygons: I) -> Result<Query>
where
T: Into<String>,
I: IntoIterator<Item = XYPolygon>,
{
Self::new_geometry_query(field, polygons)
}
pub fn new_geometry_query<S, T, I>(field: S, xy_geometries: I) -> Result<Query>
where
S: Into<String>,
T: XYGeometry + Into<XYGeometryEnum>,
I: IntoIterator<Item = T>,
{
let xy_geometries: Vec<XYGeometryEnum> = xy_geometries.into_iter().map(Into::into).collect();
Ok(XYPointInGeometryQuery::new(field.into(), xy_geometries)?.into())
}
}
impl Display for XYPointField {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let bytes = match self.parent_field.fields_data {
FieldDataEnum::Binary(ref bytes) => bytes,
_ => {
return Err(std::fmt::Error);
},
};
let x = XYEncodingUtils::decode_bytes(bytes.bytes.as_slice(), 0);
let y = XYEncodingUtils::decode_bytes(bytes.bytes.as_slice(), BitUtil::INT_BYTES);
write!(f, "XYPointField <{}:{},{}>", self.parent_field.name, x, y)
}
}
impl IndexableField for XYPointField {
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>> {
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<'_>>> {
self.parent_field.stored_value()
}
fn invertable_type(&self) -> &InvertableType {
self.parent_field.invertable_type()
}
}
impl FieldBase for XYPointField {}