Skip to content

Apache Lucene open-source search software

LUCENE-8423: Make generation carrying more robust.

Adrien Grand534204890adb
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
index ca977a9bcb4..dc16885a14f 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -728,21 +728,23 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,
mergeScheduler.setInfoStream(infoStream);
codec = config.getCodec();
OpenMode mode = config.getOpenMode();
- boolean create;
+ final boolean indexExists;
+ final boolean create;
if (mode == OpenMode.CREATE) {
+ indexExists = DirectoryReader.indexExists(directory);
create = true;
} else if (mode == OpenMode.APPEND) {
+ indexExists = true;
create = false;
} else {
// CREATE_OR_APPEND - create only if an index does not exist
- create = !DirectoryReader.indexExists(directory);
+ indexExists = DirectoryReader.indexExists(directory);
+ create = !indexExists;
}
// If index is too old, reading the segments will throw
// IndexFormatTooOldException.
- boolean initialIndexExists = true;
-
String[] files = directory.listAll();
// Set up our initial SegmentInfos:
@@ -772,14 +774,10 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,
// searching. In this case we write the next
// segments_N file with no segments:
final SegmentInfos sis = new SegmentInfos(Version.LATEST.major);
- try {
+ if (indexExists) {
final SegmentInfos previous = SegmentInfos.readLatestCommit(directory);
sis.updateGenerationVersionAndCounter(previous);
- } catch (IOException e) {
- // Likely this means it's a fresh directory
- initialIndexExists = false;
}
-
segmentInfos = sis;
rollbackSegments = segmentInfos.createBackupSegmentInfos();
@@ -889,7 +887,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,
deleter = new IndexFileDeleter(files, directoryOrig, directory,
config.getIndexDeletionPolicy(),
segmentInfos, infoStream, this,
- initialIndexExists, reader != null);
+ indexExists, reader != null);
// We incRef all files when we return an NRT reader from IW, so all files must exist even in the NRT case:
assert create || filesExist(segmentInfos);