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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * 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::doc_values_skip_index_type::DocValuesSkipIndexType;
use crate::core::index::doc_values_type::DocValuesType;
use crate::core::index::index_options::IndexOptions;
use crate::core::index::point_values::{MAX_INDEX_DIMENSIONS, MAX_NUM_BYTES};
use crate::core::index::vector_encoding::VectorEncoding;
use crate::core::index::vector_similarity_function::VectorSimilarityFunction;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use parking_lot::Mutex;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::atomic::{AtomicBool, AtomicI64};

///  Access to the Field Info file that describes document fields and whether or
/// not they are indexed.  Each segment has a separate Field Info file. Objects
/// of this struct are thread-safe for multiple  readers, but only one thread
/// can be adding documents at a time, with no other reader or writer
///  threads accessing this object.
/// # Note
/// FieldInfo Implement trait Default for padding using
#[derive(Default)] // for padding using
pub struct FieldInfo {
  /// Field's name
  pub name: String,

  /// Internal field number
  pub number: i32,
  doc_values_type: DocValuesType,
  doc_values_skip_index: DocValuesSkipIndexType,
  omit_norms: bool, // omit norms associated with indexed fields
  pub(crate) index_options: IndexOptions,
  attributes: Mutex<Arc<HashMap<String, String>>>,
  dv_gen: AtomicI64,
  ///  If both of these are positive it means this field indexed points (see
  /// [`PointsFormat`](crate::core::codecs::points_format::PointsFormat)).
  point_dimensions: [u8; 3],
  // The public constructor also accepts dimensions outside the normal codec limits.
  wide_point_dimensions: Option<Box<[usize; 3]>>,
  // if it is a positive value, it means this field indexes vectors
  vector_dimension: i32,
  custom_vector_encoding: Option<Box<VectorEncoding>>,
  float_vector_encoding: bool,
  vector_similarity_function: VectorSimilarityFunction,
  // whether this field is used as the soft-deletes field
  soft_deletes_field: bool,
  is_parent_field: bool,
  store_payloads: AtomicBool, /* whether this field stores payloads together
                               * with term positions  */
  // True if any document indexed term vectors
  store_term_vector: AtomicBool,
}
pub(crate) static EMPTY_FIELD_ATTRIBUTES: std::sync::LazyLock<Arc<HashMap<String, String>>> =
  std::sync::LazyLock::new(Arc::default);

impl FieldInfo {
  /// Creates a new instance. Attribute snapshots may be shared until either instance
  /// changes an attribute, when the map is copied on write.
  #[allow(clippy::too_many_arguments)]
  pub fn new<T, A>(
    name: T,
    number: i32,
    store_term_vector: bool,
    omit_norms: bool,
    store_payloads: bool,
    index_options: IndexOptions,
    doc_values: DocValuesType,
    doc_values_skip_index: DocValuesSkipIndexType,
    dv_gen: i64,
    attributes: A,
    point_dimension_count: usize,
    point_index_dimension_count: usize,
    point_num_bytes: usize,
    vector_dimension: i32,
    vector_encoding: VectorEncoding,
    vector_similarity_function: VectorSimilarityFunction,
    soft_deletes_field: bool,
    is_parent_field: bool,
  ) -> Result<Self>
  where
    T: Into<String>,
    A: Into<Arc<HashMap<String, String>>>,
  {
    let doc_values_type = doc_values;

    let (store_term_vector, store_payloads, omit_norms) = if index_options != IndexOptions::None {
      (store_term_vector, store_payloads, omit_norms)
    } else {
      (false, false, false)
    };
    let attributes = Mutex::new(attributes.into());
    let (custom_vector_encoding, float_vector_encoding) = match vector_encoding {
      VectorEncoding::BYTE(1) => (None, false),
      VectorEncoding::FLOAT32(4) => (None, true),
      encoding => (Some(Box::new(encoding)), false),
    };

    let dimensions = [
      point_dimension_count,
      point_index_dimension_count,
      point_num_bytes,
    ];
    let (point_dimensions, wide_point_dimensions) = match dimensions.map(u8::try_from) {
      [Ok(count), Ok(index_count), Ok(bytes)] => ([count, index_count, bytes], None),
      _ => ([0; 3], Some(Box::new(dimensions))),
    };
    let v = FieldInfo {
      name: name.into(),
      number,
      doc_values_type,
      doc_values_skip_index,
      omit_norms,
      index_options,
      attributes,
      dv_gen: AtomicI64::new(dv_gen),
      point_dimensions,
      wide_point_dimensions,
      vector_dimension,
      custom_vector_encoding,
      float_vector_encoding,
      vector_similarity_function,
      soft_deletes_field,
      is_parent_field,
      store_payloads: AtomicBool::new(store_payloads),
      store_term_vector: AtomicBool::new(store_term_vector),
    };
    v.check_consistency()?;
    Ok(v)
  }

