rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use crate::core::analysis::analyzer::Analyzer;
use crate::core::analysis::reader::ReaderEnum;
use crate::core::document::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::index::point_values::MAX_INDEX_DIMENSIONS;
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::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;
use std::sync::OnceLock;

// A point type is frozen and depends only on its number of dimensions.
static FIELD_TYPES: [OnceLock<FieldType>; MAX_INDEX_DIMENSIONS] =
  [const { OnceLock::new() }; MAX_INDEX_DIMENSIONS];

pub struct LongPoint {
  name: Cow<'static, str>,
  field_type: &'static FieldType,
  packed: Option<[u8; MAX_INDEX_DIMENSIONS * BitUtil::LONG_BYTES]>,
}

impl LongPoint {
  /// Creates a new [`LongPoint`] with the given name and `i64` values.
  /// Static names are borrowed; owned strings are moved. Convert a non-static
  /// borrowed name to an owned string before passing it to this constructor.
  pub fn new<T, P>(name: T, point: P) -> Result<LongPoint>
  where
    T: Into<Cow<'static, str>>,
    P: AsRef<[i64]>,
  {
    let point = point.as_ref();
    if point.is_empty() {
      return Err(LuceneError::illegal_argument(
        "point must not be 0 dimensions".to_string(),
      ));
    }
    let field_type = Self::get_type(point.len())?;
    let mut bytes = [0; MAX_INDEX_DIMENSIONS * BitUtil::LONG_BYTES];
    for (i, &dim) in point.iter().enumerate() {
      Self::encode_dimension(dim, &mut bytes, i * BitUtil::LONG_BYTES);
    }
    Ok(Self {
      name: name.into(),
      field_type,
      packed: Some(bytes),
    })
  }

  fn get_type(num_dims: usize) -> Result<&'static FieldType> {
    // new() rejects zero dimensions before reaching this method. Keep invalid
    // dimensions on the original validation path, before accessing the cache.
    if num_dims > MAX_INDEX_DIMENSIONS {
      FieldType::new().set_dimensions(num_dims, BitUtil::LONG_BYTES)?;
    }
    Ok(FIELD_TYPES[num_dims - 1].get_or_init(|| {
      let mut field_type = FieldType::new();
      expect_invariant!(
        field_type.set_dimensions(num_dims, BitUtil::LONG_BYTES),
        "the cached point dimension count and byte width are valid"
      );
      field_type.freeze();
      field_type
    }))
  }

  /// Change the values of this field
  #[inline(always)]
  pub fn set_long_values<V>(&mut self, point: V) -> Result<()>
  where
    V: AsRef<[i64]>,
  {
    let point = point.as_ref();
    let dims = self.field_type.point_dimension_count();
    if dims != point.len() {
      return Err(LuceneError::illegal_argument(format!(
        "this field (name={}) uses {} dimensions; cannot change to (incoming) {} dimensions",
        self.name,
        dims,
        point.len()
      )));
    }
    #[expect(
      clippy::unnecessary_lazy_evaluations,
      reason = "Keep packed-buffer initialization on the first setter call"
    )]
    let bytes = self
      .packed
      .get_or_insert_with(|| [0; MAX_INDEX_DIMENSIONS * BitUtil::LONG_BYTES]);
    for (i, &dim) in point.iter().enumerate() {
      Self::encode_dimension(dim, bytes, i * BitUtil::LONG_BYTES);
    }
    Ok(())
  }

  /// Packs an `i64` slice into bytes.
  pub fn pack<P>(point: P) -> Result<BytesRef<Vec<u8>>>
  where
    P: AsRef<[i64]>,
  {
    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::LONG_BYTES);
    let mut encoded = [0; BitUtil::LONG_BYTES];
    for &dim in point {
      Self::encode_dimension(dim, &mut encoded, 0);
      packed.extend_from_slice(&encoded);
    }
    Ok(BytesRef::from_bytes(packed))
  }

  /// Unpacks bytes into `i64` values.
  pub fn unpack(bytes_ref: &BytesRef<Vec<u8>>, start: usize, buf: &mut [i64]) {
    for (i, val) in buf.iter_mut().enumerate() {
      *val = Self::decode_dimension(&bytes_ref.bytes, start + i * BitUtil::LONG_BYTES);
    }
  }

  /// Encode single long dimension
  pub fn encode_dimension(value: i64, dest: &mut [u8], offset: usize) {
    NumericUtils::long_to_sortable_bytes(value, dest, offset);
  }

  /// Decode single long dimension
  pub fn decode_dimension(value: &[u8], offset: usize) -> i64 {
    NumericUtils::sortable_bytes_to_long(value, offset)
  }
  pub fn new_exact_query<T>(field: T, value: i64) -> Result<PointRangeQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query(field, value, value)
  }
  pub fn new_range_query<T>(field: T, lower_value: i64, upper_value: i64) -> Result<PointRangeQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query_n(field, [lower_value], [upper_value])
  }

  /// Create a query matching any of the specified 1D values. This is the points equivalent of
  /// [`TermInSetQuery`](crate::core::search::term_in_set_query::TermInSetQuery).
  ///
  /// # Arguments
  ///
  /// * `field` - Field name.
  /// * `values` - All values to match.
  ///
  /// Owned vectors are sorted in place; borrowed slices are copied before sorting.
  pub fn new_set_query<T, V>(field: T, values: V) -> Result<PointInSetQuery>
  where
    T: Into<String>,
    V: Into<Vec<i64>>,
  {
    let mut sorted_values = values.into();
    sorted_values.sort_unstable();

    PointInSetQuery::new(
      field.into(),
      1,
      BitUtil::LONG_BYTES,
      LongPointSetBytesRefIterator::new(sorted_values),
      LongPointInSetQuery,
    )
  }

  ///
  /// Lower and upper bounds may independently use arrays, vectors, or slices.
  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<[i64]>,
    U: AsRef<[i64]>,
  {
    let field = field.into();
    let len = lower_value.as_ref().len();
    let mut lower_point = LongPoint::pack(lower_value.as_ref())?;
    let mut upper_point = LongPoint::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, // numDims
      LongPointRangeQuery,
    )
  }
}

