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
/*
 * 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::index::index_reader::Identity;
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::util::accountable::Accountable;
use crate::core::util::bits::Bits;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::fixed_bit_set::FixedBitSet;
use crate::core::util::sparse_fixed_bit_set::SparseFixedBitSet;
use std::rc::Rc;
use std::sync::Arc;

/// Rust-side specialization hooks for [`BitSet`].
pub trait BitSetExtensions {
  fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
    None
  }

  fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
    None
  }
}

/// Base implementation for a bit set.
pub trait BitSet: Bits + Accountable + BitSetExtensions {
  /// Clears all the bits of the set.
  ///
  /// # Note
  /// Depending on the implementation, this may be significantly faster than
  /// `clear(0, length)`.
  fn clear(&mut self) -> Result<()> {
    self.clear_range(0, self.length())
  }

  /// Sets the bit at `i`.
  fn set(&mut self, i: usize) -> Result<()>;
  /// Sets the bit at `i`, returning `true` if it was previously set.
  fn get_and_set(&mut self, i: usize) -> Result<bool>;

  /// Clears the bit at `i`.
  fn clear_with_index(&mut self, i: usize) -> Result<()>;
  /// Clears a range of bits.
  ///
  /// # Arguments
  /// * `start_index` - The lower index.
  /// * `end_index` - One-past the last bit to clear.
  fn clear_range(&mut self, start_index: usize, end_index: usize) -> Result<()>;

  /// Returns the number of bits that are set.
  ///
  /// # Note
  /// This method is likely to run in linear time.
  fn cardinality(&self) -> usize;
  /// Returns an approximation of the cardinality of this set. Some
  /// implementations may trade accuracy for speed if they have the
  /// ability to estimate the cardinality of the set without iterating
  /// over all the data. The default implementation returns
  /// [`cardinality`](BitSet::cardinality).
  fn approximate_cardinality(&self) -> usize;

  /// Returns the index of the last set bit before or on the index specified.
  /// -1 is returned if there are no more set bits.
  fn prev_set_bit(&self, index: usize) -> Option<usize>;

  /// Returns the index of the first set bit starting at the index specified.
  /// [`DocIdSetIterator::NO_MORE_DOCS`](NO_MORE_DOCS) is returned if there are no more set bits.
  fn next_set_bit(&self, index: usize) -> usize {
    self.next_set_bit_range(index, self.length())
  }

  /// Returns the index of the first set bit from start (inclusive) until end
  /// (exclusive).
  /// [`DocIdSetIterator::NO_MORE_DOCS`](NO_MORE_DOCS) is returned if there are no more set bits.
  fn next_set_bit_range(&self, start: usize, end: usize) -> usize;

  /// Performs in-place OR of the bits provided by the iterator. The state of
  /// the iterator after this operation terminates is undefined.
  fn or<T>(&mut self, iter: &mut T) -> Result<()>
  where
    T: DocIdSetIterator;

  fn default_or<T>(&mut self, iter: &mut T) -> Result<()>
  where
    T: DocIdSetIterator,
  {
    check_unpositioned(iter)?;
    loop {
      let doc = iter.next_doc()?.try_convert()?;
      if doc == NO_MORE_DOCS as usize {
        break;
      }
      self.set(doc)?;
    }
    Ok(())
  }

  fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
    Ok(())
  }
}

// BitSet
pub enum BitSetEnum2<A, B> {
  A(A),
  B(B),
}

impl<A, B> HasIdentity for BitSetEnum2<A, B>
where
  A: HasIdentity,
  B: HasIdentity,
{
  fn identity(&self) -> &Identity {
    match self {
      BitSetEnum2::A(t) => t.identity(),
      BitSetEnum2::B(s) => s.identity(),
    }
  }
}

impl<A, B> Bits for BitSetEnum2<A, B>
where
  A: BitSet,
  B: BitSet,
{
  fn get(&self, index: usize) -> Result<bool> {
    match self {
      BitSetEnum2::A(t) => t.get(index),
      BitSetEnum2::B(s) => s.get(index),
    }
  }

  fn length(&self) -> usize {
    match self {
      BitSetEnum2::A(t) => t.length(),
      BitSetEnum2::B(s) => s.length(),
    }
  }

  fn copy_of(&self) -> Result<FixedBitSet> {
    match self {
      BitSetEnum2::A(t) => t.copy_of(),
      BitSetEnum2::B(s) => s.copy_of(),
    }
  }
}

impl<A, B> Accountable for BitSetEnum2<A, B>
where
  A: Accountable,
  B: Accountable,
{
  fn ram_bytes_used(&self) -> Result<i64> {
    match self {
      BitSetEnum2::A(t) => t.ram_bytes_used(),
      BitSetEnum2::B(s) => s.ram_bytes_used(),
    }
  }
}

impl<A, B> BitSet for BitSetEnum2<A, B>
where
  A: BitSet,
  B: BitSet,
{
  fn clear(&mut self) -> Result<()> {
    match self {
      BitSetEnum2::A(t) => t.clear(),
      BitSetEnum2::B(s) => s.clear(),
    }
  }

  fn set(&mut self, i: usize) -> Result<()> {
    match self {
      BitSetEnum2::A(t) => t.set(i),
      BitSetEnum2::B(s) => s.set(i),
    }
  }

  fn get_and_set(&mut self, i: usize) -> Result<bool> {
    match self {
      BitSetEnum2::A(t) => t.get_and_set(i),
      BitSetEnum2::B(s) => s.get_and_set(i),
    }
  }

  fn clear_with_index(&mut self, i: usize) -> Result<()> {
    match self {
      BitSetEnum2::A(t) => t.clear_with_index(i),
      BitSetEnum2::B(s) => s.clear_with_index(i),
    }
  }

  fn clear_range(&mut self, start_index: usize, end_index: usize) -> Result<()> {
    match self {
      BitSetEnum2::A(t) => t.clear_range(start_index, end_index),
      BitSetEnum2::B(s) => s.clear_range(start_index, end_index),
    }
  }

  fn cardinality(&self) -> usize {
    match self {
      BitSetEnum2::A(t) => t.cardinality(),
      BitSetEnum2::B(s) => s.cardinality(),
    }
  }

  fn approximate_cardinality(&self) -> usize {
    match self {
      BitSetEnum2::A(t) => t.approximate_cardinality(),
      BitSetEnum2::B(s) => s.approximate_cardinality(),
    }
  }

  fn prev_set_bit(&self, index: usize) -> Option<usize> {
    match self {
      BitSetEnum2::A(t) => t.prev_set_bit(index),
      BitSetEnum2::B(s) => s.prev_set_bit(index),
    }
  }

  fn next_set_bit(&self, index: usize) -> usize {
    match self {
      BitSetEnum2::A(t) => t.next_set_bit(index),
      BitSetEnum2::B(s) => s.next_set_bit(index),
    }
  }

  fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
    match self {
      BitSetEnum2::A(t) => t.next_set_bit_range(start, end),
      BitSetEnum2::B(s) => s.next_set_bit_range(start, end),
    }
  }

  fn or<T>(&mut self, iter: &mut T) -> Result<()>
  where
    T: DocIdSetIterator,
  {
    match self {
      BitSetEnum2::A(t) => t.or(iter),
      BitSetEnum2::B(s) => s.or(iter),
    }
  }

  fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
    match self {
      BitSetEnum2::A(t) => t.ensure_capacity(_num_bits),
      BitSetEnum2::B(s) => s.ensure_capacity(_num_bits),
    }
  }
}

impl<A, B> BitSetExtensions for BitSetEnum2<A, B>
where
  A: BitSet,
  B: BitSet,
{
  fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
    match self {
      BitSetEnum2::A(t) => t.as_fixed_bit_set(),
      BitSetEnum2::B(s) => s.as_fixed_bit_set(),
    }
  }

  fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
    match self {
      BitSetEnum2::A(t) => t.as_sparse_fixed_bit_set(),
      BitSetEnum2::B(s) => s.as_sparse_fixed_bit_set(),
    }
  }
}

impl<T> BitSet for Arc<T>
where
  T: BitSet,
{
  fn clear(&mut self) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn set(&mut self, _i: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn get_and_set(&mut self, _i: usize) -> Result<bool> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn clear_with_index(&mut self, _i: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn clear_range(&mut self, _start_index: usize, _end_index: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn cardinality(&self) -> usize {
    (**self).cardinality()
  }

  fn approximate_cardinality(&self) -> usize {
    (**self).approximate_cardinality()
  }

  fn prev_set_bit(&self, index: usize) -> Option<usize> {
    (**self).prev_set_bit(index)
  }

  fn next_set_bit(&self, index: usize) -> usize {
    (**self).next_set_bit(index)
  }

  fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
    (**self).next_set_bit_range(start, end)
  }

  fn or<T1>(&mut self, _iter: &mut T1) -> Result<()>
  where
    T1: DocIdSetIterator,
  {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn default_or<T1>(&mut self, _iter: &mut T1) -> Result<()>
  where
    T1: DocIdSetIterator,
  {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }

  fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Arc",
    ))
  }
}

impl<T> BitSetExtensions for Arc<T>
where
  T: BitSet,
{
  fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
    (**self).as_fixed_bit_set()
  }

  fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
    (**self).as_sparse_fixed_bit_set()
  }
}

impl<T> HasIdentity for Rc<T>
where
  T: HasIdentity,
{
  fn identity(&self) -> &Identity {
    (**self).identity()
  }
}

impl<T> Bits for Rc<T>
where
  T: BitSet,
{
  fn get(&self, index: usize) -> Result<bool> {
    (**self).get(index)
  }

  fn length(&self) -> usize {
    (**self).length()
  }
}

impl<T> BitSet for Rc<T>
where
  T: BitSet,
{
  fn clear(&mut self) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn set(&mut self, _i: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn get_and_set(&mut self, _i: usize) -> Result<bool> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn clear_with_index(&mut self, _i: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn clear_range(&mut self, _start_index: usize, _end_index: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn cardinality(&self) -> usize {
    (**self).cardinality()
  }

  fn approximate_cardinality(&self) -> usize {
    (**self).approximate_cardinality()
  }

  fn prev_set_bit(&self, index: usize) -> Option<usize> {
    (**self).prev_set_bit(index)
  }

  fn next_set_bit(&self, index: usize) -> usize {
    (**self).next_set_bit(index)
  }

  fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
    (**self).next_set_bit_range(start, end)
  }

  fn or<T1>(&mut self, _iter: &mut T1) -> Result<()>
  where
    T1: DocIdSetIterator,
  {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn default_or<T1>(&mut self, _iter: &mut T1) -> Result<()>
  where
    T1: DocIdSetIterator,
  {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }

  fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(
      "cannot mutate a BitSet through Rc",
    ))
  }
}

impl<T> BitSetExtensions for Rc<T>
where
  T: BitSet,
{
  fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
    (**self).as_fixed_bit_set()
  }

  fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
    (**self).as_sparse_fixed_bit_set()
  }
}
use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::{HasIdentity, TryIntoInt};

/// Builds a [`BitSet`] from the content of the provided
/// [`DocIdSetIterator`]. **Note**: This will fully consume the
/// [`DocIdSetIterator`].
pub fn of<I>(it: &mut I, max_doc: usize) -> Result<SparseFixedBitSetBitSet>
where
  I: DocIdSetIterator,
{
  let cost = it.cost()?;
  let threshold = max_doc >> 7;
  let mut set;
  if cost < (threshold as i64) {
    set = BitSetEnum2::A(SparseFixedBitSet::new(max_doc)?);
  } else {
    let result = FixedBitSet::new(max_doc);
    set = BitSetEnum2::B(result);
  };
  set.or(it)?;
  Ok(set)
}
///Assert that the current doc is -1.
pub(crate) fn check_unpositioned<I>(iter: &I) -> Result<()>
where
  I: DocIdSetIterator,
{
  if iter.doc_id() != -1 {
    return Err(LuceneError::illegal_state(format!(
      "This operation only works with an unpositioned iterator, got current position = {}",
      iter.doc_id()
    )));
  }
  Ok(())
}

pub type SparseFixedBitSetBitSet = BitSetEnum2<SparseFixedBitSet, FixedBitSet>;