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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
 * 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_update::DocValuesUpdate;
use crate::core::index::documents_writer_delete_queue::{DocumentsWriterDeleteQueue, Node};
use crate::core::index::documents_writer_flush_control::DocumentsWriterFlushControl;
use crate::core::index::documents_writer_flush_queue::{DocumentsWriterFlushQueue, FlushTicket};
use crate::core::index::documents_writer_per_thread_pool::DwptWrapper;
use crate::core::index::index_writer::{IndexWriter, IndexingDocument, IntoFallibleIterator};
use crate::core::index::live_index_writer_config::LiveIndexWriterConfig;
use crate::core::index::lockable_concurrent_approximate_priority_queue::Lock;
use crate::core::index::segment_info::SegmentInfo;
use crate::core::index::term::Term;
use crate::core::search::query::Query;
use crate::core::search::similarities_impl::similarities::Similarity;
use crate::core::store::directory::Directory;
use crate::core::util::accountable::Accountable;
use crate::core::util::close::{Closeable, CloseableRef};
use crate::core::util::error::lucene_error::Result;
use crate::core::util::error::lucene_error::{CaughtResult, LuceneError};
use crate::core::util::info_stream::{InfoStream, InfoStreamMT};
use crate::core::util::io_utils::IOUtils;
use parking_lot::{Mutex, MutexGuard};
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicI64, Ordering};
use std::thread;

