Apache Solr open-source search software
LUCENE-3606: remove more readOnly booleans
Uwe Schindler6bb6a81edd4a
diff --git a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/src/java/org/apache/lucene/index/DirectoryReader.javaindex 03f3d449bdc..f224099c106 100644--- a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java+++ b/lucene/src/java/org/apache/lucene/index/DirectoryReader.java@@ -44,7 +44,6 @@ import org.apache.lucene.util.MapBackedSet;*/class DirectoryReader extends IndexReader implements Cloneable {protected Directory directory;- protected boolean readOnly = true; // nocommit: remove thisIndexWriter writer;@@ -58,28 +57,23 @@ class DirectoryReader extends IndexReader implements Cloneable {private int numDocs = -1;private boolean hasDeletions = false;- // Max version in index as of when we opened; this can be- // > our current segmentInfos version in case we were- // opened on a past IndexCommit:- private long maxIndexVersion;-private final boolean applyAllDeletes;- static IndexReader open(final Directory directory, final IndexCommit commit, final boolean readOnly,+ static IndexReader open(final Directory directory, final IndexCommit commit,final int termInfosIndexDivisor) throws CorruptIndexException, IOException {return (IndexReader) new SegmentInfos.FindSegmentsFile(directory) {@Overrideprotected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {SegmentInfos infos = new SegmentInfos();infos.read(directory, segmentFileName);- return new DirectoryReader(directory, infos, readOnly, termInfosIndexDivisor);+ return new DirectoryReader(directory, infos, termInfosIndexDivisor);}}.run(commit);}+/** Construct reading the named set of readers. */- DirectoryReader(Directory directory, SegmentInfos sis, boolean readOnly, int termInfosIndexDivisor) throws IOException {+ DirectoryReader(Directory directory, SegmentInfos sis, int termInfosIndexDivisor) throws IOException {this.directory = directory;- this.readOnly = true; // nocommit: remove readOnly at allthis.segmentInfos = sis;this.termInfosIndexDivisor = termInfosIndexDivisor;readerFinishedListeners = new MapBackedSet<ReaderFinishedListener>(new ConcurrentHashMap<ReaderFinishedListener,Boolean>());@@ -94,7 +88,7 @@ class DirectoryReader extends IndexReader implements Cloneable {for (int i = sis.size()-1; i >= 0; i--) {boolean success = false;try {- readers[i] = SegmentReader.get(readOnly, sis.info(i), termInfosIndexDivisor, IOContext.READ);+ readers[i] = SegmentReader.get(true /** nocommit: remove readOnly */, sis.info(i), termInfosIndexDivisor, IOContext.READ);readers[i].readerFinishedListeners = readerFinishedListeners;success = true;} finally {@@ -117,7 +111,6 @@ class DirectoryReader extends IndexReader implements Cloneable {// Used by near real-time searchDirectoryReader(IndexWriter writer, SegmentInfos infos, boolean applyAllDeletes) throws IOException {this.directory = writer.getDirectory();- this.readOnly = true;this.applyAllDeletes = applyAllDeletes; // saved for reopenthis.termInfosIndexDivisor = writer.getConfig().getReaderTermsIndexDivisor();@@ -169,9 +162,8 @@ class DirectoryReader extends IndexReader implements Cloneable {/** This constructor is only used for {@link #doOpenIfChanged()} */DirectoryReader(Directory directory, SegmentInfos infos, SegmentReader[] oldReaders,- boolean readOnly, boolean doClone, int termInfosIndexDivisor, Collection<ReaderFinishedListener> readerFinishedListeners) throws IOException {+ boolean doClone, int termInfosIndexDivisor, Collection<ReaderFinishedListener> readerFinishedListeners) throws IOException {this.directory = directory;- this.readOnly = readOnly;this.segmentInfos = infos;this.termInfosIndexDivisor = termInfosIndexDivisor;this.readerFinishedListeners = readerFinishedListeners;@@ -214,12 +206,12 @@ class DirectoryReader extends IndexReader implements Cloneable {assert !doClone;// this is a new reader; in case we hit an exception we can close it safely- newReader = SegmentReader.get(readOnly, infos.info(i), termInfosIndexDivisor, IOContext.READ);+ newReader = SegmentReader.get(true, infos.info(i), termInfosIndexDivisor, IOContext.READ);newReader.readerFinishedListeners = readerFinishedListeners;readerShared[i] = false;newReaders[i] = newReader;} else {- newReader = newReaders[i].reopenSegment(infos.info(i), doClone, readOnly);+ newReader = newReaders[i].reopenSegment(infos.info(i), doClone, true /* nocommit: remove readOnly */);if (newReader == null) {// this reader will be shared between the old and the new one,// so we must incRef it@@ -303,10 +295,6 @@ class DirectoryReader extends IndexReader implements Cloneable {}}starts[subReaders.length] = maxDoc;-- if (!readOnly) {- maxIndexVersion = SegmentInfos.readCurrentVersion(directory);- }}@Override@@ -317,7 +305,7 @@ class DirectoryReader extends IndexReader implements Cloneable {@Overridepublic final synchronized Object clone() {try {- return clone(readOnly); // Preserve current readOnly+ return clone(true);} catch (Exception ex) {throw new RuntimeException(ex);}@@ -325,6 +313,7 @@ class DirectoryReader extends IndexReader implements Cloneable {@Overridepublic final synchronized IndexReader clone(boolean openReadOnly) throws CorruptIndexException, IOException {+ assert openReadOnly;// doOpenIfChanged calls ensureOpenDirectoryReader newReader = doOpenIfChanged((SegmentInfos) segmentInfos.clone(), true, openReadOnly);@@ -337,8 +326,7 @@ class DirectoryReader extends IndexReader implements Cloneable {@Overrideprotected final IndexReader doOpenIfChanged() throws CorruptIndexException, IOException {- // Preserve current readOnly- return doOpenIfChanged(readOnly, null);+ return doOpenIfChanged(true, null);}@Override@@ -361,11 +349,7 @@ class DirectoryReader extends IndexReader implements Cloneable {}private final IndexReader doOpenFromWriter(boolean openReadOnly, IndexCommit commit) throws CorruptIndexException, IOException {- assert readOnly;-- if (!openReadOnly) {- throw new IllegalArgumentException("a reader obtained from IndexWriter.getReader() can only be reopened with openReadOnly=true (got false)");- }+ assert openReadOnly;if (commit != null) {throw new IllegalArgumentException("a reader obtained from IndexWriter.getReader() cannot currently accept a commit");@@ -390,7 +374,7 @@ class DirectoryReader extends IndexReader implements Cloneable {private IndexReader doOpenIfChanged(final boolean openReadOnly, IndexCommit commit) throws CorruptIndexException, IOException {ensureOpen();- assert commit == null || openReadOnly;+ assert openReadOnly;// If we were obtained by writer.getReader(), re-ask the// writer to get a new reader.@@ -427,7 +411,8 @@ class DirectoryReader extends IndexReader implements Cloneable {}private synchronized DirectoryReader doOpenIfChanged(SegmentInfos infos, boolean doClone, boolean openReadOnly) throws CorruptIndexException, IOException {- return new DirectoryReader(directory, infos, subReaders, openReadOnly, doClone, termInfosIndexDivisor, readerFinishedListeners);+ assert openReadOnly;+ return new DirectoryReader(directory, infos, subReaders, doClone, termInfosIndexDivisor, readerFinishedListeners);}/** Version number when this IndexReader was opened. */diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.javaindex 29152b44360..5f8087953dc 100644--- a/lucene/src/java/org/apache/lucene/index/IndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java@@ -456,8 +456,8 @@ public abstract class IndexReader implements Cloneable,Closeable {}private static IndexReader open(final Directory directory, final IndexDeletionPolicy deletionPolicy, final IndexCommit commit, final boolean readOnly, int termInfosIndexDivisor) throws CorruptIndexException, IOException {- // nocommit: deletionPolicy is ignored -> remove it- return DirectoryReader.open(directory, commit, readOnly, termInfosIndexDivisor);+ // nocommit: deletionPolicy is ignored -> remove it, same for readonly+ return DirectoryReader.open(directory, commit, termInfosIndexDivisor);}/**diff --git a/lucene/src/java/org/apache/lucene/index/SegmentReader.java b/lucene/src/java/org/apache/lucene/index/SegmentReader.javaindex 9bee668d5b8..21225538bd9 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentReader.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentReader.java@@ -51,14 +51,13 @@ public class SegmentReader extends IndexReader implements Cloneable {boolean hasChanges = false;private boolean liveDocsDirty = false;- // TODO: we should move this tracking into SegmentInfo;- // this way SegmentInfo.toString shows pending deletes+ // nocommit: move the whole modification stuff to IWprivate int pendingDeleteCount;-private boolean rollbackHasChanges = false;private boolean rollbackDeletedDocsDirty = false;private SegmentInfo rollbackSegmentInfo;private int rollbackPendingDeleteCount;+ // end nocommitSegmentCoreReaders core;diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReaderClone.java b/lucene/src/test/org/apache/lucene/index/TestIndexReaderClone.javaindex 37c02e815f8..0b961e392d7 100644--- a/lucene/src/test/org/apache/lucene/index/TestIndexReaderClone.java+++ b/lucene/src/test/org/apache/lucene/index/TestIndexReaderClone.java@@ -17,14 +17,11 @@ package org.apache.lucene.index;* limitations under the License.*/-import org.apache.lucene.search.similarities.DefaultSimilarity;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.TextField;import org.apache.lucene.store.Directory;-import org.apache.lucene.store.LockObtainFailedException;import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.util.Bits;/*** Tests cloning multiple types of readers, modifying the liveDocs and norms@@ -32,63 +29,6 @@ import org.apache.lucene.util.Bits;* implemented properly*/public class TestIndexReaderClone extends LuceneTestCase {-- // open non-readOnly reader1 on multi-segment index, then- // fully merge the index, then clone to readOnly reader2- public void testReadOnlyCloneAfterFullMerge() throws Exception {- final Directory dir1 = newDirectory();-- TestIndexReaderReopen.createIndex(random, dir1, true);- IndexReader reader1 = IndexReader.open(dir1, false);- IndexWriter w = new IndexWriter(dir1, newIndexWriterConfig(- TEST_VERSION_CURRENT, new MockAnalyzer(random)));- w.forceMerge(1);- w.close();- IndexReader reader2 = reader1.clone(true);- assertTrue(isReadOnly(reader2));- reader1.close();- reader2.close();- dir1.close();- }-- public void testCloneReadOnlyDirectoryReader() throws Exception {- final Directory dir1 = newDirectory();-- TestIndexReaderReopen.createIndex(random, dir1, true);- IndexReader reader = IndexReader.open(dir1, false);- IndexReader readOnlyReader = reader.clone(true);- if (!isReadOnly(readOnlyReader)) {- fail("reader isn't read only");- }- reader.close();- readOnlyReader.close();- dir1.close();- }-- public static boolean isReadOnly(IndexReader r) {- if (r instanceof SegmentReader) {- return ((SegmentReader) r).readOnly;- } else if (r instanceof DirectoryReader) {- return ((DirectoryReader) r).readOnly;- } else {- return false;- }- }-- public void testSegmentReaderCloseReferencing() throws Exception {- final Directory dir1 = newDirectory();- TestIndexReaderReopen.createIndex(random, dir1, false);- SegmentReader origSegmentReader = getOnlySegmentReader(IndexReader.open(dir1, false));- origSegmentReader.deleteDocument(1);-- SegmentReader clonedSegmentReader = (SegmentReader) origSegmentReader- .clone();- assertDelDocsRefCountEquals(2, origSegmentReader);- origSegmentReader.close();- assertDelDocsRefCountEquals(1, origSegmentReader);- clonedSegmentReader.close();- dir1.close();- }private void assertDelDocsRefCountEquals(int refCount, SegmentReader reader) {assertEquals(refCount, reader.liveDocsRef.get());@@ -105,8 +45,8 @@ public class TestIndexReaderClone extends LuceneTestCase {doc.add(newField("field", "yes it's stored", TextField.TYPE_STORED));w.addDocument(doc);w.close();- IndexReader r1 = IndexReader.open(dir, false);- IndexReader r2 = r1.clone(false);+ IndexReader r1 = IndexReader.open(dir);+ IndexReader r2 = (IndexReader) r1.clone();r1.close();r2.close();dir.close();