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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 * 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::geo::component2d::Component2D;
use crate::core::geo::geo_utils::GeoUtils;
use crate::core::geo::rectangle::Rectangle;
use crate::core::index::point_values::Relation;
use crate::core::index::point_values::Relation::{CellCrossesQuery, CellInsideQuery};
use crate::core::util::SloppyMath;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::numeric_utils::NumericUtils;
use std::sync::LazyLock;
pub static MIN_LON_ENCODED: LazyLock<i32> = LazyLock::new(|| {
  expect_invariant!(
    encode_longitude(GeoUtils::MIN_LON_INCL),
    "MIN_LON_INCL is a valid longitude"
  )
});

pub static MAX_LON_ENCODED: LazyLock<i32> = LazyLock::new(|| {
  expect_invariant!(
    encode_longitude(GeoUtils::MAX_LON_INCL),
    "MAX_LON_INCL is a valid longitude"
  )
});
/// Quantizes double (64 bit) longitude into 32 bits (rounding down: in the direction of -180)
///
/// # Arguments
///
/// * `longitude` - longitude value: must be within standard +/-180 coordinate bounds.
///
/// # Returns
///
/// encoded value as a 32-bit `i32`
///
/// # Errors
///
/// Returns an error if longitude is out of bounds.
pub fn encode_longitude(longitude: f64) -> Result<i32> {
  GeoUtils::check_longitude(longitude)?;
  let mut longitude = longitude;
  // the maximum possible value cannot be encoded without overflow
  if longitude == 180.0 {
    longitude = longitude.next_down();
  }
  Ok((longitude / GeoEncodingUtils::LON_DECODE).floor() as i32)
}
pub struct GeoEncodingUtils;
impl GeoEncodingUtils {
  /// number of bits used for quantizing latitude and longitude values
  pub const BITS: u32 = 32;

  const LAT_SCALE: f64 = (1u64 << Self::BITS) as f64 / 180.0;
  const LAT_DECODE: f64 = 1.0 / Self::LAT_SCALE;
  const LON_SCALE: f64 = (1u64 << Self::BITS) as f64 / 360.0;
  const LON_DECODE: f64 = 1.0 / Self::LON_SCALE;

  /// Quantizes double (64 bit) latitude into 32 bits (rounding down: in the direction of -90)
  ///
  /// # Arguments
  ///
  /// * `latitude` - latitude value: must be within standard +/-90 coordinate bounds.
  ///
  /// # Returns
  ///
  /// encoded value as a 32-bit `i32`
  ///
  /// # Errors
  ///
  /// Returns an error if latitude is out of bounds.
  pub fn encode_latitude(latitude: f64) -> Result<i32> {
    GeoUtils::check_latitude(latitude)?;
    let mut latitude = latitude;
    // the maximum possible value cannot be encoded without overflow
    if latitude == 90.0 {
      latitude = latitude.next_down();
    }
    Ok((latitude / Self::LAT_DECODE).floor() as i32)
  }

  /// Quantizes double (64 bit) latitude into 32 bits (rounding up: in the direction of +90)
  ///
  /// # Arguments
  ///
  /// * `latitude` - latitude value: must be within standard +/-90 coordinate bounds.
  ///
  /// # Returns
  ///
  /// encoded value as a 32-bit `i32`
  ///
  /// # Errors
  ///
  /// Returns an error if latitude is out of bounds.
  pub fn encode_latitude_ceil(latitude: f64) -> Result<i32> {
    GeoUtils::check_latitude(latitude)?;
    let mut latitude = latitude;
    // the maximum possible value cannot be encoded without overflow
    if latitude == 90.0 {
      latitude = latitude.next_down();
    }
    Ok((latitude / Self::LAT_DECODE).ceil() as i32)
  }

  /// Quantizes double (64 bit) longitude into 32 bits (rounding down: in the direction of -180)
  ///
  /// # Arguments
  ///
  /// * `longitude` - longitude value: must be within standard +/-180 coordinate bounds.
  ///
  /// # Returns
  ///
  /// encoded value as a 32-bit `i32`
  ///
  /// # Errors
  ///
  /// Returns an error if longitude is out of bounds.
  pub fn encode_longitude(longitude: f64) -> Result<i32> {
    encode_longitude(longitude)
  }

  /// Quantizes double (64 bit) longitude into 32 bits (rounding up: in the direction of +180)
  ///
  /// # Arguments
  ///
  /// * `longitude` - longitude value: must be within standard +/-180 coordinate bounds.
  ///
  /// # Returns
  ///
  /// encoded value as a 32-bit `i32`
  ///
  /// # Errors
  ///
  /// Returns an error if longitude is out of bounds.
  pub fn encode_longitude_ceil(longitude: f64) -> Result<i32> {
    GeoUtils::check_longitude(longitude)?;
    let mut longitude = longitude;
    // the maximum possible value cannot be encoded without overflow
    if longitude == 180.0 {
      longitude = longitude.next_down();
    }
    Ok((longitude / Self::LON_DECODE).ceil() as i32)
  }