/// This struct accepts multiple added documents and directly writes segment files.
///
/// Each added document is passed to the indexing chain, which processes the document into
/// the different codec formats. Some formats write bytes to files immediately (e.g. stored fields
/// and term vectors), while others are buffered by the indexing chain and written only on flush.
///
/// Once we have used our allowed RAM buffer, or the number of added docs is large enough (in the
/// case we are flushing by doc count instead of RAM usage), we create a real segment and flush it to
/// the [`Directory`](crate::core::store::directory::Directory).
///
/// Threads:
///
/// Multiple threads may enter `add_document` at once. An initial locked call
/// to [`DocumentsWriterFlushControl::obtain_and_lock()`] which allocates a DWPT for this indexing
/// thread. The same thread will not necessarily get the same DWPT over time. Then `update_documents` is
/// called on that DWPT without holding the allocation lock (most of the heavy lifting is in this call). Once a
/// DWPT fills up enough RAM or holds enough documents in memory, the DWPT is checked out for flush and
/// all changes are written to the directory. Each DWPT corresponds to one segment being written.
///
/// When `flush` is called by [`IndexWriter`](crate::core::index::index_writer::IndexWriter), we check out all DWPTs associated with the
/// current [`DocumentsWriterDeleteQueue`] out of the [`DocumentsWriterPerThreadPool`] and
/// write them to disk. The flush process can piggyback on incoming indexing threads or even block
/// them from adding documents if flushing can’t keep up with new documents being added. Unless the
/// stall control kicks in to block indexing threads, flushes happen concurrently with indexing requests.
///
/// Errors:
///
/// Because this struct directly updates in-memory posting lists, and flushes stored fields and
/// term vectors directly to files in the directory, there are limited times when an
/// error can corrupt this state. For example, a disk full while flushing stored fields can leave
/// the file in a corrupt state. Or an OOM error while appending to the in-memory posting lists
/// can corrupt that posting list. We call such errors “aborting errors.” In these cases we
/// must call `abort()` to discard all docs added since the last flush.
///
/// All other errors (“non-aborting errors”) can still partially update the index
/// structures. These updates are consistent but represent only a part of the document seen up
/// until the error was hit. When this happens, we immediately mark the document as deleted so
/// that the document is always atomically (“all or none”) added to the index.
pub(crate) struct DocumentsWriter<D, FN>
where
  D: Directory,
{
  closed: AtomicBool,
  info_stream: InfoStreamMT,
  num_docs_in_ram: AtomicI32,
  ticket_queue: DocumentsWriterFlushQueue<D>,
  // we preserve changes during a full flush since IW might not check out before
  // we release all changes. NRT Readers otherwise suddenly return true from
  // isCurrent while there are actually changes currently committed. See also
  // `any_changes` and `flush_all_threads`.
  pending_changes_in_current_full_flush: AtomicBool,
  pending_num_docs: Arc<AtomicI64>,
  pub(crate) guard: Mutex<()>,
  pub(crate) inner: Mutex<Inner>,
  pub(crate) flush_control: DocumentsWriterFlushControl<D>,
  flush_notifications: FN,
}
pub(crate) struct Inner {
  current_full_flush_del_queue: Option<Arc<DocumentsWriterDeleteQueue>>,
}
impl<D, FN> DocumentsWriter<D, FN>
where
  D: Directory,
  FN: FlushNotifications,
{
  #[allow(clippy::too_many_arguments)]
  pub(crate) fn new<L>(
    flush_notifications: FN,
    pending_num_docs: Arc<AtomicI64>,
    config: &L,
  ) -> Result<Self>
  where
    L: LiveIndexWriterConfig,
  {
    let info_stream = config.get_info_stream();
    let delete_queue = Arc::new(DocumentsWriterDeleteQueue::new(info_stream.clone()));
    Ok(DocumentsWriter {
      closed: AtomicBool::new(false),
      info_stream,
      num_docs_in_ram: AtomicI32::new(0),
      ticket_queue: DocumentsWriterFlushQueue::new(),
      pending_changes_in_current_full_flush: AtomicBool::new(false),
      pending_num_docs,
      guard: Mutex::new(()),
      inner: Mutex::new(Inner {
        current_full_flush_del_queue: None,
      }),
      flush_control: DocumentsWriterFlushControl::new(config, delete_queue)?,
      flush_notifications,
    })
  }
  pub(crate) fn delete_queries<L>(&self, config: &L, queries: Vec<Query>) -> Result<i64>
  where
    L: LiveIndexWriterConfig,
  {
    self.apply_delete_or_update(config, |upd| upd.add_delete_query(queries))
  }

  pub(crate) fn delete_terms<L>(&self, config: &L, terms: Vec<Term>) -> Result<i64>
  where
    L: LiveIndexWriterConfig,
  {
    self.apply_delete_or_update(config, |upd| upd.add_delete_term(terms))
  }

  pub(crate) fn update_doc_values<L>(
    &self,
    config: &L,
    updates: Vec<DocValuesUpdate>,
  ) -> Result<i64>
  where
    L: LiveIndexWriterConfig,
  {
    self.apply_delete_or_update(config, |upd| upd.add_doc_values_updates(updates))
  }
  fn apply_delete_or_update<F, L>(&self, config: &L, func: F) -> Result<i64>
  where
    F: FnOnce(&DocumentsWriterDeleteQueue) -> Result<i64>,
    L: LiveIndexWriterConfig,
  {
    // Check the applyAllDeletes flag first. This helps exit early most of the time without checking
    // isFullFlush(), which takes a lock and introduces contention on small documents that are quick
    // to index.
    let _guard = &self.guard.lock();
    let mut seq_no = {
      let seq_no = {
        let delete_queue = &self.flush_control.delete_queue.lock();
        func(delete_queue)?
      };
      self.flush_control.do_on_delete(config)?;
      seq_no
    };
    if self.apply_all_deletes()? {
      seq_no = -seq_no;
    }
    Ok(seq_no)
  }
  /// If buffered deletes are using too much heap, resolve them and write disk and return true.
  fn apply_all_deletes(&self) -> Result<bool> {
    let delete_queue = self.flush_control.delete_queue.lock().clone();

    // Check the applyAllDeletes flag first. This helps exit early most of the time without checking
    // isFullFlush(), which takes a lock and introduces contention on small documents that are quick
    // to index.
    let should_apply_all_deletes = self.flush_control.get_apply_all_deletes()
            && !self.flush_control.is_full_flush()
            // never apply deletes during full flush this breaks happens before relationship.
            && delete_queue.is_open()
            // if it's closed then it's already fully applied and we have a new delete queue
            && self.flush_control.get_and_reset_apply_all_deletes();
    if !should_apply_all_deletes {
      return Ok(false);
    }

    let Some(_) = self
      .ticket_queue
      .add_ticket(|| Self::maybe_freeze_global_buffer(&delete_queue))?
    else {
      return Ok(false);
    };
    self.flush_notifications.on_deletes_applied()?; // apply deletes event forces a purge
    Ok(true)
  }

  fn maybe_freeze_global_buffer(
    delete_queue: &DocumentsWriterDeleteQueue,
  ) -> Result<Option<FlushTicket<D>>> {
    match delete_queue.maybe_freeze_global_buffer()? {
      Some(frozen_updates) => Ok(Some(FlushTicket::new(Some(frozen_updates), false))),
      None => Ok(None),
    }
  }
  pub(crate) fn purge_flush_tickets<F>(&self, forced: bool, consumer: F) -> Result<()>
  where
    F: Fn(FlushTicket<D>) -> Result<()>,
  {
    if forced {
      self.ticket_queue.force_purge(consumer)
    } else {
      self.ticket_queue.try_purge(consumer)
    }
  }
  /// Returns how many docs are currently buffered in RAM.
  pub(crate) fn get_num_docs(&self) -> i32 {
    self.num_docs_in_ram.load(Ordering::SeqCst)
  }

  fn ensure_open(&self) -> Result<()> {
    if self.closed.load(Ordering::SeqCst) {
      Err(LuceneError::already_closed(
        "this DocumentsWriter is closed",
      ))
    } else {
      Ok(())
    }
  }
  pub(crate) fn flush_one_dwpt(&self, writer: &IndexWriter<D>) -> Result<bool> {
    {
      if self.info_stream.is_enabled("DW") {
        self.info_stream.message("DW", "startFlushOneDWPT")?;
      }
    }

    if !self.maybe_flush(writer)? {
      if let Some(dwpt) = self
        .flush_control
        .checkout_largest_non_pending_writer(&writer.config)?
      {
        self.do_flush(dwpt, writer)?;
        return Ok(true);
      }
      return Ok(false);
    }
    Ok(true)
  }

  /// Locks all currently active DWPT and aborts them.
  /// The returned Closeable should be closed once the locks for the aborted DWPTs can be released.
  pub(crate) fn lock_and_abort_all<L>(&self, config: &L) -> Result<LockAndAbortAllGuard<'_, D, FN>>
  where
    L: LiveIndexWriterConfig,
  {
    let _guard = self.guard.lock();
    if self.info_stream.is_enabled("DW") {
      self.info_stream.message("DW", "lockAndAbortAll")?;
    }
    // Make sure we move all pending tickets into the flush queue:
    self.ticket_queue.force_purge(|ticket| {
      if let Some(flushed_segment) = ticket.get_flushed_segment() {
        self.pending_num_docs.fetch_add(
          -(flushed_segment.segment_info.info.max_doc()? as i64),
          Ordering::SeqCst,
        );
      }
      Ok(())
    })?;

    let mut finalizer = LockAndAbortAllGuard::new(self);

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      self.flush_control.delete_queue.lock().clear();
      self.flush_control.per_thread_pool.lock_new_writers();
      finalizer.writers = self
        .flush_control
        .per_thread_pool
        .filter_and_lock(|_| true)?;
      for per_thread in &finalizer.writers {
        debug_assert!(per_thread.state.is_held_by_current_thread());
        self.abort_documents_writer_per_thread(per_thread, config)?;
      }
      self.flush_control.delete_queue.lock().clear();

      // jump over any possible in flight ops:
      self
        .flush_control
        .delete_queue
        .lock()
        .skip_sequence_numbers(self.flush_control.per_thread_pool.size() as i64 + 1);

      self
        .flush_control
        .abort_pending_flushes(None, config, self)?;
      self.flush_control.wait_for_flush();
      if self.info_stream.is_enabled("DW") {
        self
          .info_stream
          .message("DW", "finished lockAndAbortAll success=true")?;
      }
      Ok(())
    }));

    match result {
      Ok(Ok(())) => Ok(finalizer),
      result => {
        if self.info_stream.is_enabled("DW") {
          self
            .info_stream
            .message("DW", "finished lockAndAbortAll success=false")?;
        }
        let close_result =
          std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| finalizer.close()));
        IOUtils::use_or_suppress_caught_result(result, close_result)?;
        Err(LuceneError::illegal_state(
          "lockAndAbortAll unexpectedly succeeded after an error",
        ))
      },
    }
  }
  /// Returns how many documents were aborted.
  fn abort_documents_writer_per_thread<L>(
    &self,
    per_thread: &Arc<DwptWrapper<D>>,
    config: &L,
  ) -> Result<()>
  where
    L: LiveIndexWriterConfig,
  {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      let num = per_thread.state.get_num_docs_in_ram();
      self.subtract_flushed_num_docs(num);
      per_thread.dwpt.lock().abort()?;
      Ok(())
    }));
    self.flush_control.do_on_abort(per_thread, config)?;
    unwrap_caught_result!(result)
  }
  /// returns the maximum sequence number for all previously completed operations
  pub(crate) fn get_max_completed_sequence_number(&self) -> i64 {
    self
      .flush_control
      .delete_queue
      .lock()
      .get_max_completed_seq_no()
  }
  pub(crate) fn any_changes(&self) -> Result<bool> {
    // changes are either in a DWPT or in the deleteQueue.
    // yet if we currently flush deletes and / or dwpt there
    // could be a window where all changes are in the ticket queue
    // before they are published to the IW. ie we need to check if the
    // ticket queue has any tickets.
    let num_docs = self.num_docs_in_ram.load(Ordering::SeqCst);
    let deletions = self.any_deletions();
    let tickets = self.ticket_queue.has_tickets();
    let pending_full = self
      .pending_changes_in_current_full_flush
      .load(Ordering::SeqCst);

    let any = num_docs != 0 || deletions || tickets || pending_full;

    if self.info_stream.is_enabled("DW") && any {
      self.info_stream.message(
                "DW",
                &format!(
                    "anyChanges? numDocsInRam={num_docs} deletes={deletions} hasTickets={tickets} pendingChangesInFullFlush={pending_full}"
                ),
            )?;
    }

    Ok(any)
  }
  #[cfg(test)]
  pub(crate) fn get_buffered_delete_terms_size(&self) -> Result<i32> {
    self
      .flush_control
      .delete_queue
      .lock()
      .get_buffered_updates_terms_size()
  }
  pub(crate) fn any_deletions(&self) -> bool {
    self.flush_control.delete_queue.lock().any_changes(None)
  }
  /// Called if we hit an error at a bad time (when updating the index files) and must discard
  /// all currently buffered docs. This resets our state, discarding any docs added since last flush.
  pub(crate) fn abort<L>(&self, config: &L) -> Result<()>
  where
    L: LiveIndexWriterConfig,
  {
    let _guard = self.guard.lock();

    let mut success = false;

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      self.flush_control.delete_queue.lock().clear();

      if self.info_stream.is_enabled("DW") {
        self.info_stream.message("DW", "abort")?;
      }

      for per_thread in self
        .flush_control
        .per_thread_pool
        .filter_and_lock(|_| true)?
      {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
          self.abort_documents_writer_per_thread(&per_thread, config)
        }));
        per_thread.unlock()?;
        unwrap_caught_result!(result)?;
      }

      self
        .flush_control
        .abort_pending_flushes(None, config, self)?;
      self.flush_control.wait_for_flush();

      debug_assert_eq!(
        0,
        self.flush_control.per_thread_pool.size(),
        "There are still active DWPT in the pool: {}",
        self.flush_control.per_thread_pool.size()
      );

      success = true;
      Ok(())
    }));

    if success {
      debug_assert_eq!(
        0,
        self.flush_control.get_flushing_bytes(None),
        "flushingBytes has unexpected value 0 != {}",
        self.flush_control.get_flushing_bytes(None)
      );
      debug_assert_eq!(
        0,
        self.flush_control.net_bytes(None),
        "netBytes has unexpected value 0 != {}",
        self.flush_control.net_bytes(None)
      );
    }

    if self.info_stream.is_enabled("DW") {
      self
        .info_stream
        .message("DW", &format!("done abort success={success}"))?;
    }

    unwrap_caught_result!(result)
  }
  fn pre_update(&self, writer: &IndexWriter<D>) -> Result<bool> {
    self.ensure_open()?;
    let mut has_events = false;

    while self.flush_control.any_stalled_threads()
      || (writer.config.get_check_pending_flush_on_update()
        && self.flush_control.num_queued_flushes() > 0)
    {
      // Help out flushing any queued DWPTs so we can un-stall:
      // Try pickup pending threads here if possible
      // no need to loop over the next pending flushes... doFlush will take care of this
      has_events |= self.maybe_flush(writer)?;
      self.flush_control.wait_if_stalled();
    }

    Ok(has_events)
  }

  fn post_update(
    &self,
    flushing_dwpt: Option<Arc<DwptWrapper<D>>>,
    mut has_events: bool,
    writer: &IndexWriter<D>,
  ) -> Result<bool> {
    has_events |= self.apply_all_deletes()?;
    if let Some(dwpt) = flushing_dwpt {
      self.do_flush(dwpt, writer)?;
      has_events = true;
    } else if writer.config.get_check_pending_flush_on_update() {
      has_events |= self.maybe_flush(writer)?;
    }
    Ok(has_events)
  }
  pub(crate) fn update_documents<DI, S>(
    &self,
    docs: DI,
    del_node: Option<Arc<Node>>,
    writer: &IndexWriter<D>,
    similarity: &S,
  ) -> Result<i64>
  where
    S: Similarity,
    DI: IntoFallibleIterator<Item: IndexingDocument>,
  {
    let has_events = self.pre_update(writer)?;

    let dwpt_wrapper = self.flush_control.obtain_and_lock(writer)?;
    let mut flushing_dwpt_opt = None;
    let mut seq_no = 0;
    let config = &writer.config;
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      // This must happen after we've pulled the DWPT because IW.close
      // waits for all DWPT to be released:
      self.ensure_open()?;
      let mut dw = Some(dwpt_wrapper.dwpt.lock());
      let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let dw = dw
          .as_mut()
          .ok_or_else(|| LuceneError::illegal_state("DWPT lock is missing"))?;
        dw.update_documents(
          docs,
          del_node,
          &self.flush_notifications,
          &self.num_docs_in_ram,
          writer,
          similarity,
        )
      }));
      let aborted = dwpt_wrapper.state.is_aborted();
      if aborted || !matches!(&res, Ok(Ok(_))) {
        dw.take();
      }
      if aborted {
        self.flush_control.do_on_abort(&dwpt_wrapper, config)?;
      }
      seq_no = unwrap_caught_result!(res)?;
      flushing_dwpt_opt = {
        match dw.as_ref() {
          Some(dw) => self.flush_control.do_after_document(dw, config)?,
          None => self
            .flush_control
            .do_after_document(&dwpt_wrapper.dwpt.lock(), config)?,
        }
      };
      Ok(())
    }));

    {
      let inner = self.flush_control.inner.lock();
      if dwpt_wrapper.state.is_flush_pending()
        || dwpt_wrapper.state.is_aborted()
        || dwpt_wrapper.state.delete_queue.is_advanced()
      {
        dwpt_wrapper.state.unlock()?;
      } else {
        self
          .flush_control
          .per_thread_pool
          .mark_as_free_and_unlock(dwpt_wrapper)?;
      }
      drop(inner);
    }

    unwrap_caught_result!(result)?;
    if self.post_update(flushing_dwpt_opt, has_events, writer)? {
      seq_no = -seq_no;
    }
    Ok(seq_no)
  }

  fn maybe_flush(&self, writer: &IndexWriter<D>) -> Result<bool> {
    let flushing_dwpt = self
      .flush_control
      .next_pending_flush(None, &writer.config)?;

    if let Some(flushing_dwpt) = flushing_dwpt {
      self.do_flush(flushing_dwpt, writer)?;
      Ok(true)
    } else {
      Ok(false)
    }
  }
  fn do_flush(
    &self,
    mut flushing_dwpt: Arc<DwptWrapper<D>>,
    writer: &IndexWriter<D>,
  ) -> Result<()> {
    loop {
      debug_assert!(!flushing_dwpt.state.has_flushed());

      let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
        debug_assert!({
          let current_full_flush_del_queue = &self.inner.lock().current_full_flush_del_queue;
          match current_full_flush_del_queue.as_ref() {
            Some(queue) => Arc::ptr_eq(&flushing_dwpt.state.delete_queue, queue),
            None => true,
          }
        });

        // Since with DWPT the flush process is concurrent and several DWPT
        // could flush at the same time we must maintain the order of the
        // flushes before we can apply the flushed segment and the frozen global
        // deletes it is buffering. The reason for this is that the global
        // deletes mark a certain point in time where we took a DWPT out of
        // rotation and freeze the global deletes.
        //
        // Example: A flush 'A' starts and freezes the global deletes, then
        // flush 'B' starts and freezes all deletes occurred since 'A' has
        // started. if 'B' finishes before 'A' we need to wait until 'A' is done
        // otherwise the deletes frozen by 'B' are not applied to 'A' and we
        // might miss to deletes documents in 'A'.
        let mut has_ticket = None;
        let mut success = false;
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
          debug_assert!(self.assert_ticket_queue_modification(&flushing_dwpt.state.delete_queue));
          let ticket = {
            let mut dwpt = flushing_dwpt.dwpt.lock();
            self.ticket_queue.add_ticket(|| {
              let frozen_buffered_updates = dwpt.prepare_flush()?;
              Ok(Some(FlushTicket::new(frozen_buffered_updates, true)))
            })?
          };
          let ticket = ticket.ok_or_else(|| LuceneError::illegal_state("ticket returned None"))?;
          let ticket = has_ticket.insert(ticket);
          let flushing_docs_in_ram = flushing_dwpt.state.get_num_docs_in_ram();
          {
            let mut dwpt = flushing_dwpt.dwpt.lock();
            let mut dwpt_success = false;
            let result =
              std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
                let v = dwpt.flush(&self.flush_notifications, writer)?;
                match v {
                  Some(new_segment) => {
                    self.ticket_queue.add_segment(ticket, new_segment)?;
                    dwpt_success = true;
                    Ok(())
                  },
                  None => Err(LuceneError::illegal_state("flush_segment returned None")),
                }
              }));
            self.subtract_flushed_num_docs(flushing_docs_in_ram);
            let files = dwpt.take_pending_files_to_delete();
            if !files.is_empty() {
              self.flush_notifications.delete_unused_files(files)?;
            }
            if !dwpt_success {
              let dir = dwpt.segment_info.dir.clone();
              self.flush_notifications.flush_failed(std::mem::replace(
                &mut dwpt.segment_info,
                SegmentInfo::dummy(dir, Arc::from("")),
              ))?
            }
            unwrap_caught_result!(result)
          }?;
          success = true;
          Ok(())
        }));
        if !success && let Some(ticket_idx) = has_ticket {
          // In the case of a failure make sure we are making progress and
          // apply all the deletes since the segment flush failed since the flush
          // The ticket could hold global deletes; see `FlushTicket::can_publish`.
          self.ticket_queue.mark_ticket_failed(ticket_idx)?;
        }
        unwrap_caught_result!(result)?;
        //Now we are done and try to flush the ticket queue if the head of the
        // queue has already finished the flush.
        if self.ticket_queue.get_ticket_count() as usize
          >= self.flush_control.per_thread_pool.size()
        {
          // This means there is a backlog: the one
          // thread in innerPurge can't keep up with all
          // other threads flushing segments.  In this case
          // we forcefully stall the producers.
          self.flush_notifications.on_ticket_backlog()?;
        }
        Ok(())
      }));
      let config = &writer.config;
      self
        .flush_control
        .do_after_flush(None, flushing_dwpt, config)?;
      unwrap_caught_result!(res)?;
      let v = self.flush_control.next_pending_flush(None, config)?;

      match v {
        Some(next_dwpt) => {
          flushing_dwpt = next_dwpt;
          continue;
        },
        None => break,
      }
    }
    self.flush_notifications.after_segments_flushed(writer)?;
    Ok(())
  }
  pub(crate) fn get_next_sequence_number(&self) -> i64 {
    // The delete queue mutex serializes allocation with reset_delete_queue.
    // A caller holding the IndexWriter lock must not wait for a full flush
    // that is waiting for a DWPT to finish registering with that same writer.
    self
      .flush_control
      .delete_queue
      .lock()
      .get_next_sequence_number()
  }

  pub(crate) fn reset_delete_queue(
    &self,
    _guard: &MutexGuard<'_, ()>,
    max_num_pending_ops: i64,
  ) -> Result<i64> {
    let mut delete_queue = self.flush_control.delete_queue.lock();
    let new_queue = delete_queue.advance_queue(max_num_pending_ops)?;
    debug_assert!(delete_queue.is_advanced());
    debug_assert!(!new_queue.is_advanced());
    debug_assert!(delete_queue.get_last_sequence_number() <= new_queue.get_last_sequence_number());
    debug_assert!(
      delete_queue.get_max_seq_no() <= new_queue.get_last_sequence_number(),
      "max_seq_no: {} vs. {}",
      delete_queue.get_max_seq_no(),
      new_queue.get_last_sequence_number()
    );
    let old_max_seq_no = delete_queue.get_max_seq_no();
    *delete_queue = Arc::new(new_queue);
    Ok(old_max_seq_no)
  }

  pub(crate) fn subtract_flushed_num_docs(&self, num_flushed: i32) {
    self
      .num_docs_in_ram
      .fetch_sub(num_flushed, Ordering::SeqCst);
    debug_assert!(self.num_docs_in_ram.load(Ordering::SeqCst) >= 0);
  }

  fn set_flushing_delete_queue(
    &self,
    session: Option<Arc<DocumentsWriterDeleteQueue>>,
    guard: Option<&mut MutexGuard<'_, ()>>,
  ) -> bool {
    let _guard = match guard {
      Some(v) => v,
      None => &self.guard.lock(),
    };
    let mut inner = self.inner.lock();
    debug_assert!(
      inner
        .current_full_flush_del_queue
        .as_ref()
        .is_none_or(|q| !q.is_open()),
      "Can not replace a full flush queue if the queue is not closed"
    );
    inner.current_full_flush_del_queue = session;
    true
  }
  fn assert_ticket_queue_modification(
    &self,
    delete_queue: &Arc<DocumentsWriterDeleteQueue>,
  ) -> bool {
    let inner = self.inner.lock();
    debug_assert!(
      inner
        .current_full_flush_del_queue
        .as_ref()
        .is_none_or(|q| Arc::ptr_eq(q, delete_queue)),
      "only modifications from the current flushing queue are permitted while doing a full flush"
    );
    true
  }

  // FlushAllThreads is synced by IW fullFlushLock. Flushing all threads is a
  // two stage operation; the caller must ensure (in try/finally) that finishFlush
  // is called after this method, to release the flush lock in DWFlushControl
  pub(crate) fn flush_all_threads<L>(&self, writer: &IndexWriter<D>, config: &L) -> Result<i64>
  where
    L: LiveIndexWriterConfig,
  {
    if self.info_stream.is_enabled("DW") {
      self.info_stream.message("DW", "startFullFlush")?;
    }
    let (flushing_delete_queue, seq_no) = {
      let mut guard = self.guard.lock();
      let pending = self.any_changes()?;
      self
        .pending_changes_in_current_full_flush
        .store(pending, Ordering::SeqCst);
      let flushing_delete_queue = self.flush_control.delete_queue.lock().clone();
      // Cutover to a new delete queue.  This must be synced on the flush control
      // otherwise a new DWPT could sneak into the loop with an already flushing
      // delete queue
      let sn = self
        .flush_control
        .mark_for_full_flush(self, &guard, config)?;
      debug_assert!(
        self.set_flushing_delete_queue(Some(Arc::clone(&flushing_delete_queue)), Some(&mut guard))
      );
      (flushing_delete_queue, sn)
    };
    debug_assert!({
      let inner = self.inner.lock();
      let current_full_flush_del_queue = &inner.current_full_flush_del_queue;
      current_full_flush_del_queue
        .as_ref()
        .is_some_and(|current| !Arc::ptr_eq(&self.flush_control.delete_queue.lock(), current))
    });

    let mut anything_flushed = false;
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      anything_flushed |= self.maybe_flush(writer)?;
      // If a concurrent flush is still in flight wait for it
      self.flush_control.wait_for_flush();
      if !anything_flushed && flushing_delete_queue.any_changes(None) {
        {
          if self.info_stream.is_enabled("DW") {
            let v = thread::current();
            let name = v.name().unwrap_or("<unnamed>");
            self
              .info_stream
              .message("DW", &format!("{name}: flush naked frozen global deletes"))?;
          }
        }

        debug_assert!(self.assert_ticket_queue_modification(&flushing_delete_queue));
        self
          .ticket_queue
          .add_ticket(|| Self::maybe_freeze_global_buffer(&flushing_delete_queue))?;
      }
      // we can't assert that we don't have any tickets in the queue since we might add a
      // DocumentsWriterDeleteQueue
      // concurrently if we have very small ram buffers this happens quite frequently
      debug_assert!(!flushing_delete_queue.any_changes(None));
      Ok(())
    }));
    debug_assert!({
      let inner = self.inner.lock();
      match inner.current_full_flush_del_queue.as_ref() {
        Some(queue) => Arc::ptr_eq(&flushing_delete_queue, queue),
        None => false,
      }
    });
    // all DWPT have been processed and this queue has been fully flushed to the
    // ticket-queue
    flushing_delete_queue.close()?;
    unwrap_caught_result!(result)?;
    Ok(if anything_flushed { -seq_no } else { seq_no })
  }
  pub(crate) fn finish_full_flush<L>(&self, success: bool, config: &L) -> Result<()>
  where
    L: LiveIndexWriterConfig,
  {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
      if self.info_stream.is_enabled("DW") {
        let current_thread = thread::current();
        let thread_name = current_thread.name().unwrap_or("<unnamed>");
        self.info_stream.message(
          "DW",
          &format!("{thread_name} finishFullFlush success={success}"),
        )?;
      }
      debug_assert!(self.set_flushing_delete_queue(None, None));
      if success {
        self.flush_control.finish_full_flush(config)?;
      } else {
        self.flush_control.abort_full_flushes(self, config)?;
      }
      Ok(())
    }));
    self
      .pending_changes_in_current_full_flush
      .store(false, Ordering::SeqCst);
    // make sure we do execute this since we block applying deletes during full
    // flush
    self.apply_all_deletes()?;

    unwrap_caught_result!(result)
  }

  /// Returns the number of bytes currently being flushed
  /// This is a subset of the value returned by ramBytesUsed()
  pub(crate) fn get_flushing_bytes(&self) -> i64 {
    self.flush_control.get_flushing_bytes(None)
  }
}
impl<D, FN> Accountable for DocumentsWriter<D, FN>
where
  D: Directory,
{
  fn ram_bytes_used(&self) -> Result<i64> {
    Ok(self.flush_control.get_delete_bytes_used()? + self.flush_control.net_bytes(None))
  }
}

