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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * 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::codec::Codec;
use crate::core::codecs::live_docs_format::LiveDocsFormat;
use crate::core::index::segment_info::{SegmentInfo, named_for_this_segment, renamed_segment_file};
use crate::core::store::directory::Directory;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::{StringHelper, TryIntoInt};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};

/// Embeds a [read-only] [SegmentInfo] and adds per-commit fields.
///
/// @lucene.experimental
pub struct SegmentCommitInfo<D> {
  /// The SegmentInfo that we wrap.
  pub info: Arc<SegmentInfo<D>>,
  /// Id that uniquely identifies this segment commit.
  id: Option<[u8; StringHelper::ID_LENGTH]>,
  /// How many deleted docs in the segment.
  del_count: i32,
  /// How many soft-deleted docs in the segment that are not also
  /// hard-deleted.
  soft_del_count: i32,
  /// Generation number of the live docs file (-1 if there are no deletes
  /// yet).
  del_gen: i64,
  /// Normally 1 + del_gen, unless an error was hit on the last attempt
  /// to write.
  next_write_del_gen: i64,
  /// Generation number of the FieldInfos (-1 if there are no updates).
  field_infos_gen: i64,
  /// Normally 1 + field_infos_gen, unless an error was hit on the last
  /// attempt to write.
  next_write_field_infos_gen: i64,
  /// Generation number of the DocValues (-1 if there are no updates).
  doc_values_gen: i64,
  /// Normally 1 + doc_values_gen, unless an error was hit on the last
  /// attempt to write.
  next_write_doc_values_gen: i64,
  /// Track the per-field DocValues update files.
  dv_updates_files: Option<Arc<HashMap<i32, HashSet<String>>>>,
  /// Track the FieldInfos update files.
  field_infos_files: Option<Arc<HashSet<String>>>,
  /// Size of the segment in bytes (-1 of unknown).
  size_in_bytes: AtomicI64,
  /// Used in memory by IndexWriter to track buffered deletes. Not persisted
  /// to disk.
  buffered_deletes_gen: i64,
}
impl<D> SegmentCommitInfo<D> {
  /// Creates a new instance.
  ///
  /// # Arguments
  /// - `info`: The [`SegmentInfo`] that this wraps.
  /// - `del_count`: Number of deleted documents in this segment.
  /// - `Soft_del_count`: Number of soft-deleted documents not also
  ///   hard-deleted.
  /// - `del_gen`: Deletion generation number (used to name deletion files).
  /// - `field_infos_gen`: FieldInfos generation number (used to name
  ///   field-infos files).
  /// - `Doc_values_gen`: DocValues generation number (used to name doc-values
  ///   updates files).
  /// - `ID`: ID that uniquely identifies this segment commit.
  pub fn new<T>(
    info: T,
    del_count: i32,
    soft_del_count: i32,
    del_gen: i64,
    field_infos_gen: i64,
    doc_values_gen: i64,
    id: Option<[u8; StringHelper::ID_LENGTH]>,
  ) -> Self
  where
    T: Into<Arc<SegmentInfo<D>>>,
  {
    Self {
      info: info.into(),
      del_count,
      soft_del_count,
      del_gen,
      next_write_del_gen: if del_gen == -1 {
        1
      } else {
        del_gen.wrapping_add(1)
      },
      field_infos_gen,
      next_write_field_infos_gen: if field_infos_gen == -1 {
        1
      } else {
        field_infos_gen.wrapping_add(1)
      },
      doc_values_gen,
      next_write_doc_values_gen: if doc_values_gen == -1 {
        1
      } else {
        doc_values_gen.wrapping_add(1)
      },
      id,
      dv_updates_files: None,
      field_infos_files: None,
      size_in_bytes: AtomicI64::new(-1),
      buffered_deletes_gen: -1,
    }
  }
  /// Returns a reference to the per-field DocValues updates files.
  pub fn get_doc_values_updates_files(&self) -> &HashMap<i32, HashSet<String>> {
    static EMPTY: std::sync::LazyLock<HashMap<i32, HashSet<String>>> =
      std::sync::LazyLock::new(HashMap::new);
    self.dv_updates_files.as_deref().unwrap_or(&EMPTY)
  }

