LUCENE-2858: Hide ReaderContext ctors for users. You should only get top contexts from the reader.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene2858@1237512 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-01-30 00:35:08 +00:00
parent 2d4c738067
commit 20fde71669
4 changed files with 66 additions and 69 deletions

View File

@ -293,7 +293,7 @@ public abstract class AtomicReader extends IndexReader {
/**
* Creates a new {@link AtomicReaderContext}
*/
public AtomicReaderContext(CompositeReaderContext parent, AtomicReader reader,
AtomicReaderContext(CompositeReaderContext parent, AtomicReader reader,
int ord, int docBase, int leafOrd, int leafDocBase) {
super(parent, ord, docBase);
this.ord = leafOrd;
@ -301,7 +301,7 @@ public abstract class AtomicReader extends IndexReader {
this.reader = reader;
}
public AtomicReaderContext(AtomicReader atomicReader) {
AtomicReaderContext(AtomicReader atomicReader) {
this(null, atomicReader, 0, 0, 0, 0);
}

View File

@ -17,6 +17,8 @@ package org.apache.lucene.index;
* limitations under the License.
*/
import java.io.IOException;
import org.apache.lucene.index.AtomicReader.AtomicReaderContext;
import org.apache.lucene.search.SearcherManager; // javadocs
import org.apache.lucene.store.*;
@ -84,7 +86,7 @@ public abstract class CompositeReader extends IndexReader {
// lazy init without thread safety for perf resaons: Building the readerContext twice does not hurt!
if (readerContext == null) {
assert getSequentialSubReaders() != null;
readerContext = (CompositeReaderContext) ReaderUtil.buildReaderContext(this);
readerContext = (CompositeReaderContext) new ReaderContextBuilder(this).build();
}
return readerContext;
}
@ -111,7 +113,7 @@ public abstract class CompositeReader extends IndexReader {
* Creates a {@link CompositeReaderContext} for intermediate readers that aren't
* not top-level readers in the current context
*/
public CompositeReaderContext(CompositeReaderContext parent, CompositeReader reader,
CompositeReaderContext(CompositeReaderContext parent, CompositeReader reader,
int ordInParent, int docbaseInParent, ReaderContext[] children) {
this(parent, reader, ordInParent, docbaseInParent, children, null);
}
@ -119,7 +121,7 @@ public abstract class CompositeReader extends IndexReader {
/**
* Creates a {@link CompositeReaderContext} for top-level readers with parent set to <code>null</code>
*/
public CompositeReaderContext(CompositeReader reader, ReaderContext[] children, AtomicReaderContext[] leaves) {
CompositeReaderContext(CompositeReader reader, ReaderContext[] children, AtomicReaderContext[] leaves) {
this(null, reader, 0, 0, children, leaves);
}
@ -148,4 +150,62 @@ public abstract class CompositeReader extends IndexReader {
return reader;
}
}
private static class ReaderContextBuilder {
private final IndexReader reader;
private final AtomicReaderContext[] leaves;
private int leafOrd = 0;
private int leafDocBase = 0;
public ReaderContextBuilder(IndexReader reader) {
this.reader = reader;
leaves = new AtomicReaderContext[numLeaves(reader)];
}
public ReaderContext build() {
return build(null, reader, 0, 0);
}
private ReaderContext build(CompositeReaderContext parent, IndexReader reader, int ord, int docBase) {
if (reader instanceof AtomicReader) {
AtomicReaderContext atomic = new AtomicReaderContext(parent, (AtomicReader) reader, ord, docBase, leafOrd, leafDocBase);
leaves[leafOrd++] = atomic;
leafDocBase += reader.maxDoc();
return atomic;
} else {
CompositeReader cr = (CompositeReader) reader;
IndexReader[] sequentialSubReaders = cr.getSequentialSubReaders();
ReaderContext[] children = new ReaderContext[sequentialSubReaders.length];
final CompositeReaderContext newParent;
if (parent == null) {
newParent = new CompositeReaderContext(cr, children, leaves);
} else {
newParent = new CompositeReaderContext(parent, cr, ord, docBase, children);
}
int newDocBase = 0;
for (int i = 0; i < sequentialSubReaders.length; i++) {
children[i] = build(newParent, sequentialSubReaders[i], i, newDocBase);
newDocBase += sequentialSubReaders[i].maxDoc();
}
return newParent;
}
}
private int numLeaves(IndexReader reader) {
final int[] numLeaves = new int[1];
try {
new ReaderUtil.Gather(reader) {
@Override
protected void add(int base, AtomicReader r) {
numLeaves[0]++;
}
}.run();
} catch (IOException ioe) {
// won't happen
throw new RuntimeException(ioe);
}
return numLeaves[0];
}
}
}

View File

@ -24,7 +24,6 @@ import java.io.IOException;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.CompositeReader;
import org.apache.lucene.index.CompositeReader.CompositeReaderContext;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReader.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
@ -121,68 +120,6 @@ public final class ReaderUtil {
protected abstract void add(int base, AtomicReader r) throws IOException;
}
public static ReaderContext buildReaderContext(IndexReader reader) {
return new ReaderContextBuilder(reader).build();
}
public static class ReaderContextBuilder {
private final IndexReader reader;
private final AtomicReaderContext[] leaves;
private int leafOrd = 0;
private int leafDocBase = 0;
public ReaderContextBuilder(IndexReader reader) {
this.reader = reader;
leaves = new AtomicReaderContext[numLeaves(reader)];
}
public ReaderContext build() {
return build(null, reader, 0, 0);
}
private ReaderContext build(CompositeReaderContext parent, IndexReader reader, int ord, int docBase) {
if (reader instanceof AtomicReader) {
AtomicReaderContext atomic = new AtomicReaderContext(parent, (AtomicReader) reader, ord, docBase, leafOrd, leafDocBase);
leaves[leafOrd++] = atomic;
leafDocBase += reader.maxDoc();
return atomic;
} else {
CompositeReader cr = (CompositeReader) reader;
IndexReader[] sequentialSubReaders = cr.getSequentialSubReaders();
ReaderContext[] children = new ReaderContext[sequentialSubReaders.length];
final CompositeReaderContext newParent;
if (parent == null) {
newParent = new CompositeReaderContext(cr, children, leaves);
} else {
newParent = new CompositeReaderContext(parent, cr, ord, docBase, children);
}
int newDocBase = 0;
for (int i = 0; i < sequentialSubReaders.length; i++) {
children[i] = build(newParent, sequentialSubReaders[i], i, newDocBase);
newDocBase += sequentialSubReaders[i].maxDoc();
}
return newParent;
}
}
private int numLeaves(IndexReader reader) {
final int[] numLeaves = new int[1];
try {
new Gather(reader) {
@Override
protected void add(int base, AtomicReader r) {
numLeaves[0]++;
}
}.run();
} catch (IOException ioe) {
// won't happen
throw new RuntimeException(ioe);
}
return numLeaves[0];
}
}
/**
* Returns the context's leaves or the context itself as the only element of
* the returned array. If the context's #leaves() method returns

View File

@ -132,7 +132,7 @@ public class BooleanFilterTest extends LuceneTestCase {
private void tstFilterCard(String mes, int expected, Filter filt)
throws Exception {
// BooleanFilter never returns null DIS or null DISI!
DocIdSetIterator disi = filt.getDocIdSet(new AtomicReaderContext(reader), reader.getLiveDocs()).iterator();
DocIdSetIterator disi = filt.getDocIdSet(reader.getTopReaderContext(), reader.getLiveDocs()).iterator();
int actual = 0;
while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
actual++;