impl<D, FN> CloseableRef for DocumentsWriter<D, FN>
where
  D: Directory,
  FN: FlushNotifications,
{
  fn close(&self) -> Result<()> {
    self.closed.store(true, Ordering::SeqCst);
    IOUtils::close_with(0..2, |operation| match operation {
      0 => {
        self.flush_control.close();
        Ok(())
      },
      _ => self.flush_control.per_thread_pool.close(),
    })
  }
}

pub(crate) struct LockAndAbortAllGuard<'a, D, FN>
where
  D: Directory,
  FN: FlushNotifications,
{
  documents_writer: &'a DocumentsWriter<D, FN>,
  writers: smallvec::SmallVec<[Arc<DwptWrapper<D>>; 4]>,
  released: AtomicBool,
}

impl<'a, D, FN> LockAndAbortAllGuard<'a, D, FN>
where
  D: Directory,
  FN: FlushNotifications,
{
  fn new(documents_writer: &'a DocumentsWriter<D, FN>) -> Self {
    Self {
      documents_writer,
      writers: smallvec::SmallVec::new(),
      released: AtomicBool::new(false),
    }
  }
}

impl<D, FN> Closeable for LockAndAbortAllGuard<'_, D, FN>
where
  D: Directory,
  FN: FlushNotifications,
{
  fn close(&mut self) -> Result<()> {
    if self
      .released
      .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
      .is_ok()
    {
      self
        .documents_writer
        .flush_control
        .per_thread_pool
        .unlock_new_writers();
      for writer in &self.writers {
        writer.unlock()?;
      }
      if self.documents_writer.info_stream.is_enabled("DW") {
        self
          .documents_writer
          .info_stream
          .message("DW", "unlockAllAbortedThread")?;
      }
    }
    Ok(())
  }
}