  /// Turns quantized value from `encode_latitude` back into a double.
  ///
  /// # Arguments
  ///
  /// * `encoded` - encoded value: 32-bit quantized value.
  ///
  /// # Returns
  ///
  /// decoded latitude value.
  pub fn decode_latitude(encoded: i32) -> f64 {
    let result = encoded as f64 * Self::LAT_DECODE;
    debug_assert!((GeoUtils::MIN_LAT_INCL..GeoUtils::MAX_LAT_INCL).contains(&result));
    result
  }

  /// Turns quantized value from byte array back into a double.
  ///
  /// # Arguments
  ///
  /// * `src` - byte array containing 4 bytes to decode at `offset`
  /// * `offset` - offset into `src` to decode from.
  ///
  /// # Returns
  ///
  /// decoded latitude value.
  pub fn decode_latitude_from_bytes(src: &[u8], offset: usize) -> f64 {
    Self::decode_latitude(NumericUtils::sortable_bytes_to_int(src, offset))
  }

  /// Turns quantized value from [`encode_longitude`] back into a double.
  ///
  /// # Arguments
  ///
  /// * `encoded` - encoded value: 32-bit quantized value.
  ///
  /// # Returns
  ///
  /// decoded longitude value.
  pub fn decode_longitude(encoded: i32) -> f64 {
    let result = encoded as f64 * Self::LON_DECODE;
    debug_assert!((GeoUtils::MIN_LON_INCL..GeoUtils::MAX_LON_INCL).contains(&result));
    result
  }

  /// Turns quantized value from byte array back into a double.
  ///
  /// # Arguments
  ///
  /// * `src` - byte array containing 4 bytes to decode at `offset`
  /// * `offset` - offset into `src` to decode from.
  ///
  /// # Returns
  ///
  /// decoded longitude value.
  pub fn decode_longitude_from_bytes(src: &[u8], offset: usize) -> f64 {
    Self::decode_longitude(NumericUtils::sortable_bytes_to_int(src, offset))
  }
  /// Create a predicate that checks whether points are within a distance of a given point. It works
  /// by computing the bounding box around the circle that is defined by the given points/distance
  /// and splitting it into between 1024 and 4096 smaller boxes (4096*0.75^2=2304 on average). Then
  /// for each sub box, it computes the relation between this box and the distance query. Finally at
  /// search time, it first computes the sub box that the point belongs to, most of the time, no
  /// distance computation will need to be performed since all points from the sub box will either be
  /// in or out of the circle.
  pub fn create_distance_predicate(
    lat: f64,
    lon: f64,
    radius_meters: f64,
  ) -> Result<DistancePredicate> {
    let bounding_box = Rectangle::from_point_distance(lat, lon, radius_meters)?;
    let axis_lat = Rectangle::axis_lat(lat, radius_meters);
    let distance_sort_key = GeoUtils::distance_query_sort_key(radius_meters);
    let sub_boxes = create_sub_boxes(
      bounding_box.min_lat,
      bounding_box.max_lat,
      bounding_box.min_lon,
      bounding_box.max_lon,
      |box_| {
        GeoUtils::relate(
          box_.min_lat,
          box_.max_lat,
          box_.min_lon,
          box_.max_lon,
          lat,
          lon,
          distance_sort_key,
          axis_lat,
        )
      },
    )?;

    DistancePredicate::new(
      sub_boxes.lat_shift,
      sub_boxes.lon_shift,
      sub_boxes.lat_base,
      sub_boxes.lon_base,
      sub_boxes.max_lat_delta,
      sub_boxes.max_lon_delta,
      sub_boxes.relations,
      lat,
      lon,
      distance_sort_key,
    )
  }
  /// Create a predicate that checks whether points are within a geometry. It works the same way as
  /// [`Self::create_distance_predicate`].
  ///
  /// @lucene.internal
  pub fn create_component_predicate<C>(tree: C) -> Result<Component2DPredicate<C>>
  where
    C: Component2D,
  {
    let box_to_relation =
      |rect: Rectangle| tree.relate(rect.min_lon, rect.max_lon, rect.min_lat, rect.max_lat);
    let sub_boxes = create_sub_boxes(
      tree.get_min_y(),
      tree.get_max_y(),
      tree.get_min_x(),
      tree.get_max_x(),
      box_to_relation,
    )?;

    Component2DPredicate::new(
      sub_boxes.lat_shift,
      sub_boxes.lon_shift,
      sub_boxes.lat_base,
      sub_boxes.lon_base,
      sub_boxes.max_lat_delta,
      sub_boxes.max_lon_delta,
      sub_boxes.relations,
      tree,
    )
  }
}
struct Grid {
  lat_shift: u32,
  lon_shift: u32,
  lat_base: i32,
  lon_base: i32,
  max_lat_delta: i32,
  max_lon_delta: i32,
  relations: Box<[u8]>,
}

