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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 * 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::collections::{HashMap, HashSet};
use std::fmt::{Display, Write};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use crate::core::codecs::Codecs;
use crate::core::search::sort::Sort;
#[cfg(test)]
use crate::core::store::dummy::dummy_directory::DummyDirectory;
use crate::core::util::StringHelper;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::version::Version;

/// Information about a segment such as its name, directory, and files related
/// to the segment.
///
/// # Experimental
/// This API is experimental and may change in future releases.
pub struct SegmentInfo<D> {
  /// Unique segment name in the directory.
  pub(crate) name: Arc<str>,
  max_doc: i32, // number of docs in seg
  /// Where this segment resides.
  pub(crate) dir: Arc<D>,
  is_compound_file: bool,
  /// Id that uniquely identifies this segment.
  id: [u8; StringHelper::ID_LENGTH],
  #[cfg(test)]
  id_str: String,
  pub(crate) id_key: Arc<str>,
  codec: Option<Codecs>,
  diagnostics: HashMap<String, String>,
  attributes: HashMap<String, String>,
  pub(crate) index_sort: Option<Arc<Sort>>,
  /// Tracks the Lucene version this segment was created with, since 3.1.
  /// Null indicates an older than 3.0 index, and it's used to detect a
  /// too-old index. The format expected is "x.y" - "2.x" for pre-3.0
  /// indexes (or None), and specific versions afterward ("3.0.0",
  /// "3.1.0" etc.). See [`Version`](crate::core::util::version::Version) for details.
  pub(crate) version: Option<Version>,
  /// Tracks the minimum version that contributed documents to a segment.
  /// For flush segments, that is the version that wrote it.
  /// For merged segments, this is the minimum `min_version` of all the
  /// segments that have been merged into this segment.
  pub(crate) min_version: Option<Version>,
  has_blocks: bool,
  /// The SegmentCommitInfo wraps a SegmentInfo inside an Rc,
  /// but sometimes we need to modify the set_files of the SegmentInfo contained within the SegmentCommitInfo,
  set_files: Option<Arc<HashSet<String>>>,
}

static EMPTY_SEGMENT_FILES: std::sync::LazyLock<Arc<HashSet<String>>> =
  std::sync::LazyLock::new(Arc::default);

// padding using
#[cfg(test)]
impl Default for SegmentInfo<DummyDirectory> {
  fn default() -> Self {
    let id = [0; 16];
    let id_str = StringHelper::id_to_string(Some(&id));
    SegmentInfo {
      name: Arc::from(""),
      max_doc: -1,
      dir: Arc::new(DummyDirectory),
      is_compound_file: false,
      id: [0; 16],
      id_str: id_str.clone(),
      id_key: Arc::from(id_str),
      diagnostics: HashMap::new(),
      attributes: HashMap::new(),
      codec: None,
      index_sort: None,
      version: None,
      min_version: None,
      has_blocks: false,
      set_files: None,
    }
  }
}

