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
/*
 * 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::codecs::stored_fields_writer::StoredFieldsWriter;
use crate::core::codecs::term_vectors_reader::DefaultTermVectorsReader;
use crate::core::index::composite_reader::CompositeReader;
use crate::core::index::fields::Fields;
use crate::core::index::index_reader::{IndexReader, IndexReaderBase};
use crate::core::index::index_writer::get_actual_max_docs;
use crate::core::index::reader_util::ReaderUtil;
use crate::core::index::stored_field_visitor::StoredFieldVisitor;
use crate::core::index::stored_fields::{RawStoredFieldsReader, StoredFields};
use crate::core::index::term::Term;
use crate::core::index::term_vectors::{RawTermVectors, TermVectors};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::{Comparator, TryIntoInt};
use std::cmp::Ordering::Equal;
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};

/// Base trait for implementing [`CompositeReader`]s based on an array of sub-readers.
///
/// User code will most likely use [`MultiReader`](crate::core::index::multi_reader::MultiReader) to build a composite reader
/// on a set of sub-readers (such as several [`DirectoryReader`](crate::core::index::directory_reader::DirectoryReader)s).
///
/// For efficiency, in this API documents are often referred to via *document numbers*,
/// non-negative integers that uniquely identify documents in the index.
/// These document numbers are ephemeral — they may change as documents are added to
/// or deleted from an index. Clients should therefore **not rely** on a document
/// having the same number between sessions.
///
///
/// ## Thread Safety
///
/// **NOTE:** [`IndexReader`] instances are completely thread-safe, meaning multiple
/// threads can call any of its methods concurrently.
/// If your application requires external synchronization, you should **not**
/// synchronize on the [`IndexReader`] instance itself; instead, use your own (non-Lucene)
/// synchronization objects.
///
///
/// See also: [`MultiReader`](crate::core::index::multi_reader::MultiReader)
///
/// *Lucene internal API*
pub trait BaseCompositeReader: CompositeReader {}
pub struct BaseCompositeReaderBase<R> {
  pub(crate) sub_reader: Arc<[R]>,
  starts: Arc<[usize]>,
  max_doc: i32,
  num_docs: AtomicI32,
}
impl<R> BaseCompositeReaderBase<R>
where
  R: IndexReader,
{
  /// Constructs a [`BaseCompositeReader`] on the given sub-readers.
  ///
  /// # Parameters
  ///
  /// * `sub_readers` – the wrapped sub-readers.
  ///   This vector is returned by [`get_sequential_sub_readers`](Self::get_sequential_sub_readers)
  ///   and used to resolve the correct sub-reader for docID-based methods.
  ///   **Please note:** this vector is **not** cloned and **not** protected for modification;
  ///   the implementation is responsible for doing this.
  ///
  /// * `sub_readers_sorter` – a comparator for sorting sub-readers.
  ///   If not `None`, this comparator is used to sort sub-readers before resolving doc IDs.
  ///
  /// * `index_reader_base` – the base state of the parent reader that is being
  ///   constructed.
  pub fn new<C>(
    mut sub_readers: Vec<R>,
    sub_reader_sorter: Option<&C>,
    index_reader_base: &IndexReaderBase,
  ) -> Result<Self>
  where
    C: Comparator<R>,
  {
    if let Some(sorter) = sub_reader_sorter {
      let mut err: Option<LuceneError> = None;
      sub_readers.sort_by(|a, b| match sorter.compare(a, b) {
        Ok(v) => v.cmp(&0),
        Err(e) => {
          if err.is_none() {
            err = Some(e);
          }
          Equal
        },
      });
      if let Some(e) = err {
        return Err(e);
      }
    }

    Self::new_shared(Arc::from(sub_readers), index_reader_base)
  }

  pub(crate) fn new_shared(
    sub_readers: Arc<[R]>,
    index_reader_base: &IndexReaderBase,
  ) -> Result<Self> {
    let mut starts = Vec::with_capacity(sub_readers.len() + 1);
    let mut max_doc: i64 = 0;

    for reader in sub_readers.iter() {
      starts.push(max_doc as usize);
      max_doc += reader.max_doc()? as i64;
      reader.register_parent_reader(index_reader_base)?;
    }

    let max_allowed = get_actual_max_docs();
    if max_doc > max_allowed as i64 {
      return Err(LuceneError::illegal_argument(format!(
        "Too many documents: composite IndexReaders cannot exceed {}, total maxDoc={}",
        max_allowed, max_doc
      )));
    }
    let max_doc_i32 = max_doc.try_convert()?;
    starts.push(max_doc_i32);

    Ok(Self {
      sub_reader: sub_readers,
      starts: Arc::from(starts),
      max_doc: max_doc_i32 as i32,
      num_docs: AtomicI32::new(-1),
    })
  }

  pub fn term_vector<T>(&self, reader: &T) -> Result<BCRTermVectorsImpl<R>>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;
    Ok(TermVectorsImpl::new(
      self.sub_reader.clone(),
      self.starts.clone(),
      self.max_doc,
    ))
  }
  pub fn num_docs(&self) -> Result<i32> {
    // Don't call ensureOpen() here (it could affect performance)
    // We want to compute numDocs() lazily so that creating a wrapper that hides
    // some documents isn't slow at wrapping time, but on the first time that
    // numDocs() is called. This can help as there are lots of use-cases of a
    // reader that don't involve calling numDocs().
    // However it's not crucial to make sure that we don't call numDocs() more
    // than once on the sub readers, since they likely cache numDocs() anyway,
    // hence the opaque read.
    // http://gee.cs.oswego.edu/dl/html/j9mm.html#opaquesec.
    let num_docs = self.num_docs.load(Ordering::Relaxed);
    if num_docs != -1 {
      return Ok(num_docs);
    }

    let mut num_docs: i32 = 0;
    for r in self.sub_reader.iter() {
      num_docs += r.num_docs()?;
    }

    debug_assert!(num_docs >= 0);
    self.num_docs.store(num_docs, Ordering::SeqCst);
    Ok(num_docs)
  }
  pub fn max_doc(&self) -> i32 {
    self.max_doc
  }
  pub fn stored_fields<T>(&self, reader: &T) -> Result<BCRStoredFieldsImpl<R>>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;
    Ok(StoredFieldsImpl::new(
      self.sub_reader.clone(),
      self.starts.clone(),
      self.max_doc,
    ))
  }
  pub fn doc_freq<T>(&self, term: &Term, reader: &T) -> Result<i32>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;

    let mut total: i32 = 0;
    for sub_reader in self.sub_reader.iter() {
      let sub = sub_reader.doc_freq(term)?;
      debug_assert!(sub >= 0);
      debug_assert!(sub <= sub_reader.get_doc_count(term.field())?);
      total += sub;
    }
    Ok(total)
  }
  pub fn total_term_freq<T>(&self, term: &Term, reader: &T) -> Result<i64>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;

    let mut total: i64 = 0;
    for sub_reader in self.sub_reader.iter() {
      let sub = sub_reader.total_term_freq(term)?;
      debug_assert!(sub >= 0);
      debug_assert!(sub <= sub_reader.get_sum_total_term_freq(term.field())?);
      total += sub;
    }
    Ok(total)
  }

  pub fn get_sum_doc_freq<T>(&self, field: &str, reader: &T) -> Result<i64>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;

    let mut total: i64 = 0;
    for sub_reader in self.sub_reader.iter() {
      let sub = sub_reader.get_sum_doc_freq(field)?;
      debug_assert!(sub >= 0);
      debug_assert!(sub <= sub_reader.get_sum_total_term_freq(field)?);
      total += sub;
    }
    Ok(total)
  }

  pub fn get_doc_count<T>(&self, field: &str, reader: &T) -> Result<i32>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;

    let mut total: i32 = 0;
    for sub_reader in self.sub_reader.iter() {
      let sub = sub_reader.get_doc_count(field)?;
      debug_assert!(sub >= 0);
      debug_assert!(sub <= sub_reader.max_doc()?);
      total += sub;
    }
    Ok(total)
  }
  pub fn get_sum_total_term_freq<T>(&self, field: &str, reader: &T) -> Result<i64>
  where
    T: BaseCompositeReader,
  {
    reader.ensure_open()?;

    let mut total: i64 = 0;
    for sub_reader in self.sub_reader.iter() {
      let sub = sub_reader.get_sum_total_term_freq(field)?;
      debug_assert!(sub >= 0);
      debug_assert!(sub >= sub_reader.get_sum_doc_freq(field)?);
      total += sub;
    }
    Ok(total)
  }
  /// Helper method for implementations to get the document base of a sub-reader index.
  pub fn reader_base(&self, reader_index: usize) -> Result<usize> {
    if reader_index >= self.sub_reader.len() {
      return Err(LuceneError::illegal_argument(
        "readerIndex must be >= 0 and < getSequentialSubReaders().size()",
      ));
    }
    Ok(self.starts[reader_index])
  }
  pub fn get_sequential_sub_readers(&self) -> &[R] {
    self.sub_reader.as_ref()
  }
}
pub type BCRTermVectorsImpl<R> = TermVectorsImpl<R>;
pub type BCRStoredFieldsImpl<R> = StoredFieldsImpl<R>;

pub struct TermVectorsImpl<R>
where
  R: IndexReader,
{
  sub_reader: Arc<[R]>,
  starts: Arc<[usize]>,
  sub_term_vectors: Vec<Option<R::TermVectors>>,
  max_doc: i32,
}
impl<R> TermVectorsImpl<R>
where
  R: IndexReader,
{
  pub fn new(sub_reader: Arc<[R]>, starts: Arc<[usize]>, max_doc: i32) -> Self {
    let mut sub_term_vectors = Vec::with_capacity(sub_reader.len());
    for _ in 0..sub_reader.len() {
      sub_term_vectors.push(None);
    }
    Self {
      sub_reader,
      starts,
      sub_term_vectors,
      max_doc,
    }
  }
}
impl<R> TermVectors for TermVectorsImpl<R>
where
  R: IndexReader,
{
  fn prefetch(&mut self, doc_id: i32) -> Result<()> {
    let i = reader_index(doc_id, self.max_doc, self.starts.as_ref())?;
    match self.sub_term_vectors[i] {
      Some(ref mut tv) => tv.prefetch(doc_id - self.starts[i] as i32)?,
      None => {
        let mut tv_reader = self.sub_reader[i].term_vectors()?;
        tv_reader.prefetch(doc_id - self.starts[i] as i32)?;
        self.sub_term_vectors[i] = Some(tv_reader);
      },
    }
    Ok(())
  }

  type Fields = <R::TermVectors as TermVectors>::Fields;

  fn get(&mut self, doc_id: i32) -> Result<Option<Self::Fields>> {
    let i = reader_index(doc_id, self.max_doc, self.starts.as_ref())?;

    if self.sub_term_vectors[i].is_none() {
      self.sub_term_vectors[i] = Some(self.sub_reader[i].term_vectors()?);
    }

    let tv = match self.sub_term_vectors[i].as_mut() {
      Some(tv) => tv,
      None => return Err(LuceneError::illegal_state("not initialized")),
    };
    tv.get(doc_id - self.starts[i] as i32)
  }

  type Terms = <Self::Fields as Fields>::Terms;

  fn get_field_terms(
    &mut self,
    doc: i32,
    field: &str,
  ) -> Result<Option<<Self::Fields as Fields>::Terms>> {
    self.default_get_field_terms(doc, field)
  }
}

impl<R> RawTermVectors for TermVectorsImpl<R>
where
  R: IndexReader,
  R::TermVectors: RawTermVectors,
{
  type IndexInput = <R::TermVectors as RawTermVectors>::IndexInput;

  fn raw_term_vectors_mut(&mut self) -> Result<&mut DefaultTermVectorsReader<Self::IndexInput>> {
    Err(LuceneError::illegal_state(
      "raw term vectors reader is not available".to_string(),
    ))
  }

  fn raw_term_vectors(&self) -> Result<&DefaultTermVectorsReader<Self::IndexInput>> {
    Err(LuceneError::illegal_state(
      "raw term vectors reader is not available".to_string(),
    ))
  }
}
pub struct StoredFieldsImpl<R>
where
  R: IndexReader,
{
  sub_reader: Arc<[R]>,
  starts: Arc<[usize]>,
  sub_stored_fields: Vec<Option<R::StoredFields>>,
  max_doc: i32,
}

impl<R> StoredFieldsImpl<R>
where
  R: IndexReader,
{
  pub fn new(sub_reader: Arc<[R]>, starts: Arc<[usize]>, max_doc: i32) -> Self {
    let mut sub_stored_fields = Vec::with_capacity(sub_reader.len());
    for _ in 0..sub_reader.len() {
      sub_stored_fields.push(None);
    }
    Self {
      sub_reader,
      starts,
      sub_stored_fields,
      max_doc,
    }
  }
}

impl<R> StoredFields for StoredFieldsImpl<R>
where
  R: IndexReader,
{
  fn prefetch(&mut self, doc_id: i32) -> Result<()> {
    let i = reader_index(doc_id, self.max_doc, self.starts.as_ref())?;

    match self.sub_stored_fields[i] {
      Some(ref mut sf) => sf.prefetch(doc_id - self.starts[i] as i32)?,
      None => {
        let mut sf_reader = self.sub_reader[i].stored_fields()?;
        sf_reader.prefetch(doc_id - self.starts[i] as i32)?;
        self.sub_stored_fields[i] = Some(sf_reader);
      },
    }

    Ok(())
  }

  fn document_with_visitor<S, V>(
    &mut self,
    doc_id: i32,
    visitor: &mut V,
    writer: Option<&mut S>,
  ) -> Result<()>
  where
    S: StoredFieldsWriter,
    V: StoredFieldVisitor,
  {
    let i = reader_index(doc_id, self.max_doc, self.starts.as_ref())?;

    let sf = match &mut self.sub_stored_fields[i] {
      Some(sf) => sf,
      slot @ None => slot.insert(self.sub_reader[i].stored_fields()?),
    };
    sf.document_with_visitor(doc_id - self.starts[i] as i32, visitor, writer)
  }
}

impl<R> RawStoredFieldsReader for StoredFieldsImpl<R>
where
  R: IndexReader,
  R::StoredFields: RawStoredFieldsReader,
{
  type IndexInput = <R::StoredFields as RawStoredFieldsReader>::IndexInput;
}
/// Helper method for implementations to get the corresponding reader for a document ID.
pub fn reader_index(doc_id: i32, max_doc: i32, starts: &[usize]) -> Result<usize> {
  if doc_id < 0 || doc_id >= max_doc {
    return Err(LuceneError::illegal_argument(format!(
      "docID must be >= 0 and < maxDoc={} (got docID={})",
      max_doc, doc_id
    )));
  }
  let v = ReaderUtil::sub_index(doc_id as usize, starts);
  if v < 0 {
    return Err(LuceneError::illegal_state("index should >= 0"));
  }
  Ok(v as usize)
}