  /// Check correctness of the FieldInfo options
  ///
  /// # Errors
  ///
  /// Returns [`LuceneErrorKind::IllegalArgument`](crate::core::util::error::lucene_error::LuceneErrorKind::IllegalArgument) if any option is invalid.
  pub fn check_consistency(&self) -> Result<()> {
    {
      if self.index_options != IndexOptions::None {
        // Cannot store payloads unless positions are indexed
        if self
          .index_options
          .cmp(&IndexOptions::DocsAndFreqsAndPositions)
          == Ordering::Less
          && self.store_payloads.load(Relaxed)
        {
          return Err(LuceneError::illegal_argument(format!(
            "indexed field '{}' cannot have payloads without positions",
            self.name
          )));
        }
      } else {
        if self.store_term_vector.load(Relaxed) {
          return Err(LuceneError::illegal_argument(format!(
            "non-indexed field '{}' cannot store term vectors",
            self.name
          )));
        }
        if self.store_payloads.load(Relaxed) {
          return Err(LuceneError::illegal_argument(format!(
            "non-indexed field '{}' cannot store payloads",
            self.name
          )));
        }
        if self.omit_norms {
          return Err(LuceneError::illegal_argument(format!(
            "non-indexed field '{}' cannot omit norms",
            self.name
          )));
        }
      }
    }

    if !self
      .doc_values_skip_index
      .is_compatible_with(self.doc_values_type)
    {
      return Err(LuceneError::illegal_argument(format!(
        "field '{}' cannot have docValuesSkipIndexType={:?} with doc values type {:?}",
        self.name, self.doc_values_skip_index, self.doc_values_type
      )));
    }
    if self.dv_gen.load(std::sync::atomic::Ordering::Relaxed) != -1
      && self.doc_values_type == DocValuesType::None
    {
      return Err(LuceneError::illegal_argument(format!(
        "field '{}' cannot have a docvalues update generation without having docvalues",
        self.name
      )));
    }

    if self.get_point_dimension_count() != 0 && self.get_point_num_bytes() == 0 {
      return Err(LuceneError::illegal_argument(format!(
        "pointNumBytes must be > 0 when pointDimensionCount={} (field: '{}')",
        self.get_point_dimension_count(),
        self.name
      )));
    }
    if self.get_point_index_dimension_count() != 0 && self.get_point_dimension_count() == 0 {
      return Err(LuceneError::illegal_argument(format!(
        "pointIndexDimensionCount must be 0 when pointDimensionCount=0 (field: '{}')",
        self.name
      )));
    }
    if self.get_point_num_bytes() != 0 && self.get_point_dimension_count() == 0 {
      return Err(LuceneError::illegal_argument(format!(
        "pointDimensionCount must be > 0 when pointNumBytes={} (field: '{}')",
        self.get_point_num_bytes(),
        self.name
      )));
    }

    if self.vector_dimension < 0 {
      return Err(LuceneError::illegal_argument(format!(
        "vectorDimension must be >=0; got {} (field: '{}')",
        self.vector_dimension, self.name
      )));
    }

    if self.soft_deletes_field && self.is_parent_field {
      return Err(LuceneError::illegal_argument(format!(
        "field can't be used as soft-deletes field and parent document field (field: '{}')",
        self.name
      )));
    }

    Ok(())
  }
  /// Verify that the provided FieldInfo has the same schema as this FieldInfo
  ///
  /// # Errors
  ///
  /// Returns [`LuceneErrorKind::IllegalArgument`](crate::core::util::error::lucene_error::LuceneErrorKind::IllegalArgument) if the field schemas differ.
  pub fn verify_same_schema(&self, other: &FieldInfo) -> Result<()> {
    let field_name = &self.name;

    Self::verify_same_index_options(field_name, &self.index_options, &other.index_options)?;

    if self.index_options != IndexOptions::None {
      Self::verify_same_omit_norms(field_name, self.omit_norms, other.omit_norms)?;
      Self::verify_same_store_term_vectors(
        field_name,
        self.store_term_vector.load(Relaxed),
        other.store_term_vector.load(Relaxed),
      )?;
    }

    Self::verify_same_doc_values_type(field_name, &self.doc_values_type, &other.doc_values_type)?;
    Self::verify_same_doc_values_skip_index(
      field_name,
      &self.doc_values_skip_index,
      &other.doc_values_skip_index,
    )?;
    Self::verify_same_points_options(
      field_name,
      self.get_point_dimension_count(),
      self.get_point_index_dimension_count(),
      self.get_point_num_bytes(),
      other.get_point_dimension_count(),
      other.get_point_index_dimension_count(),
      other.get_point_num_bytes(),
    )?;
    Self::verify_same_vector_options(
      field_name,
      self.vector_dimension,
      self.get_vector_encoding(),
      &self.vector_similarity_function,
      other.vector_dimension,
      other.get_vector_encoding(),
      &other.vector_similarity_function,
    )?;

    Ok(())
  }