  /// Removes and returns the per-field DocValues updates files.
  pub(crate) fn take_doc_values_updates_files(&mut self) -> HashMap<i32, HashSet<String>> {
    self
      .dv_updates_files
      .take()
      .map(Arc::unwrap_or_clone)
      .unwrap_or_default()
  }

  /// Sets the DocValues updates file names, per field number. Borrowed maps
  /// are copied into renamed sets; owned maps are renamed in place.
  pub fn set_doc_values_updates_files<'a>(
    &mut self,
    dv_updates_files: Cow<'a, HashMap<i32, HashSet<String>>>,
  ) {
    let old_files = self.dv_updates_files.take();
    match dv_updates_files {
      Cow::Borrowed(files) => {
        if files.is_empty() {
          return;
        }
        let mut renamed = old_files.and_then(Arc::into_inner).unwrap_or_default();
        renamed.clear();
        renamed.reserve(files.len());
        let renamed = self.dv_updates_files.insert(Arc::new(renamed));
        let renamed = Arc::make_mut(renamed);
        for (key, file_set) in files {
          let mut renamed_set = HashSet::with_capacity(file_set.len());
          for file in file_set {
            renamed_set.insert(renamed_segment_file(&self.info.name, file));
          }
          renamed.insert(*key, renamed_set);
        }
      },
      Cow::Owned(mut files) => {
        for file_set in files.values_mut() {
          if file_set.iter().all(|file| {
            let suffix = crate::core::index::IndexFileNames::strip_segment_name(file);
            file.len() == self.info.name.len() + suffix.len()
              && file.starts_with(self.info.name.as_ref())
          }) {
            continue;
          }
          let previous_files = std::mem::replace(file_set, HashSet::with_capacity(file_set.len()));
          for file in previous_files {
            file_set.insert(named_for_this_segment(&self.info.name, file));
          }
        }
        self.dv_updates_files = (!files.is_empty()).then(|| Arc::new(files));
      },
    }
  }
  pub(crate) fn set_imported_field_infos_files(&mut self, source: &HashSet<String>) {
    self.set_field_infos_files(std::iter::empty::<String>());
    if source.is_empty() {
      return;
    }
    let files = self.field_infos_files.get_or_insert_with(Arc::default);
    let files = Arc::make_mut(files);
    files.reserve(source.len());
    for file in source {
      files.insert(renamed_segment_file(&self.info.name, file));
    }
  }

  pub(crate) fn set_owned_field_infos_files(&mut self, files: HashSet<String>) {
    if files.iter().all(|file| {
      let suffix = crate::core::index::IndexFileNames::strip_segment_name(file);
      file.len() == self.info.name.len() + suffix.len() && file.starts_with(self.info.name.as_ref())
    }) {
      self.field_infos_files = (!files.is_empty()).then(|| Arc::new(files));
    } else {
      self.set_field_infos_files(files);
    }
  }

  /// Returns a reference to the FieldInfos file names.
  /// Returns the [`FieldInfos`](crate::core::index::field_infos::FieldInfos) file names.
  pub fn get_field_infos_files(&self) -> &HashSet<String> {
    static EMPTY: std::sync::LazyLock<HashSet<String>> = std::sync::LazyLock::new(HashSet::new);
    self.field_infos_files.as_deref().unwrap_or(&EMPTY)
  }

  /// Sets the FieldInfos file names.
  /// Sets the [`FieldInfos`](crate::core::index::field_infos::FieldInfos) file names.
  pub fn set_field_infos_files<I, S>(&mut self, field_infos_files: I)
  where
    I: IntoIterator<Item = S>,
    S: AsRef<str> + Into<String>,
  {
    if let Some(current) = &mut self.field_infos_files {
      if let Some(set) = Arc::get_mut(current) {
        set.clear();
      } else {
        self.field_infos_files = None;
      }
    }
    for file in field_infos_files {
      let files = self.field_infos_files.get_or_insert_with(Arc::default);
      Arc::make_mut(files).insert(named_for_this_segment(&self.info.name, file));
    }
  }

  /// Called when we succeed in writing deletes.
  pub fn advance_del_gen(&mut self) {
    self.del_gen = self.next_write_del_gen;
    self.next_write_del_gen = self.del_gen.wrapping_add(1);
    self.generation_advanced();
  }

  /// Called if there was an error while writing deletes, so that we don't
  /// try to write to the same file more than once.
  pub fn advance_next_write_del_gen(&mut self) {
    self.next_write_del_gen = self.next_write_del_gen.wrapping_add(1);
  }

  /// Gets the `nextWriteDelGen`.
  pub fn get_next_write_del_gen(&self) -> i64 {
    self.next_write_del_gen
  }

  /// Sets the `nextWriteDelGen`.
  pub fn set_next_write_del_gen(&mut self, v: i64) {
    self.next_write_del_gen = v;
  }

  /// Called when we succeed in writing a new FieldInfos generation.
  pub fn advance_field_infos_gen(&mut self) {
    self.field_infos_gen = self.next_write_field_infos_gen;
    self.next_write_field_infos_gen = self.field_infos_gen.wrapping_add(1);
    self.generation_advanced();
  }

  /// Called if there was an error while writing a new generation of
  /// FieldInfos, so that we don't try to write to the same file more than
  /// once.
  pub fn advance_next_write_field_infos_gen(&mut self) {
    self.next_write_field_infos_gen = self.next_write_field_infos_gen.wrapping_add(1);
  }

  /// Gets the `nextWriteFieldInfosGen`.
  pub fn get_next_write_field_infos_gen(&self) -> i64 {
    self.next_write_field_infos_gen
  }

  /// Sets the `nextWriteFieldInfosGen`.
  pub fn set_next_write_field_infos_gen(&mut self, v: i64) {
    self.next_write_field_infos_gen = v;
  }

  /// Called when we succeed in writing a new DocValues generation.
  pub fn advance_doc_values_gen(&mut self) {
    self.doc_values_gen = self.next_write_doc_values_gen;
    self.next_write_doc_values_gen = self.doc_values_gen.wrapping_add(1);
    self.generation_advanced();
  }

  /// Called if there was an error while writing a new generation of
  /// DocValues, so that we don't try to write to the same file more than
  /// once.
  pub fn advance_next_write_doc_values_gen(&mut self) {
    self.next_write_doc_values_gen = self.next_write_doc_values_gen.wrapping_add(1);
  }

  /// Gets the `nextWriteDocValuesGen`.
  pub fn get_next_write_doc_values_gen(&self) -> i64 {
    self.next_write_doc_values_gen
  }

  /// Sets the `nextWriteDocValuesGen`.
  pub fn set_next_write_doc_values_gen(&mut self, v: i64) {
    self.next_write_doc_values_gen = v;
  }

  /// Returns the total size in bytes of all files for this segment.
  pub fn size_in_bytes(&self) -> Result<i64>
  where
    D: Directory,
  {
    let current_size = self.size_in_bytes.load(Ordering::SeqCst);
    if current_size != -1 {
      return Ok(current_size);
    }
    let mut sum = 0;
    let files = self.files_for_read()?;
    for file_name in files.iter() {
      sum += self.info.dir.file_length(file_name)?;
    }
    let v: i64 = sum.try_convert()?;
    self.size_in_bytes.store(v, Ordering::SeqCst);

    Ok(v)
  }
  pub(crate) fn has_only_base_files(&self) -> bool {
    !self.has_deletions()
      && self.get_field_infos_files().is_empty()
      && self
        .dv_updates_files
        .as_ref()
        .is_none_or(|files| files.is_empty())
  }

  pub(crate) fn files_for_read(&self) -> Result<Cow<'_, HashSet<String>>> {
    if self.has_only_base_files() {
      Ok(Cow::Borrowed(self.info.files()?))
    } else {
      Ok(Cow::Owned(self.files()?))
    }
  }

  pub(crate) fn append_files_to(&self, files: &mut HashSet<String>) -> Result<()> {
    let info_files = self.info.files()?;
    files.reserve(info_files.len() + self.update_file_count() + usize::from(self.has_deletions()));
    files.extend(info_files.iter().cloned());
    if self.has_deletions() {
      self
        .info
        .get_codec()?
        .live_docs_format()
        .files(self, files)?;
    }
    self.append_update_files(files);
    Ok(())
  }

  fn update_file_count(&self) -> usize {
    self
      .get_doc_values_updates_files()
      .values()
      .map(HashSet::len)
      .sum::<usize>()
      + self.get_field_infos_files().len()
  }

  fn append_update_files(&self, files: &mut HashSet<String>) {
    for update_files in self.get_doc_values_updates_files().values() {
      files.extend(update_files.iter().cloned());
    }
    files.extend(self.get_field_infos_files().iter().cloned());
  }

  /// Returns all files in use by this segment.
  pub fn files(&self) -> Result<HashSet<String>> {
    let mut files = HashSet::new();
    self.append_files_to(&mut files)?;
    Ok(files)
  }

  /// Returns the buffered deletes generation number.
  pub fn get_buffered_deletes_gen(&self) -> i64 {
    self.buffered_deletes_gen
  }

  /// Sets the buffered deletes generation number.
  /// Can only be set once, otherwise it will return an error.
  pub fn set_buffered_deletes_gen(&mut self, v: i64) -> Result<()> {
    if self.buffered_deletes_gen == -1 {
      self.buffered_deletes_gen = v;
      self.generation_advanced();
      Ok(())
    } else {
      Err(LuceneError::illegal_state(
        "buffered deletes gen should only be set once",
      ))
    }
  }
  /// Determines if this segment has deletions.
  pub fn has_deletions(&self) -> bool {
    self.del_gen != -1
  }
  /// Returns `true` if there are any field updates for the segment in this
  /// commit.
  pub fn has_field_updates(&self) -> bool {
    self.field_infos_gen != -1
  }

  /// Returns the next available generation number of the FieldInfos files.
  pub fn get_next_field_infos_gen(&self) -> i64 {
    self.next_write_field_infos_gen
  }

  /// Returns the generation number of the field infos file, or `-1` if there
  /// are no field updates yet.
  pub fn get_field_infos_gen(&self) -> i64 {
    self.field_infos_gen
  }

  /// Returns the next available generation number of the DocValues files.
  pub fn get_next_doc_values_gen(&self) -> i64 {
    self.next_write_doc_values_gen
  }

  /// Returns the generation number of the DocValues file or `-1` if there are
  /// no doc-values updates yet.
  pub fn get_doc_values_gen(&self) -> i64 {
    self.doc_values_gen
  }

  /// Returns the next available generation number of the live docs file.
  pub fn get_next_del_gen(&self) -> i64 {
    self.next_write_del_gen
  }

  /// Returns the generation number of the live docs file or `-1` if there are
  /// no deletes yet.
  pub fn get_del_gen(&self) -> i64 {
    self.del_gen
  }

  /// Returns the number of deleted docs in the segment.
  pub fn get_del_count(&self) -> i32 {
    self.del_count
  }

  /// Returns the number of only soft-deleted docs.
  pub fn get_soft_del_count(&self) -> i32 {
    self.soft_del_count
  }

  pub fn set_del_count(&mut self, del_count: i32) -> Result<()> {
    let max_doc = self.info.max_doc()?;
    if del_count < 0 || del_count > max_doc {
      return Err(LuceneError::illegal_argument(format!(
        "invalid delCount={del_count} (maxDoc={max_doc})"
      )));
    }

    debug_assert!(
      self.soft_del_count + del_count <= max_doc,
      "maxDoc={}, delCount={}, softDelCount={}",
      max_doc,
      del_count,
      self.soft_del_count
    );

    self.del_count = del_count;
    Ok(())
  }

  pub fn set_soft_del_count(&mut self, soft_del_count: i32) -> Result<()> {
    let max_doc = self.info.max_doc()?;
    validate_soft_del_count(self.del_count, max_doc, soft_del_count)?;
    self.soft_del_count = soft_del_count;
    Ok(())
  }
  pub fn set_soft_del_count_without_check(&mut self, soft_del_count: i32) {
    self.soft_del_count = soft_del_count;
  }

  /// Returns a description of this segment.
  pub fn to_string_with_pending_del_count(
    &self,
    pending_del_count: i32,
  ) -> std::result::Result<String, std::fmt::Error> {
    let mut description = String::new();
    self.write_description(&mut description, pending_del_count)?;
    Ok(description)
  }

  fn write_description(&self, output: &mut impl Write, pending_del_count: i32) -> std::fmt::Result {
    self
      .info
      .write_description(output, self.del_count + pending_del_count)?;

    if self.del_gen != -1 {
      write!(output, ":delGen={}", self.del_gen)?;
    }
    if self.field_infos_gen != -1 {
      write!(output, ":fieldInfosGen={}", self.field_infos_gen)?;
    }
    if self.doc_values_gen != -1 {
      write!(output, ":dvGen={}", self.doc_values_gen)?;
    }
    if self.soft_del_count > 0 {
      write!(output, " :softDel={}", self.soft_del_count)?;
    }
    if self.id.is_some() {
      write!(
        output,
        " :id={}",
        StringHelper::id_to_string(self.id.as_ref())
      )?;
    }
    Ok(())
  }
  /// Returns the number of deleted documents in the segment.
  /// If `include_soft_deletes` is `true`, it includes soft-deleted documents.
  pub fn get_del_count_with_soft_deletes(&self, include_soft_deletes: bool) -> i32 {
    if include_soft_deletes {
      self.get_del_count() + self.get_soft_del_count()
    } else {
      self.get_del_count()
    }
  }

  /// Advances the generation, resetting `size_in_bytes` and generating a new
  /// `id`.
  pub fn generation_advanced(&mut self) {
    self.size_in_bytes.store(-1, Ordering::SeqCst);
    self.id = Option::from(StringHelper::random_id());
  }

  /// Returns an ID that uniquely identifies this segment commit.
  /// If no ID is assigned, returns `None`.
  pub fn get_id(&self) -> Option<&[u8; StringHelper::ID_LENGTH]> {
    if self.id.is_none() {
      None
    } else {
      self.id.as_ref()
    }
  }
}
pub fn validate_soft_del_count(del_count: i32, max_doc: i32, soft_del_count: i32) -> Result<()> {
  if soft_del_count < 0 || soft_del_count > max_doc {
    return Err(LuceneError::illegal_argument(format!(
      "invalid softDelCount={soft_del_count} (maxDoc={max_doc})"
    )));
  }

  debug_assert!(
    del_count + soft_del_count <= max_doc,
    "maxDoc={}, delCount={}, softDelCount={}",
    max_doc,
    del_count,
    soft_del_count
  );
  Ok(())
}
/// Implement `Display` for [`SegmentCommitInfo`].
impl<D> std::fmt::Display for SegmentCommitInfo<D> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    self.write_description(f, 0)
  }
}
impl<D> Clone for SegmentCommitInfo<D> {
  fn clone(&self) -> Self {
    let mut other = SegmentCommitInfo::new(
      self.info.clone(),
      self.del_count,
      self.soft_del_count,
      self.del_gen,
      self.field_infos_gen,
      self.doc_values_gen,
      self.get_id().copied(),
    );

    other.next_write_del_gen = self.next_write_del_gen;
    other.next_write_field_infos_gen = self.next_write_field_infos_gen;
    other.next_write_doc_values_gen = self.next_write_doc_values_gen;

    other.dv_updates_files = self.dv_updates_files.clone();

    other.field_infos_files = self.field_infos_files.clone();

    other
  }
}
impl<D> PartialEq for SegmentCommitInfo<D> {
  fn eq(&self, other: &Self) -> bool {
    std::ptr::eq(self, other)
  }
}