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
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
/*
 * 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::codecs::CodecUtil;
use crate::core::codecs::dummy::dummy_mutable_point_tree::DummyMutablePointTree;
use crate::core::index::BytesRef;
use crate::core::index::point_values::{IntersectVisitor, PointTree, PointValues, Relation};
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::store::{DataInput, IndexInput};
use crate::core::util::array_util::ByteArrayComparator;
use crate::core::util::bkd::bkd_bytes::BKDBytes;
use crate::core::util::bkd::bkd_config::BKDConfig;
use crate::core::util::bkd::bkd_writer::{
  CODEC_NAME, VERSION_CURRENT, VERSION_LEAF_STORES_BOUNDS, VERSION_LOW_CARDINALITY_LEAVES,
  VERSION_META_FILE, VERSION_SELECTIVE_INDEXING, VERSION_START,
};
use crate::core::util::bkd::doc_ids_writer::DocIdsWriter;
use crate::core::util::clone::TryClone;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::math_util::MathUtil;
use crate::core::util::{SliceCopyOps, TryIntoInt};
use parking_lot::Mutex;
use std::borrow::Cow;
use std::sync::Arc;

/// Handles reading a block KD-tree in byte-slice space previously written with
/// `BKDWriter`
pub struct BKDReader<I>
where
  I: IndexInput,
{
  config: BKDConfig,
  pub(crate) num_leaves: i32,
  index_in: Option<Arc<I>>,
  data_in: Arc<Mutex<I>>,
  // Minimum followed by maximum, or only the minimum when both are equal.
  packed_bounds: Box<[u8]>,
  point_count: usize,
  doc_count: i32,
  version: i32,
  #[allow(dead_code)]
  pub(crate) min_leaf_block_fp: i64,
  pub(crate) index_start_pointer: usize,
  num_index_bytes: usize,
  pub(crate) is_tree_balanced: bool,
  /// index_in,data_in are the same instance
  #[cfg(test)]
  pub same_in: bool,
}
impl<I: IndexInput> BKDReader<I>
where
  I: IndexInput,
{
  /// Caller must pre-seek the provided [`IndexInput`] to the index location
  /// that `BKDWriter::finish()` returned. BKD tree is always stored off-heap.
  ///
  /// Retains a cloned index view so the returned reader can create point trees.
  pub fn new<I1>(meta_in: &mut I1, index_in: &mut I, data_in: Arc<Mutex<I>>) -> Result<Self>
  where
    I1: IndexInput,
  {
    let mut reader = Self::read_metadata(meta_in, index_in, data_in)?;
    reader.init_index_in(Arc::new(index_in.try_clone()?))?;
    Ok(reader)
  }

  /// Reads metadata before the caller initializes the shared index input.
  pub(crate) fn read_metadata<I1>(
    meta_in: &mut I1,
    index_in: &mut I,
    data_in: Arc<Mutex<I>>,
  ) -> Result<Self>
  where
    I1: IndexInput,
  {
    let (mut reader, version) = Self::init_with_meta(meta_in, data_in)?;

    let (min_leaf_block_fp, index_start_pointer) = if version >= VERSION_META_FILE {
      (
        DataInput::read_long(meta_in)?,
        DataInput::read_long(meta_in)?.try_convert()?,
      )
    } else {
      let index_start_pointer = index_in.get_file_pointer()?;
      let min_leaf_block_fp = index_in.read_vlong()?;
      index_in.seek(index_start_pointer)?;
      (min_leaf_block_fp, index_start_pointer)
    };
    reader.min_leaf_block_fp = min_leaf_block_fp;
    reader.index_start_pointer = index_start_pointer;
    Ok(reader)
  }
  pub(crate) fn init_with_meta<I1>(meta_in: &mut I1, data_in: Arc<Mutex<I>>) -> Result<(Self, i32)>
  where
    I1: IndexInput,
  {
    let version = CodecUtil::check_header(meta_in, CODEC_NAME, VERSION_START, VERSION_CURRENT)?;

    let num_dims: usize = meta_in.read_vint()?.try_convert()?;
    let num_index_dims = if version >= VERSION_SELECTIVE_INDEXING {
      meta_in.read_vint()?.try_convert()?
    } else {
      num_dims
    };

    let max_points_in_leaf_node = meta_in.read_vint()?.try_convert()?;
    let bytes_per_dim = meta_in.read_vint()?.try_convert()?;
    let config = BKDConfig::new(
      num_dims,
      num_index_dims,
      bytes_per_dim,
      max_points_in_leaf_node,
    )?;

    // Read index:
    let num_leaves = meta_in.read_vint()?;
    debug_assert!(num_leaves > 0);
    let packed_index_bytes_length = config.packed_index_bytes_length();
    let mut min_packed_value = meta_in.get_bytes(packed_index_bytes_length)?.into_owned();
    let max_packed_value = meta_in.get_bytes(packed_index_bytes_length)?.into_owned();

    let bytes_per_dim = config.bytes_per_dim;
    let comparator = BKDBytes::<0>::new(bytes_per_dim);
    for dim in 0..config.num_index_dims {
      let offset = dim * bytes_per_dim;
      if comparator.compare(&min_packed_value, offset, &max_packed_value, offset) > 0 {
        return Err(LuceneError::corrupt_index(format!(
          "minPackedValue {} is > maxPackedValue {} for dim={}, (resource={})",
          BytesRef::from_bytes(min_packed_value),
          BytesRef::from_bytes(max_packed_value),
          dim,
          meta_in
        )));
      }
    }

    if min_packed_value != max_packed_value {
      min_packed_value.extend_from_slice(&max_packed_value);
    }
    let packed_bounds = min_packed_value.into_boxed_slice();
    let point_count = meta_in.read_vlong()?.try_convert()?;
    let doc_count = meta_in.read_vint()?;
    let num_index_bytes = meta_in.read_vint()?.try_convert()?;
    Ok((
      Self {
        config,
        num_leaves,
        index_in: None,
        data_in,
        packed_bounds,
        point_count,
        doc_count,
        version,
        min_leaf_block_fp: 0,
        index_start_pointer: 0,
        num_index_bytes,
        is_tree_balanced: false,
        #[cfg(test)]
        same_in: false,
      },
      version,
    ))
  }
  pub(crate) fn init_index_in(&mut self, index_in: Arc<I>) -> Result<()> {
    self.index_in = Some(index_in);
    self.is_tree_balanced = self.num_leaves != 1 && self.is_tree_balanced()?;
    Ok(())
  }
  fn min_packed_value(&self) -> &[u8] {
    &self.packed_bounds[..self.config.packed_index_bytes_length()]
  }

  fn max_packed_value(&self) -> &[u8] {
    let start = self.packed_bounds.len() - self.config.packed_index_bytes_length();
    &self.packed_bounds[start..]
  }

  /// Checks if the tree is balanced.
  pub(crate) fn is_tree_balanced(&self) -> Result<bool> {
    if self.version >= VERSION_META_FILE {
      // Since Lucene 8.6 all trees are unbalanced.
      return Ok(false);
    }
    if self.config.num_dims > 1 {
      // High dimensional tree in pre-8.6 indices are balanced.
      debug_assert!((1 << MathUtil::log(self.num_leaves as i64, 2)?) == self.num_leaves);
      return Ok(true);
    }
    if (1 << MathUtil::log(self.num_leaves as i64, 2)?) != self.num_leaves {
      // If we don't have enough leaves to fill the last level then it is
      // unbalanced.
      return Ok(false);
    }

    // Count of the last node for unbalanced trees.
    let last_leaf_node_point_count = self.point_count % self.config.max_points_in_leaf_node;

    // Navigate to last node.
    let mut point_tree = self.get_point_tree()?;
    while point_tree.move_to_sibling()? {}
    while point_tree.move_to_child()? {}

    // Count number of docs in the node.
    let mut visitor = IntersectVisitorImpl::new();
    point_tree.visit_doc_ids(&mut visitor)?;

    Ok(visitor.count != last_leaf_node_point_count)
  }
}
impl<I> PointValues for BKDReader<I>
where
  I: IndexInput,
{
  fn get_min_packed_value(&self) -> Result<Option<Cow<'_, [u8]>>> {
    Ok(Some(Cow::Borrowed(self.min_packed_value())))
  }

  fn get_max_packed_value(&self) -> Result<Option<Cow<'_, [u8]>>> {
    Ok(Some(Cow::Borrowed(self.max_packed_value())))
  }

  fn get_num_dimensions(&self) -> Result<usize> {
    Ok(self.config.num_dims)
  }

  fn get_num_index_dimensions(&self) -> Result<usize> {
    Ok(self.config.num_index_dims)
  }

  fn get_bytes_per_dimension(&self) -> Result<usize> {
    Ok(self.config.bytes_per_dim)
  }

  fn size(&self) -> Result<usize> {
    Ok(self.point_count)
  }

  fn get_doc_count(&self) -> Result<i32> {
    Ok(self.doc_count)
  }

  type PointTree = BKDPointTree<I>;

  fn get_point_tree(&self) -> Result<Self::PointTree> {
    let slice = match self.index_in {
      Some(ref index_in) => index_in.slice(
        "packedIndex",
        self.index_start_pointer,
        self.num_index_bytes,
      )?,
      None => {
        #[cfg(test)]
        {
          if !self.same_in {
            return Err(LuceneError::illegal_state(
              "index_in data_int not the same instance",
            ));
          }
          let data_in = &*self.data_in.lock();
          data_in.slice(
            "packedIndex",
            self.index_start_pointer,
            self.num_index_bytes,
          )?
        }
        #[cfg(not(test))]
        {
          return Err(LuceneError::illegal_state("index_in is None"));
        }
      },
    };
    BKDPointTree::new(
      slice,
      self.data_in.clone(),
      self.config.clone(),
      self.num_leaves,
      self.version,
      self.point_count,
      self.min_packed_value(),
      self.max_packed_value(),
      self.is_tree_balanced,
    )
  }
}

#[derive(Clone, Copy)]
struct LeafLevelPositions {
  leaf_block_fp: usize,
  read_node_data: usize,
}

#[derive(Clone, Copy)]
struct SplitLevelPositions {
  right_node: usize,
  dimension: usize,
}

pub struct BKDPointTree<I: IndexInput> {
  /// Current node ID in the tree.
  node_id: i32,
  /// During clone, the node root can be different from 1.
  node_root: i32,
  /// Level is 1-based so that we can do `level - 1` without checking each
  /// time.
  level: usize,
  /// Used to read the packed tree off-heap.
  inner_nodes: I::IndexInput,
  /// Used to read the packed leaves off-heap.
  leaf_nodes: Arc<Mutex<I>>,
  /// Leaf file pointer and position after node data, including the leaf sentinel level.
  leaf_positions: Box<[LeafLevelPositions]>,
  /// Right-child address and split dimension for internal levels.
  split_positions: Box<[SplitLevelPositions]>,
  /// True if the per-dimension delta we read for the node at this level is a
  /// negative offset versus the last split on this dimension.
  /// This is a packed 2D array, i.e., to access `array[level][dim]`,
  /// you read from `negative_deltas[level * num_dims + dim]`.
  /// This will be true if the last time we split on this dimension,
  /// we next pushed to the left sub-tree.
  negative_deltas: Box<[bool]>,
  /// Holds the packed per-level split values.
  split_values_stack: Box<[Box<[u8]>]>,
  /// Minimum then maximum value of the current node, each packed_index_bytes_length bytes.
  packed_bounds: Box<[u8]>,
  /// Holds the previous value of the split dimension.
  split_dim_value_stack: Box<[Box<[u8]>]>,
  /// Tree parameters.
  config: BKDConfig,
  /// Number of leaves.
  leaf_node_offset: i32,
  /// Version of the index.
  version: i32,
  /// Total number of points.
  point_count: usize,
  /// Last node might not be fully populated.
  last_leaf_node_point_count: usize,
  /// Right-most leaf node ID.
  right_most_leaf_node: i32,
  /// Data value, then indexed minimum and maximum, using the config's fixed widths.
  scratch_packed_values: Box<[u8]>,
  common_prefix_lengths: Box<[usize]>,
  scratch_iterator: BKDReaderDocIDSetIterator,
  /// If true, the tree is balanced; otherwise, it is unbalanced.
  is_tree_balanced: bool,
}

impl<I> BKDPointTree<I>
where
  I: IndexInput,
{
  #[allow(clippy::too_many_arguments)]
  fn new(
    inner_nodes: I::IndexInput,
    leaf_nodes: Arc<Mutex<I>>,
    config: BKDConfig,
    num_leaves: i32,
    version: i32,
    point_count: usize,
    min_packed_value: &[u8],
    max_packed_value: &[u8],
    is_tree_balanced: bool,
  ) -> Result<Self> {
    let packed_bytes_len = config.packed_bytes_length();
    let packed_index_bytes_len = config.packed_index_bytes_length();
    let num_dims = config.num_dims;
    let disi_len = config.max_points_in_leaf_node;

    let mut tree = Self::with_scratch_iterator(
      inner_nodes,
      leaf_nodes,
      config,
      num_leaves,
      version,
      point_count,
      1,
      1,
      min_packed_value,
      max_packed_value,
      BKDReaderDocIDSetIterator::new(disi_len),
      vec![0; packed_bytes_len + 2 * packed_index_bytes_len],
      vec![0; num_dims],
      is_tree_balanced,
    )?;
    tree.split_values_stack[0] = vec![0; packed_index_bytes_len].into_boxed_slice();
    tree.read_node_data(false)?;
    Ok(tree)
  }
  #[allow(clippy::too_many_arguments)]
  fn with_scratch_iterator(
    inner_nodes: I::IndexInput,
    leaf_nodes: Arc<Mutex<I>>,
    config: BKDConfig,
    num_leaves: i32,
    version: i32,
    point_count: usize,
    node_id: i32,
    level: usize,
    min_packed_value: &[u8],
    max_packed_value: &[u8],
    scratch_iterator: BKDReaderDocIDSetIterator,
    scratch_packed_values: Vec<u8>,
    common_prefix_lengths: Vec<usize>,
    is_tree_balanced: bool,
  ) -> Result<Self> {
    // stack arrays that keep information at different levels
    let tree_depth = Self::get_tree_depth(num_leaves)? as usize;
    let split_values_stack = vec![Box::<[u8]>::default(); tree_depth];
    let right_most_leaf_node = (1 << (tree_depth - 1)) - 1;
    let last_leaf_node_point_count = point_count % config.max_points_in_leaf_node;
    let last_leaf_node_point_count = if last_leaf_node_point_count == 0 {
      config.max_points_in_leaf_node
    } else {
      last_leaf_node_point_count
    };
    let negative_deltas_len = config.num_index_dims * tree_depth;

    Ok(BKDPointTree {
      config,
      version,
      node_id,
      node_root: node_id,
      level,
      is_tree_balanced,
      leaf_node_offset: num_leaves,
      inner_nodes,
      leaf_nodes,
      packed_bounds: [min_packed_value, max_packed_value]
        .concat()
        .into_boxed_slice(),
      split_dim_value_stack: vec![Box::<[u8]>::default(); tree_depth].into_boxed_slice(),
      split_values_stack: split_values_stack.into_boxed_slice(),
      leaf_positions: vec![
        LeafLevelPositions {
          leaf_block_fp: 0,
          read_node_data: 0
        };
        tree_depth + 1
      ]
      .into_boxed_slice(),
      split_positions: vec![
        SplitLevelPositions {
          right_node: 0,
          dimension: 0
        };
        tree_depth
      ]
      .into_boxed_slice(),
      negative_deltas: vec![false; negative_deltas_len].into_boxed_slice(),
      point_count,
      right_most_leaf_node,
      last_leaf_node_point_count,
      // scratch objects, reused between clones so NN search are not
      // creating those objects in every clone.
      scratch_iterator,
      common_prefix_lengths: common_prefix_lengths.into_boxed_slice(),
      scratch_packed_values: scratch_packed_values.into_boxed_slice(),
    })
  }
  fn reset_node_data_position(&mut self) -> Result<()> {
    // move position of the inner nodes index to visit the first child
    let position = self.leaf_positions[self.level].read_node_data;
    debug_assert!(position <= self.inner_nodes.get_file_pointer()?);
    self.inner_nodes.seek(position)?;
    Ok(())
  }
  fn push_bounds_left(&mut self) {
    let level = self.level;
    let bytes_per_dim = self.config.bytes_per_dim;
    let split_dim_pos = self.split_positions[level].dimension;

    // Save the dimension we are going to change.
    let start = self.config.packed_index_bytes_length() + split_dim_pos;
    let bound = &self.packed_bounds[start..start + bytes_per_dim];
    let saved = &mut self.split_dim_value_stack[level];
    if saved.is_empty() {
      *saved = Box::from(bound);
    } else {
      saved.copy_from_slice(bound);
    }

    debug_assert!(
      BKDBytes::<0>::new(bytes_per_dim).compare(
        &self.packed_bounds[self.config.packed_index_bytes_length()..],
        split_dim_pos,
        &self.split_values_stack[level],
        split_dim_pos
      ) >= 0,
      "config.bytesPerDim={} splitDimPos={} config.numIndexDims={} config.numDims={}",
      self.config.bytes_per_dim,
      self.split_positions[level].dimension,
      self.config.num_index_dims,
      self.config.num_dims
    );

    // add the split dim value:
    self.packed_bounds[self.config.packed_index_bytes_length()..].copy_from(
      &self.split_values_stack[level][split_dim_pos..split_dim_pos + bytes_per_dim],
      split_dim_pos,
    );
  }
  fn push_left(&mut self) -> Result<()> {
    self.node_id *= 2;
    self.level += 1;
    self.read_node_data(true)
  }
  fn push_bounds_right(&mut self) {
    let level = self.level;
    let bytes_per_dim = self.config.bytes_per_dim;
    let split_dim_pos = self.split_positions[level].dimension;
    // we should have already visited the left node
    debug_assert!(!self.split_dim_value_stack[level].is_empty());
    // save the dimension we are going to change
    self.split_dim_value_stack[level].copy_from(
      &self.packed_bounds[..self.config.packed_index_bytes_length()]
        [split_dim_pos..split_dim_pos + bytes_per_dim],
      0,
    );

    debug_assert!(
      BKDBytes::<0>::new(bytes_per_dim).compare(
        &self.packed_bounds[..self.config.packed_index_bytes_length()],
        split_dim_pos,
        &self.split_values_stack[level],
        split_dim_pos
      ) <= 0,
      "config.bytesPerDim={} splitDimPos={} config.numIndexDims={} config.numDims={}",
      self.config.bytes_per_dim,
      self.split_positions[level].dimension,
      self.config.num_index_dims,
      self.config.num_dims
    );
    // add the split dim value:
    self.packed_bounds[..self.config.packed_index_bytes_length()].copy_from(
      &self.split_values_stack[level][split_dim_pos..split_dim_pos + bytes_per_dim],
      split_dim_pos,
    );
  }
  fn push_right(&mut self) -> Result<()> {
    let node_position = self.split_positions[self.level].right_node;

    debug_assert!(
      node_position >= self.inner_nodes.get_file_pointer()?,
      "nodePosition = {} < currentPosition={}",
      node_position,
      self.inner_nodes.get_file_pointer()?
    );

    self.inner_nodes.seek(node_position)?;
    self.node_id = 2 * self.node_id + 1;
    self.level += 1;
    self.read_node_data(false)
  }
  fn pop(&mut self) {
    self.node_id /= 2;
    self.level -= 1;
  }

  fn pop_bounds(&mut self, is_left: bool) {
    let level = self.level;
    let split_dim_pos = self.split_positions[level].dimension;
    let bytes_per_dim = self.config.bytes_per_dim;

    if is_left {
      self.packed_bounds[self.config.packed_index_bytes_length()..].copy_from(
        &self.split_dim_value_stack[level][..bytes_per_dim],
        split_dim_pos,
      );
    } else {
      self.packed_bounds[..self.config.packed_index_bytes_length()].copy_from(
        &self.split_dim_value_stack[level][..bytes_per_dim],
        split_dim_pos,
      );
    }
  }
  fn is_root_node(&self) -> bool {
    self.node_id == self.node_root
  }

  fn is_left_node(&self) -> bool {
    (self.node_id & 1) == 0
  }

  fn is_leaf_node(&self) -> bool {
    self.node_id >= self.leaf_node_offset
  }

  fn node_exists(&self) -> bool {
    self.node_id - self.leaf_node_offset < self.leaf_node_offset
  }
  /// Only valid after pushLeft or pushRight, not pop!.
  fn get_leaf_block_fp(&self) -> Result<usize> {
    debug_assert!(self.is_leaf_node(), "nodeID={} is not a leaf", self.node_id);
    Ok(self.leaf_positions[self.level].leaf_block_fp)
  }
  fn size_from_balanced_tree(
    &self,
    left_most_leaf_node: i32,
    right_most_leaf_node: i32,
  ) -> Result<usize> {
    // number of points that need to be distributed between leaves, one per
    // leaf
    let extra_points: i32 = (self.config.max_points_in_leaf_node * self.leaf_node_offset as usize
      - self.point_count)
      .try_convert()?;

    debug_assert!(
      extra_points < self.leaf_node_offset,
      "point excess should be lower than leafNodeOffset"
    );

    // offset where we stop adding one point to the leaves
    let node_offset = self.leaf_node_offset - extra_points;
    let mut count = 0;

    for node in left_most_leaf_node..=right_most_leaf_node {
      // offsetPosition provides which extra point will be added to this
      // node
      if Self::balance_tree_node_position(
        0,
        self.leaf_node_offset,
        node - self.leaf_node_offset,
        0,
        0,
      ) < node_offset
      {
        count += self.config.max_points_in_leaf_node;
      } else {
        count += self.config.max_points_in_leaf_node - 1;
      }
    }
    Ok(count)
  }
  fn balance_tree_node_position(
    min_node: i32,
    max_node: i32,
    node: i32,
    position: i32,
    level: i32,
  ) -> i32 {
    if max_node - min_node == 1 {
      return position;
    }
    let mid = (min_node + max_node + 1) / 2;
    if mid > node {
      Self::balance_tree_node_position(min_node, mid, node, position, level + 1)
    } else {
      Self::balance_tree_node_position(mid, max_node, node, position + (1 << level), level + 1)
    }
  }
  fn add_all<V>(&mut self, visitor: &mut V, mut grown: bool, leaf_nodes: &mut I) -> Result<()>
  where
    V: IntersectVisitor,
  {
    if !grown {
      let size = self.size()?;
      if size <= i32::MAX as usize {
        visitor.grow(size)?;
        grown = true;
      }
    }

    if self.is_leaf_node() {
      // Leaf node
      let leaf_fp = self.get_leaf_block_fp()?;
      leaf_nodes.seek(leaf_fp)?;
      // How many points are stored in this leaf cell:
      let count: usize = leaf_nodes.read_vint()?.try_convert()?;
      // No need to call grow(), it has been called up-front
      self
        .scratch_iterator
        .doc_ids_writer
        .read_ints_with_visitor(leaf_nodes, count, visitor)?;
    } else {
      self.push_left()?;
      self.add_all(visitor, grown, leaf_nodes)?;
      self.pop();
      self.push_right()?;
      self.add_all(visitor, grown, leaf_nodes)?;
      self.pop();
    }

    Ok(())
  }
  fn visit_leaves_one_by_one<V>(&mut self, visitor: &mut V, leaf_node: &mut I) -> Result<()>
  where
    V: IntersectVisitor,
  {
    if self.is_leaf_node() {
      let leaf_fp = self.get_leaf_block_fp()?;
      self.visit_doc_values(visitor, leaf_fp, leaf_node)?;
    } else {
      self.push_left()?;
      self.visit_leaves_one_by_one(visitor, leaf_node)?;
      self.pop();

      self.push_right()?;
      self.visit_leaves_one_by_one(visitor, leaf_node)?;
      self.pop();
    }
    Ok(())
  }

  fn visit_doc_values<V>(&mut self, visitor: &mut V, fp: usize, leaf_node: &mut I) -> Result<()>
  where
    V: IntersectVisitor,
  {
    let count = self.read_doc_ids(fp, leaf_node)?;

    if self.version >= VERSION_LOW_CARDINALITY_LEAVES {
      self.visit_doc_values_with_cardinality(count, visitor, leaf_node)?;
    } else {
      self.visit_doc_values_no_cardinality(count, visitor, leaf_node)?;
    }

    Ok(())
  }

  fn read_doc_ids(&mut self, block_fp: usize, index_input: &mut I) -> Result<usize> {
    index_input.seek(block_fp)?;
    let count = index_input.read_vint()?.try_convert()?;
    self.scratch_iterator.doc_ids_writer.read_ints(
      index_input,
      count,
      &mut self.scratch_iterator.doc_ids,
    )?;
    Ok(count)
  }

  fn get_num_leaves_slow(&self, node: i32) -> i32 {
    if node >= 2 * self.leaf_node_offset {
      0
    } else if node >= self.leaf_node_offset {
      1
    } else {
      let left_count = self.get_num_leaves_slow(node * 2);
      let right_count = self.get_num_leaves_slow(node * 2 + 1);
      left_count + right_count
    }
  }

  fn read_node_data(&mut self, is_left: bool) -> Result<()> {
    self.leaf_positions[self.level].leaf_block_fp =
      self.leaf_positions[self.level - 1].leaf_block_fp;
    if !is_left {
      // Read leaf block FP delta
      let v: usize = self.inner_nodes.read_vlong()?.try_convert()?;
      self.leaf_positions[self.level].leaf_block_fp += v;
    }

    if !self.is_leaf_node() {
      let num_index_dims = self.config.num_index_dims;

      // Copy the negative deltas from the previous level
      let prev_offset = (self.level - 1) * num_index_dims;
      let curr_offset = self.level * num_index_dims;
      self
        .negative_deltas
        .copy_within(prev_offset..prev_offset + num_index_dims, curr_offset);
      self.negative_deltas[curr_offset
        + self.split_positions[self.level - 1].dimension / self.config.bytes_per_dim] = is_left;

      // Clone or copy the previous level's split values
      if self.split_values_stack[self.level].is_empty() {
        self.split_values_stack[self.level] = self.split_values_stack[self.level - 1].clone();
      } else {
        let (before, after) = self.split_values_stack.split_at_mut(self.level);
        let source = &before[self.level - 1][..self.config.packed_index_bytes_length()];
        after[0].copy_from(source, 0);
      }

      // Read split dim, prefix, and firstDiffByteDelta encoded as an int
      let mut code: usize = self.inner_nodes.read_vint()?.try_convert()?;
      let split_dim = code % self.config.num_index_dims;
      self.split_positions[self.level].dimension = split_dim * self.config.bytes_per_dim;
      code /= self.config.num_index_dims;
      let prefix = code % (1 + self.config.bytes_per_dim);
      let suffix = self.config.bytes_per_dim - prefix;

      if suffix > 0 {
        let mut first_diff_byte_delta = (code / (1 + self.config.bytes_per_dim)) as i32;
        if self.negative_deltas[curr_offset + split_dim] {
          first_diff_byte_delta = -first_diff_byte_delta;
        }
        let start_pos = self.split_positions[self.level].dimension + prefix;
        let old_byte = self.split_values_stack[self.level][start_pos] as i32;
        self.split_values_stack[self.level][start_pos] = (old_byte + first_diff_byte_delta) as u8;
        DataInput::read_bytes(
          &mut self.inner_nodes,
          &mut self.split_values_stack[self.level],
          start_pos + 1,
          suffix - 1,
        )?;
      } else {
        // Our split value is == last split value in this dim, which can
        // happen when there are many duplicate values.
      }

      let left_num_bytes = if self.node_id * 2 < self.leaf_node_offset {
        self.inner_nodes.read_vint()?.try_convert()?
      } else {
        0
      };
      self.split_positions[self.level].right_node =
        self.inner_nodes.get_file_pointer()? + left_num_bytes;
      self.leaf_positions[self.level].read_node_data = self.inner_nodes.get_file_pointer()?;
    }
    Ok(())
  }
  /// Computes the depth of the tree based on the number of leaves.
  ///
  /// - The first `+1` accounts for the fact that all non-leaf nodes form
  ///   another power of 2. For example, to have a fully balanced tree with 4
  ///   leaves, you need a tree of depth 3.
  /// - The second `+1` ensures that the depth is correctly calculated, as
  ///   `log2(num_leaves)` computes the floor of the logarithm. For example,
  ///   with 5 leaves, you need a tree of depth 4.
  fn get_tree_depth(num_leaves: i32) -> Result<i32> {
    Ok(MathUtil::log(num_leaves as i64, 2)? + 2)
  }
  fn visit_doc_values_no_cardinality<V>(
    &mut self,
    count: usize,
    visitor: &mut V,
    leaf_node: &mut I,
  ) -> Result<()>
  where
    V: IntersectVisitor,
  {
    let packed_bytes_length = self.config.packed_bytes_length();
    let packed_index_bytes_length = self.config.packed_index_bytes_length();

    self.read_common_prefixes(leaf_node)?;

    if self.config.num_index_dims > 1 && self.version >= VERSION_LEAF_STORES_BOUNDS {
      self.scratch_packed_values.copy_within(
        ..packed_index_bytes_length,
        packed_bytes_length + packed_index_bytes_length,
      );
      self.scratch_packed_values.copy_within(
        packed_bytes_length..packed_bytes_length + packed_index_bytes_length,
        packed_bytes_length + packed_index_bytes_length,
      );
      self.read_min_max(leaf_node)?;

      // The index gives us range of values for each dimension, but the
      // actual range of values might be much more narrow than
      // what the index told us, so we double check the relation
      // here, which is cheap yet might help figure out that the block
      // either entirely matches or does not match at all.
      // This is especially more likely in the case that there are
      // multiple dimensions that have correlation, ie. splitting on one
      // dimension also significantly changes the range of
      // values in another dimension.
      let relation = visitor.compare(
        &self.scratch_packed_values
          [packed_bytes_length..packed_bytes_length + packed_index_bytes_length],
        &self.scratch_packed_values[packed_bytes_length + packed_index_bytes_length..],
      )?;
      if relation == Relation::CellOutsideQuery {
        return Ok(());
      }
      visitor.grow(count)?;

      if relation == Relation::CellInsideQuery {
        for i in 0..count {
          visitor.visit(self.scratch_iterator.doc_ids[i])?;
        }
        return Ok(());
      }
    } else {
      visitor.grow(count)?;
    }

    let compressed_dim = self.read_compressed_dim(leaf_node)?;

    if compressed_dim == -1 {
      self.visit_unique_raw_doc_values(count, visitor)?;
    } else {
      self.visit_compressed_doc_values(count, visitor, compressed_dim, leaf_node)?;
    }

    Ok(())
  }
  fn visit_doc_values_with_cardinality<V>(
    &mut self,
    count: usize,
    visitor: &mut V,
    leaf_node: &mut I,
  ) -> Result<()>
  where
    V: IntersectVisitor,
  {
    let packed_bytes_length = self.config.packed_bytes_length();
    let packed_index_bytes_length = self.config.packed_index_bytes_length();
    self.read_common_prefixes(leaf_node)?;
    let compressed_dim = self.read_compressed_dim(leaf_node)?;
    if compressed_dim == -1 {
      // all values are the same
      visitor.grow(count)?;
      self.visit_unique_raw_doc_values(count, visitor)?;
    } else {
      if self.config.num_index_dims != 1 {
        self
          .scratch_packed_values
          .copy_within(..packed_index_bytes_length, packed_bytes_length);
        self.scratch_packed_values.copy_within(
          packed_bytes_length..packed_bytes_length + packed_index_bytes_length,
          packed_bytes_length + packed_index_bytes_length,
        );
        self.read_min_max(leaf_node)?;

        // The index gives us range of values for each dimension, but
        // the actual range of values might be much more
        // narrow than what the index told us, so we double check the
        // relation here, which is cheap yet might help
        // figure out that the block either entirely matches
        // or does not match at all. This is especially more likely in
        // the case that there are multiple dimensions
        // that have correlation, ie. splitting on one dimension also
        // significantly changes the range of values in another
        // dimension.
        let relation = visitor.compare(
          &self.scratch_packed_values
            [packed_bytes_length..packed_bytes_length + packed_index_bytes_length],
          &self.scratch_packed_values[packed_bytes_length + packed_index_bytes_length..],
        )?;
        if relation == Relation::CellOutsideQuery {
          return Ok(());
        }
        visitor.grow(count)?;

        if relation == Relation::CellInsideQuery {
          for i in 0..count {
            visitor.visit(self.scratch_iterator.doc_ids[i])?;
          }
          return Ok(());
        }
      } else {
        visitor.grow(count)?;
      }

      if compressed_dim == -2 {
        // low cardinality values
        self.visit_sparse_raw_doc_values(count, visitor, leaf_node)?;
      } else {
        // high cardinality
        self.visit_compressed_doc_values(count, visitor, compressed_dim, leaf_node)?;
      }
    }

    Ok(())
  }
  fn read_min_max(&mut self, index_input: &mut I) -> Result<()> {
    for dim in 0..self.config.num_index_dims {
      let prefix = self.common_prefix_lengths[dim];
      let offset = dim * self.config.bytes_per_dim + prefix;
      let len = self.config.bytes_per_dim - prefix;
      DataInput::read_bytes(
        index_input,
        &mut self.scratch_packed_values[self.config.packed_bytes_length()
          ..self.config.packed_bytes_length() + self.config.packed_index_bytes_length()],
        offset,
        len,
      )?;
      DataInput::read_bytes(
        index_input,
        &mut self.scratch_packed_values
          [self.config.packed_bytes_length() + self.config.packed_index_bytes_length()..],
        offset,
        len,
      )?;
    }

    Ok(())
  }

  // read cardinality and point
  fn visit_sparse_raw_doc_values<V>(
    &mut self,
    count: usize,
    visitor: &mut V,
    index_input: &mut I,
  ) -> Result<()>
  where
    V: IntersectVisitor,
  {
    let mut i: usize = 0;
    {
      while i < count {
        let length = DataInput::read_vint(index_input)?.try_convert()?;
        for dim in 0..self.config.num_dims {
          let prefix = self.common_prefix_lengths[dim];
          DataInput::read_bytes(
            index_input,
            &mut self.scratch_packed_values[..self.config.packed_bytes_length()],
            dim * self.config.bytes_per_dim + prefix,
            self.config.bytes_per_dim - prefix,
          )?;
        }
        self.scratch_iterator.reset(i, length);
        visitor.visit_iterator_with_packed_value(
          &mut self.scratch_iterator,
          &self.scratch_packed_values[..self.config.packed_bytes_length()],
        )?;
        i += length;
      }
    }

    if i != count {
      return Err(LuceneError::corrupt_index(format!(
        "Sub blocks do not add up to the expected count: {} != {}, (resource={})",
        count, i, index_input
      )));
    }

    Ok(())
  }

  // point is under commonPrefix
  pub fn visit_unique_raw_doc_values<V>(&mut self, count: usize, visitor: &mut V) -> Result<()>
  where
    V: IntersectVisitor,
  {
    self.scratch_iterator.reset(0, count);
    visitor.visit_iterator_with_packed_value(
      &mut self.scratch_iterator,
      &self.scratch_packed_values[..self.config.packed_bytes_length()],
    )?;
    Ok(())
  }
  fn visit_compressed_doc_values<V>(
    &mut self,
    count: usize,
    visitor: &mut V,
    compressed_dim: i32,
    index_input: &mut I,
  ) -> Result<()>
  where
    V: IntersectVisitor,
  {
    let bytes_per_dim = self.config.bytes_per_dim;
    let compressed_dim = compressed_dim as usize;

    // the byte at `compressedByteOffset` is compressed using run-length
    // compression, other suffix bytes are stored verbatim
    let compressed_byte_offset =
      compressed_dim * bytes_per_dim + self.common_prefix_lengths[compressed_dim];
    self.common_prefix_lengths[compressed_dim] += 1;

    let num_dims = self.config.num_dims;
    let common_prefix_lengths = &self.common_prefix_lengths[..num_dims];
    let scratch_data_packed_value =
      &mut self.scratch_packed_values[..self.config.packed_bytes_length()];
    let doc_ids = self.scratch_iterator.doc_ids.as_ref();
    let mut i = 0;
    {
      while i < count {
        scratch_data_packed_value[compressed_byte_offset] = DataInput::read_byte(index_input)?;
        let run_len = DataInput::read_byte(index_input)? as usize;
        for j in 0..run_len {
          // Preserve the measured indexed loop over the bounded dimension slice.
          #[allow(clippy::needless_range_loop)]
          for dim in 0..num_dims {
            let prefix = common_prefix_lengths[dim];
            DataInput::read_bytes(
              index_input,
              scratch_data_packed_value,
              dim * bytes_per_dim + prefix,
              bytes_per_dim - prefix,
            )?;
          }
          visitor.visit_with_packed_value(doc_ids[i + j], scratch_data_packed_value)?;
        }
        i += run_len;
      }
    }

    if i != count {
      return Err(LuceneError::corrupt_index(format!(
        "Sub blocks do not add up to the expected count: {} != {}, (resource={})",
        count, i, index_input,
      )));
    }

    Ok(())
  }
  fn read_compressed_dim(&mut self, leaf_node: &mut I) -> Result<i32> {
    let compressed_dim = DataInput::read_byte(leaf_node)? as i8 as i32;

    if compressed_dim < -2
      || compressed_dim >= self.config.num_dims as i32
      || (self.version < VERSION_LOW_CARDINALITY_LEAVES && compressed_dim == -2)
    {
      return Err(LuceneError::corrupt_index(format!(
        "Got compressedDim={} from input, (resource={})",
        compressed_dim, leaf_node,
      )));
    }

    Ok(compressed_dim)
  }

  pub fn read_common_prefixes(&mut self, index_input: &mut I) -> Result<()> {
    let num_dims = self.config.num_dims;
    for dim in 0..num_dims {
      let prefix = index_input.read_vint()?.try_convert()?;
      self.common_prefix_lengths[dim] = prefix;
      if prefix > 0 {
        DataInput::read_bytes(
          index_input,
          &mut self.scratch_packed_values[..self.config.packed_bytes_length()],
          dim * self.config.bytes_per_dim,
          prefix,
        )?;
      }
    }

    Ok(())
  }
}
impl<I> TryClone for BKDPointTree<I>
where
  I: IndexInput,
{
  fn try_clone(&self) -> Result<Self>
  where
    Self: Sized,
  {
    let mut index = BKDPointTree::with_scratch_iterator(
      self.inner_nodes.try_clone()?,
      self.leaf_nodes.clone(),
      self.config.clone(),
      self.leaf_node_offset,
      self.version,
      self.point_count,
      self.node_id,
      self.level,
      &self.packed_bounds[..self.config.packed_index_bytes_length()],
      &self.packed_bounds[self.config.packed_index_bytes_length()..],
      self.scratch_iterator.clone(),
      self.scratch_packed_values.to_vec(),
      self.common_prefix_lengths.to_vec(),
      self.is_tree_balanced,
    )?;

    let level = self.level;
    let dims = self.config.num_index_dims;

    index.leaf_positions[level].leaf_block_fp = self.leaf_positions[level].leaf_block_fp;

    if !self.is_leaf_node() {
      index.split_positions[level].right_node = self.split_positions[level].right_node;
      index.leaf_positions[level].read_node_data = self.leaf_positions[level].read_node_data;
      index.split_values_stack[level] = self.split_values_stack[level].clone();

      let src = level * dims;
      index
        .negative_deltas
        .copy_from(&self.negative_deltas[src..src + dims], src);

      index.split_positions[level].dimension = self.split_positions[level].dimension;
    }

    Ok(index)
  }
}

impl<I> PointTree for BKDPointTree<I>
where
  I: IndexInput,
{
  type MutablePointTree = DummyMutablePointTree;

  fn move_to_child(&mut self) -> Result<bool> {
    if self.is_leaf_node() {
      return Ok(false);
    }
    self.reset_node_data_position()?;
    self.push_bounds_left();
    self.push_left()?;
    Ok(true)
  }

  fn move_to_sibling(&mut self) -> Result<bool> {
    if !self.is_left_node() || self.is_root_node() {
      return Ok(false);
    }

    self.pop();
    self.pop_bounds(true);
    self.push_bounds_right();
    self.push_right()?;

    debug_assert!(self.node_exists(), "Sibling node must exist");
    Ok(true)
  }

  fn move_to_parent(&mut self) -> Result<bool> {
    if self.is_root_node() {
      return Ok(false);
    }
    let is_left = self.is_left_node();
    self.pop();
    self.pop_bounds(is_left);
    Ok(true)
  }

  fn get_min_packed_value(&self) -> Result<Cow<'_, [u8]>> {
    Ok(Cow::Borrowed(
      &self.packed_bounds[..self.config.packed_index_bytes_length()],
    ))
  }

  fn get_max_packed_value(&self) -> Result<Cow<'_, [u8]>> {
    Ok(Cow::Borrowed(
      &self.packed_bounds[self.config.packed_index_bytes_length()..],
    ))
  }

  fn size(&self) -> Result<usize> {
    let mut left_most_leaf_node = self.node_id;
    while left_most_leaf_node < self.leaf_node_offset {
      left_most_leaf_node *= 2;
    }

    let mut right_most_leaf_node = self.node_id;
    while right_most_leaf_node < self.leaf_node_offset {
      right_most_leaf_node = right_most_leaf_node * 2 + 1;
    }

    let num_leaves = if right_most_leaf_node >= left_most_leaf_node {
      // both are on the same level
      right_most_leaf_node - left_most_leaf_node + 1
    } else {
      // left is one level deeper than right
      right_most_leaf_node - left_most_leaf_node + 1 + self.leaf_node_offset
    };

    debug_assert!(
      num_leaves == self.get_num_leaves_slow(self.node_id),
      "numLeaves mismatch: {} vs {}",
      num_leaves,
      self.get_num_leaves_slow(self.node_id)
    );

    if self.is_tree_balanced {
      // before lucene 8.6, trees might have been constructed as fully
      // balanced trees.
      return self.size_from_balanced_tree(left_most_leaf_node, right_most_leaf_node);
    }

    // size for an unbalanced tree.
    let size = if right_most_leaf_node == self.right_most_leaf_node {
      (num_leaves as usize - 1) * self.config.max_points_in_leaf_node
        + self.last_leaf_node_point_count
    } else {
      num_leaves as usize * self.config.max_points_in_leaf_node
    };

    Ok(size)
  }

  fn visit_doc_ids<IV>(&mut self, visitor: &mut IV) -> Result<()>
  where
    IV: IntersectVisitor,
  {
    let leaf_node = self.leaf_nodes.clone();
    self.reset_node_data_position()?;
    self.add_all(visitor, false, &mut *leaf_node.lock())
  }

  fn visit_doc_values<IV>(&mut self, visitor: &mut IV) -> Result<()>
  where
    IV: IntersectVisitor,
  {
    let leaf_node = self.leaf_nodes.clone();
    self.reset_node_data_position()?;
    self.visit_leaves_one_by_one(visitor, &mut *leaf_node.lock())
  }
}
/// Reusable [`DocIdSetIterator`] to handle low cardinality leaves.
#[derive(Clone)]
struct BKDReaderDocIDSetIterator {
  idx: usize,
  length: usize,
  offset: usize,
  doc_id: i32,
  doc_ids: Box<[i32]>,
  doc_ids_writer: DocIdsWriter,
}

impl BKDReaderDocIDSetIterator {
  pub fn new(max_points_in_leaf_node: usize) -> Self {
    Self {
      idx: 0,
      length: 0,
      offset: 0,
      doc_id: -1,
      doc_ids: vec![0; max_points_in_leaf_node].into_boxed_slice(),
      doc_ids_writer: DocIdsWriter::new(max_points_in_leaf_node),
    }
  }
  fn reset(&mut self, offset: usize, length: usize) {
    self.offset = offset;
    self.length = length;
    debug_assert!((offset + length) <= self.doc_ids.len());
    self.doc_id = -1;
    self.idx = 0;
  }
}
impl crate::core::search::doc_id_set_iterator::DocIdSetIteratorExtensions
  for BKDReaderDocIDSetIterator
{
}
impl crate::core::search::doc_id_set_iterator::BitSetIteratorAccess for BKDReaderDocIDSetIterator {}

impl DocIdSetIterator for BKDReaderDocIDSetIterator {
  fn doc_id(&self) -> i32 {
    self.doc_id
  }

  fn next_doc(&mut self) -> Result<i32> {
    if self.idx == self.length {
      self.doc_id = NO_MORE_DOCS;
    } else {
      self.doc_id = self.doc_ids[self.offset + self.idx];
      self.idx += 1;
    }
    Ok(self.doc_id)
  }

  fn advance(&mut self, target: i32) -> Result<i32> {
    DocIdSetIterator::slow_advance(self, target)
  }

  fn cost(&self) -> Result<i64> {
    Ok(self.length as i64)
  }
}

struct IntersectVisitorImpl {
  count: usize,
}
impl IntersectVisitorImpl {
  fn new() -> Self {
    Self { count: 0 }
  }
}
impl IntersectVisitor for IntersectVisitorImpl {
  fn visit(&mut self, _doc_id: i32) -> Result<()> {
    self.count += 1;
    Ok(())
  }

  fn visit_with_packed_value(&mut self, _doc_id: i32, _packed_value: &[u8]) -> Result<()> {
    Err(LuceneError::not_implemented(""))
  }

  fn compare(&self, _min_packed_value: &[u8], _max_packed_value: &[u8]) -> Result<Relation> {
    Err(LuceneError::not_implemented(""))
  }
}