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
/*
 * 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::byte_slice_pool::ByteSlicePool;
use crate::core::index::byte_slice_reader::ByteSliceReader;
use crate::core::index::field_invert_state::FieldInvertState;
use crate::core::index::freq_prox_terms_writer_per_field::FreqProxPostingsArray;
use crate::core::index::index_options::IndexOptions;
use crate::core::index::parallel_postings_array::PostingsArray;
use crate::core::util::attribute_source::AttributeSource;
use crate::core::util::bytes_ref_hash::{BytesRefHash, BytesStartArray};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::int_block_pool::{
  INT_BLOCK_MASK, INT_BLOCK_SHIFT, INT_BLOCK_SIZE, IntBlockPool,
};
use crate::core::util::ram_usage_estimator::size_of_array_capacity;
use crate::core::util::{
  BYTE_BLOCK_MASK, BYTE_BLOCK_SHIFT, BYTE_BLOCK_SIZE, ByteBlockPool, Counter, SharedCounter,
  SliceCopyOps,
};

/// This struct stores streams of information per term without knowing the size
/// of the stream ahead of time. Each stream typically encodes one level of
/// information, like term frequency per document or term proximity.
///
/// Internally, this struct allocates a linked list of slices that can be read
/// by a [`ByteSliceReader`] for each term. Terms are first deduplicated in a
/// [`BytesRefHash`]. Once this is done, internal data structures point to the
/// current offset of each stream that can be written to.
pub struct TermsHashPerField<PA: PostingsArray = FreqProxPostingsArray> {
  slice_pool: ByteSlicePool,
  // for each term we store an integer per stream that points into the
  // bytePool above the address is updated once data is written to the
  // stream to point to the next free offset in the terms stream. The
  // start address for the stream is stored in postingsArray.
  // `byte_starts[term_id]` is initialized in `add_term`,
  // either to a brand new per term stream if the term is new or
  // to the addresses where the term stream was written to when we saw it the
  // last time.    term_stream_address_buffer: Vec<i32>,
  stream_address: i32,
  stream_count: i32,
  // This stores the actual term bytes for postings and offsets into the
  // parent hash in the case that this TermsHashPerField is hashing term
  // vectors.
  pub(crate) bytes_hash: BytesRefHash<PostingsBytesStartArray<PA>>,
  // Sorted term IDs stay in bytes_hash.ids; track whether they are available separately.
  #[cfg(debug_assertions)]
  is_sorted: bool,
  #[cfg(debug_assertions)]
  last_doc_id: i32,
  pub(crate) index_options: IndexOptions,
}
impl<PA: PostingsArray> TermsHashPerField<PA> {
  ///  streamCount: how many streams this field stores per term. E.g.
  /// doc(+freq) is 1 stream, prox+offset is a second.
  pub(crate) fn new(
    stream_count: i32,
    bytes_used: SharedCounter,
    postings_array_wrapper: PostingsArrayWrapper<PA>,
    index_options: IndexOptions,
    initialize_hash: bool,
  ) -> Result<Self> {
    debug_assert!(index_options != IndexOptions::None);
    let slice_pool = ByteSlicePool;
    let byte_starts = PostingsBytesStartArray::new(postings_array_wrapper, bytes_used);

    let bytes_hash = if initialize_hash {
      BytesRefHash::from_bytes_start_array(HASH_INIT_SIZE, byte_starts)?
    } else {
      BytesRefHash::without_storage(HASH_INIT_SIZE, byte_starts)
    };
    Ok(TermsHashPerField {
      slice_pool,
      stream_address: 0,
      stream_count,
      bytes_hash,
      #[cfg(debug_assertions)]
      is_sorted: false,
      #[cfg(debug_assertions)]
      last_doc_id: 0,
      index_options,
    })
  }
  pub(crate) fn init_reader(
    &self,
    reader: &mut ByteSliceReader<'_>,
    term_id: usize,
    stream: i32,
    int_pool: &IntBlockPool,
  ) -> Result<()> {
    debug_assert!(stream < self.stream_count);
    let postings_array_wrapper = &self.bytes_hash.bytes_start_array.per_field;
    let stream_start_offset = postings_array_wrapper
      .postings_array
      .as_ref()
      .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))?
      .get_address_offset()[term_id][1];
    let buffer_index = (stream_start_offset >> INT_BLOCK_SHIFT) as usize;
    let offset_in_address_buffer = stream_start_offset & INT_BLOCK_MASK;
    let addr;
    {
      let stream_address_buffer = int_pool.get_buffer(buffer_index);
      addr = stream_address_buffer[(offset_in_address_buffer + stream) as usize];
    }
    let init_offset = postings_array_wrapper
      .postings_array
      .as_ref()
      .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))?
      .get_byte_starts()[term_id][2]
      + stream * ByteSlicePool::FIRST_LEVEL_SIZE;
    reader.init(init_offset as usize, addr as usize);
    Ok(())
  }
  /// Collapse the hash table and sort in-place; also sets this.sortedTermIDs
  /// to the results. This method must not be called twice unless
  /// [`reset()`](Self::reset) or
  /// [`reinit_hash()`](Self::reinit_hash) was called.
  pub(crate) fn sort_terms(&mut self, byte_pool: &ByteBlockPool) -> Result<()> {
    #[cfg(debug_assertions)]
    debug_assert!(!self.is_sorted);
    self.bytes_hash.sort(byte_pool)?;
    #[cfg(debug_assertions)]
    {
      self.is_sorted = true;
    }
    Ok(())
  }
  /// Returns the sorted term IDs.
  /// [`sort_terms()`](TermsHashPerField::sort_terms) must be called before.
  pub(crate) fn get_sorted_term_ids(&self) -> &[i32] {
    #[cfg(debug_assertions)]
    debug_assert!(self.is_sorted);
    self.bytes_hash.ids.as_slice()
  }

  fn write_byte(
    &self,
    stream_address: &mut i32,
    b: u8,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()> {
    let upto = *stream_address;
    let block_index = (upto >> BYTE_BLOCK_SHIFT) as usize;
    debug_assert!(block_index <= byte_pool.buffer_upto()?);
    let offset = (upto & BYTE_BLOCK_MASK) as usize;
    let slot = &mut byte_pool.get_buffer_mut(block_index)[offset];
    if *slot == 0 {
      *slot = b;
    } else {
      // End of slice; allocate a new one
      let offset = self
        .slice_pool
        .alloc_slice(block_index, offset, byte_pool)?;
      *stream_address = offset as i32 + byte_pool.byte_offset;
      let block_index = byte_pool.buffer_upto()?;
      byte_pool.get_buffer_mut(block_index)[offset] = b;
    }
    *stream_address += 1;
    Ok(())
  }
  pub(crate) fn write_bytes(
    &self,
    stream: i32,
    b: &[u8],
    mut offset: usize,
    len: usize,
    int_pool: &mut IntBlockPool,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()> {
    let end = offset + len;
    let stream_address = ((self.stream_address & INT_BLOCK_MASK) + stream) as usize;
    let buffer_index = (self.stream_address >> INT_BLOCK_SHIFT) as usize;

    let term_stream_address_buffer = int_pool.get_buffer_mut(buffer_index);
    let upto = term_stream_address_buffer[stream_address];
    {
      let mut block_index = (upto >> BYTE_BLOCK_SHIFT) as usize;
      debug_assert!(block_index <= byte_pool.buffer_upto()?);
      let slice = byte_pool.get_buffer_mut(block_index);
      let mut slice_offset = (upto & BYTE_BLOCK_MASK) as usize;

      while offset < end && slice[slice_offset] == 0 {
        slice[slice_offset] = b[offset];
        slice_offset += 1;
        offset += 1;
        term_stream_address_buffer[stream_address] += 1;
      }

      while offset < end {
        let offset_and_length =
          self
            .slice_pool
            .alloc_known_size_slice(block_index, slice_offset, byte_pool)?;
        slice_offset = (offset_and_length >> 8) as usize;
        let slice_length = offset_and_length & 0xff;
        let buffer_upto = byte_pool.buffer_upto()?;
        block_index = buffer_upto;
        let slice = byte_pool.get_buffer_mut(buffer_upto);
        let write_length = std::cmp::min(slice_length as usize - 1, end - offset);
        slice.copy_from(&b[offset..offset + write_length], slice_offset);
        slice_offset += write_length;
        offset += write_length;
        debug_assert!(slice_offset <= i32::MAX as usize);
        term_stream_address_buffer[stream_address] = slice_offset as i32 + byte_pool.byte_offset;
      }
    }
    Ok(())
  }
  #[inline(always)]
  pub(crate) fn write_vint(
    &self,
    stream: i32,
    mut i: i32,
    int_pool: &mut IntBlockPool,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()> {
    debug_assert!(stream < self.stream_count);
    let stream_address = ((self.stream_address & INT_BLOCK_MASK) + stream) as usize;
    let buffer_index = (self.stream_address >> INT_BLOCK_SHIFT) as usize;
    let term_stream_address_buffer = int_pool.get_buffer_mut(buffer_index);
    let stream_address = &mut term_stream_address_buffer[stream_address];
    if (i & !0x7F) == 0 {
      let upto = *stream_address;
      let block_index = (upto >> BYTE_BLOCK_SHIFT) as usize;
      debug_assert!(block_index <= byte_pool.buffer_upto()?);
      let offset = (upto & BYTE_BLOCK_MASK) as usize;
      let slot = &mut byte_pool.get_buffer_mut(block_index)[offset];
      if *slot == 0 {
        *slot = i as u8;
        *stream_address += 1;
        return Ok(());
      }
    } else if stream == 0 && (i as u32) < 0x4000 {
      let upto = *stream_address;
      let block_index = (upto >> BYTE_BLOCK_SHIFT) as usize;
      debug_assert!(block_index <= byte_pool.buffer_upto()?);
      let offset = (upto & BYTE_BLOCK_MASK) as usize;
      let buffer = byte_pool.get_buffer_mut(block_index);
      if offset < BYTE_BLOCK_MASK as usize
        && let Some(bytes) = buffer.get_mut(offset..offset + 2)
        && bytes[0] == 0
        && bytes[1] == 0
      {
        bytes[0] = ((i & 0x7F) | 0x80) as u8;
        *stream_address += 1;
        bytes[1] = ((i as u32) >> 7) as u8;
        *stream_address += 1;
        return Ok(());
      }
    }
    while (i & !0x7F) != 0 {
      self.write_byte(stream_address, ((i & 0x7F) | 0x80) as u8, byte_pool)?;
      i = ((i as u32) >> 7) as i32;
    }
    self.write_byte(stream_address, i as u8, byte_pool)
  }

  pub(crate) fn get_num_terms(&self) -> usize {
    self.bytes_hash.size()
  }

  pub(crate) fn postings_array(&self) -> Option<&PA> {
    self
      .bytes_hash
      .bytes_start_array
      .per_field
      .postings_array
      .as_ref()
  }

  pub(crate) fn postings_array_mut(&mut self) -> Option<&mut PA> {
    self
      .bytes_hash
      .bytes_start_array
      .per_field
      .postings_array
      .as_mut()
  }

  pub(crate) fn reset(&mut self, byte_pool: &mut ByteBlockPool) {
    self.bytes_hash.clear_with_reset_pool(false, byte_pool);
    #[cfg(debug_assertions)]
    {
      self.is_sorted = false;
    }
  }

  pub(crate) fn reinit_hash(&mut self) -> Result<()> {
    #[cfg(debug_assertions)]
    {
      self.is_sorted = false;
    }
    self.bytes_hash.reinit()
  }

  /// Called when we first encounter a new term. We must allocate slices to
  /// store the postings (vInt compressed doc/freq/prox), and also the int
  /// pointers to where (in our [`ByteBlockPool`] storage) the postings
  /// for this term begin.
  pub(crate) fn init_stream_slices(
    &mut self,
    term_id: usize,
    _doc_id: i32,
    int_pool: &mut IntBlockPool,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()> {
    {
      if self.stream_count + int_pool.int_upto > INT_BLOCK_SIZE {
        // not enough space remaining in this buffer -- jump to next buffer and lose this remaining
        // piece
        int_pool.next_buffer()?;
      }

      if BYTE_BLOCK_SIZE - byte_pool.byte_upto
        < 2 * self.stream_count * ByteSlicePool::FIRST_LEVEL_SIZE
      {
        // can we fit at least one byte per stream in the current
        // buffer, if not allocate a new one
        byte_pool.next_buffer()?;
      }
      let byte_offset = byte_pool.byte_offset;
      let buffer_index = int_pool
        .buffer_upto
        .ok_or_else(|| LuceneError::illegal_state("term stream has no current int buffer"))?;
      let stream_address_index = int_pool.int_upto as usize;
      self.stream_address = int_pool.int_offset + int_pool.int_upto;
      int_pool.int_upto += self.stream_count;
      let postings_array_wrapper = &mut self.bytes_hash.bytes_start_array.per_field;
      debug_assert!(postings_array_wrapper.postings_array.is_some());
      postings_array_wrapper
        .postings_array
        .as_mut()
        .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))?
        .set_address_offset(term_id, self.stream_address);

      let term_stream_address_buffer = int_pool.get_buffer_mut(buffer_index);
      if let Some(addresses) = term_stream_address_buffer
        .get_mut(stream_address_index..)
        .and_then(|buffer| buffer.get_mut(..self.stream_count as usize))
      {
        for address in addresses {
          let upto = self
            .slice_pool
            .new_slice(ByteSlicePool::FIRST_LEVEL_SIZE, byte_pool)?;
          *address = upto + byte_offset;
        }
      } else {
        for i in 0..self.stream_count as usize {
          let upto = self
            .slice_pool
            .new_slice(ByteSlicePool::FIRST_LEVEL_SIZE, byte_pool)?;
          term_stream_address_buffer[stream_address_index + i] = upto + byte_offset;
        }
      }
      postings_array_wrapper
        .postings_array
        .as_mut()
        .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))?
        .set_byte_starts(term_id, term_stream_address_buffer[stream_address_index]);
    }
    Ok(())
  }

  #[cfg(debug_assertions)]
  pub(crate) fn assert_doc_id(&mut self, doc_id: i32) -> bool {
    debug_assert!(
      doc_id >= self.last_doc_id,
      "docID must be >= {} but was: {}",
      self.last_doc_id,
      doc_id
    );
    self.last_doc_id = doc_id;
    true
  }

  pub(crate) fn position_stream_slice(&mut self, term_id: i32, _doc_id: i32) -> Result<usize> {
    let term_id = (-term_id) - 1;
    let term_id = term_id as usize;
    let postings_array_wrapper = &self.bytes_hash.bytes_start_array.per_field;
    debug_assert!(postings_array_wrapper.postings_array.is_some());
    let int_start = postings_array_wrapper
      .postings_array
      .as_ref()
      .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))?
      .get_address_offset()[term_id][1];
    self.stream_address = int_start;
    Ok(term_id)
  }
}

pub(crate) trait TermsHashPerFieldBase {
  /// Called when a term is seen for the first time.
  fn new_term<AS>(
    &mut self,
    term_id: usize,
    doc_id: i32,
    state: &mut FieldInvertState,
    attribute_source: &AS,
    int_pool: &mut IntBlockPool,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()>
  where
    AS: AttributeSource;
  /// Called when a previously seen term is seen again.
  fn add_term<AS>(
    &mut self,
    term_id: usize,
    doc_id: i32,
    state: &mut FieldInvertState,
    attribute_source: &AS,
    int_pool: &mut IntBlockPool,
    byte_pool: &mut ByteBlockPool,
  ) -> Result<()>
  where
    AS: AttributeSource;
  /// Called when the postings array is initialized or resized.
  #[allow(dead_code)]
  fn new_postings_array(&mut self) -> Result<()> {
    Err(LuceneError::not_implemented("should nerve called"))
  }
}
pub(crate) struct PostingsArrayWrapper<PA: PostingsArray> {
  pub(crate) postings_array: Option<PA>,
  pub(crate) index_options: IndexOptions,
}

impl<PA: PostingsArray> PostingsArrayWrapper<PA> {
  pub fn new(index_options: IndexOptions) -> Self {
    Self {
      postings_array: None,
      index_options,
    }
  }
}

pub(crate) struct PostingsBytesStartArray<PA: PostingsArray> {
  pub(crate) per_field: PostingsArrayWrapper<PA>,
  bytes_used: SharedCounter,
}

impl<PA: PostingsArray> PostingsBytesStartArray<PA> {
  pub(crate) fn new(per_field: PostingsArrayWrapper<PA>, bytes_used: SharedCounter) -> Self {
    Self {
      per_field,
      bytes_used,
    }
  }

  fn postings_array(&self) -> Result<&PA> {
    debug_assert!(self.per_field.postings_array.is_some());
    self
      .per_field
      .postings_array
      .as_ref()
      .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))
  }

  fn postings_array_mut(&mut self) -> Result<&mut PA> {
    debug_assert!(self.per_field.postings_array.is_some());
    self
      .per_field
      .postings_array
      .as_mut()
      .ok_or_else(|| LuceneError::illegal_state("postings array is missing"))
  }
}
impl<PA: PostingsArray> BytesStartArray for PostingsBytesStartArray<PA> {
  fn init(&mut self) -> Result<()> {
    if self.per_field.postings_array.is_none() {
      self.per_field.postings_array = Some(PA::new_for_index(2, self.per_field.index_options));
      if let Some(ref mut postings_array) = self.per_field.postings_array {
        let byte_used = postings_array.bytes_per_posting() * postings_array.get_size();
        let _ = self.bytes_used.add_and_get(byte_used as i64);
      }
    }
    Ok(())
  }

  fn grow(&mut self) -> Result<()> {
    let bytes_used = {
      let postings_array = self.postings_array_mut()?;
      let old_size = postings_array.get_size();
      postings_array.grow()?;
      postings_array.bytes_per_posting() * (postings_array.get_size() - old_size)
    };
    self.bytes_used.add_and_get(bytes_used as i64);
    Ok(())
  }

  fn clear(&mut self) {
    if let Some(postings_array) = self.per_field.postings_array.take() {
      let byte_used = postings_array.bytes_per_posting() * postings_array.get_size();
      debug_assert!(byte_used <= i64::MAX as usize);
      let _ = self.bytes_used.add_and_get(-(byte_used as i64));
    }
  }

  fn bytes_used(&mut self) -> SharedCounter {
    self.bytes_used.clone()
  }

  fn get_value(&self, index: usize) -> Result<i32> {
    let postings_array = self.postings_array()?;
    postings_array
      .get_text_starts()
      .get(index)
      .map(|row| row[0])
      .ok_or_else(|| {
        LuceneError::illegal_argument(format!("postings array index {index} is out of bounds"))
      })
  }

  fn set_value(&mut self, index: usize, value: i32) -> Result<()> {
    let postings_array = self.postings_array_mut()?;
    if index >= postings_array.get_text_starts().len() {
      return Err(LuceneError::illegal_argument(format!(
        "postings array index {index} is out of bounds"
      )));
    }
    postings_array.set_text_starts(index, value);
    Ok(())
  }

  fn len(&self) -> Result<usize> {
    let postings_array = self.postings_array()?;
    Ok(postings_array.get_text_starts().len())
  }

  fn need_init(&self) -> bool {
    self.per_field.postings_array.is_none()
  }

  fn ram_bytes_used(&self) -> Result<i64> {
    match self.per_field.postings_array.as_ref() {
      Some(postings_array) => Ok(size_of_array_capacity::<i32>(
        postings_array.get_text_starts().len(),
      )),
      None => Ok(0),
    }
  }
}

pub(crate) const HASH_INIT_SIZE: usize = 4;