Directory in now only closed if implicitly generated from

File or String. Behavior is identical to IndexReader.

addIndexes(IndexReader[]) no longer closes the readers.


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150302 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christoph Goller 2004-04-20 19:33:35 +00:00
parent 236466c37f
commit 0cc255bf7b
5 changed files with 57 additions and 38 deletions

View File

@ -118,6 +118,7 @@ public class IndexWriter {
*/ */
private boolean useCompoundFile = true; private boolean useCompoundFile = true;
private boolean closeDir;
/** Setting to turn on usage of a compound file. When on, multiple files /** Setting to turn on usage of a compound file. When on, multiple files
* for each segment are merged into a single file once the segment creation * for each segment are merged into a single file once the segment creation
@ -169,7 +170,7 @@ public class IndexWriter {
*/ */
public IndexWriter(String path, Analyzer a, boolean create) public IndexWriter(String path, Analyzer a, boolean create)
throws IOException { throws IOException {
this(FSDirectory.getDirectory(path, create), a, create); this(FSDirectory.getDirectory(path, create), a, create, true);
} }
/** /**
@ -189,7 +190,7 @@ public class IndexWriter {
*/ */
public IndexWriter(File path, Analyzer a, boolean create) public IndexWriter(File path, Analyzer a, boolean create)
throws IOException { throws IOException {
this(FSDirectory.getDirectory(path, create), a, create); this(FSDirectory.getDirectory(path, create), a, create, true);
} }
/** /**
@ -207,8 +208,14 @@ public class IndexWriter {
* if it does not exist, and <code>create</code> is * if it does not exist, and <code>create</code> is
* <code>false</code> * <code>false</code>
*/ */
public IndexWriter(Directory d, Analyzer a, final boolean create) public IndexWriter(Directory d, Analyzer a, boolean create)
throws IOException { throws IOException {
this(d, a, create, false);
}
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
throws IOException {
this.closeDir = closeDir;
directory = d; directory = d;
analyzer = a; analyzer = a;
@ -230,13 +237,13 @@ public class IndexWriter {
} }
} }
/** Flushes all changes to an index, closes all associated files, and closes /** Flushes all changes to an index and closes all associated files. */
the directory that the index is stored in. */
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
flushRamSegments(); flushRamSegments();
ramDirectory.close(); ramDirectory.close();
writeLock.release(); // release write lock writeLock.release(); // release write lock
writeLock = null; writeLock = null;
if(closeDir)
directory.close(); directory.close();
} }
@ -379,7 +386,9 @@ public class IndexWriter {
} }
/** Merges the provided indexes into this index. /** Merges the provided indexes into this index.
* <p>After this completes, the index is optimized. */ * <p>After this completes, the index is optimized. </p>
* <p>The provided IndexReaders are not closed.</p>
*/
public synchronized void addIndexes(IndexReader[] readers) public synchronized void addIndexes(IndexReader[] readers)
throws IOException { throws IOException {
@ -490,6 +499,8 @@ public class IndexWriter {
} }
}.run(); }.run();
} }
merger.closeReaders();
} }
/* Some operating systems (e.g. Windows) don't permit a file to be deleted /* Some operating systems (e.g. Windows) don't permit a file to be deleted

View File

@ -88,7 +88,7 @@ final class SegmentMerger {
*/ */
final int merge() throws IOException { final int merge() throws IOException {
int value; int value;
try {
value = mergeFields(); value = mergeFields();
mergeTerms(); mergeTerms();
mergeNorms(); mergeNorms();
@ -96,19 +96,24 @@ final class SegmentMerger {
if (fieldInfos.hasVectors()) if (fieldInfos.hasVectors())
mergeVectors(); mergeVectors();
} finally {
for (int i = 0; i < readers.size(); i++) { // close readers
IndexReader reader = (IndexReader) readers.elementAt(i);
reader.close();
}
}
if (useCompoundFile) if (useCompoundFile)
createCompoundFile(); createCompoundFile();
return value; return value;
} }
/**
* close all IndexReaders that have been added.
* Should not be called before merge().
* @throws IOException
*/
final void closeReaders() throws IOException {
for (int i = 0; i < readers.size(); i++) { // close readers
IndexReader reader = (IndexReader) readers.elementAt(i);
reader.close();
}
}
private final void createCompoundFile() private final void createCompoundFile()
throws IOException { throws IOException {
CompoundFileWriter cfsWriter = CompoundFileWriter cfsWriter =

View File

@ -80,6 +80,7 @@ class DocTest {
merger.add(r1); merger.add(r1);
merger.add(r2); merger.add(r2);
merger.merge(); merger.merge();
merger.closeReaders();
directory.close(); directory.close();
} }

View File

@ -186,6 +186,7 @@ public class TestDoc extends TestCase {
merger.add(r1); merger.add(r1);
merger.add(r2); merger.add(r2);
merger.merge(); merger.merge();
merger.closeReaders();
directory.close(); directory.close();
} }

View File

@ -77,6 +77,7 @@ public class TestSegmentMerger extends TestCase {
merger.add(reader2); merger.add(reader2);
try { try {
int docsMerged = merger.merge(); int docsMerged = merger.merge();
merger.closeReaders();
assertTrue(docsMerged == 2); assertTrue(docsMerged == 2);
//Should be able to open a new SegmentReader against the new directory //Should be able to open a new SegmentReader against the new directory
SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged, mergedDir)); SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged, mergedDir));