1 package org.apache.lucene.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.apache.lucene.index.IndexReader;
27 * Common util methods for dealing with {@link IndexReader}s.
31 public final class ReaderUtil {
33 private ReaderUtil() {} // no instance
36 * Gathers sub-readers from reader into a List.
38 * @param allSubReaders
41 public static void gatherSubReaders(List<IndexReader> allSubReaders, IndexReader reader) {
42 IndexReader[] subReaders = reader.getSequentialSubReaders();
43 if (subReaders == null) {
44 // Add the reader itself, and do not recurse
45 allSubReaders.add(reader);
47 for (int i = 0; i < subReaders.length; i++) {
48 gatherSubReaders(allSubReaders, subReaders[i]);
53 /** Recursively visits all sub-readers of a reader. You
54 * should subclass this and override the add method to
55 * gather what you need.
57 * @lucene.experimental */
58 public static abstract class Gather {
59 private final IndexReader topReader;
61 public Gather(IndexReader r) {
65 public int run() throws IOException {
66 return run(0, topReader);
69 public int run(int docBase) throws IOException {
70 return run(docBase, topReader);
73 private int run(int base, IndexReader reader) throws IOException {
74 IndexReader[] subReaders = reader.getSequentialSubReaders();
75 if (subReaders == null) {
78 base += reader.maxDoc();
81 for (int i = 0; i < subReaders.length; i++) {
82 base = run(base, subReaders[i]);
89 protected abstract void add(int base, IndexReader r) throws IOException;
93 * Returns sub IndexReader that contains the given document id.
95 * @param doc id of document
96 * @param reader parent reader
97 * @return sub reader of parent which contains the specified doc id
99 public static IndexReader subReader(int doc, IndexReader reader) {
100 List<IndexReader> subReadersList = new ArrayList<IndexReader>();
101 ReaderUtil.gatherSubReaders(subReadersList, reader);
102 IndexReader[] subReaders = subReadersList
103 .toArray(new IndexReader[subReadersList.size()]);
104 int[] docStarts = new int[subReaders.length];
106 for (int i = 0; i < subReaders.length; i++) {
107 docStarts[i] = maxDoc;
108 maxDoc += subReaders[i].maxDoc();
110 return subReaders[ReaderUtil.subIndex(doc, docStarts)];
114 * Returns sub-reader subIndex from reader.
116 * @param reader parent reader
117 * @param subIndex index of desired sub reader
118 * @return the subreader at subIndex
120 public static IndexReader subReader(IndexReader reader, int subIndex) {
121 List<IndexReader> subReadersList = new ArrayList<IndexReader>();
122 ReaderUtil.gatherSubReaders(subReadersList, reader);
123 IndexReader[] subReaders = subReadersList
124 .toArray(new IndexReader[subReadersList.size()]);
125 return subReaders[subIndex];
130 * Returns index of the searcher/reader for document <code>n</code> in the
131 * array used to construct this searcher/reader.
133 public static int subIndex(int n, int[] docStarts) { // find
134 // searcher/reader for doc n:
135 int size = docStarts.length;
136 int lo = 0; // search starts array
137 int hi = size - 1; // for first element less than n, return its index
139 int mid = (lo + hi) >>> 1;
140 int midValue = docStarts[mid];
143 else if (n > midValue)
145 else { // found a match
146 while (mid + 1 < size && docStarts[mid + 1] == midValue) {
147 mid++; // scan to last match