mirror of https://github.com/apache/lucene.git
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:
parent
236466c37f
commit
0cc255bf7b
|
@ -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,37 +208,43 @@ 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 {
|
||||||
directory = d;
|
this(d, a, create, false);
|
||||||
analyzer = a;
|
|
||||||
|
|
||||||
Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
|
|
||||||
if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
|
|
||||||
throw new IOException("Index locked for write: " + writeLock);
|
|
||||||
this.writeLock = writeLock; // save it
|
|
||||||
|
|
||||||
synchronized (directory) { // in- & inter-process sync
|
|
||||||
new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
|
|
||||||
public Object doBody() throws IOException {
|
|
||||||
if (create)
|
|
||||||
segmentInfos.write(directory);
|
|
||||||
else
|
|
||||||
segmentInfos.read(directory);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Flushes all changes to an index, closes all associated files, and closes
|
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
|
||||||
the directory that the index is stored in. */
|
throws IOException {
|
||||||
|
this.closeDir = closeDir;
|
||||||
|
directory = d;
|
||||||
|
analyzer = a;
|
||||||
|
|
||||||
|
Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
|
||||||
|
if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
|
||||||
|
throw new IOException("Index locked for write: " + writeLock);
|
||||||
|
this.writeLock = writeLock; // save it
|
||||||
|
|
||||||
|
synchronized (directory) { // in- & inter-process sync
|
||||||
|
new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
|
||||||
|
public Object doBody() throws IOException {
|
||||||
|
if (create)
|
||||||
|
segmentInfos.write(directory);
|
||||||
|
else
|
||||||
|
segmentInfos.read(directory);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Flushes all changes to an index and closes all associated files. */
|
||||||
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;
|
||||||
directory.close();
|
if(closeDir)
|
||||||
|
directory.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Release the write lock, if needed. */
|
/** Release the write lock, if needed. */
|
||||||
|
@ -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
|
||||||
|
|
|
@ -88,20 +88,13 @@ final class SegmentMerger {
|
||||||
*/
|
*/
|
||||||
final int merge() throws IOException {
|
final int merge() throws IOException {
|
||||||
int value;
|
int value;
|
||||||
try {
|
|
||||||
value = mergeFields();
|
|
||||||
mergeTerms();
|
|
||||||
mergeNorms();
|
|
||||||
|
|
||||||
if (fieldInfos.hasVectors())
|
value = mergeFields();
|
||||||
mergeVectors();
|
mergeTerms();
|
||||||
|
mergeNorms();
|
||||||
|
|
||||||
} finally {
|
if (fieldInfos.hasVectors())
|
||||||
for (int i = 0; i < readers.size(); i++) { // close readers
|
mergeVectors();
|
||||||
IndexReader reader = (IndexReader) readers.elementAt(i);
|
|
||||||
reader.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useCompoundFile)
|
if (useCompoundFile)
|
||||||
createCompoundFile();
|
createCompoundFile();
|
||||||
|
@ -109,6 +102,18 @@ final class SegmentMerger {
|
||||||
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 =
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
|
Loading…
Reference in New Issue