lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brendan Grainger <brendan.grain...@gmail.com>
Subject Closing directory in LuceneTestCase causes stacktrace
Date Thu, 28 Jun 2012 23:06:22 GMT
Hi,

I'm trying to brush up on some of the *cough* newer APIs (we've been using 2.9.2 up until
now). Anyway, I have the test below which a modified version of one of the tests in Lucene
In Action, but uses LuceneTestCase as a base class. 

public class MetaphoneReplacementAnaylyzerTest extends LuceneTestCase {
	
	@Test
	public void testKoolKat() throws Exception {
		
		Analyzer analyzer = new MetaphoneReplacementAnalyzer();
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
		// Directory directory = new RAMDirectory();
		Directory directory = newDirectory();
		
		IndexWriter writer = new IndexWriter(directory, config);
		
		Document doc = new Document();
		doc.add(new Field("contents", "cool cat", Field.Store.YES, Field.Index.ANALYZED));
		
		writer.addDocument(doc, analyzer);
		writer.commit();
		
		IndexSearcher searcher = new IndexSearcher(IndexReader.open(directory));
		// IndexSearcher searcher = new IndexSearcher(IndexReader.open(writer, true));
		Query q = new QueryParser(Version.LUCENE_36, "contents", analyzer).parse("kool kat");
		TopDocs hits = searcher.search(q, 1);
		assertEquals(1, hits.totalHits);
		
		int docID = hits.scoreDocs[0].doc;
		doc = searcher.doc(docID);
		assertEquals("cool cat", doc.get("contents"));
		
		writer.close();
		searcher.close();
		directory.close(); // if I comment this out, I get an assertion failure about the directory
not being closed
	}

}

My issue is that I get the following stack trace when I try to close the directory. I've tried
variations of closing the writer instead of calling commit etc, but I think I'm just missing
something fundamental here. Note that obviously if I change :

	Directory directory = newDirectory();

to:
	Directory directory = new RAMDirectory()

everything works, but I'd love to know how to correctly close the writer, searcher etc.

java.lang.RuntimeException: MockDirectoryWrapper: cannot close: there are still open files:
{_0.tis=1, _0.frq=1, _0.fdx=1, _0.prx=1, _0.fdt=1}
	at org.apache.lucene.store.MockDirectoryWrapper.close(MockDirectoryWrapper.java:548)
	at com.kuripai.lucene.analysis.MetaphoneReplacementAnaylyzerTest.testKoolKat(MetaphoneReplacementAnaylyzerTest.java:48)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	...
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: unclosed IndexInput: _0.prx
	at org.apache.lucene.store.MockDirectoryWrapper.addFileHandle(MockDirectoryWrapper.java:472)
	at org.apache.lucene.store.MockDirectoryWrapper.openInput(MockDirectoryWrapper.java:497)
	at org.apache.lucene.store.Directory.openInput(Directory.java:145)
	at org.apache.lucene.index.SegmentCoreReaders.<init>(SegmentCoreReaders.java:96)
	at org.apache.lucene.index.SegmentReader.get(SegmentReader.java:116)
	at org.apache.lucene.index.SegmentReader.get(SegmentReader.java:94)
	at org.apache.lucene.index.DirectoryReader.<init>(DirectoryReader.java:105)
	at org.apache.lucene.index.ReadOnlyDirectoryReader.<init>(ReadOnlyDirectoryReader.java:27)
	at org.apache.lucene.index.DirectoryReader$1.doBody(DirectoryReader.java:78)
	at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:709)
	at org.apache.lucene.index.DirectoryReader.open(DirectoryReader.java:72)
	at org.apache.lucene.index.IndexReader.open(IndexReader.java:256)
	at com.kuripai.lucene.analysis.MetaphoneReplacementAnaylyzerTest.testKoolKat(MetaphoneReplacementAnaylyzerTest.java:36)
	... 39 more


Any insight would be greatly appreciated.
Thanks
Brendan


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message