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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/*
 * 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::binary_doc_values_field_updates::BinaryDocValuesFieldUpdates;
use crate::core::index::buffered_updates::BufferedUpdates;
use crate::core::index::buffered_updates_stream::SegmentState;
use crate::core::index::doc_values_field_updates::{
  DocValuesFieldUpdates, DocValuesFieldUpdatesBase, DocValuesFieldUpdatesEnum,
  SingleValueDocValuesFieldUpdates, SingleValueDocValuesFieldUpdatesBase,
};
use crate::core::index::field_term_iterator::FieldTermIterator;
use crate::core::index::field_updates_buffer::FieldUpdatesBuffer;
use crate::core::index::fields::Fields;
use crate::core::index::index_reader::IndexReader;
use crate::core::index::leaf_reader::LeafReader;
use crate::core::index::numeric_doc_values_field_updates::{
  NumericDocValuesFieldUpdates, SingleValueNumericDocValuesFieldUpdates,
};
use crate::core::index::postings_enum::NONE;
use crate::core::index::prefix_coded_terms::{PrefixCodedTerms, PrefixCodedTermsBuilder};
use crate::core::index::segment_infos::SegmentInfos;
use crate::core::index::sorter::DocMap;
use crate::core::index::terms::Terms;
use crate::core::index::terms_enum::{SeekStatus, TermsEnum};
use crate::core::index::{BytesRef, BytesRefValue};
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::query::Query;
use crate::core::search::score_mode::ScoreMode::CompleteNoScores;
use crate::core::store::directory::Directory;
use crate::core::util::access::ByteSource;
use crate::core::util::accountable::Accountable;
use crate::core::util::bits::Bits;
use crate::core::util::bytes_ref_iterator::BytesRefIterator;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::info_stream::{InfoStream, InfoStreamMT};
use crate::core::util::int_consumer::IntConsumer;
use crate::core::util::ram_usage_estimator::size_of_array_capacity;
use crate::core::util::{Counter, ToInt};
use parking_lot::lock_api::ReentrantMutexGuard;
use parking_lot::{RawMutex, RawThreadId, ReentrantMutex};
use std::collections::HashMap;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::sync::atomic::AtomicI64;
use std::sync::{
  Arc,
  atomic::{AtomicBool, Ordering},
};
use std::time::Instant;

// NOTE: we now apply this frozen packet immediately on creation, yet this
// process is heavy, and runs in multiple threads, and this compression
// is sizable (~8.3% of the original size), so it's important
// we run this before applying the deletes/updates.
/// Holds buffered deletes and updates by term or query, once pushed.
///
/// Pushed deletes/updates are write-once, so a more memory-efficient data structure is used
/// to store them. We don’t keep document IDs because they are applied on flush.
#[derive(Debug)]
pub(crate) struct FrozenBufferedUpdates {
  info_stream: InfoStreamMT,
  // Terms, in sorted order:
  pub delete_terms: PrefixCodedTerms,
  // Query owners and their matching docIDUpto limits.
  pub delete_queries: Vec<(Query, i32)>,
  // Counts down once all deletes/updates have been applied
  pub(crate) applied: AtomicBool,
  pub(crate) apply_lock: ReentrantMutex<()>,
  field_updates: HashMap<Box<str>, Box<FieldUpdatesBuffer>>,
  /// How many total documents were deleted/updated.
  pub(crate) total_del_count: AtomicI64,
  field_updates_count: i32,
  pub(crate) bytes_used: i32,
  // assigned by BufferedUpdatesStream once pushed
  del_gen: i64,
  // SegmentInfo ID in SegmentCommitInfo
  pub(crate) private_segment: Option<Arc<str>>,
}

impl FrozenBufferedUpdates {
  pub fn new(
    info_stream: InfoStreamMT,
    updates: &mut BufferedUpdates,
    private_segment: Option<Arc<str>>,
  ) -> Result<Self> {
    debug_assert!(
      private_segment.is_none() || updates.delete_terms.is_empty(),
      "segment private packet should only have del queries"
    );

    let mut builder = PrefixCodedTermsBuilder::new();
    updates
      .delete_terms
      .for_each_ordered(|field, bytes, _| builder.add(field, bytes))?;
    let delete_terms = builder.finish();

    let mut delete_queries = Vec::with_capacity(updates.delete_queries.len());
    for value in updates.field_updates.values_mut() {
      value.finish()?
    }
    let field_updates_count = updates.num_field_updates.load(Ordering::SeqCst);

    let bytes_used = delete_terms
      .ram_bytes_used()?
      .saturating_add(size_of_array_capacity::<Query>(delete_queries.capacity()))
      .saturating_add(size_of_array_capacity::<i32>(delete_queries.capacity()))
      .saturating_add(updates.field_updates_bytes_used.get()) as i32;
    if info_stream.is_enabled("BD") {
      let (segment_prefix, segment) = match private_segment.as_deref() {
        Some(id) => ("; private segment ", id),
        None => ("", "None"),
      };
      info_stream.message(
        "BD",
        &format!(
          "compressed {} to {} bytes ({:.2}%) for deletes/updates; private segment {segment_prefix}{segment}",
          updates.ram_bytes_used()?,
          bytes_used,
          100.0 * bytes_used as f64 / updates.ram_bytes_used()? as f64,
        ),
      )?;
    }
    // Retain the source maps if a fallible freeze operation fails.
    for (query, limit) in std::mem::take(&mut updates.delete_queries) {
      delete_queries.push((query, limit));
    }
    let field_updates = std::mem::take(&mut updates.field_updates);
    Ok(Self {
      info_stream,
      delete_terms,
      delete_queries,
      applied: AtomicBool::new(false),
      apply_lock: ReentrantMutex::new(()),
      field_updates,
      total_del_count: AtomicI64::new(0),
      bytes_used,
      field_updates_count,
      del_gen: -1,
      private_segment,
    })
  }
  /// Tries to lock this buffered update instance
  pub(crate) fn try_lock(&self) -> Option<ReentrantMutexGuard<'_, RawMutex, RawThreadId, ()>> {
    self.apply_lock.try_lock()
  }
  /// locks this buffered update instance
  pub(crate) fn lock(&self) -> ReentrantMutexGuard<'_, RawMutex, RawThreadId, ()> {
    self.apply_lock.lock()
  }

  /// Returns `true` if this buffered updates instance has already been
  /// applied.
  pub(crate) fn is_applied(&self) -> bool {
    debug_assert!(self.apply_lock.is_owned_by_current_thread());
    self.applied.load(Ordering::Relaxed)
  }
  /// Applies pending delete-by-term, delete-by-query and doc values updates to all segments in the index,
  /// Returning the number of new deleted or updated documents.
  pub(crate) fn apply<D>(
    &self,
    seg_states: &[SegmentState<D>],
    infos: &SegmentInfos<D>,
  ) -> Result<i64>
  where
    D: Directory + 'static,
  {
    debug_assert!(
      self.apply_lock.is_owned_by_current_thread(),
      "apply() must be called while holding apply_lock"
    );

    if self.del_gen == -1 {
      return Err(LuceneError::illegal_argument(
        "gen is not yet set; call BufferedUpdatesStream.push first",
      ));
    }

    debug_assert!(!self.applied.load(Ordering::Relaxed));

    if let Some(ref private_segment) = self.private_segment {
      debug_assert_eq!(
        seg_states.len(),
        1,
        "private packet must target exactly one segment"
      );
      let seg0_id = seg_states[0].reader.get_original_segment_info_id();
      debug_assert!(
        private_segment.as_ref() == seg0_id,
        "privateSegment={} vs seg0={}",
        private_segment,
        seg0_id
      );
    }

    self.total_del_count.fetch_add(
      self.apply_term_deletes(seg_states, infos)?,
      Ordering::Relaxed,
    );
    self.total_del_count.fetch_add(
      self.apply_query_deletes(seg_states, infos)?,
      Ordering::Relaxed,
    );
    self.total_del_count.fetch_add(
      self.apply_doc_values_updates_all(seg_states)?,
      Ordering::Relaxed,
    );
    Ok(self.total_del_count.load(Ordering::Relaxed))
  }
  pub(crate) fn apply_doc_values_updates_all<D>(
    &self,
    seg_states: &[SegmentState<D>],
  ) -> Result<i64>
  where
    D: Directory,
  {
    if self.field_updates.is_empty() {
      return Ok(0);
    }

    let start = Instant::now();
    let mut update_count: i64 = 0;

    for seg_state in seg_states {
      if self.del_gen() < seg_state.del_gen {
        // segment is newer than this deletes packet
        continue;
      }
      if seg_state.rld.ref_count() == 1 {
        // This means we are the only remaining reference to this segment, meaning
        // it was merged away while we were running, so we can safely skip running
        // because we will run on the newly merged segment next:
        continue;
      }

      let is_segment_private_deletes = self.private_segment.is_some();

      if !self.field_updates.is_empty() {
        update_count += Self::apply_doc_values_updates(
          seg_state,
          &self.field_updates,
          self.del_gen,
          is_segment_private_deletes,
        )?;
      }
    }

    if self.info_stream.is_enabled("BD") {
      let elapsed_ms = start.elapsed().as_secs_f64() * 1_000.0;
      self.info_stream.message(
        "BD",
        &format!(
          "applyDocValuesUpdates {:.1} msec for {} segments, {} field updates; {} new updates",
          elapsed_ms,
          seg_states.len(),
          self.field_updates_count,
          update_count
        ),
      )?;
    }

    Ok(update_count)
  }
  pub(crate) fn apply_doc_values_updates<D>(
    seg_state: &SegmentState<D>,
    updates: &HashMap<Box<str>, Box<FieldUpdatesBuffer>>,
    del_gen: i64,
    segment_private_deletes: bool,
  ) -> Result<i64>
  where
    D: Directory,
  {
    let mut update_count: i64 = 0;

    let mut resolved_updates = Vec::new();

    for (update_field, value) in updates.iter() {
      let is_numeric = value.is_numeric();
      let mut iterator = value.iterator()?;
      let mut term_docs_iterator = TermDocsIterator::new(
        TermsProviderImpl2::new(seg_state.reader.as_ref()),
        iterator.is_sorted_terms(),
      );
      let mut field_update = None;

      while let Some(buffered_update) = iterator.next_value()? {
        let term_value = buffered_update.term_value.ok_or_else(|| {
          LuceneError::illegal_state(format!(
            "buffered update for field [{}] has no term value",
            buffered_update.term_field
          ))
        })?;
        if let Some(doc_id_set_iterator) =
          term_docs_iterator.next_term(buffered_update.term_field, term_value)?
        {
          let limit = if del_gen == seg_state.del_gen {
            debug_assert!(segment_private_deletes);
            buffered_update.doc_upto
          } else {
            i32::MAX
          };

          let (long_value, binary_value) = if !buffered_update.has_value {
            (-1, None)
          } else {
            (
              buffered_update.numeric_value,
              buffered_update.binary_value.as_ref(),
            )
          };

          let update = if let Some(update) = &mut field_update {
            update
          } else {
            let max_doc = seg_state.reader.max_doc()?;
            let field = Arc::<str>::from(update_field.as_ref());
            let v = if is_numeric {
              if value.has_single_value() {
                let sub = SingleValueNumericDocValuesFieldUpdates::new(value.get_numeric_value(0)?);
                let sub_type = sub.sub_type();
                let sub = SingleValueDocValuesFieldUpdates::new(sub, max_doc, del_gen, sub_type)?;
                let sub_type = sub.sub_type();
                DocValuesFieldUpdates::new(max_doc, del_gen, field, sub_type, sub)?
              } else {
                let sub = NumericDocValuesFieldUpdates::with_range(
                  value.get_min_numeric(),
                  value.get_max_numeric(),
                )?;
                let sub_type = sub.sub_type();
                DocValuesFieldUpdates::new(max_doc, del_gen, field, sub_type, sub)?
              }
            } else {
              let sub = BinaryDocValuesFieldUpdates::new()?;
              let sub_type = sub.sub_type();
              DocValuesFieldUpdates::new(max_doc, del_gen, field, sub_type, sub)?
            };
            field_update.insert(v)
          };
          let count = if !buffered_update.has_value {
            let mut consumer = ResetDocValuesConsumer { update };
            Self::apply_update_docs(
              seg_state,
              doc_id_set_iterator,
              limit,
              segment_private_deletes,
              &mut consumer,
            )?
          } else if is_numeric {
            let mut consumer = NumericDocValuesConsumer { update, long_value };
            Self::apply_update_docs(
              seg_state,
              doc_id_set_iterator,
              limit,
              segment_private_deletes,
              &mut consumer,
            )?
          } else {
            let mut consumer = BinaryDocValuesConsumer {
              update,
              binary_value,
            };
            Self::apply_update_docs(
              seg_state,
              doc_id_set_iterator,
              limit,
              segment_private_deletes,
              &mut consumer,
            )?
          };
          update_count += count;
        }
      }
      if let Some(update) = field_update
        && update.any()
      {
        // Use the same allocation while staging and after publishing.
        resolved_updates.push(Arc::new(update));
      }
    }

    // Resolve all fields before finishing or publishing any packet.
    for mut update in resolved_updates {
      Arc::get_mut(&mut update)
        .ok_or_else(|| LuceneError::illegal_state("staged doc values update is shared"))?
        .finish()?;
      seg_state.rld.add_dv_update(update)?;
    }

    Ok(update_count)
  }
  fn apply_update_docs<D, P, C>(
    seg_state: &SegmentState<D>,
    doc_id_set_iterator: &mut P,
    limit: i32,
    segment_private_deletes: bool,
    doc_id_consumer: &mut C,
  ) -> Result<i64>
  where
    D: Directory,
    P: DocIdSetIterator,
    C: IntConsumer,
  {
    let mut update_count = 0;
    let accept_docs = seg_state.rld.get_live_docs();
    if let Some(sort_map) = seg_state.rld.sort_map.as_ref()
      && segment_private_deletes
    {
      // This segment was sorted on flush; we must apply seg-private deletes carefully in this case:
      loop {
        let doc = doc_id_set_iterator.next_doc()?;
        if doc == NO_MORE_DOCS {
          break;
        }

        let live = match accept_docs.as_ref() {
          None => true,
          Some(bits) => bits.get(doc as usize)?,
        };
        if live {
          // The limit is in the pre-sorted doc space:
          if sort_map.new_to_old(doc)? < limit {
            doc_id_consumer.accept(doc)?;
            update_count += 1;
          }
        }
      }
    } else {
      loop {
        let doc = doc_id_set_iterator.next_doc()?;
        if doc == NO_MORE_DOCS {
          break;
        }
        if doc >= limit {
          break; // no more docs that can be updated for this term
        }

        let live = match accept_docs.as_ref() {
          None => true,
          Some(bits) => bits.get(doc as usize)?,
        };
        if live {
          doc_id_consumer.accept(doc)?;
          update_count += 1;
        }
      }
    }
    Ok(update_count)
  }
  fn apply_query_deletes<D>(
    &self,
    seg_states: &[SegmentState<D>],
    infos: &SegmentInfos<D>,
  ) -> Result<i64>
  where
    D: Directory + 'static,
  {
    if self.delete_queries.is_empty() {
      return Ok(0);
    }

    let mut del_count: i64 = 0;
    for seg_state in seg_states.iter() {
      if self.del_gen < seg_state.del_gen {
        // segment is newer than this deletes packet
        continue;
      }

      if seg_state.rld.ref_count() == 1 {
        // This means we are the only remaining reference to this segment, meaning
        // it was merged away while we were running, so we can safely skip running
        // because we will run on the newly merged segment next:
        continue;
      }

      let info = infos
        .index_of(&seg_state.rld.info_id)
        .ok_or_else(|| LuceneError::illegal_state("segment is missing"))?;
      let mut reader_context = seg_state.reader.clone().get_context()?;

      for (query0, query_limit) in self.delete_queries.iter().cloned() {
        let limit = if self.del_gen == seg_state.del_gen {
          debug_assert!(self.private_segment.is_some());
          query_limit
        } else {
          i32::MAX
        };

        let mut searcher = IndexSearcher::new(reader_context)?;
        let query_result = (|| -> Result<()> {
          searcher.set_query_cache(None);
          let query = searcher.rewrite(query0)?;
          let weight = searcher.create_weight(query, CompleteNoScores, 1.0)?;
          let scorer = weight.scorer(&searcher.reader_context, &searcher)?;

          if let Some(mut scorer) = scorer {
            let it = &mut scorer;
            if let (Some(sort_map), true) = (seg_state.rld.sort_map.as_ref(), limit != i32::MAX) {
              debug_assert!(self.private_segment.is_some());

              loop {
                let doc_id = it.next_doc()?;
                if doc_id == NO_MORE_DOCS {
                  break;
                }
                if sort_map.new_to_old(doc_id)? < limit
                  && seg_state.rld.delete(doc_id, info, None)?
                {
                  del_count += 1;
                }
              }
            } else {
              let mut doc_id;
              loop {
                doc_id = it.next_doc()?;
                if doc_id >= limit {
                  break;
                }
                if seg_state.rld.delete(doc_id, info, None)? {
                  del_count += 1;
                }
              }
            }
          }
          Ok(())
        })();
        // Keep the same context identity for the next query and reclaim its owner
        // before propagating an error from query processing.
        reader_context = searcher.reader_context;
        query_result?;
      }
    }

    Ok(del_count)
  }
  fn apply_term_deletes<D>(
    &self,
    seg_states: &[SegmentState<D>],
    infos: &SegmentInfos<D>,
  ) -> Result<i64>
  where
    D: Directory,
  {
    if self.delete_terms.size() == 0 {
      return Ok(0);
    }

    debug_assert!(self.private_segment.is_none());

    let start = Instant::now();
    let mut del_count: i64 = 0;

    for seg_state in seg_states {
      debug_assert!(
        seg_state.del_gen != self.del_gen(),
        "segState.delGen={} vs this.gen={}",
        seg_state.del_gen,
        self.del_gen()
      );
      if seg_state.del_gen > self.del_gen() {
        continue;
      }

      if seg_state.rld.ref_count() == 1 {
        continue;
      }

      let info = infos
        .index_of(&seg_state.rld.info_id)
        .ok_or_else(|| LuceneError::illegal_state("not find in IndexWriter's SegmentInfos"))?;
      let mut iter = self.delete_terms.iterator()?;
      {
        let mut inner = seg_state.rld.inner.lock();
        let mut term_docs_iter =
          TermDocsIterator::new(TermsProviderImpl2::new(&seg_state.reader), true);
        while iter.set_next()? {
          if let Some(it) = term_docs_iter.next_term(iter.field(), &iter.builder.bytes_ref)? {
            loop {
              let doc_id = it.next_doc()?;
              if doc_id == NO_MORE_DOCS {
                break;
              }
              if seg_state.rld.delete(doc_id, info, Some(&mut inner))? {
                del_count += 1;
              }
            }
          }
        }
      }
    }

    if self.info_stream.is_enabled("BD") {
      let elapsed_ms = start.elapsed().as_secs_f64() * 1_000.0;
      self.info_stream.message(
        "BD",
        &format!(
          "applyTermDeletes took {:.2} msec for {} segments and {} del terms; {} new deletions",
          elapsed_ms,
          seg_states.len(),
          self.delete_terms.size(),
          del_count
        ),
      )?;
    }

    Ok(del_count)
  }
  pub fn set_del_gen(&mut self, del_gen: i64) {
    debug_assert!(
      self.del_gen == -1,
      "del_gen was already previously set to {}",
      self.del_gen
    );
    self.del_gen = del_gen;
    self.delete_terms.set_del_gen(del_gen);
  }

  pub fn del_gen(&self) -> i64 {
    debug_assert!(self.del_gen != -1);
    self.del_gen
  }
  pub(crate) fn any(&self) -> bool {
    self.delete_terms.size() > 0 || !self.delete_queries.is_empty() || self.field_updates_count > 0
  }
}

impl Display for FrozenBufferedUpdates {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "delGen={}", self.del_gen)?;
    if self.delete_terms.size() != 0 {
      write!(f, " unique deleteTerms={}", self.delete_terms.size())?;
    }
    if !self.delete_queries.is_empty() {
      write!(f, " numDeleteQueries={}", self.delete_queries.len())?;
    }
    if self.field_updates_count > 0 {
      write!(f, " fieldUpdates={}", self.field_updates_count)?;
    }
    if self.bytes_used != 0 {
      write!(f, " bytesUsed={}", self.bytes_used)?;
    }
    if let Some(ref seg) = self.private_segment {
      write!(f, " privateSegment={seg}")?;
    }

    Ok(())
  }
}
/// This struct helps iterating a term dictionary and consuming all the docs for each term.
/// It accepts a (field, value) tuple and returns a [`DocIdSetIterator`](DocIdSetIterator) if the field has an entry
/// for the given value.  
///
/// It has an optimized way of iterating the term dictionary if the terms are  
/// passed in sorted order and makes sure terms and postings are reused as much as possible.
pub(crate) struct TermDocsIterator<P>
where
  P: TermsProvider,
  <P as TermsProvider>::Terms:,
{
  provider: P,
  field: Option<String>,
  terms_enum: Option<<<P as TermsProvider>::Terms as Terms>::TermsEnum>,
  postings_enum: Option<Disi<P>>,
  sorted_terms: bool,
  #[cfg(debug_assertions)]
  last_term: Option<BytesRef<Vec<u8>>>, // only set with debug_assert
}

impl<P> TermDocsIterator<P>
where
  P: TermsProvider,
{
  pub(crate) fn new(provider: P, sorted_terms: bool) -> Self {
    TermDocsIterator {
      provider,
      field: None,
      terms_enum: None,
      postings_enum: None,
      sorted_terms,
      #[cfg(debug_assertions)]
      last_term: None,
    }
  }
  fn set_field(&mut self, field: &str) -> Result<()> {
    if self.field.as_deref() != Some(field) {
      field.clone_into(self.field.get_or_insert_default());

      match self.provider.terms(field)? {
        Some(terms) => {
          let mut terms_enum = terms.iterator()?;
          if self.sorted_terms {
            // need to reset otherwise we fail the assertSorted below since we sort per field
            #[cfg(debug_assertions)]
            {
              self.last_term = None;
            }
            if terms_enum.next()?.is_none() {
              self.terms_enum = None;
              return Ok(());
            }
          }
          self.terms_enum = Some(terms_enum);
        },
        _ => {
          self.terms_enum = None;
        },
      }
    }
    Ok(())
  }
  pub(crate) fn next_term<BS: ByteSource>(
    &mut self,
    field: &str,
    term: &BytesRef<BS>,
  ) -> Result<Option<&mut Disi<P>>> {
    self.set_field(field)?;

    if let Some(terms_enum) = self.terms_enum.as_mut() {
      if self.sorted_terms {
        #[cfg(debug_assertions)]
        Self::assert_sorted(self.sorted_terms, &mut self.last_term, term)?;
        // in the sorted case we can take advantage of the "seeking forward" property
        // this allows us depending on the term dict impl to reuse data-structures internally
        // which speed up iteration over terms and docs significantly.
        let cmp = {
          let reader_term = terms_enum.term()?;
          term.as_byte_slice().cmp(reader_term.as_bytes()).to_int()
        };

        return if cmp < 0 {
          Ok(None) // requested term does not exist in this segment
        } else if cmp == 0 {
          self.get_docs().map(Some)
        } else {
          match terms_enum.seek_ceil(term)? {
            SeekStatus::Found => self.get_docs().map(Some),
            SeekStatus::NotFound => Ok(None),
            SeekStatus::End => {
              self.terms_enum = None;
              Ok(None)
            },
          }
        };
      } else if terms_enum.seek_exact(term)? {
        return self.get_docs().map(Some);
      }
    }

    Ok(None)
  }
  #[cfg(debug_assertions)]
  fn assert_sorted<BS: ByteSource>(
    sorted_terms: bool,
    last_term: &mut Option<BytesRef<Vec<u8>>>,
    term: &BytesRef<BS>,
  ) -> Result<()> {
    debug_assert!(sorted_terms);
    if let Some(last) = last_term.as_ref() {
      debug_assert!(
        term.compare_to(last) != std::cmp::Ordering::Less,
        "boom: {term} last: {last}",
      );
    }
    *last_term = Some(BytesRef::from(term.as_byte_slice()));
    Ok(())
  }
  fn get_docs(&mut self) -> Result<&mut Disi<P>> {
    let terms_enum = self
      .terms_enum
      .as_mut()
      .ok_or_else(|| LuceneError::illegal_state("terms enum is missing"))?;
    let postings_enum = terms_enum.postings_with_flags(self.postings_enum.take(), NONE as i32)?;
    self.postings_enum = Some(postings_enum);
    self
      .postings_enum
      .as_mut()
      .ok_or_else(|| LuceneError::illegal_state("postings enum is missing"))
  }
}
type Disi<P> = <<<P as TermsProvider>::Terms as Terms>::TermsEnum as TermsEnum>::PostingsEnum;

pub(crate) trait TermsProvider {
  type Terms: Terms;
  fn terms(&self, field: &str) -> Result<Option<Self::Terms>>;
}

pub(crate) struct TermsProviderImpl1<'a, F> {
  pub(crate) fields: &'a F,
}
impl<'a, F> TermsProviderImpl1<'a, F> {
  pub(crate) fn new(fields: &'a F) -> Self {
    Self { fields }
  }
}
impl<F> TermsProvider for TermsProviderImpl1<'_, F>
where
  F: Fields,
{
  type Terms = F::Terms;

  fn terms(&self, field: &str) -> Result<Option<Self::Terms>> {
    self.fields.terms(field)
  }
}

pub struct TermsProviderImpl2<'a, L> {
  reader: &'a L,
}
impl<'a, L> TermsProviderImpl2<'a, L> {
  pub(crate) fn new(reader: &'a L) -> Self {
    Self { reader }
  }
}
impl<L> TermsProvider for TermsProviderImpl2<'_, L>
where
  L: LeafReader,
{
  type Terms = L::Terms;

  fn terms(&self, field: &str) -> Result<Option<Self::Terms>> {
    self.reader.terms(field)
  }
}

struct ResetDocValuesConsumer<'a> {
  update: &'a mut DocValuesFieldUpdatesEnum,
}
impl IntConsumer for ResetDocValuesConsumer<'_> {
  fn accept(&mut self, doc: i32) -> Result<()> {
    self.update.reset(doc)
  }
}

struct NumericDocValuesConsumer<'a> {
  update: &'a mut DocValuesFieldUpdatesEnum,
  long_value: i64,
}
impl IntConsumer for NumericDocValuesConsumer<'_> {
  fn accept(&mut self, doc: i32) -> Result<()> {
    self.update.add_value(doc, self.long_value)
  }
}

struct BinaryDocValuesConsumer<'a> {
  update: &'a mut DocValuesFieldUpdatesEnum,
  binary_value: Option<&'a std::borrow::Cow<'a, [u8]>>,
}
impl IntConsumer for BinaryDocValuesConsumer<'_> {
  fn accept(&mut self, doc: i32) -> Result<()> {
    let binary_value = self
      .binary_value
      .ok_or_else(|| LuceneError::illegal_state("binary doc values update has no value"))?;
    let value = BytesRef {
      bytes: binary_value.as_ref(),
      offset: 0,
      length: binary_value.len(),
    };
    self.update.add_byte_ref(doc, &value)
  }
}
impl AsRef<FrozenBufferedUpdates> for FrozenBufferedUpdates {
  fn as_ref(&self) -> &FrozenBufferedUpdates {
    self
  }
}

use crate::core::search::weight::Weight;