pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / ReaderUtil.java
1 package org.apache.lucene.util;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.lucene.index.IndexReader;
25
26 /**
27  * Common util methods for dealing with {@link IndexReader}s.
28  *
29  * @lucene.internal
30  */
31 public final class ReaderUtil {
32
33   private ReaderUtil() {} // no instance
34
35   /**
36    * Gathers sub-readers from reader into a List.
37    * 
38    * @param allSubReaders
39    * @param reader
40    */
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);
46     } else {
47       for (int i = 0; i < subReaders.length; i++) {
48         gatherSubReaders(allSubReaders, subReaders[i]);
49       }
50     }
51   }
52
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.
56    *
57    * @lucene.experimental */
58   public static abstract class Gather {
59     private final IndexReader topReader;
60
61     public Gather(IndexReader r) {
62       topReader = r;
63     }
64
65     public int run() throws IOException {
66       return run(0, topReader);
67     }
68
69     public int run(int docBase) throws IOException {
70       return run(docBase, topReader);
71     }
72
73     private int run(int base, IndexReader reader) throws IOException {
74       IndexReader[] subReaders = reader.getSequentialSubReaders();
75       if (subReaders == null) {
76         // atomic reader
77         add(base, reader);
78         base += reader.maxDoc();
79       } else {
80         // composite reader
81         for (int i = 0; i < subReaders.length; i++) {
82           base = run(base, subReaders[i]);
83         }
84       }
85
86       return base;
87     }
88
89     protected abstract void add(int base, IndexReader r) throws IOException;
90   }
91
92   /**
93    * Returns sub IndexReader that contains the given document id.
94    *    
95    * @param doc id of document
96    * @param reader parent reader
97    * @return sub reader of parent which contains the specified doc id
98    */
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];
105     int maxDoc = 0;
106     for (int i = 0; i < subReaders.length; i++) {
107       docStarts[i] = maxDoc;
108       maxDoc += subReaders[i].maxDoc();
109     }
110     return subReaders[ReaderUtil.subIndex(doc, docStarts)];
111   }
112   
113   /**
114    * Returns sub-reader subIndex from reader.
115    * 
116    * @param reader parent reader
117    * @param subIndex index of desired sub reader
118    * @return the subreader at subIndex
119    */
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];
126   }
127
128
129   /**
130    * Returns index of the searcher/reader for document <code>n</code> in the
131    * array used to construct this searcher/reader.
132    */
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
138     while (hi >= lo) {
139       int mid = (lo + hi) >>> 1;
140       int midValue = docStarts[mid];
141       if (n < midValue)
142         hi = mid - 1;
143       else if (n > midValue)
144         lo = mid + 1;
145       else { // found a match
146         while (mid + 1 < size && docStarts[mid + 1] == midValue) {
147           mid++; // scan to last match
148         }
149         return mid;
150       }
151     }
152     return hi;
153   }
154 }