  /// Verify that the provided index options are the same
  pub(crate) fn verify_same_index_options(
    field_name: &str,
    index_options1: &IndexOptions,
    index_options2: &IndexOptions,
  ) -> Result<()> {
    if index_options1 != index_options2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from index options={index_options1:?} to inconsistent index options={index_options2:?}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided docValues type are the same
  pub(crate) fn verify_same_doc_values_type(
    field_name: &str,
    doc_values_type1: &DocValuesType,
    doc_values_type2: &DocValuesType,
  ) -> Result<()> {
    if doc_values_type1 != doc_values_type2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from doc values type={doc_values_type1:?} to inconsistent doc values type={doc_values_type2:?}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided docValuesSkipIndex are the same
  pub(crate) fn verify_same_doc_values_skip_index(
    field_name: &str,
    skip_index1: &DocValuesSkipIndexType,
    skip_index2: &DocValuesSkipIndexType,
  ) -> Result<()> {
    if skip_index1 != skip_index2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from docValuesSkipIndexType={skip_index1:?} to inconsistent docValuesSkipIndexType={skip_index2:?}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided store term vectors options are the same
  pub(crate) fn verify_same_store_term_vectors(
    field_name: &str,
    store_term_vector1: bool,
    store_term_vector2: bool,
  ) -> Result<()> {
    if store_term_vector1 != store_term_vector2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from storeTermVector={store_term_vector1} to inconsistent storeTermVector={store_term_vector2}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided omitNorms are the same
  pub(crate) fn verify_same_omit_norms(
    field_name: &str,
    omit_norms1: bool,
    omit_norms2: bool,
  ) -> Result<()> {
    if omit_norms1 != omit_norms2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from omitNorms={omit_norms1} to inconsistent omitNorms={omit_norms2}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided points indexing options are the same
  pub(crate) fn verify_same_points_options(
    field_name: &str,
    point_dimension_count1: usize,
    index_dimension_count1: usize,
    num_bytes1: usize,
    point_dimension_count2: usize,
    index_dimension_count2: usize,
    num_bytes2: usize,
  ) -> Result<()> {
    if point_dimension_count1 != point_dimension_count2
      || index_dimension_count1 != index_dimension_count2
      || num_bytes1 != num_bytes2
    {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from points dimensionCount={point_dimension_count1}, indexDimensionCount={index_dimension_count1}, numBytes={num_bytes1} to inconsistent dimensionCount={point_dimension_count2}, indexDimensionCount={index_dimension_count2}, numBytes={num_bytes2}"
      )));
    }
    Ok(())
  }

  /// Verify that the provided vector indexing options are the same
  pub(crate) fn verify_same_vector_options(
    field_name: &str,
    vd1: i32,
    ve1: &VectorEncoding,
    vsf1: &VectorSimilarityFunction,
    vd2: i32,
    ve2: &VectorEncoding,
    vsf2: &VectorSimilarityFunction,
  ) -> Result<()> {
    if vd1 != vd2 || vsf1 != vsf2 || ve1 != ve2 {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change field \"{field_name}\" from vector dimension={vd1}, vector encoding={ve1:?}, vector similarity function={vsf1:?} to inconsistent vector dimension={vd2}, vector encoding={ve2:?}, vector similarity function={vsf2:?}"
      )));
    }
    Ok(())
  }

  /// Record that this field is indexed with points, with the specified number
  /// of dimensions and bytes per dimension.
  pub fn set_point_dimensions(
    &mut self,
    dimension_count: usize,
    index_dimension_count: usize,
    num_bytes: usize,
  ) -> Result<()> {
    if dimension_count == 0 {
      return Err(LuceneError::illegal_argument(format!(
        "point dimension count must be >= 0; got {} for field=\"{}\"",
        dimension_count, self.name
      )));
    }
    if index_dimension_count > MAX_INDEX_DIMENSIONS {
      return Err(LuceneError::illegal_argument(format!(
        "point index dimension count must be < MAX_INDEX_DIMENSIONS  (= {}); got {} for field=\"{}\"",
        MAX_INDEX_DIMENSIONS, index_dimension_count, self.name
      )));
    }
    if index_dimension_count > dimension_count {
      return Err(LuceneError::illegal_argument(format!(
        "point index dimension count must be <= point dimension count (= {}); got {} for field=\"{}\"",
        dimension_count, index_dimension_count, self.name
      )));
    }
    if num_bytes == 0 {
      return Err(LuceneError::illegal_argument(format!(
        "point numBytes must be >= 0; got {} for field=\"{}\"",
        num_bytes, self.name
      )));
    }
    if num_bytes > MAX_NUM_BYTES {
      return Err(LuceneError::illegal_argument(format!(
        "point numBytes must be <= MAX_NUM_BYTES  (= {}); got {} for field=\"{}\"",
        MAX_NUM_BYTES, num_bytes, self.name
      )));
    }
    if self.get_point_dimension_count() != 0 && self.get_point_dimension_count() != dimension_count
    {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change point dimension count from {} to {} for field=\"{}\"",
        self.get_point_dimension_count(),
        dimension_count,
        self.name
      )));
    }
    if self.get_point_index_dimension_count() != 0
      && self.get_point_index_dimension_count() != index_dimension_count
    {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change point index dimension count from {} to {} for field=\"{}\"",
        self.get_point_index_dimension_count(),
        index_dimension_count,
        self.name
      )));
    }
    if self.get_point_num_bytes() != 0 && self.get_point_num_bytes() != num_bytes {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change point numBytes from {} to {} for field=\"{}\"",
        self.get_point_num_bytes(),
        num_bytes,
        self.name
      )));
    }

    // The bounds checked above all fit in the compact representation.
    self.point_dimensions = [
      dimension_count as u8,
      index_dimension_count as u8,
      num_bytes as u8,
    ];
    self.wide_point_dimensions = None;

    self.check_consistency()
  }
  /// Return point data dimension count
  pub fn get_point_dimension_count(&self) -> usize {
    self
      .wide_point_dimensions
      .as_ref()
      .map_or(usize::from(self.point_dimensions[0]), |dimensions| {
        dimensions[0]
      })
  }

  /// Return point data dimension count
  pub fn get_point_index_dimension_count(&self) -> usize {
    self
      .wide_point_dimensions
      .as_ref()
      .map_or(usize::from(self.point_dimensions[1]), |dimensions| {
        dimensions[1]
      })
  }

  /// Return number of bytes per dimension
  pub fn get_point_num_bytes(&self) -> usize {
    self
      .wide_point_dimensions
      .as_ref()
      .map_or(usize::from(self.point_dimensions[2]), |dimensions| {
        dimensions[2]
      })
  }

  /// Returns the number of dimensions of the vector value
  pub fn get_vector_dimension(&self) -> i32 {
    self.vector_dimension
  }

  /// Returns the number of dimensions of the vector value
  pub fn get_vector_encoding(&self) -> &VectorEncoding {
    self
      .custom_vector_encoding
      .as_deref()
      .unwrap_or(if self.float_vector_encoding {
        &VectorEncoding::FLOAT32(4)
      } else {
        &VectorEncoding::BYTE(1)
      })
  }

  /// Returns the VectorSimilarityFunction for the field
  pub fn get_vector_similarity_function(&self) -> &VectorSimilarityFunction {
    &self.vector_similarity_function
  }

  /// Record that this field is indexed with docvalues, with the specified
  /// type
  pub fn set_doc_values_type(&mut self, doc_values_type: DocValuesType) -> Result<()> {
    if self.doc_values_type != DocValuesType::None
      && doc_values_type != DocValuesType::None
      && self.doc_values_type != doc_values_type
    {
      return Err(LuceneError::illegal_argument(format!(
        "cannot change DocValues type from {:?} to {:?} for field \"{}\"",
        self.doc_values_type, doc_values_type, self.name
      )));
    }
    self.doc_values_type = doc_values_type;
    self.check_consistency()?;
    Ok(())
  }

  /// Returns [`IndexOptions::None`] if the field is not indexed.
  pub fn get_index_options(&self) -> &IndexOptions {
    &self.index_options
  }

  /// Returns name of this field
  pub fn get_name(&self) -> &str {
    &self.name
  }

  /// Returns the field number
  pub fn get_field_number(&self) -> i32 {
    self.number
  }

  /// Returns [`DocValuesType::None`] if the field has no doc values.
  pub fn get_doc_values_type(&self) -> &DocValuesType {
    &self.doc_values_type
  }

  /// Returns true if this field has a skip index
  pub fn doc_values_skip_index_type(&self) -> &DocValuesSkipIndexType {
    &self.doc_values_skip_index
  }

  /// Sets the docValues generation of this field.
  pub fn set_doc_values_gen(&self, dv_gen: i64) -> Result<()> {
    self.dv_gen.store(dv_gen, Relaxed);
    self.check_consistency()?;
    Ok(())
  }

  /// Returns the docValues generation of this field, or -1 if no docValues
  /// updates exist for it.
  pub fn get_doc_values_gen(&self) -> i64 {
    self.dv_gen.load(Relaxed)
  }

  /// Set store term vectors
  pub fn set_store_term_vectors(&self) -> Result<()> {
    self.store_term_vector.store(true, Relaxed);
    self.check_consistency()?;
    Ok(())
  }

  /// Set store payloads
  pub fn set_store_payloads(&self) -> Result<()> {
    if self.index_options >= IndexOptions::DocsAndFreqsAndPositions {
      self.store_payloads.store(true, Relaxed);
    }
    self.check_consistency()?;
    Ok(())
  }

  /// Returns true if norms are explicitly omitted for this field
  pub fn omits_norms(&self) -> bool {
    self.omit_norms
  }

  /// Omit norms for this field.
  pub fn set_omits_norms(&mut self) -> Result<()> {
    if self.index_options == IndexOptions::None {
      return Err(LuceneError::illegal_state(
        "cannot omit norms: this field is not indexed".to_string(),
      ));
    }
    self.omit_norms = true;
    self.check_consistency()?;
    Ok(())
  }

  /// Returns true if this field actually has any norms.
  pub fn has_norms(&self) -> bool {
    self.index_options != IndexOptions::None && !self.omit_norms
  }

  /// Returns true if any payloads exist for this field.
  pub fn has_payloads(&self) -> bool {
    self.store_payloads.load(Relaxed)
  }

  /// Returns true if any term vectors exist for this field.
  #[inline]
  pub fn has_term_vectors(&self) -> bool {
    self.store_term_vector.load(Relaxed)
  }

  /// Returns whether any (numeric) vector values exist for this field
  pub fn has_vector_values(&self) -> bool {
    self.vector_dimension > 0
  }

  /// Get a codec attribute value, or None if it does not exist
  pub fn get_attribute(&self, key: &str) -> Option<String> {
    self.attributes.lock().get(key).cloned()
  }

  /// 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 [`Self::get_attribute`].
  ///
  /// If a value already exists for the key in the field, it will be replaced
  /// with the new value. If the value of the attributes for the same
  /// field is changed between documents, the behavior after merge is
  /// undefined.
  pub fn put_attribute<K, V>(&self, key: K, value: V) -> Option<String>
  where
    K: Into<String>,
    V: Into<String>,
  {
    let value = value.into();
    let key = key.into();
    let mut attributes = self.attributes.lock();
    Arc::make_mut(&mut attributes).insert(key, value)
  }

  pub(crate) fn put_borrowed_attribute(&self, key: &str, value: &str) {
    let mut attributes = self.attributes.lock();
    if attributes.get(key).is_some_and(|old| old == value) {
      return;
    }
    let attributes = Arc::make_mut(&mut attributes);
    if let Some(current) = attributes.get_mut(key) {
      current.clear();
      current.push_str(value);
    } else {
      attributes.insert(key.to_string(), value.to_string());
    }
  }

  /// Returns a shared snapshot of the internal codec attributes map.
  pub fn attributes(&self) -> Arc<HashMap<String, String>> {
    self.attributes.lock().clone()
  }

  /// Returns true if this field is configured and used as the soft-deletes
  /// field.
  pub fn is_soft_deletes_field(&self) -> bool {
    self.soft_deletes_field
  }

  /// Returns true if this field is configured and used as the parent document
  /// field.
  pub fn is_parent_field(&self) -> bool {
    self.is_parent_field
  }
}