struct LongPointSetBytesRefIterator {
  sorted_values: Vec<i64>,
  upto: usize,
  encoded: BytesRef<[u8; BitUtil::LONG_BYTES]>,
}

impl LongPointSetBytesRefIterator {
  fn new(sorted_values: Vec<i64>) -> Self {
    Self {
      sorted_values,
      upto: 0,
      encoded: BytesRef {
        bytes: [0u8; BitUtil::LONG_BYTES],
        offset: 0,
        length: BitUtil::LONG_BYTES,
      },
    }
  }
}

impl BytesRefIterator for LongPointSetBytesRefIterator {
  type Value<'a>
    = &'a BytesRef<[u8; BitUtil::LONG_BYTES]>
  where
    Self: 'a;

  #[inline]
  fn next(&mut self) -> Result<Option<Self::Value<'_>>> {
    if self.upto == self.sorted_values.len() {
      Ok(None)
    } else {
      LongPoint::encode_dimension(self.sorted_values[self.upto], &mut self.encoded.bytes, 0);
      self.upto += 1;
      Ok(Some(&self.encoded))
    }
  }
}

impl FieldBase for LongPoint {
  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 long to BytesRef",
    ))
  }

  fn set_long_value(&mut self, value: i64) -> Result<()> {
    self.set_long_values([value])
  }
}

impl IndexableField for LongPoint {
  fn name(&self) -> &str {
    &self.name
  }

  type FieldType<'a>
    = &'a FieldType
  where
    Self: 'a;

  fn field_type(&self) -> Self::FieldType<'_> {
    self.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,
  {
    Ok(None)
  }

  #[inline]
  fn binary_value(&self) -> Result<Option<BinaryValueEnum<'_>>> {
    Ok(self.packed.as_ref().map(|bytes| {
      let length = self.field_type.point_dimension_count() * BitUtil::LONG_BYTES;
      BinaryValueEnum::Slice(BytesRef {
        bytes: &bytes[..length],
        offset: 0,
        length,
      })
    }))
  }

  fn take_binary_value(&mut self) -> Result<Option<BytesRef<Vec<u8>>>> {
    let value = self.binary_value()?.map(BinaryValueEnum::into_owned);
    self.packed = None;
    Ok(value)
  }

  fn string_value(&self) -> Result<Option<Cow<'_, str>>> {
    Ok(None)
  }

  fn take_string_value(&mut self) -> Result<Option<String>> {
    Ok(None)
  }

  fn take_reader_value(&mut self) -> Result<Option<ReaderEnum>> {
    todo!()
  }

  fn numeric_value(&self) -> Result<Option<Number>> {
    let dims = self.field_type.point_dimension_count();
    if dims != 1 {
      return Err(LuceneError::illegal_state(format!(
        "this field (name={}) uses {} dimensions; cannot convert to a single numeric value",
        self.name, dims
      )));
    }
    match &self.packed {
      Some(bytes) => Ok(Some(Number::I64(Self::decode_dimension(bytes, 0)))),
      _ => {
        debug_assert!(false, "no possible here");
        Ok(None)
      },
    }
  }

  type StoredValue<'a>
    = &'a FieldDataEnum
  where
    Self: 'a;

  fn stored_value(&self) -> Result<Option<Self::StoredValue<'_>>> {
    Ok(None)
  }

  fn invertable_type(&self) -> &InvertableType {
    &InvertableType::TokenStream
  }
}

impl fmt::Display for LongPoint {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{} <{}:", std::any::type_name::<Self>(), self.name)?;
    match &self.packed {
      Some(bytes) => {
        let dim_count = self.field_type.point_dimension_count();
        for dim in 0..dim_count {
          if dim > 0 {
            write!(f, ",")?;
          }
          let value = Self::decode_dimension(bytes, dim * BitUtil::LONG_BYTES);
          write!(f, "{value}")?;
        }
      },
      _ => {
        debug_assert!(false, "no possible here");
        write!(f, "Unsupported FieldDataEnum variant")?;
      },
    }
    write!(f, ">")
  }
}

#[derive(Debug, Clone)]
pub struct LongPointRangeQuery;

impl PointRangeBase for LongPointRangeQuery {
  fn to_string(&self, _dimension: usize, value: &[u8]) -> Result<String> {
    Ok(LongPoint::decode_dimension(value, 0).to_string())
  }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LongPointInSetQuery;

impl PointInSetBase for LongPointInSetQuery {
  fn to_string(&self, value: &[u8]) -> Result<String> {
    debug_assert!(value.len() == BitUtil::LONG_BYTES);
    Ok(LongPoint::decode_dimension(value, 0).to_string())
  }
}