impl Grid {
  const ARITY: i32 = 64;

  fn new(
    lat_shift: u32,
    lon_shift: u32,
    lat_base: i32,
    lon_base: i32,
    max_lat_delta: i32,
    max_lon_delta: i32,
    relations: impl Into<Box<[u8]>>,
  ) -> Result<Self> {
    if !(1..=31).contains(&lat_shift) {
      return Err(LuceneError::illegal_argument(
        "lat_shift must be between 1 and 31",
      ));
    }
    if !(1..=31).contains(&lon_shift) {
      return Err(LuceneError::illegal_argument(
        "lon_shift must be between 1 and 31",
      ));
    }

    Ok(Self {
      lat_shift,
      lon_shift,
      lat_base,
      lon_base,
      max_lat_delta,
      max_lon_delta,
      relations: relations.into(),
    })
  }
}
pub struct DistancePredicate {
  base: Grid,
  lat: f64,
  lon: f64,
  distance_key: f64,
}

impl DistancePredicate {
  #[allow(clippy::too_many_arguments)]
  fn new(
    lat_shift: u32,
    lon_shift: u32,
    lat_base: i32,
    lon_base: i32,
    max_lat_delta: i32,
    max_lon_delta: i32,
    relations: Box<[u8]>,
    lat: f64,
    lon: f64,
    distance_key: f64,
  ) -> Result<Self> {
    Ok(Self {
      base: Grid::new(
        lat_shift,
        lon_shift,
        lat_base,
        lon_base,
        max_lat_delta,
        max_lon_delta,
        relations,
      )?,
      lat,
      lon,
      distance_key,
    })
  }
  /// Check whether the given point is within a distance of another point.
  /// NOTE: this operates directly on the encoded representation of points.
  pub fn test(&self, lat: i32, lon: i32) -> bool {
    let lat2 = ((lat.wrapping_sub(i32::MIN)) as u32 >> self.base.lat_shift) as i32;
    let lat_delta = lat2.wrapping_sub(self.base.lat_base);
    if lat2 < self.base.lat_base || lat_delta >= self.base.max_lat_delta {
      return false;
    }

    let mut lon2 = ((lon.wrapping_sub(i32::MIN)) as u32 >> self.base.lon_shift) as i32;
    if lon2 < self.base.lon_base {
      lon2 = lon2.wrapping_add(1i32.wrapping_shl(32 - self.base.lon_shift));
    }

    debug_assert!((lon2 as u32) >= (self.base.lon_base as u32));
    let lon_delta = lon2.wrapping_sub(self.base.lon_base);
    debug_assert!(lon_delta >= 0);

    if lon_delta >= self.base.max_lon_delta {
      return false;
    }

    let relation = self.base.relations[(lat_delta
      .wrapping_mul(self.base.max_lon_delta)
      .wrapping_add(lon_delta)) as usize];

    if relation == CellCrossesQuery.ordinal() as u8 {
      SloppyMath::haversin_sort_key(
        GeoEncodingUtils::decode_latitude(lat),
        GeoEncodingUtils::decode_longitude(lon),
        self.lat,
        self.lon,
      ) <= self.distance_key
    } else {
      relation == CellInsideQuery.ordinal() as u8
    }
  }
}

