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
/*
* 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::leaf_reader::LeafReader;
use crate::core::index::leaf_reader_context::{LeafReaderContext, TopParentMeta};
/// Common utility methods for dealing with
/// [`IndexReader`](crate::core::index::index_reader::IndexReader)s and
/// [`IndexReaderContext`](crate::core::index::index_reader_context::IndexReaderContext)s.
///
/// @lucene.internal
pub struct ReaderUtil;
impl ReaderUtil {
/// Walks up the reader tree and returns the given context's top
/// level reader context, or in other words the reader tree's root
/// context.
pub fn get_top_level_context<LR>(leaf_reader: &LeafReaderContext<LR>) -> &TopParentMeta
where
LR: LeafReader,
{
leaf_reader.top_parent()
}
/// Returns index of the searcher/reader for document n in the array used to construct this searcher/reader.
pub fn sub_index(n: usize, doc_starts: &[usize]) -> i32 {
debug_assert!(doc_starts.len() <= i32::MAX as usize);
// find searcher/reader for doc n:
let size = doc_starts.len() as i32;
let mut lo: i32 = 0; // search starts array
let mut hi: i32 = size - 1; // for first element less than n, return its index
while hi >= lo {
let mid = (lo + hi) >> 1;
let mid_value = doc_starts[mid as usize];
if n < mid_value {
hi = mid - 1;
} else if n > mid_value {
lo = mid + 1;
} else {
// found a match
let mut mid = mid;
while (mid + 1) < size && doc_starts[(mid + 1) as usize] == mid_value {
mid += 1; // scan to last match
}
return mid;
}
}
hi
}
/// Returns index of the searcher/reader for document n in the array used to construct this searcher/reader.
pub fn sub_index_with_leaves<LR>(n: i32, leaves: &[LeafReaderContext<LR>]) -> usize
where
LR: LeafReader,
{
// find searcher/reader for doc n:
let size = leaves.len() as i32;
let mut lo: i32 = 0; // search starts array
let mut hi: i32 = size - 1; // for first element less than n, return its index
while hi >= lo {
let mid = (lo + hi) >> 1;
let mid_value = leaves[mid as usize].doc_base;
let mid_doc = mid_value as i32;
if n < mid_doc {
hi = mid - 1;
} else if n > mid_doc {
lo = mid + 1;
} else {
let mut mid = mid;
while (mid + 1) < size && leaves[(mid + 1) as usize].doc_base == mid_value {
mid += 1;
}
return mid as usize;
}
}
hi.max(0) as usize
}
}