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
/*
 * 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 std::env;
use std::sync::LazyLock;
use std::time::SystemTime;

use parking_lot::Mutex;
use rand::RngExt;

use crate::core::index::BytesRef;
use crate::core::util::CoreHelper;
use crate::core::util::access::{ByteSource, SharedAccessVec, WritableVec};
use crate::core::util::bit_util::BitUtil;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::ints_ref::IntsRef;
use crate::with_other;

/// Methods for manipulating strings.
///
/// # Note
/// This is an internal API.
pub struct StringHelper;
impl StringHelper {
  /// Compares two [`BytesRef`], element by element, and returns the number of
  /// elements common to both arrays (from the start of each). This method
  /// assumes `currentTerm` comes after `priorTerm`.
  ///
  /// # Arguments
  ///
  /// * `prior_term` - The first [`BytesRef`] to compare
  /// * `current_term` - The second [`BytesRef`] to compare
  ///
  /// # Returns
  ///
  /// The number of common elements (from the start of each).
  pub fn bytes_difference<AV, BV>(
    prior_term: &BytesRef<AV>,
    current_term: &BytesRef<BV>,
  ) -> Result<usize>
  where
    AV: ByteSource,
    BV: ByteSource,
  {
    let prior_term_bytes = prior_term.bytes.as_slice();
    let current_term_bytes = current_term.bytes.as_slice();
    let mismatch = CoreHelper::miss_match_u8(
      &prior_term_bytes[prior_term.offset..(prior_term.offset + prior_term.length)],
      &current_term_bytes[current_term.offset..(current_term.offset + current_term.length)],
    );

    if mismatch < 0 {
      return Err(LuceneError::illegal_argument(format!(
        "terms out of order: priorTerm={prior_term}, currentTerm={current_term}"
      )));
    }
    Ok(mismatch as usize)
  }
  /// Returns the length of `current_term` needed for use as a sort key so
  /// that `BytesRef::compare_to()` still returns the same result.
  /// This method assumes `current_term` comes after `prior_term`.
  ///
  /// # Arguments
  ///
  /// * `prior_term` - The first [`BytesRef`] to compare
  /// * `current_term` - The second [`BytesRef`] to compare
  ///
  /// # Returns
  ///
  /// The length needed for the sort key.
  pub fn sort_key_length<AV, BV>(
    prior_term: &BytesRef<AV>,
    current_term: &BytesRef<BV>,
  ) -> Result<usize>
  where
    AV: ByteSource,
    BV: ByteSource,
  {
    let difference = Self::bytes_difference(prior_term, current_term)?;
    Ok(difference + 1)
  }

  /// Returns `true` if the given `ref` starts with the given `prefix`.
  /// Otherwise returns `false`.
  ///
  /// # Arguments
  ///
  /// * `ref_bytes` - The byte slice to test.
  /// * `prefix` - The expected prefix as [`BytesRef`]
  ///
  /// # Returns
  ///
  /// `true` if `ref_bytes` starts with the given `prefix`, otherwise `false`.
  pub fn starts_with_byte_array<AV>(ref_bytes: &[u8], prefix: &BytesRef<AV>) -> bool
  where
    AV: SharedAccessVec<u8>,
  {
    // Not long enough to start with the prefix
    if ref_bytes.len() < prefix.length {
      return false;
    }
    let ref_slice = &ref_bytes[0..prefix.length];
    prefix.bytes.access(|bytes| {
      let prefix_slice = &bytes[prefix.offset..prefix.offset + prefix.length];
      ref_slice == prefix_slice
    })
  }
  /// Returns `true` if the given `ref` starts with the given `prefix`.
  /// Otherwise returns `false`.
  ///
  /// # Arguments
  ///
  /// * `ref_bytes` - The [`BytesRef`] to test
  /// * `prefix` - The expected prefix as [`BytesRef`]
  ///
  /// # Returns
  ///
  /// `true` if `ref_bytes` starts with the given `prefix`, otherwise `false`.
  pub fn starts_with_byte_ref<AV, BV>(ref_bytes: &BytesRef<AV>, prefix: &BytesRef<BV>) -> bool
  where
    AV: SharedAccessVec<u8>,
    BV: SharedAccessVec<u8>,
  {
    with_other!(
      ref_bytes.bytes,
      prefix.bytes,
      |ref_bytes_bytes, prefix_bytes| {
        Self::starts_with(
          ref_bytes_bytes,
          ref_bytes.offset,
          ref_bytes.length,
          prefix_bytes,
          prefix.offset,
          prefix.length,
        )
      }
    )
  }
  pub fn starts_with(
    ref_bytes: &[u8],
    ref_offset: usize,
    ref_length: usize,
    prefix: &[u8],
    prefix_offset: usize,
    prefix_length: usize,
  ) -> bool {
    // Not long enough to start with the prefix
    if ref_length < prefix_length {
      return false;
    }

    // Check if the prefix matches
    let ref_slice = &ref_bytes[ref_offset..(ref_offset + prefix_length)];
    let prefix_slice = &prefix[prefix_offset..(prefix_offset + prefix_length)];

    ref_slice == prefix_slice
  }

  /// Returns `true` if the `ref` ends with the given `suffix`. Otherwise
  /// returns `false`.
  ///
  /// # Arguments
  ///
  /// * `ref` - The [`BytesRef`] to test
  /// * `suffix` - The expected suffix as [`BytesRef`]
  ///
  /// # Returns
  ///
  /// `true` if `reference` ends with the given `suffix`, otherwise `false`.
  pub fn ends_with<AV, BV>(ref_bytes: &BytesRef<AV>, suffix: &BytesRef<BV>) -> bool
  where
    AV: SharedAccessVec<u8>,
    BV: SharedAccessVec<u8>,
  {
    with_other!(
      ref_bytes.bytes,
      suffix.bytes,
      |ref_bytes_bytes, suffix_bytes| {
        // Not long enough to start with the suffix
        if ref_bytes.length < suffix.length {
          return false;
        }
        let start_at = ref_bytes.length - suffix.length;

        let ref_slice = &ref_bytes_bytes
          [ref_bytes.offset + start_at..(ref_bytes.offset + start_at + suffix.length)];
        let suffix_slice = &suffix_bytes[suffix.offset..(suffix.offset + suffix.length)];
        ref_slice == suffix_slice
      }
    )
  }

  /// Returns the MurmurHash3_x86_32 hash.
  /// Original source/tests at <https://github.com/yonik/java_util>
  pub fn murmurhash3_x86_32_with_byte(data: &[u8], offset: usize, len: usize, seed: i32) -> i32 {
    let c1: u32 = 0xcc9e2d51;
    let c2: u32 = 0x1b873593;

    let mut h1 = seed as u32;
    let rounded_end = offset + (len & 0xfffffffc); // round down to 4 byte block

    if let Some(blocks) = data.get(offset..rounded_end) {
      for block in blocks.as_chunks::<4>().0 {
        let mut k1 = u32::from_le_bytes(*block).wrapping_mul(c1);
        k1 = k1.rotate_left(15);
        k1 = k1.wrapping_mul(c2);

        h1 ^= k1;
        h1 = h1.rotate_left(13);
        h1 = h1.wrapping_mul(5).wrapping_add(0xe6546b64);
      }
    } else {
      let mut i = offset;
      while i < rounded_end {
        let k1 = BitUtil::get_i32_le(data, i) as u32;
        let mut k1 = k1.wrapping_mul(c1);
        k1 = k1.rotate_left(15);
        k1 = k1.wrapping_mul(c2);

        h1 ^= k1;
        h1 = h1.rotate_left(13);
        h1 = h1.wrapping_mul(5).wrapping_add(0xe6546b64);

        i += 4;
      }
    }

    // tail
    let mut k1 = 0u32;
    match len & 0x03 {
      3 => {
        k1 = (data[rounded_end + 2] as u32) << 16;
        k1 |= (data[rounded_end + 1] as u32) << 8;
        k1 |= data[rounded_end] as u32;
        k1 = k1.wrapping_mul(c1);
        k1 = k1.rotate_left(15);
        k1 = k1.wrapping_mul(c2);
        h1 ^= k1;
      },
      // fallthrough
      2 => {
        k1 |= (data[rounded_end + 1] as u32) << 8;
        k1 |= data[rounded_end] as u32;
        k1 = k1.wrapping_mul(c1);
        k1 = k1.rotate_left(15);
        k1 = k1.wrapping_mul(c2);
        h1 ^= k1;
      },
      // fallthrough
      1 => {
        k1 |= data[rounded_end] as u32;
        k1 = k1.wrapping_mul(c1);
        k1 = k1.rotate_left(15);
        k1 = k1.wrapping_mul(c2);
        h1 ^= k1;
      },
      _ => {},
    }

    // Finalization
    debug_assert!(len <= i32::MAX as usize);
    h1 ^= len as u32;
    // fmix(h1);
    h1 ^= h1 >> 16;
    h1 = h1.wrapping_mul(0x85ebca6b);
    h1 ^= h1 >> 13;
    h1 = h1.wrapping_mul(0xc2b2ae35);

    // Return the final hash value as i32
    (h1 ^ (h1 >> 16)) as i32
  }
  pub fn murmurhash3_x86_32<AV>(bytes: &BytesRef<AV>, seed: i32) -> i32
  where
    AV: SharedAccessVec<u8>,
  {
    bytes.bytes.access(|bytes_ref| {
      Self::murmurhash3_x86_32_with_byte(bytes_ref, bytes.offset, bytes.length, seed)
    })
  }

  pub const ID_LENGTH: usize = 16;
  pub fn random_id() -> [u8; StringHelper::ID_LENGTH] {
    let mut next_id = NEXT_ID.lock();
    let result = next_id.to_be_bytes();
    *next_id = next_id.wrapping_add(1);
    result
  }
  /// Helper method to render an ID as a string for debugging.
  ///
  /// Returns the string `"null"` if the ID is `None`. Otherwise, returns a
  /// string representation for debugging. Never returns an error. The
  /// returned string may indicate if the ID is definitely invalid.
  pub fn id_to_string(id: Option<&[u8; StringHelper::ID_LENGTH]>) -> String {
    let mut digits = [0; 25];
    id.map_or("(null)", |id| Self::format_id(id, &mut digits))
      .to_owned()
  }

  pub(crate) fn format_id<'a>(id: &[u8; Self::ID_LENGTH], digits: &'a mut [u8; 25]) -> &'a str {
    let mut value = u128::from_be_bytes(*id);
    let mut start = digits.len();
    loop {
      start -= 1;
      digits[start] = b"0123456789abcdefghijklmnopqrstuvwxyz"[(value % 36) as usize];
      value /= 36;
      if value == 0 {
        break;
      }
    }
    expect_invariant!(
      std::str::from_utf8(&digits[start..]),
      "base-36 digits are ASCII"
    )
  }
  /// Converts each `i32` in the incoming [`IntsRef`] to a `u8` in the
  /// returned [`BytesRef`].
  ///
  /// Returns an [`IllegalArgument`](crate::core::util::error::IllegalArgumentError)
  /// if any `i32` value is out of bounds for a `u8`.
  pub fn ints_ref_to_bytes_ref<AV, AV1>(ints: &IntsRef<AV>) -> Result<BytesRef<AV1>>
  where
    AV: SharedAccessVec<i32>,
    AV1: SharedAccessVec<u8> + WritableVec<u8>,
  {
    let mut bytes = AV1::with_len(ints.length, || 0)?;
    for i in 0..ints.length {
      ints.ints.access(|v| {
        let x = v[ints.offset + i];
        if !(0..=255).contains(&x) {
          return Err(LuceneError::illegal_argument(format!(
            "int at pos={i} with value={x} is out-of-bounds for byte"
          )));
        }
        bytes.access_mut(|v| {
          v[i] = x as u8;
        });
        // Help the compiler infer types.
        Ok::<(), LuceneError>(())
      })?;
    }
    Ok(BytesRef::from_bytes(bytes))
  }
}
/// A constant seed used for hashing, intended to prevent hash key collision
/// attacks and ensure reproducibility of test failures if needed.
///
/// This seed is based on a system property `tests.seed` if present, or the
/// current system time if not. It's used as a seed for the MurmurHash3
/// algorithm to ensure a different salt/seed for each run.
pub static GOOD_FAST_HASH_SEED: LazyLock<i32> = LazyLock::new(|| {
  if let Ok(prop) = env::var("tests.seed") {
    // If the system property `tests.seed` is set, use it as the seed.
    prop.encode_utf16().fold(0i32, |hash, code_unit| {
      hash.wrapping_mul(31).wrapping_add(code_unit as i32)
    })
  } else {
    // Otherwise, fall back to using the current system time in
    // milliseconds.
    match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
      Ok(duration) => duration.as_millis() as i32,
      Err(error) => (error.duration().as_millis() as i128).wrapping_neg() as i32,
    }
  }
});

static NEXT_ID: LazyLock<Mutex<u128>> = LazyLock::new(|| Mutex::new(initial_next_id()));

#[cfg(not(test))]
fn initial_next_id() -> u128 {
  let mut random = rand::rng();
  scatter_id_seed(random.random(), random.random())
}

#[cfg(test)]
fn initial_next_id() -> u128 {
  let (x0, x1) = if let Ok(prop) = env::var("tests.seed") {
    if !prop.is_ascii() {
      panic!("tests.seed must end in up to eight hexadecimal digits: {prop}");
    }
    let seed_text = if prop.len() > 8 {
      &prop[prop.len() - 8..]
    } else {
      &prop
    };
    let seed = match i64::from_str_radix(seed_text, 16) {
      Ok(seed) => seed as u64,
      Err(error) => panic!("invalid tests.seed {prop}: {error}"),
    };
    (seed, seed)
  } else {
    let mut random = rand::rng();
    (random.random(), random.random())
  };
  scatter_id_seed(x0, x1)
}

fn scatter_id_seed(mut x0: u64, mut x1: u64) -> u128 {
  for _ in 0..10 {
    let mut s1 = x0;
    let s0 = x1;
    x0 = s0;
    s1 ^= s1 << 23;
    x1 = s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26);
  }
  ((x0 as u128) << 64) | x1 as u128
}