pub(crate) trait FlushNotifications {
  /// Called when files were written to disk that are not used anymore.
  /// It's the implementation's responsibility to clean these files up.
  fn delete_unused_files(&self, files: HashSet<String>) -> Result<()>;

  /// Called when a segment failed to flush.
  fn flush_failed<D>(&self, info: SegmentInfo<D>) -> Result<()>;

  /// Called after one or more segments were flushed to disk.
  fn after_segments_flushed<D>(&self, writer: &IndexWriter<D>) -> Result<()>
  where
    D: Directory;

  /// Should be called if a flush or an indexing operation caused
  /// a tragic / unrecoverable event.
  fn on_tragic_event<D>(
    &self,
    event: &CaughtResult,
    message: &str,
    writer: &IndexWriter<D>,
  ) -> Result<()>
  where
    D: Directory;

  /// Called once deletes have been applied either after a flush or on a deletes call.
  fn on_deletes_applied(&self) -> Result<()>;

  /// Called once the DocumentsWriter ticket queue has a backlog. This means there is an inner
  /// thread that tries to publish flushed segments but can't keep up with the other threads
  /// flushing new segments. This likely requires other thread to forcefully purge the buffer to
  /// help publishing. This can't be done in-place since we might hold index writer locks when this
  /// is called. The caller must ensure that the purge happens without an index writer lock being
  /// held.
  fn on_ticket_backlog(&self) -> Result<()>;
}