/// Used by some member fields to mean not present (e.g., norms, deletions).
pub const NO: i32 = -1; // e.g. no norms; no deletes;
/// Used by some member fields to mean present (e.g., norms, deletions).
pub const YES: i32 = 1; // e.g. have norms; have deletes;
impl<D> SegmentInfo<D> {
  /// Constructs a new complete [`SegmentInfo`] instance from input.
  ///
  /// # Arguments
  ///
  /// * `dir` - Directory where this segment resides.
  /// * `version` - The Lucene version this segment was created with.
  /// * `min_version` - The minimum version that contributed documents to this
  ///   segment.
  /// * `name` - Unique segment name.
  /// * `max_doc` - Number of documents in the segment.
  /// * `is_compound_file` - Indicates if this segment uses a compound file
  ///   format.
  /// * `has_blocks` - Indicates if the segment has blocks.
  /// * `codec` - Codec that wrote this segment, or `None` while reading the
  ///   segment info file before [`SegmentInfos`](crate::core::index::segment_infos::SegmentInfos) fills it in.
  /// * `diagnostics` - Diagnostic information related to the segment.
  /// * `id` - Unique identifier for this segment.
  /// * `attributes` - Additional attributes for the segment.
  /// * `index_sort` - The sort order of the index, if any.
  #[allow(clippy::too_many_arguments)]
  pub fn new<N: Into<Arc<str>>>(
    dir: Arc<D>,
    version: Option<Version>,
    min_version: Option<Version>,
    name: N,
    max_doc: i32,
    is_compound_file: bool,
    has_blocks: bool,
    codec: Option<Codecs>,
    diagnostics: HashMap<String, String>,
    id: [u8; StringHelper::ID_LENGTH],
    attributes: HashMap<String, String>,
    index_sort: Option<Arc<Sort>>,
  ) -> Result<SegmentInfo<D>> {
    if version.is_none() {
      return Err(LuceneError::illegal_argument(
        "segment version must not be None",
      ));
    }
    if id.len() != StringHelper::ID_LENGTH {
      return Err(LuceneError::illegal_argument(format!("Invalid id: {id:?}")));
    }
    #[cfg(test)]
    let id_key = {
      // for easy debugging
      let random = StringHelper::id_to_string(Some(&StringHelper::random_id()));
      let id_str = StringHelper::id_to_string(Some(&id));
      format!("{}-{}", id_str, random)
    };

    #[cfg(not(test))]
    let mut id_digits = [0; 25];
    #[cfg(not(test))]
    let id_key = StringHelper::format_id(&StringHelper::random_id(), &mut id_digits);
    Ok(SegmentInfo {
      dir,
      version,
      min_version,
      name: name.into(),
      max_doc,
      is_compound_file,
      has_blocks,
      codec,
      diagnostics,
      id,
      #[cfg(test)]
      id_str: StringHelper::id_to_string(Some(&id)),
      id_key: id_key.into(),
      attributes,
      index_sort,
      set_files: None,
    })
  }
}
impl<D> SegmentInfo<D> {
  /// Returns this segment's name.
  pub fn name(&self) -> &str {
    &self.name
  }

  /// Returns the directory that contains this segment.
  pub fn dir(&self) -> &Arc<D> {
    &self.dir
  }

  /// Sets the diagnostics map by taking ownership of the given map.
  pub(crate) fn set_diagnostics(&mut self, diagnostics: HashMap<String, String>) {
    self.diagnostics = diagnostics;
  }

  /// Adds or modifies this segment's diagnostics.
  ///
  /// Entries in the given map whose keys are not present in the current
  /// diagnostics are added. Otherwise, existing entries are modified with
  /// the given map's value.
  ///
  /// # Arguments
  /// * `diagnostics` - The additional diagnostics to be added or modified.
  pub fn add_diagnostics(&mut self, diagnostics: HashMap<String, String>) {
    self.diagnostics.extend(diagnostics);
  }

  /// Can only be called once.
  pub fn set_codec<C>(&mut self, codec: C) -> Result<()>
  where
    C: Into<Codecs>,
  {
    if self.codec.is_some() {
      return Err(LuceneError::illegal_state("codec can only be set once"));
    }
    self.codec = Some(codec.into());
    Ok(())
  }

  /// Return the [`Codec`](crate::core::codecs::Codec) that wrote this segment.
  pub fn get_codec(&self) -> Result<&Codecs> {
    self
      .codec
      .as_ref()
      .ok_or_else(|| LuceneError::illegal_state("segment codec is not set"))
  }

  /// Returns diagnostics saved into the segment when it was written.
  /// The map is immutable.
  pub fn get_diagnostics(&self) -> &HashMap<String, String> {
    &self.diagnostics
  }
  /// Marks whether this segment is stored as a compound file.
  ///
  /// # Arguments
  ///
  /// * `is_compound_file` - `true` if this is a compound file; otherwise,
  ///   `false`.
  pub(crate) fn set_use_compound_file(&mut self, is_compound_file: bool) {
    self.is_compound_file = is_compound_file;
  }
  /// Returns `true` if this segment is stored as a compound file; otherwise,
  /// `false`.
  pub fn get_use_compound_file(&self) -> bool {
    self.is_compound_file
  }

  /// Returns `true` if this segment contains documents written as blocks.
  ///
  /// # See Also
  /// [`LeafMetaData::get_has_blocks`](crate::core::index::leaf_metadata::LeafMetaData::get_has_blocks)
  pub fn get_has_blocks(&self) -> bool {
    self.has_blocks
  }

