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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
/*
 * 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::store::byte_buffers_index_input::ByteBuffersIndexInputOwned;
use crate::core::store::data_input_ext::DataInputExt;
use crate::core::store::memory_segment_index_input::{
  MappedFloatVectors, MemorySegmentIndexInput, MemorySegmentRandomAccessInput,
};
use crate::core::store::nio_fs_directory::NIOFSIndexInput;
use crate::core::store::random_access_input::{
  BoxRandomAccessInput, RandomAccessInput, RandomAccessInputEnum2, RandomAccessInputEnum3,
};
use crate::core::store::{BufferedIndexInput, DataInput, ReadAdvice};
use crate::core::util::TryIntoInt;
use crate::core::util::clone::TryClone;
use crate::core::util::close::CloseableRef;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use std::sync::Arc;

/// Provides random-access input operations for files within a
/// [`Directory`](crate::core::store::directory::Directory).
///
/// [`IndexInput`] supports reading data from a file and maintains its own
/// internal state, such as the current file position.
///
/// # Thread Safety
///
/// [`IndexInput`] is **not thread-safe**. If you need to use it in multiple
/// threads, you must **clone** the [`IndexInput`] instance. Each clone operates
/// on the same underlying resource but maintains an independent position.
///
///
/// # See Also
/// - [`Directory`](crate::core::store::directory::Directory) for file-based
///   operations.
pub trait IndexInput: DataInput + TryClone + CloseableRef {
  /// The index input type returned by slicing operations.
  type IndexInput: IndexInput;
  /// Returns the current position in this file, where the next read will
  /// occur.
  ///
  /// # See Also
  /// [`seek`](IndexInput::seek)
  fn get_file_pointer(&self) -> Result<usize>;

  /// Sets the current position in this file, where the next read will occur.
  /// If this position is beyond the end of the file, it will return an
  /// [`LuceneErrorKind::Io`](crate::core::util::error::lucene_error::LuceneErrorKind::Io), and the stream
  /// will be in an undetermined state.
  ///
  /// # See Also
  /// [`get_file_pointer`](IndexInput::get_file_pointer)
  fn seek(&mut self, pos: usize) -> Result<()>;
  /// Inherits documentation from the parent implementation.
  ///
  /// # Behavior
  /// This is functionally equivalent to seeking to `get_file_pointer() +
  /// num_bytes`.
  ///
  /// # See Also
  /// [`get_file_pointer`](IndexInput::get_file_pointer)
  ///
  /// [`seek`](IndexInput::seek)
  fn skip_bytes(&mut self, num_bytes: i64) -> Result<()> {
    if num_bytes < 0 {
      return Err(LuceneError::illegal_argument(format!(
        "num_bytes must be >= 0, got {num_bytes}"
      )));
    }
    let num_bytes: usize = num_bytes.try_convert()?;
    let skip_to = self.get_file_pointer()? + num_bytes;
    self.seek(skip_to)?;
    Ok(())
  }
  /// The number of bytes in the file.
  fn length(&self) -> Result<usize>;

  /// Creates a slice of this index input, with the given description, offset,
  /// and length. The slice is positioned at the beginning.
  fn slice(
    &self,
    _slice_description: &str,
    _offset: usize,
    _length: usize,
  ) -> Result<Self::IndexInput> {
    Err(LuceneError::unsupported_operation("not support slicing"))
  }
  /// Creates a slice with a specific [`ReadAdvice`]. This is typically used
  /// by [`CompoundFormat`](crate::core::codecs::compound_format)
  /// implementations to honor the [`ReadAdvice`] of each file within the
  /// compound file.
  ///
  /// # Note
  /// It is only legal to call this method if this [`IndexInput`] has been
  /// opened with [`ReadAdvice::Normal`]. However, this method accepts any
  /// [`ReadAdvice`] value except `None` for the slice.
  ///
  /// The default implementation delegates to [`slice`](IndexInput::slice) and
  /// ignores the [`ReadAdvice`].
  fn slice_with_read_advice(
    &self,
    description: &str,
    offset: usize,
    length: usize,
    _read_advice: &ReadAdvice,
  ) -> Result<Self::IndexInput> {
    self.slice(description, offset, length)
  }
  type RandomAccessSlice: RandomAccessInput;
  /// Creates a random-access slice of this index input, with the given offset
  /// and length.
  ///
  /// # Note
  /// The default implementation calls [`slice`](IndexInput::slice), and if it
  /// doesn't support random access. Wrap with [`RandomAccessInputWrapper`](crate::core::store::random_access_input::RandomAccessInputWrapper) It implements absolute reads as
  /// seek+read.
  fn random_access_slice(&self, offset: usize, length: usize) -> Result<Self::RandomAccessSlice>;

  /// Optional method: Gives a hint to this input that some bytes will be read
  /// soon. [`IndexInput`] implementations may take advantage of this hint
  /// to start fetching pages of data immediately from storage.
  ///
  /// # Arguments
  /// * `offset` - The starting offset.
  /// * `length` - The number of bytes to prefetch.
  ///
  /// # Note
  /// The default implementation is a no-op.
  fn prefetch(&self, _pos: usize, _len: usize) -> Result<()> {
    Ok(())
  }

  fn update_read_advice(&self, _read_advice: ReadAdvice) -> Result<()> {
    Ok(())
  }

  /// Returns a hint whether all the contents of this input are resident in physical memory. It's a
  /// hint because the operating system may have paged out some of the data by the time this method
  /// returns. If the optional is true, then it's likely that the contents of this input are resident
  /// in physical memory. A value of false does not imply that the contents are not resident in
  /// physical memory. [`None`] is returned if it is not possible to determine.
  ///
  /// This runs in linear time with the [`length`](IndexInput::length) of this input / page size.
  ///
  /// The default implementation returns [`None`].
  fn is_loaded(&self) -> Result<Option<bool>> {
    Ok(None)
  }

  /// Optional immutable native-float view. Unknown inputs and wrappers keep
  /// their existing buffered behavior; this never reads or advances a cursor.
  fn mapped_float_vectors(&self, _dimension: usize, _count: usize) -> Option<MappedFloatVectors> {
    None
  }
}
pub trait ErasedIndexInput:
  IndexInput<RandomAccessSlice = BoxRandomAccessInput, IndexInput = IndexInputEnum>
{
  fn try_clone_index_input(&self) -> Result<IndexInputEnum>;
}

pub type DynIndexInput = dyn ErasedIndexInput + Send + Sync;
pub type CustomIndexInput = Box<DynIndexInput>;

pub type IndexInputEnumRandomAccessSlice =
  RandomAccessInputEnum2<BufferedIndexInput<NIOFSIndexInput>, BoxRandomAccessInput>;

pub enum IndexInputEnum {
  Fs(BufferedIndexInput<NIOFSIndexInput>),
  Custom(CustomIndexInput),
}

impl IndexInputEnum {
  pub fn custom<I>(input: I) -> Self
  where
    I: ErasedIndexInput + Send + Sync + 'static,
  {
    Self::Custom(Box::new(input))
  }
}

impl CloseableRef for IndexInputEnum {
  fn close(&self) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.close(),
      IndexInputEnum::Custom(inner) => inner.close(),
    }
  }
}

impl DataInput for IndexInputEnum {
  fn read_byte(&mut self) -> Result<u8> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_byte(inner),
      IndexInputEnum::Custom(inner) => inner.read_byte(),
    }
  }

  fn get_bytes<'a>(&'a mut self, len: usize) -> Result<std::borrow::Cow<'a, [u8]>> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::get_bytes(inner, len),
      IndexInputEnum::Custom(inner) => DataInput::get_bytes(inner, len),
    }
  }

  fn read_bytes(&mut self, b: &mut [u8], offset: usize, len: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_bytes(inner, b, offset, len),
      IndexInputEnum::Custom(inner) => inner.read_bytes(b, offset, len),
    }
  }

  fn read_bytes_with_buffer(
    &mut self,
    b: &mut [u8],
    offset: usize,
    len: usize,
    _use_buffer: bool,
  ) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.read_bytes_with_buffer(b, offset, len, _use_buffer),
      IndexInputEnum::Custom(inner) => inner.read_bytes_with_buffer(b, offset, len, _use_buffer),
    }
  }

  fn read_short(&mut self) -> Result<i16> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_short(inner),
      IndexInputEnum::Custom(inner) => inner.read_short(),
    }
  }

  fn read_int(&mut self) -> Result<i32> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_int(inner),
      IndexInputEnum::Custom(inner) => inner.read_int(),
    }
  }

  fn read_group_vint(&mut self, dst: &mut [i32], offset: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.read_group_vint(dst, offset),
      IndexInputEnum::Custom(inner) => inner.read_group_vint(dst, offset),
    }
  }

  fn read_vint(&mut self) -> Result<i32> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_vint(inner),
      IndexInputEnum::Custom(inner) => inner.read_vint(),
    }
  }

  fn read_zint(&mut self) -> Result<i32> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_zint(inner),
      IndexInputEnum::Custom(inner) => inner.read_zint(),
    }
  }

  fn read_long(&mut self) -> Result<i64> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_long(inner),
      IndexInputEnum::Custom(inner) => inner.read_long(),
    }
  }

  fn read_longs(&mut self, dst: &mut [i64], offset: usize, len: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.read_longs(dst, offset, len),
      IndexInputEnum::Custom(inner) => inner.read_longs(dst, offset, len),
    }
  }

  fn read_ints(&mut self, dst: &mut [i32], offset: usize, len: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.read_ints(dst, offset, len),
      IndexInputEnum::Custom(inner) => inner.read_ints(dst, offset, len),
    }
  }

  fn read_floats(&mut self, dst: &mut [f32], offset: usize, len: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.read_floats(dst, offset, len),
      IndexInputEnum::Custom(inner) => inner.read_floats(dst, offset, len),
    }
  }

  fn read_vlong(&mut self) -> Result<i64> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_vlong(inner),
      IndexInputEnum::Custom(inner) => inner.read_vlong(),
    }
  }

  fn read_zlong(&mut self) -> Result<i64> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_zlong(inner),
      IndexInputEnum::Custom(inner) => inner.read_zlong(),
    }
  }

  fn read_string<'a>(&'a mut self) -> Result<std::borrow::Cow<'a, str>> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_string(inner),
      IndexInputEnum::Custom(inner) => inner.read_string(),
    }
  }

  fn read_map_of_strings(&mut self) -> Result<HashMap<String, String>> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_map_of_strings(inner),
      IndexInputEnum::Custom(inner) => inner.read_map_of_strings(),
    }
  }

  fn read_set_of_strings(&mut self) -> Result<HashSet<String>> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::read_set_of_strings(inner),
      IndexInputEnum::Custom(inner) => inner.read_set_of_strings(),
    }
  }

  fn skip_bytes(&mut self, num_bytes: i64) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => DataInput::skip_bytes(inner, num_bytes),
      IndexInputEnum::Custom(inner) => DataInput::skip_bytes(inner, num_bytes),
    }
  }
}

impl DataInputExt for IndexInputEnum {
  crate::core::store::data_input_ext::impl_index_input_ext!();
}

impl Display for IndexInputEnum {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      IndexInputEnum::Fs(inner) => inner.fmt(f),
      IndexInputEnum::Custom(inner) => inner.fmt(f),
    }
  }
}
impl TryClone for IndexInputEnum {
  fn try_clone(&self) -> Result<Self>
  where
    Self: Sized,
  {
    match self {
      IndexInputEnum::Fs(inner) => Ok(IndexInputEnum::Fs(inner.try_clone()?)),
      IndexInputEnum::Custom(inner) => inner.try_clone_index_input(),
    }
  }
}

impl IndexInput for IndexInputEnum {
  type IndexInput = IndexInputEnum;

  fn get_file_pointer(&self) -> Result<usize> {
    match self {
      IndexInputEnum::Fs(inner) => inner.get_file_pointer(),
      IndexInputEnum::Custom(inner) => inner.get_file_pointer(),
    }
  }

  fn seek(&mut self, pos: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.seek(pos),
      IndexInputEnum::Custom(inner) => inner.seek(pos),
    }
  }

  fn skip_bytes(&mut self, num_bytes: i64) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => IndexInput::skip_bytes(inner, num_bytes),
      IndexInputEnum::Custom(inner) => inner.skip_bytes(num_bytes),
    }
  }

  fn length(&self) -> Result<usize> {
    match self {
      IndexInputEnum::Fs(inner) => IndexInput::length(inner),
      IndexInputEnum::Custom(inner) => inner.length(),
    }
  }

  fn slice(
    &self,
    slice_description: &str,
    offset: usize,
    length: usize,
  ) -> Result<Self::IndexInput> {
    match self {
      IndexInputEnum::Fs(inner) => Ok(IndexInputEnum::Fs(inner.slice(
        slice_description,
        offset,
        length,
      )?)),
      IndexInputEnum::Custom(inner) => inner.slice(slice_description, offset, length),
    }
  }

  fn slice_with_read_advice(
    &self,
    description: &str,
    offset: usize,
    length: usize,
    _read_advice: &ReadAdvice,
  ) -> Result<Self::IndexInput> {
    match self {
      IndexInputEnum::Fs(inner) => Ok(IndexInputEnum::Fs(inner.slice_with_read_advice(
        description,
        offset,
        length,
        _read_advice,
      )?)),
      IndexInputEnum::Custom(inner) => {
        inner.slice_with_read_advice(description, offset, length, _read_advice)
      },
    }
  }

  type RandomAccessSlice = IndexInputEnumRandomAccessSlice;

  fn random_access_slice(&self, offset: usize, length: usize) -> Result<Self::RandomAccessSlice> {
    match self {
      IndexInputEnum::Fs(inner) => Ok(IndexInputEnumRandomAccessSlice::A(
        inner.random_access_slice(offset, length)?,
      )),
      IndexInputEnum::Custom(inner) => Ok(IndexInputEnumRandomAccessSlice::B(
        inner.random_access_slice(offset, length)?,
      )),
    }
  }

  fn prefetch(&self, pos: usize, len: usize) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => IndexInput::prefetch(inner, pos, len),
      IndexInputEnum::Custom(inner) => inner.prefetch(pos, len),
    }
  }

  fn update_read_advice(&self, read_advice: ReadAdvice) -> Result<()> {
    match self {
      IndexInputEnum::Fs(inner) => inner.update_read_advice(read_advice),
      IndexInputEnum::Custom(inner) => inner.update_read_advice(read_advice),
    }
  }

  fn is_loaded(&self) -> Result<Option<bool>> {
    match self {
      IndexInputEnum::Fs(inner) => IndexInput::is_loaded(inner),
      IndexInputEnum::Custom(inner) => IndexInput::is_loaded(inner.as_ref()),
    }
  }
}

/// Implementations call this to build the resource description for a slice of
/// this [`IndexInput`].
pub fn get_full_slice_description(slice_description: &str) -> String {
  format!(" [slice={slice_description}] ")
}

macro_rules! either_index_input {
    (
        $vis:vis $name:ident<$( $G:ident ),+>,
        index_input = $index_input:ty,
        random_access = $random_access_type:ty,
        random_access_enum = $random_access:ident
        { $( $Variant:ident : $T:ty ),+ $(,)? }
    ) => {
        either_index_input!(@impl
            $vis $name [<$( $G ),+>],
            index_input = $index_input,
            random_access = $random_access_type,
            random_access_enum = $random_access
            { $( $Variant : $T ),+ }
        );
    };
    (
        $vis:vis $name:ident,
        index_input = $index_input:ty,
        random_access = $random_access_type:ty,
        random_access_enum = $random_access:ident
        { $( $Variant:ident : $T:ty ),+ $(,)? }
    ) => {
        either_index_input!(@impl
            $vis $name [],
            index_input = $index_input,
            random_access = $random_access_type,
            random_access_enum = $random_access
            { $( $Variant : $T ),+ }
        );
    };
    (@impl
        $vis:vis $name:ident [$($generics:tt)*],
        index_input = $index_input:ty,
        random_access = $random_access_type:ty,
        random_access_enum = $random_access:ident
        { $( $Variant:ident : $T:ty ),+ $(,)? }
    ) => {
        $vis enum $name $($generics)* {
            $( $Variant($T), )+
        }

        impl $($generics)* CloseableRef for $name $($generics)*
        where
            $( $T: CloseableRef ),+
        {
            fn close(&self) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.close(), )+
                }
            }
        }

        impl $($generics)* DataInput for $name $($generics)*
        where
            $( $T: DataInput ),+
        {
            fn read_byte(&mut self) -> Result<u8> {
                match self {
                    $( Self::$Variant(inner) => DataInput::read_byte(inner), )+
                }
            }

            fn get_bytes<'a>(&'a mut self, len: usize) -> Result<std::borrow::Cow<'a, [u8]>> {
                match self {
                    $( Self::$Variant(inner) => DataInput::get_bytes(inner, len), )+
                }
            }

            fn read_bytes(&mut self, b: &mut [u8], offset: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => DataInput::read_bytes(inner, b, offset, len), )+
                }
            }

            fn read_bytes_with_buffer(
                &mut self,
                b: &mut [u8],
                offset: usize,
                len: usize,
                _use_buffer: bool,
            ) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.read_bytes_with_buffer(
                        b,
                        offset,
                        len,
                        _use_buffer
                    ), )+
                }
            }

            fn read_short(&mut self) -> Result<i16> {
                match self {
                    $( Self::$Variant(inner) => DataInput::read_short(inner), )+
                }
            }



            fn read_int(&mut self) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => DataInput::read_int(inner), )+
                }
            }



            fn read_group_vint(&mut self, dst: &mut [i32], offset: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.read_group_vint(dst, offset), )+
                }
            }



            fn read_vint(&mut self) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.read_vint(), )+
                }
            }

            fn read_zint(&mut self) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.read_zint(), )+
                }
            }

            fn read_long(&mut self) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => DataInput::read_long(inner), )+
                }
            }



            fn read_longs(&mut self, dst: &mut [i64], offset: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.read_longs(dst, offset, len), )+
                }
            }

            fn read_ints(&mut self, dst: &mut [i32], offset: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.read_ints(dst, offset, len), )+
                }
            }

            fn read_floats(&mut self, dst: &mut [f32], offset: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.read_floats(dst, offset, len), )+
                }
            }

            fn read_vlong(&mut self) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => inner.read_vlong(), )+
                }
            }

            fn read_zlong(&mut self) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => inner.read_zlong(), )+
                }
            }

            fn read_string<'a>(&'a mut self) -> Result<std::borrow::Cow<'a, str>> {
                match self {
                    $( Self::$Variant(inner) => inner.read_string(), )+
                }
            }

            fn read_map_of_strings(&mut self) -> Result<HashMap<String, String>> {
                match self {
                    $( Self::$Variant(inner) => inner.read_map_of_strings(), )+
                }
            }

            fn read_set_of_strings(&mut self) -> Result<HashSet<String>> {
                match self {
                    $( Self::$Variant(inner) => inner.read_set_of_strings(), )+
                }
            }

            fn skip_bytes(&mut self, num_bytes: i64) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => DataInput::skip_bytes(inner, num_bytes), )+
                }
            }
        }

        impl $($generics)* $crate::core::store::data_input_ext::DataInputExt for $name $($generics)*
        where
            $( $T: DataInput ),+
        {
            fn is_index_input(&self) -> bool {
                match self {
                    $( Self::$Variant(inner) => inner.is_index_input(), )+
                }
            }

            fn seek_in_data_input(&mut self, _pos: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.seek_in_data_input(_pos), )+
                }
            }

            fn get_file_pointer_in_data_input(&self) -> Result<usize>{
                match self {
                    $( Self::$Variant(inner) => inner.get_file_pointer_in_data_input(), )+
                }
            }
        }

        impl $($generics)* Display for $name $($generics)*
        where
            $( $T: Display ),+
        {
            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                match self {
                    $( Self::$Variant(inner) => inner.fmt(f), )+
                }
            }
        }

        impl $($generics)* TryClone for $name $($generics)*
        where
            $( $T: TryClone ),+
        {
            fn try_clone(&self) -> Result<Self>
            where
                Self: Sized,
            {
                match self {
                    $( Self::$Variant(inner) => Ok(Self::$Variant(inner.try_clone()?)), )+
                }
            }
        }

        impl $($generics)* RandomAccessInput for $name $($generics)*
        where
            $( $T: RandomAccessInput ),+
        {
            fn length(&self) -> Result<usize> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::length(inner), )+
                }
            }

            fn read_byte(&self, pos: usize) -> Result<u8> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::read_byte(inner, pos), )+
                }
            }

            fn read_bytes(&self, pos: usize, len: usize) -> Result<std::borrow::Cow<'_, [u8]>> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::read_bytes(inner, pos, len), )+
                }
            }

            fn read_short(&self, pos: usize) -> Result<i16> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::read_short(inner, pos), )+
                }
            }

            fn read_int(&self, pos: usize) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::read_int(inner, pos), )+
                }
            }

            fn read_long(&self, pos: usize) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::read_long(inner, pos), )+
                }
            }

            fn prefetch(&self, pos: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => RandomAccessInput::prefetch(inner, pos, len), )+
                }
            }
        }

        impl $($generics)* IndexInput for $name $($generics)*
        where
            $( $T: IndexInput ),+
        {
            type IndexInput = $index_input;

            fn get_file_pointer(&self) -> Result<usize>{
                match self {
                    $( Self::$Variant(inner) => inner.get_file_pointer(), )+
                }
            }

            fn seek(&mut self, pos: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.seek(pos), )+
                }
            }

            fn skip_bytes(&mut self, num_bytes: i64) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => IndexInput::skip_bytes(inner, num_bytes), )+
                }
            }

            fn length(&self) -> Result<usize> {
                match self {
                    $( Self::$Variant(inner) => IndexInput::length(inner), )+
                }
            }

            fn slice(
                &self,
                slice_description: &str,
                offset: usize,
                length: usize,
            ) -> Result<Self::IndexInput> {
                match self {
                    $( Self::$Variant(inner) => Ok($name::$Variant(
                        inner.slice(slice_description, offset, length)?,
                    )), )+
                }
            }

            fn slice_with_read_advice(
                &self,
                description: &str,
                offset: usize,
                length: usize,
                read_advice: &ReadAdvice,
            ) -> Result<Self::IndexInput> {
                match self {
                    $( Self::$Variant(inner) => Ok($name::$Variant(inner.slice_with_read_advice(
                        description,
                        offset,
                        length,
                        read_advice,
                    )?)), )+
                }
            }

            type RandomAccessSlice = $random_access_type;

            fn random_access_slice(&self, offset: usize, length: usize) -> Result<Self::RandomAccessSlice> {
                match self {
                    $( Self::$Variant(inner) => Ok($random_access::$Variant(
                        inner.random_access_slice(offset, length)?,
                    )), )+
                }
            }

            fn prefetch(&self, pos: usize, len: usize) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => IndexInput::prefetch(inner, pos, len), )+
                }
            }

            fn update_read_advice(&self, read_advice: ReadAdvice) -> Result<()> {
                match self {
                    $( Self::$Variant(inner) => inner.update_read_advice(read_advice), )+
                }
            }

            fn is_loaded(&self) -> Result<Option<bool>> {
                match self {
                    $( Self::$Variant(inner) => IndexInput::is_loaded(inner), )+
                }
            }

        }
    };
}

impl<I> DataInput for Arc<I>
where
  I: IndexInput,
{
  fn read_byte(&mut self) -> Result<u8> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn get_bytes(&mut self, _len: usize) -> Result<std::borrow::Cow<'_, [u8]>> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_bytes(&mut self, _b: &mut [u8], _offset: usize, _len: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_bytes_with_buffer(
    &mut self,
    _b: &mut [u8],
    _offset: usize,
    _len: usize,
    _use_buffer: bool,
  ) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_short(&mut self) -> Result<i16> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_int(&mut self) -> Result<i32> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_group_vint(&mut self, _dst: &mut [i32], _offset: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_vint(&mut self) -> Result<i32> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_zint(&mut self) -> Result<i32> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_long(&mut self) -> Result<i64> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_longs(&mut self, _dst: &mut [i64], _offset: usize, _len: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_ints(&mut self, _dst: &mut [i32], _offset: usize, _len: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_floats(&mut self, _dst: &mut [f32], _offset: usize, _len: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_vlong(&mut self) -> Result<i64> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_zlong(&mut self) -> Result<i64> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_string<'a>(&'a mut self) -> Result<std::borrow::Cow<'a, str>> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_map_of_strings(&mut self) -> Result<HashMap<String, String>> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn read_set_of_strings(&mut self) -> Result<HashSet<String>> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn skip_bytes(&mut self, _num_bytes: i64) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }
}

impl<I> DataInputExt for Arc<I>
where
  I: IndexInput,
{
  fn is_index_input(&self) -> bool {
    (**self).is_index_input()
  }

  fn seek_in_data_input(&mut self, _pos: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn get_file_pointer_in_data_input(&self) -> Result<usize> {
    (**self).get_file_pointer_in_data_input()
  }
}

impl<I> TryClone for Arc<I>
where
  I: IndexInput,
{
  fn try_clone(&self) -> Result<Self>
  where
    Self: Sized,
  {
    Ok(Arc::clone(self))
  }
}

impl<I> IndexInput for Arc<I>
where
  I: IndexInput,
{
  type IndexInput = I::IndexInput;

  fn get_file_pointer(&self) -> Result<usize> {
    (**self).get_file_pointer()
  }

  fn seek(&mut self, _pos: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn skip_bytes(&mut self, _num_bytes: i64) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn length(&self) -> Result<usize> {
    (**self).length()
  }

  fn slice(
    &self,
    _slice_description: &str,
    _offset: usize,
    _length: usize,
  ) -> Result<Self::IndexInput> {
    (**self).slice(_slice_description, _offset, _length)
  }

  fn slice_with_read_advice(
    &self,
    description: &str,
    offset: usize,
    length: usize,
    _read_advice: &ReadAdvice,
  ) -> Result<Self::IndexInput> {
    self
      .as_ref()
      .slice_with_read_advice(description, offset, length, _read_advice)
  }

  type RandomAccessSlice = I::RandomAccessSlice;

  fn random_access_slice(&self, offset: usize, length: usize) -> Result<Self::RandomAccessSlice> {
    (**self).random_access_slice(offset, length)
  }

  fn prefetch(&self, _pos: usize, _len: usize) -> Result<()> {
    Err(LuceneError::unsupported_operation(""))
  }

  fn update_read_advice(&self, read_advice: ReadAdvice) -> Result<()> {
    self.as_ref().update_read_advice(read_advice)
  }

  fn is_loaded(&self) -> Result<Option<bool>> {
    IndexInput::is_loaded(self.as_ref())
  }
}
either_index_input!(
    pub IndexInputEnum2<A, B>,
    index_input = IndexInputEnum2<A::IndexInput, B::IndexInput>,
    random_access = RandomAccessInputEnum2<A::RandomAccessSlice, B::RandomAccessSlice>,
    random_access_enum = RandomAccessInputEnum2
    { A: A, B: B }
);
either_index_input!(
    pub IndexInputEnum3<A, B, C>,
    index_input = IndexInputEnum3<A::IndexInput, B::IndexInput, C::IndexInput>,
    random_access = RandomAccessInputEnum3<
        A::RandomAccessSlice,
        B::RandomAccessSlice,
        C::RandomAccessSlice,
    >,
    random_access_enum = RandomAccessInputEnum3
    { A: A, B: B, C: C }
);
either_index_input!(
    pub NRTCachingIndexInput<I>,
    index_input = NRTCachingIndexInput<I::IndexInput>,
    random_access = RandomAccessInputEnum2<
        <ByteBuffersIndexInputOwned as IndexInput>::RandomAccessSlice,
        I::RandomAccessSlice,
    >,
    random_access_enum = RandomAccessInputEnum2
    { A: ByteBuffersIndexInputOwned, B: I }
);
either_index_input!(
    pub BuiltInFSIndexInput,
    index_input = BuiltInFSIndexInput,
    random_access = RandomAccessInputEnum2<
        MemorySegmentRandomAccessInput,
        BufferedIndexInput<NIOFSIndexInput>,
    >,
    random_access_enum = RandomAccessInputEnum2
    {
        A: MemorySegmentIndexInput,
        B: BufferedIndexInput<NIOFSIndexInput>,
    }
);