LUCENE-2858: Fix more tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene2858@1237342 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-01-29 16:52:18 +00:00
parent f85c85c240
commit c6e848d2b1
3 changed files with 100 additions and 337 deletions

View File

@ -50,7 +50,7 @@ public class TestReuseDocsEnum extends LuceneTestCase {
createRandomIndex(numdocs, writer, random);
writer.commit();
IndexReader open = IndexReader.open(dir);
DirectoryReader open = DirectoryReader.open(dir);
new ReaderUtil.Gather(open) {
@Override
protected void add(int base, AtomicIndexReader r) throws IOException {
@ -79,10 +79,10 @@ public class TestReuseDocsEnum extends LuceneTestCase {
createRandomIndex(numdocs, writer, random);
writer.commit();
IndexReader open = IndexReader.open(dir);
DirectoryReader open = DirectoryReader.open(dir);
IndexReader[] sequentialSubReaders = open.getSequentialSubReaders();
for (IndexReader indexReader : sequentialSubReaders) {
Terms terms = indexReader.terms("body");
Terms terms = ((AtomicIndexReader) indexReader).terms("body");
TermsEnum iterator = terms.iterator(null);
IdentityHashMap<DocsEnum, Boolean> enums = new IdentityHashMap<DocsEnum, Boolean>();
MatchNoBits bits = new Bits.MatchNoBits(open.maxDoc());
@ -124,13 +124,13 @@ public class TestReuseDocsEnum extends LuceneTestCase {
createRandomIndex(numdocs, writer, random);
writer.commit();
IndexReader firstReader = IndexReader.open(dir);
IndexReader secondReader = IndexReader.open(dir);
DirectoryReader firstReader = DirectoryReader.open(dir);
DirectoryReader secondReader = DirectoryReader.open(dir);
IndexReader[] sequentialSubReaders = firstReader.getSequentialSubReaders();
IndexReader[] sequentialSubReaders2 = secondReader.getSequentialSubReaders();
for (IndexReader indexReader : sequentialSubReaders) {
Terms terms = indexReader.terms("body");
Terms terms = ((AtomicIndexReader) indexReader).terms("body");
TermsEnum iterator = terms.iterator(null);
IdentityHashMap<DocsEnum, Boolean> enums = new IdentityHashMap<DocsEnum, Boolean>();
MatchNoBits bits = new Bits.MatchNoBits(firstReader.maxDoc());
@ -159,7 +159,7 @@ public class TestReuseDocsEnum extends LuceneTestCase {
if (random.nextInt(10) == 0) {
return null;
}
IndexReader indexReader = readers[random.nextInt(readers.length)];
AtomicIndexReader indexReader = (AtomicIndexReader) readers[random.nextInt(readers.length)];
return indexReader.termDocsEnum(bits, field, term, random.nextBoolean());
}

View File

@ -58,7 +58,7 @@ public class TestIndexReader extends LuceneTestCase {
addDocumentWithFields(writer);
writer.close();
// set up reader:
IndexReader reader = IndexReader.open(d);
DirectoryReader reader = DirectoryReader.open(d);
assertTrue(reader.isCurrent());
// modify index by adding another document:
writer = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT,
@ -101,8 +101,8 @@ public class TestIndexReader extends LuceneTestCase {
writer.close();
// set up reader
IndexReader reader = IndexReader.open(d);
FieldInfos fieldInfos = ReaderUtil.getMergedFieldInfos(reader);
DirectoryReader reader = DirectoryReader.open(d);
FieldInfos fieldInfos = MultiFields.getMergedFieldInfos(reader);
assertNotNull(fieldInfos.fieldInfo("keyword"));
assertNotNull(fieldInfos.fieldInfo("text"));
assertNotNull(fieldInfos.fieldInfo("unindexed"));
@ -162,8 +162,8 @@ public class TestIndexReader extends LuceneTestCase {
writer.close();
// verify fields again
reader = IndexReader.open(d);
fieldInfos = ReaderUtil.getMergedFieldInfos(reader);
reader = DirectoryReader.open(d);
fieldInfos = MultiFields.getMergedFieldInfos(reader);
Collection<String> allFieldNames = new HashSet<String>();
Collection<String> indexedFieldNames = new HashSet<String>();
@ -301,7 +301,7 @@ public class TestIndexReader extends LuceneTestCase {
doc.add(new TextField("junk", "junk text"));
writer.addDocument(doc);
writer.close();
IndexReader reader = IndexReader.open(dir);
DirectoryReader reader = DirectoryReader.open(dir);
Document doc2 = reader.document(reader.maxDoc() - 1);
IndexableField[] fields = doc2.getFields("bin1");
assertNotNull(fields);
@ -320,7 +320,7 @@ public class TestIndexReader extends LuceneTestCase {
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND).setMergePolicy(newLogMergePolicy()));
writer.forceMerge(1);
writer.close();
reader = IndexReader.open(dir);
reader = DirectoryReader.open(dir);
doc2 = reader.document(reader.maxDoc() - 1);
fields = doc2.getFields("bin1");
assertNotNull(fields);
@ -343,8 +343,8 @@ public class TestIndexReader extends LuceneTestCase {
fileDirName.mkdir();
}
try {
IndexReader.open(fileDirName);
fail("opening IndexReader on empty directory failed to produce FileNotFoundException");
DirectoryReader.open(fileDirName);
fail("opening DirectoryReader on empty directory failed to produce FileNotFoundException");
} catch (FileNotFoundException e) {
// GOOD
}
@ -372,7 +372,7 @@ public class TestIndexReader extends LuceneTestCase {
// Now open existing directory and test that reader closes all files
dir = newFSDirectory(dirFile);
IndexReader reader1 = IndexReader.open(dir);
DirectoryReader reader1 = DirectoryReader.open(dir);
reader1.close();
dir.close();
@ -390,7 +390,7 @@ public class TestIndexReader extends LuceneTestCase {
assertTrue(IndexWriter.isLocked(dir)); // writer open, so dir is locked
writer.close();
assertTrue(DirectoryReader.indexExists(dir));
IndexReader reader = IndexReader.open(dir);
DirectoryReader reader = DirectoryReader.open(dir);
assertFalse(IndexWriter.isLocked(dir)); // reader only, no lock
long version = DirectoryReader.lastModified(dir);
if (i == 1) {
@ -405,8 +405,8 @@ public class TestIndexReader extends LuceneTestCase {
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
addDocumentWithFields(writer);
writer.close();
reader = IndexReader.open(dir);
assertTrue("old lastModified is " + version + "; new lastModified is " + DirectoryReader.lastModified(dir), version <= IndexReader.lastModified(dir));
reader = DirectoryReader.open(dir);
assertTrue("old lastModified is " + version + "; new lastModified is " + DirectoryReader.lastModified(dir), version <= DirectoryReader.lastModified(dir));
reader.close();
dir.close();
}
@ -420,7 +420,7 @@ public class TestIndexReader extends LuceneTestCase {
assertTrue(IndexWriter.isLocked(dir)); // writer open, so dir is locked
writer.close();
assertTrue(DirectoryReader.indexExists(dir));
IndexReader reader = IndexReader.open(dir);
DirectoryReader reader = DirectoryReader.open(dir);
assertFalse(IndexWriter.isLocked(dir)); // reader only, no lock
long version = DirectoryReader.getCurrentVersion(dir);
reader.close();
@ -429,8 +429,8 @@ public class TestIndexReader extends LuceneTestCase {
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
addDocumentWithFields(writer);
writer.close();
reader = IndexReader.open(dir);
assertTrue("old version is " + version + "; new version is " + DirectoryReader.getCurrentVersion(dir), version < IndexReader.getCurrentVersion(dir));
reader = DirectoryReader.open(dir);
assertTrue("old version is " + version + "; new version is " + DirectoryReader.getCurrentVersion(dir), version < DirectoryReader.getCurrentVersion(dir));
reader.close();
dir.close();
}
@ -439,7 +439,7 @@ public class TestIndexReader extends LuceneTestCase {
File dirFile = _TestUtil.getTempDir("deletetest");
Directory dir = newFSDirectory(dirFile);
try {
IndexReader.open(dir);
DirectoryReader.open(dir);
fail("expected FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
@ -449,7 +449,7 @@ public class TestIndexReader extends LuceneTestCase {
// Make sure we still get a CorruptIndexException (not NPE):
try {
IndexReader.open(dir);
DirectoryReader.open(dir);
fail("expected FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
@ -515,17 +515,15 @@ public class TestIndexReader extends LuceneTestCase {
}
// TODO: maybe this can reuse the logic of test dueling codecs?
public static void assertIndexEquals(IndexReader index1, IndexReader index2) throws IOException {
public static void assertIndexEquals(DirectoryReader index1, DirectoryReader index2) throws IOException {
assertEquals("IndexReaders have different values for numDocs.", index1.numDocs(), index2.numDocs());
assertEquals("IndexReaders have different values for maxDoc.", index1.maxDoc(), index2.maxDoc());
assertEquals("Only one IndexReader has deletions.", index1.hasDeletions(), index2.hasDeletions());
if (!(index1 instanceof ParallelReader)) {
assertEquals("Single segment test differs.", index1.getSequentialSubReaders().length == 1, index2.getSequentialSubReaders().length == 1);
}
assertEquals("Single segment test differs.", index1.getSequentialSubReaders().length == 1, index2.getSequentialSubReaders().length == 1);
// check field names
FieldInfos fieldInfos1 = ReaderUtil.getMergedFieldInfos(index1);
FieldInfos fieldInfos2 = ReaderUtil.getMergedFieldInfos(index2);
FieldInfos fieldInfos1 = MultiFields.getMergedFieldInfos(index1);
FieldInfos fieldInfos2 = MultiFields.getMergedFieldInfos(index2);
assertEquals("IndexReaders have different numbers of fields.", fieldInfos1.size(), fieldInfos2.size());
final int numFields = fieldInfos1.size();
for(int fieldID=0;fieldID<numFields;fieldID++) {
@ -635,7 +633,7 @@ public class TestIndexReader extends LuceneTestCase {
SegmentInfos sis = new SegmentInfos();
sis.read(d);
IndexReader r = IndexReader.open(d);
DirectoryReader r = DirectoryReader.open(d);
IndexCommit c = r.getIndexCommit();
assertEquals(sis.getCurrentSegmentFileName(), c.getSegmentsFileName());
@ -654,7 +652,7 @@ public class TestIndexReader extends LuceneTestCase {
addDocumentWithFields(writer);
writer.close();
IndexReader r2 = IndexReader.openIfChanged(r);
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
assertFalse(c.equals(r2.getIndexCommit()));
assertFalse(r2.getIndexCommit().getSegmentCount() == 1);
@ -666,9 +664,9 @@ public class TestIndexReader extends LuceneTestCase {
writer.forceMerge(1);
writer.close();
r2 = IndexReader.openIfChanged(r);
r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
assertNull(IndexReader.openIfChanged(r2));
assertNull(DirectoryReader.openIfChanged(r2));
assertEquals(1, r2.getIndexCommit().getSegmentCount());
r.close();
@ -687,12 +685,12 @@ public class TestIndexReader extends LuceneTestCase {
}
// LUCENE-1468 -- make sure on attempting to open an
// IndexReader on a non-existent directory, you get a
// DirectoryReader on a non-existent directory, you get a
// good exception
public void testNoDir() throws Throwable {
Directory dir = newFSDirectory(_TestUtil.getTempDir("doesnotexist"));
try {
IndexReader.open(dir);
DirectoryReader.open(dir);
fail("did not hit expected exception");
} catch (NoSuchDirectoryException nsde) {
// expected
@ -742,8 +740,8 @@ public class TestIndexReader extends LuceneTestCase {
writer.commit();
// Open reader1
IndexReader r = IndexReader.open(dir);
IndexReader r1 = getOnlySegmentReader(r);
DirectoryReader r = DirectoryReader.open(dir);
AtomicIndexReader r1 = getOnlySegmentReader(r);
final int[] ints = FieldCache.DEFAULT.getInts(r1, "number", false);
assertEquals(1, ints.length);
assertEquals(17, ints[0]);
@ -753,10 +751,10 @@ public class TestIndexReader extends LuceneTestCase {
writer.commit();
// Reopen reader1 --> reader2
IndexReader r2 = IndexReader.openIfChanged(r);
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
r.close();
IndexReader sub0 = r2.getSequentialSubReaders()[0];
AtomicIndexReader sub0 = (AtomicIndexReader) r2.getSequentialSubReaders()[0];
final int[] ints2 = FieldCache.DEFAULT.getInts(sub0, "number", false);
r2.close();
assertTrue(ints == ints2);
@ -776,19 +774,18 @@ public class TestIndexReader extends LuceneTestCase {
writer.addDocument(doc);
writer.commit();
IndexReader r = IndexReader.open(dir);
IndexReader r1 = getOnlySegmentReader(r);
DirectoryReader r = DirectoryReader.open(dir);
AtomicIndexReader r1 = getOnlySegmentReader(r);
assertEquals(36, r1.getUniqueTermCount());
writer.addDocument(doc);
writer.commit();
IndexReader r2 = IndexReader.openIfChanged(r);
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
r.close();
assertEquals(-1, r2.getUniqueTermCount());
IndexReader[] subs = r2.getSequentialSubReaders();
for(int i=0;i<subs.length;i++) {
assertEquals(36, subs[i].getUniqueTermCount());
assertEquals(36, ((AtomicIndexReader) subs[i]).getUniqueTermCount());
}
r2.close();
writer.close();
@ -806,7 +803,7 @@ public class TestIndexReader extends LuceneTestCase {
writer.addDocument(doc);
writer.close();
IndexReader r = IndexReader.open(dir, -1);
DirectoryReader r = DirectoryReader.open(dir, -1);
try {
r.docFreq(new Term("field", "f"));
fail("did not hit expected exception");
@ -825,9 +822,9 @@ public class TestIndexReader extends LuceneTestCase {
writer.close();
// LUCENE-1718: ensure re-open carries over no terms index:
IndexReader r2 = IndexReader.openIfChanged(r);
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
assertNull(IndexReader.openIfChanged(r2));
assertNull(DirectoryReader.openIfChanged(r2));
r.close();
IndexReader[] subReaders = r2.getSequentialSubReaders();
assertEquals(2, subReaders.length);
@ -851,12 +848,12 @@ public class TestIndexReader extends LuceneTestCase {
writer.commit();
Document doc = new Document();
writer.addDocument(doc);
IndexReader r = IndexReader.open(dir);
DirectoryReader r = DirectoryReader.open(dir);
assertTrue(r.isCurrent());
writer.addDocument(doc);
writer.prepareCommit();
assertTrue(r.isCurrent());
IndexReader r2 = IndexReader.openIfChanged(r);
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNull(r2);
writer.commit();
assertFalse(r.isCurrent());
@ -909,7 +906,7 @@ public class TestIndexReader extends LuceneTestCase {
Document d = new Document();
d.add(newField("f", "a a b", TextField.TYPE_UNSTORED));
writer.addDocument(d);
IndexReader r = writer.getReader();
DirectoryReader r = writer.getReader();
writer.close();
try {
// Make sure codec impls totalTermFreq (eg PreFlex doesn't)
@ -932,7 +929,7 @@ public class TestIndexReader extends LuceneTestCase {
writer.commit();
writer.addDocument(new Document());
writer.commit();
final IndexReader reader = writer.getReader();
final DirectoryReader reader = writer.getReader();
final int[] closeCount = new int[1];
final IndexReader.ReaderClosedListener listener = new IndexReader.ReaderClosedListener() {
public void onClose(IndexReader reader) {
@ -948,7 +945,7 @@ public class TestIndexReader extends LuceneTestCase {
assertEquals(1, closeCount[0]);
writer.close();
IndexReader reader2 = IndexReader.open(dir);
DirectoryReader reader2 = DirectoryReader.open(dir);
reader2.addReaderClosedListener(listener);
closeCount[0] = 0;
@ -961,7 +958,7 @@ public class TestIndexReader extends LuceneTestCase {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.addDocument(new Document());
IndexReader r = writer.getReader();
DirectoryReader r = writer.getReader();
writer.close();
r.document(0);
try {
@ -979,7 +976,7 @@ public class TestIndexReader extends LuceneTestCase {
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.addDocument(new Document());
writer.commit();
IndexReader r = IndexReader.open(dir);
DirectoryReader r = DirectoryReader.open(dir);
assertTrue(r.tryIncRef());
r.decRef();
r.close();
@ -993,7 +990,7 @@ public class TestIndexReader extends LuceneTestCase {
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.addDocument(new Document());
writer.commit();
IndexReader r = IndexReader.open(dir);
DirectoryReader r = DirectoryReader.open(dir);
int numThreads = atLeast(2);
IncThread[] threads = new IncThread[numThreads];
@ -1047,7 +1044,7 @@ public class TestIndexReader extends LuceneTestCase {
doc.add(newField("field1", "foobar", StringField.TYPE_STORED));
doc.add(newField("field2", "foobaz", StringField.TYPE_STORED));
writer.addDocument(doc);
IndexReader r = writer.getReader();
DirectoryReader r = writer.getReader();
writer.close();
Set<String> fieldsToLoad = new HashSet<String>();
assertEquals(0, r.document(0, fieldsToLoad).getFields().size());

View File

@ -60,8 +60,8 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
@Override
protected IndexReader openReader() throws IOException {
return IndexReader.open(dir1);
protected DirectoryReader openReader() throws IOException {
return DirectoryReader.open(dir1);
}
});
@ -78,69 +78,14 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
@Override
protected IndexReader openReader() throws IOException {
return IndexReader.open(dir2);
protected DirectoryReader openReader() throws IOException {
return DirectoryReader.open(dir2);
}
});
dir2.close();
}
public void testParallelReaderReopen() throws Exception {
final Directory dir1 = newDirectory();
createIndex(random, dir1, true);
final Directory dir2 = newDirectory();
createIndex(random, dir2, true);
performDefaultTests(new TestReopen() {
@Override
protected void modifyIndex(int i) throws IOException {
TestIndexReaderReopen.modifyIndex(i, dir1);
TestIndexReaderReopen.modifyIndex(i, dir2);
}
@Override
protected IndexReader openReader() throws IOException {
ParallelReader pr = new ParallelReader();
pr.add(IndexReader.open(dir1));
pr.add(IndexReader.open(dir2));
return pr;
}
});
dir1.close();
dir2.close();
final Directory dir3 = newDirectory();
createIndex(random, dir3, true);
final Directory dir4 = newDirectory();
createIndex(random, dir4, true);
performTestsWithExceptionInReopen(new TestReopen() {
@Override
protected void modifyIndex(int i) throws IOException {
TestIndexReaderReopen.modifyIndex(i, dir3);
TestIndexReaderReopen.modifyIndex(i, dir4);
}
@Override
protected IndexReader openReader() throws IOException {
ParallelReader pr = new ParallelReader();
pr.add(IndexReader.open(dir3));
pr.add(IndexReader.open(dir4));
// Does not implement reopen, so
// hits exception:
pr.add(new FilterIndexReader(IndexReader.open(dir3)));
return pr;
}
});
dir3.close();
dir4.close();
}
// LUCENE-1228: IndexWriter.commit() does not update the index version
// populate an index in iterations.
// at the end of every iteration, commit the index and reopen/recreate the reader.
@ -162,7 +107,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(
OpenMode.CREATE).setMergeScheduler(new SerialMergeScheduler()).setMergePolicy(newLogMergePolicy()));
iwriter.commit();
IndexReader reader = IndexReader.open(dir);
DirectoryReader reader = DirectoryReader.open(dir);
try {
int M = 3;
FieldType customType = new FieldType(TextField.TYPE_STORED);
@ -191,7 +136,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
iwriter.commit();
if (withReopen) {
// reopen
IndexReader r2 = IndexReader.openIfChanged(reader);
DirectoryReader r2 = DirectoryReader.openIfChanged(reader);
if (r2 != null) {
reader.close();
reader = r2;
@ -199,7 +144,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
} else {
// recreate
reader.close();
reader = IndexReader.open(dir);
reader = DirectoryReader.open(dir);
}
}
} finally {
@ -207,99 +152,11 @@ public class TestIndexReaderReopen extends LuceneTestCase {
reader.close();
}
}
public void testMultiReaderReopen() throws Exception {
final Directory dir1 = newDirectory();
createIndex(random, dir1, true);
final Directory dir2 = newDirectory();
createIndex(random, dir2, true);
performDefaultTests(new TestReopen() {
@Override
protected void modifyIndex(int i) throws IOException {
TestIndexReaderReopen.modifyIndex(i, dir1);
TestIndexReaderReopen.modifyIndex(i, dir2);
}
@Override
protected IndexReader openReader() throws IOException {
return new MultiReader(IndexReader.open(dir1),
IndexReader.open(dir2));
}
});
dir1.close();
dir2.close();
final Directory dir3 = newDirectory();
createIndex(random, dir3, true);
final Directory dir4 = newDirectory();
createIndex(random, dir4, true);
performTestsWithExceptionInReopen(new TestReopen() {
@Override
protected void modifyIndex(int i) throws IOException {
TestIndexReaderReopen.modifyIndex(i, dir3);
TestIndexReaderReopen.modifyIndex(i, dir4);
}
@Override
protected IndexReader openReader() throws IOException {
return new MultiReader(IndexReader.open(dir3),
IndexReader.open(dir4),
// Does not implement reopen, so
// hits exception:
new FilterIndexReader(IndexReader.open(dir3)));
}
});
dir3.close();
dir4.close();
}
public void testMixedReaders() throws Exception {
final Directory dir1 = newDirectory();
createIndex(random, dir1, true);
final Directory dir2 = newDirectory();
createIndex(random, dir2, true);
final Directory dir3 = newDirectory();
createIndex(random, dir3, false);
final Directory dir4 = newDirectory();
createIndex(random, dir4, true);
final Directory dir5 = newDirectory();
createIndex(random, dir5, false);
performDefaultTests(new TestReopen() {
@Override
protected void modifyIndex(int i) throws IOException {
TestIndexReaderReopen.modifyIndex(i, dir4);
TestIndexReaderReopen.modifyIndex(i, dir5);
}
@Override
protected IndexReader openReader() throws IOException {
MultiReader mr1 = new MultiReader(IndexReader.open(dir1), IndexReader.open(dir2));
MultiReader mr2 = new MultiReader(IndexReader.open(dir3), IndexReader.open(dir4));
return new MultiReader(mr1, mr2, IndexReader.open(dir5));
}
});
dir1.close();
dir2.close();
dir3.close();
dir4.close();
dir5.close();
}
private void performDefaultTests(TestReopen test) throws Exception {
IndexReader index1 = test.openReader();
IndexReader index2 = test.openReader();
DirectoryReader index1 = test.openReader();
DirectoryReader index2 = test.openReader();
TestIndexReader.assertIndexEquals(index1, index2);
@ -312,7 +169,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
index1.close();
index1 = couple.newReader;
IndexReader index2_refreshed = couple.refreshedReader;
DirectoryReader index2_refreshed = couple.refreshedReader;
index2.close();
// test if refreshed reader and newly opened reader return equal results
@ -328,7 +185,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
index1.close();
couple = refreshReader(index2, test, i, true);
// refresh IndexReader
// refresh DirectoryReader
index2.close();
index2 = couple.refreshedReader;
@ -341,85 +198,10 @@ public class TestIndexReaderReopen extends LuceneTestCase {
assertReaderClosed(index1, true, true);
assertReaderClosed(index2, true, true);
}
public void testReferenceCountingMultiReader() throws IOException {
for (int mode = 0; mode <=1; mode++) {
Directory dir1 = newDirectory();
createIndex(random, dir1, false);
Directory dir2 = newDirectory();
createIndex(random, dir2, true);
IndexReader reader1 = IndexReader.open(dir1);
assertRefCountEquals(1, reader1);
IndexReader initReader2 = IndexReader.open(dir2);
IndexReader multiReader1 = new MultiReader(new IndexReader[] {reader1, initReader2}, (mode == 0));
modifyIndex(0, dir2);
assertRefCountEquals(1 + mode, reader1);
IndexReader multiReader2 = IndexReader.openIfChanged(multiReader1);
assertNotNull(multiReader2);
// index1 hasn't changed, so multiReader2 should share reader1 now with multiReader1
assertRefCountEquals(2 + mode, reader1);
modifyIndex(0, dir1);
IndexReader reader2 = IndexReader.openIfChanged(reader1);
assertNotNull(reader2);
assertNull(IndexReader.openIfChanged(reader2));
assertRefCountEquals(2 + mode, reader1);
if (mode == 1) {
initReader2.close();
}
modifyIndex(1, dir1);
IndexReader reader3 = IndexReader.openIfChanged(reader2);
assertNotNull(reader3);
assertRefCountEquals(2 + mode, reader1);
assertRefCountEquals(1, reader2);
multiReader1.close();
assertRefCountEquals(1 + mode, reader1);
multiReader1.close();
assertRefCountEquals(1 + mode, reader1);
if (mode == 1) {
initReader2.close();
}
reader1.close();
assertRefCountEquals(1, reader1);
multiReader2.close();
assertRefCountEquals(0, reader1);
multiReader2.close();
assertRefCountEquals(0, reader1);
reader3.close();
assertRefCountEquals(0, reader1);
assertReaderClosed(reader1, true, false);
reader2.close();
assertRefCountEquals(0, reader1);
assertReaderClosed(reader1, true, false);
reader2.close();
assertRefCountEquals(0, reader1);
reader3.close();
assertRefCountEquals(0, reader1);
assertReaderClosed(reader1, true, true);
dir1.close();
dir2.close();
}
}
private void performTestsWithExceptionInReopen(TestReopen test) throws Exception {
IndexReader index1 = test.openReader();
IndexReader index2 = test.openReader();
DirectoryReader index1 = test.openReader();
DirectoryReader index2 = test.openReader();
TestIndexReader.assertIndexEquals(index1, index2);
@ -459,28 +241,28 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
@Override
protected IndexReader openReader() throws IOException {
return IndexReader.open(dir);
protected DirectoryReader openReader() throws IOException {
return DirectoryReader.open(dir);
}
};
final List<ReaderCouple> readers = Collections.synchronizedList(new ArrayList<ReaderCouple>());
IndexReader firstReader = IndexReader.open(dir);
IndexReader reader = firstReader;
DirectoryReader firstReader = DirectoryReader.open(dir);
DirectoryReader reader = firstReader;
final Random rnd = random;
ReaderThread[] threads = new ReaderThread[n];
final Set<IndexReader> readersToClose = Collections.synchronizedSet(new HashSet<IndexReader>());
final Set<DirectoryReader> readersToClose = Collections.synchronizedSet(new HashSet<DirectoryReader>());
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
IndexReader refreshed = IndexReader.openIfChanged(reader);
DirectoryReader refreshed = DirectoryReader.openIfChanged(reader);
if (refreshed != null) {
readersToClose.add(reader);
reader = refreshed;
}
}
final IndexReader r = reader;
final DirectoryReader r = reader;
final int index = i;
@ -502,7 +284,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
break;
} else {
// not synchronized
IndexReader refreshed = IndexReader.openIfChanged(r);
DirectoryReader refreshed = DirectoryReader.openIfChanged(r);
if (refreshed == null) {
refreshed = r;
}
@ -569,14 +351,14 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
for (final IndexReader readerToClose : readersToClose) {
for (final DirectoryReader readerToClose : readersToClose) {
readerToClose.close();
}
firstReader.close();
reader.close();
for (final IndexReader readerToClose : readersToClose) {
for (final DirectoryReader readerToClose : readersToClose) {
assertReaderClosed(readerToClose, true, true);
}
@ -587,13 +369,13 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
private static class ReaderCouple {
ReaderCouple(IndexReader r1, IndexReader r2) {
ReaderCouple(DirectoryReader r1, DirectoryReader r2) {
newReader = r1;
refreshedReader = r2;
}
IndexReader newReader;
IndexReader refreshedReader;
DirectoryReader newReader;
DirectoryReader refreshedReader;
}
private abstract static class ReaderThreadTask {
@ -631,21 +413,21 @@ public class TestIndexReaderReopen extends LuceneTestCase {
private Object createReaderMutex = new Object();
private ReaderCouple refreshReader(IndexReader reader, boolean hasChanges) throws IOException {
private ReaderCouple refreshReader(DirectoryReader reader, boolean hasChanges) throws IOException {
return refreshReader(reader, null, -1, hasChanges);
}
ReaderCouple refreshReader(IndexReader reader, TestReopen test, int modify, boolean hasChanges) throws IOException {
ReaderCouple refreshReader(DirectoryReader reader, TestReopen test, int modify, boolean hasChanges) throws IOException {
synchronized (createReaderMutex) {
IndexReader r = null;
DirectoryReader r = null;
if (test != null) {
test.modifyIndex(modify);
r = test.openReader();
}
IndexReader refreshed = null;
DirectoryReader refreshed = null;
try {
refreshed = IndexReader.openIfChanged(reader);
refreshed = DirectoryReader.openIfChanged(reader);
if (refreshed == null) {
refreshed = reader;
}
@ -658,11 +440,11 @@ public class TestIndexReaderReopen extends LuceneTestCase {
if (hasChanges) {
if (refreshed == reader) {
fail("No new IndexReader instance created during refresh.");
fail("No new DirectoryReader instance created during refresh.");
}
} else {
if (refreshed != reader) {
fail("New IndexReader instance created during refresh even though index had no changes.");
fail("New DirectoryReader instance created during refresh even though index had no changes.");
}
}
@ -689,7 +471,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
w.close();
IndexReader r = IndexReader.open(dir);
DirectoryReader r = DirectoryReader.open(dir);
if (multiSegment) {
assertTrue(r.getSequentialSubReaders().length > 1);
} else {
@ -758,41 +540,25 @@ public class TestIndexReaderReopen extends LuceneTestCase {
static void assertReaderClosed(IndexReader reader, boolean checkSubReaders, boolean checkNormsClosed) {
assertEquals(0, reader.getRefCount());
if (checkNormsClosed && reader instanceof SegmentReader) {
if (checkNormsClosed && reader instanceof AtomicIndexReader) {
// TODO: should we really assert something here? we check for open files and this is obselete...
// assertTrue(((SegmentReader) reader).normsClosed());
}
if (checkSubReaders) {
if (reader instanceof DirectoryReader) {
IndexReader[] subReaders = reader.getSequentialSubReaders();
for (int i = 0; i < subReaders.length; i++) {
assertReaderClosed(subReaders[i], checkSubReaders, checkNormsClosed);
}
}
if (reader instanceof MultiReader) {
IndexReader[] subReaders = reader.getSequentialSubReaders();
for (int i = 0; i < subReaders.length; i++) {
assertReaderClosed(subReaders[i], checkSubReaders, checkNormsClosed);
}
}
if (reader instanceof ParallelReader) {
IndexReader[] subReaders = ((ParallelReader) reader).getSubReaders();
for (int i = 0; i < subReaders.length; i++) {
assertReaderClosed(subReaders[i], checkSubReaders, checkNormsClosed);
}
if (checkSubReaders && reader instanceof CompositeIndexReader) {
IndexReader[] subReaders = ((CompositeIndexReader) reader).getSequentialSubReaders();
for (int i = 0; i < subReaders.length; i++) {
assertReaderClosed(subReaders[i], checkSubReaders, checkNormsClosed);
}
}
}
/*
private void assertReaderOpen(IndexReader reader) {
private void assertReaderOpen(DirectoryReader reader) {
reader.ensureOpen();
if (reader instanceof DirectoryReader) {
IndexReader[] subReaders = reader.getSequentialSubReaders();
DirectoryReader[] subReaders = reader.getSequentialSubReaders();
for (int i = 0; i < subReaders.length; i++) {
assertReaderOpen(subReaders[i]);
}
@ -800,13 +566,13 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
*/
private void assertRefCountEquals(int refCount, IndexReader reader) {
private void assertRefCountEquals(int refCount, DirectoryReader reader) {
assertEquals("Reader has wrong refCount value.", refCount, reader.getRefCount());
}
private abstract static class TestReopen {
protected abstract IndexReader openReader() throws IOException;
protected abstract DirectoryReader openReader() throws IOException;
protected abstract void modifyIndex(int i) throws IOException;
}
@ -842,12 +608,12 @@ public class TestIndexReaderReopen extends LuceneTestCase {
}
writer.close();
IndexReader r = IndexReader.open(dir);
DirectoryReader r = DirectoryReader.open(dir);
assertEquals(0, r.numDocs());
Collection<IndexCommit> commits = DirectoryReader.listCommits(dir);
for (final IndexCommit commit : commits) {
IndexReader r2 = IndexReader.openIfChanged(r, commit);
DirectoryReader r2 = DirectoryReader.openIfChanged(r, commit);
assertNotNull(r2);
assertTrue(r2 != r);