  /// Sets the `has_blocks` property to `true`. This setting is viral and
  /// can't be unset.
  pub(crate) fn set_has_blocks(&mut self) {
    self.has_blocks = true;
  }
  /// Returns the number of documents in this segment (deletions are not taken
  /// into account)
  pub fn max_doc(&self) -> Result<i32> {
    if self.max_doc == -1 {
      return Err(LuceneError::illegal_state("maxDoc isn't set yet"));
    }
    Ok(self.max_doc)
  }

  /// Sets the max_doc value, can only be called once
  pub(crate) fn set_max_doc(&mut self, max_doc: i32) -> Result<()> {
    if self.max_doc != -1 {
      return Err(LuceneError::illegal_state(format!(
        "maxDoc was already set: this.maxDoc={} vs maxDoc {}",
        self.max_doc, max_doc
      )));
    }
    self.max_doc = max_doc;
    Ok(())
  }

  /// Returns all files referenced by this SegmentInfo
  pub fn files(&self) -> Result<&HashSet<String>> {
    self.files_snapshot().map(Arc::as_ref)
  }
  pub(crate) fn files_snapshot(&self) -> Result<&Arc<HashSet<String>>> {
    match self.set_files {
      Some(ref files) => Ok(files),
      None => Err(LuceneError::illegal_state(format!(
        "files were not set; segment={} maxDoc={}",
        self.name, self.max_doc
      ))),
    }
  }

  pub fn take_files(&mut self) -> Result<HashSet<String>> {
    match self.set_files.take() {
      Some(files) => Ok(Arc::unwrap_or_clone(files)),
      None => Err(LuceneError::illegal_state(format!(
        "files were not set; segment={} maxDoc={}",
        self.name, self.max_doc
      ))),
    }
  }

  /// Sets the files written for this segment.
  pub fn set_files<I, S>(&mut self, files: I) -> Result<()>
  where
    I: IntoIterator<Item = S>,
    S: AsRef<str> + Into<String>,
  {
    self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
    self.add_files(files)
  }
  pub(crate) fn set_imported_files(&mut self, files: &HashSet<String>) -> Result<()> {
    self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
    self.check_file_names(files)?;
    let renamed = self
      .set_files
      .insert(Arc::new(HashSet::with_capacity(files.len())));
    let renamed = Arc::make_mut(renamed);
    for file in files {
      renamed.insert(renamed_segment_file(&self.name, file));
    }
    Ok(())
  }

  /// Replaces the files written for this segment.
  pub(crate) fn replace_files(&mut self, files: HashSet<String>) -> Result<()> {
    self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
    self.check_file_names(&files)?;
    self.set_files = Some(Arc::new(
      if files
        .iter()
        .all(|file| IndexFileNames::parse_segment_name(file) == self.name.as_ref())
      {
        files
      } else {
        files
          .into_iter()
          .map(|file| named_for_this_segment(&self.name, file))
          .collect()
      },
    ));
    Ok(())
  }
  /// Converts this segment information into a formatted string with deletions
  /// count.
  ///
  /// # Arguments
  ///
  /// * `del_count` - Number of deletions in the segment.
  ///
  /// # Format
  ///
  /// `_a(3.1):c45/4:[sorter=<long: "timestamp">!]`
  ///
  /// - `_a`: The segment's name.
  /// - `(3.1)`: Lucene version used to create the segment (`?` if unknown).
  /// - `c`: Indicates the compound file format (`C` if not compound).
  /// - `45`: Number of documents in the segment.
  /// - `/4`: Number of deletions (only present if deletions exist).
  /// - `[sorter=<long: "timestamp">!]`: Indicates the segment is sorted by
  ///   the `timestamp` field in descending order (optional, omitted for
  ///   unsorted segments).
  pub fn to_string(&self, del_count: i32) -> std::result::Result<String, std::fmt::Error> {
    let mut description = String::new();
    self.write_description(&mut description, del_count)?;
    Ok(description)
  }