fn create_sub_boxes<F>(
  shape_min_lat: f64,
  shape_max_lat: f64,
  shape_min_lon: f64,
  shape_max_lon: f64,
  box_to_relation: F,
) -> Result<Grid>
where
  F: Fn(Rectangle) -> Result<Relation>,
{
  let min_lat = GeoEncodingUtils::encode_latitude_ceil(shape_min_lat)?;
  let max_lat = GeoEncodingUtils::encode_latitude(shape_max_lat)?;
  let min_lon = GeoEncodingUtils::encode_longitude_ceil(shape_min_lon)?;
  let max_lon = GeoEncodingUtils::encode_longitude(shape_max_lon)?;

  if max_lat < min_lat || (shape_max_lon >= shape_min_lon && max_lon < min_lon) {
    return Grid::new(1, 1, 0, 0, 0, 0, vec![]);
  }

  let min_lat2 = min_lat as i64 - i32::MIN as i64;
  let max_lat2 = max_lat as i64 - i32::MIN as i64;
  let lat_shift = compute_shift(min_lat2, max_lat2);
  let lat_base = (min_lat2 as u64 >> lat_shift) as i32;
  let max_lat_delta = (max_lat2 as u64 >> lat_shift) as i32 - lat_base + 1;
  debug_assert!(max_lat_delta > 0);

  let min_lon2 = min_lon as i64 - i32::MIN as i64;
  let mut max_lon2 = max_lon as i64 - i32::MIN as i64;
  if shape_max_lon < shape_min_lon {
    max_lon2 += 1i64 << 32;
  }
  let lon_shift = compute_shift(min_lon2, max_lon2);
  let lon_base = (min_lon2 as u64 >> lon_shift) as i32;
  let max_lon_delta = ((max_lon2 as u64 >> lon_shift) as i32)
    .wrapping_sub(lon_base)
    .wrapping_add(1);
  debug_assert!(max_lon_delta > 0);

  let mut relations = Vec::with_capacity((max_lat_delta * max_lon_delta) as usize);
  for i in 0..max_lat_delta {
    for j in 0..max_lon_delta {
      let box_min_lat = lat_base
        .wrapping_add(i)
        .wrapping_shl(lat_shift)
        .wrapping_add(i32::MIN);
      let box_min_lon = lon_base
        .wrapping_add(j)
        .wrapping_shl(lon_shift)
        .wrapping_add(i32::MIN);
      let box_max_lat = box_min_lat.wrapping_add(1i32.wrapping_shl(lat_shift).wrapping_sub(1));
      let box_max_lon = box_min_lon.wrapping_add(1i32.wrapping_shl(lon_shift).wrapping_sub(1));
      let rect = Rectangle::new(
        GeoEncodingUtils::decode_latitude(box_min_lat),
        GeoEncodingUtils::decode_latitude(box_max_lat),
        GeoEncodingUtils::decode_longitude(box_min_lon),
        GeoEncodingUtils::decode_longitude(box_max_lon),
      )?;
      relations.push(box_to_relation(rect)?.ordinal() as u8);
    }
  }

  Grid::new(
    lat_shift,
    lon_shift,
    lat_base,
    lon_base,
    max_lat_delta,
    max_lon_delta,
    relations,
  )
}

fn compute_shift(a: i64, b: i64) -> u32 {
  debug_assert!(a <= b);
  let mut shift = 1;
  loop {
    let delta = (b as u64 >> shift) as i64 - (a as u64 >> shift) as i64;
    if (0..Grid::ARITY as i64).contains(&delta) {
      return shift;
    }
    shift += 1;
  }
}
/// A predicate that checks whether a given point is within a component2D geometry.
pub struct Component2DPredicate<C> {
  base: Grid,
  tree: C,
}

impl<C> Component2DPredicate<C> {
  pub(crate) fn component2d(&self) -> &C {
    &self.tree
  }

  #[allow(clippy::too_many_arguments)]
  fn new(
    lat_shift: u32,
    lon_shift: u32,
    lat_base: i32,
    lon_base: i32,
    max_lat_delta: i32,
    max_lon_delta: i32,
    relations: Box<[u8]>,
    tree: C,
  ) -> Result<Self> {
    Ok(Self {
      base: Grid::new(
        lat_shift,
        lon_shift,
        lat_base,
        lon_base,
        max_lat_delta,
        max_lon_delta,
        relations,
      )?,
      tree,
    })
  }
}

impl<C> Component2DPredicate<C>
where
  C: Component2D,
{
  /// Check whether the given point is within the considered polygon. NOTE: this operates directly
  /// on the encoded representation of points.
  pub fn test(&self, lat: i32, lon: i32) -> bool {
    let base = &self.base;

    let lat2 = ((lat.wrapping_sub(i32::MIN) as u32) >> base.lat_shift) as i32;
    let lat_delta = lat2.wrapping_sub(base.lat_base);
    if lat2 < base.lat_base || lat_delta >= base.max_lat_delta {
      return false;
    }

    let mut lon2 = ((lon.wrapping_sub(i32::MIN) as u32) >> base.lon_shift) as i32;
    if lon2 < base.lon_base {
      lon2 = lon2.wrapping_add(1i32.wrapping_shl(32 - base.lon_shift));
    }

    debug_assert!((lon2 as u32) >= (base.lon_base as u32));
    let lon_delta = lon2.wrapping_sub(base.lon_base);
    debug_assert!(lon_delta >= 0);

    if lon_delta >= base.max_lon_delta {
      return false;
    }

    let relation = base.relations[(lat_delta
      .wrapping_mul(base.max_lon_delta)
      .wrapping_add(lon_delta)) as usize];

    if relation == CellCrossesQuery.ordinal() as u8 {
      self.tree.contains(
        GeoEncodingUtils::decode_longitude(lon),
        GeoEncodingUtils::decode_latitude(lat),
      )
    } else {
      relation == CellInsideQuery.ordinal() as u8
    }
  }
}