  pub(crate) fn write_description(
    &self,
    output: &mut impl Write,
    del_count: i32,
  ) -> std::fmt::Result {
    output.write_str(&self.name)?;
    output.write_char('(')?;
    if let Some(version) = &self.version {
      write!(output, "{version}")?;
    } else {
      output.write_char('?')?;
    }
    output.write_char(')')?;

    let cfs = if self.is_compound_file { 'c' } else { 'C' };
    write!(output, ":{cfs}{}", self.max_doc)?;

    if del_count != 0 {
      write!(output, "/{del_count}")?;
    }
    if let Some(index_sort) = &self.index_sort {
      write!(output, ":[indexSort={index_sort}]")?;
    }
    if !self.diagnostics.is_empty() {
      write!(output, ":[diagnostics={:?}]", self.diagnostics)?;
    }
    if !self.attributes.is_empty() {
      write!(output, ":[attributes={:?}]", self.attributes)?;
    }
    Ok(())
  }
  /// Returns the version of the code which wrote the segment.
  pub fn get_version_ref(&self) -> Option<&Version> {
    self.version.as_ref()
  }

  /// Returns the minimum Lucene version that contributed documents to this
  /// segment, or `None` if it is unknown.
  pub fn get_min_version_ref(&self) -> Option<&Version> {
    self.min_version.as_ref()
  }
  pub fn get_min_version(&self) -> Option<Version> {
    self.min_version.as_ref().cloned()
  }

  /// Returns the id that uniquely identifies this segment.
  pub fn get_id(&self) -> &[u8; StringHelper::ID_LENGTH] {
    &self.id
  }
  pub fn get_id_key(&self) -> &str {
    &self.id_key
  }

  /// Add these files to the set of files written for this segment.
  pub fn add_files<I, S>(&mut self, files: I) -> Result<()>
  where
    I: IntoIterator<Item = S>,
    S: AsRef<str> + Into<String>,
  {
    let files: smallvec::SmallVec<[S; 4]> = files.into_iter().collect();
    self.check_file_names(&files)?;
    match self.set_files {
      Some(ref mut set_files) if !files.is_empty() => {
        let set_files = Arc::make_mut(set_files);
        set_files.reserve(files.len());
        for f in files {
          set_files.insert(named_for_this_segment(&self.name, f));
        }
      },
      Some(_) => {},
      None => return Err(LuceneError::illegal_state("set_files was not initialized")),
    }
    Ok(())
  }

  /// Add this file to the set of files written for this segment.
  pub fn add_file<F>(&mut self, file: F) -> Result<()>
  where
    F: Into<String>,
  {
    let file = file.into();
    self.check_file_names(std::iter::once(&file))?;
    match self.set_files {
      Some(ref mut set_files) => {
        Arc::make_mut(set_files).insert(named_for_this_segment(&self.name, file));
      },
      None => return Err(LuceneError::illegal_state("set_files was not initialized")),
    }
    Ok(())
  }

  fn check_file_names<I, S>(&self, files: I) -> Result<()>
  where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
  {
    for file in files {
      let file = file.as_ref();
      // Check if the file name matches the codec file pattern
      if !CODEC_FILE_PATTERN.is_match(file) {
        return Err(LuceneError::illegal_argument(format!(
          "Invalid codec filename '{}', must match: {}",
          file,
          CODEC_FILE_PATTERN.as_str()
        )));
      }

      if file.as_bytes()[file.len().saturating_sub(4)..].eq_ignore_ascii_case(b".tmp") {
        return Err(LuceneError::illegal_argument(format!(
          "Invalid codec filename '{file}', cannot end with .tmp extension"
        )));
      }
    }

    Ok(())
  }

  /// Get a codec attribute value, or None if it does not exist.
  pub fn get_attribute(&self, key: &str) -> Option<&str> {
    self.attributes.get(key).map(|v| v.as_str())
  }
  /// Puts a codec attribute value.
  ///
  /// This is a key-value mapping for the field that the codec can use to
  /// store additional metadata, and will be available to the codec when
  /// reading the segment via `get_attribute`.
  ///
  /// If a value already exists for the field, it will be replaced with the
  /// new value. This method ensures thread safety by making a
  /// copy-on-write for every attribute change.
  pub fn put_attribute<K, V>(&mut self, key: K, value: V) -> Option<String>
  where
    K: Into<String>,
    V: Into<String>,
  {
    let value = value.into();
    let key = key.into();
    // This needs to be thread-safe because multiple threads may be updating
    // (different) attributes at the same time due to concurrent
    // merging, while other threads may be formatting
    // segment info while other threads are updating attributes.
    self.attributes.insert(key, value)
  }
  /// Returns the internal codec attributes map.
  pub fn get_attributes(&self) -> Result<&HashMap<String, String>> {
    Ok(&self.attributes)
  }

  /// Returns the sort order of this segment, or None if the index has no
  /// sort.
  pub fn get_index_sort(&self) -> Option<Arc<Sort>> {
    self.index_sort.as_ref().cloned()
  }

  pub(crate) fn index_sort_ref(&self) -> Option<&Sort> {
    self.index_sort.as_deref()
  }

  /// Shares the file snapshot already validated and normalized by the flush path.
  /// The sole caller invokes this immediately after a successful `replace_files`.
  pub(crate) fn flush_failure_mirror(&self) -> Result<Self> {
    let mut mirror = Self::dummy(Arc::clone(&self.dir), Arc::clone(&self.name));
    mirror.set_files = Some(Arc::clone(self.files_snapshot()?));
    Ok(mirror)
  }

  // padding using
  pub(crate) fn dummy(dir: Arc<D>, name: Arc<str>) -> Self {
    let id = [0u8; StringHelper::ID_LENGTH];
    static DUMMY_ID: std::sync::LazyLock<Arc<str>> = std::sync::LazyLock::new(|| Arc::from("0"));
    SegmentInfo {
      name,
      max_doc: 0,
      dir,
      is_compound_file: false,
      id,
      #[cfg(test)]
      id_str: DUMMY_ID.to_string(),
      id_key: Arc::clone(&DUMMY_ID),
      diagnostics: HashMap::new(),
      attributes: HashMap::new(),
      codec: None,
      index_sort: None,
      version: None,
      min_version: None,
      has_blocks: false,
      set_files: None,
    }
  }
}
impl<D> PartialEq for SegmentInfo<D> {
  fn eq(&self, other: &Self) -> bool {
    Arc::ptr_eq(&self.dir, &other.dir) && self.name == other.name
  }
}
impl<D> Eq for SegmentInfo<D> {}

impl<D> Hash for SegmentInfo<D> {
  fn hash<H>(&self, state: &mut H)
  where
    H: Hasher,
  {
    Arc::as_ptr(&self.dir).hash(state);
    self.name.hash(state);
  }
}
impl<D> Display for SegmentInfo<D> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    self.write_description(f, 0)
  }
}
#[cfg(test)]
impl<D> Clone for SegmentInfo<D> {
  fn clone(&self) -> Self {
    SegmentInfo {
      name: self.name.clone(),
      max_doc: self.max_doc,
      dir: self.dir.clone(),
      is_compound_file: self.is_compound_file,
      id: self.id,
      id_str: self.id_str.clone(),
      id_key: self.id_key.clone(),
      diagnostics: self.diagnostics.clone(),
      attributes: self.attributes.clone(),
      codec: self.codec.clone(),
      index_sort: self.index_sort.clone(),
      version: self.version.clone(),
      min_version: self.min_version.clone(),
      has_blocks: self.has_blocks,
      set_files: self.set_files.clone(),
    }
  }
}

use crate::core::index::{CODEC_FILE_PATTERN, IndexFileNames};

pub(crate) fn renamed_segment_file(name: &str, file: &str) -> String {
  let suffix = IndexFileNames::strip_segment_name(file);
  if file.len() == name.len() + suffix.len() && file.starts_with(name) {
    file.to_owned()
  } else {
    [name, suffix].concat()
  }
}

/// Strips any segment name from the file and renames it with this segment.
/// This is because "segment names" can change, e.g., by addIndexes(Dir).
pub fn named_for_this_segment<T: AsRef<str> + Into<String>>(name: &str, file: T) -> String {
  let mut file = file.into();
  let stripped = IndexFileNames::strip_segment_name(&file);
  if file.len() == name.len() + stripped.len() && file.starts_with(name) {
    file
  } else {
    let prefix_length = file.len() - stripped.len();
    file.replace_range(..prefix_length, name);
    file
  }
}