Prune all segment_N files not just newer ones

This commit is contained in:
Simon Willnauer 2015-02-20 22:30:02 +01:00
parent 136d36b724
commit d0a9c35c69
1 changed files with 11 additions and 9 deletions

View File

@ -152,24 +152,26 @@ public class Lucene {
*/
public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Directory directory) throws IOException {
final SegmentInfos si = readSegmentInfos(segmentsFileName, directory);
while (true) {
int foundSegmentFiles = 0;
for (final String file : directory.listAll()) {
/**
* we could also use a deletion policy here but in the case of snapshot and restore
* sometimes we restore an index and override files that were referenced by a "future"
* commit. If such a commit is opened by the IW it would likely throw a corrupted index exception
* since checksums don's match anymore. that's why we prune the name here directly.
* We also want the caller to know if we were not able to remove a segments_N file.
*
*/
String lastSegmentsFile = SegmentInfos.getLastCommitSegmentsFileName(directory);
if (lastSegmentsFile == null) {
if (file.startsWith(IndexFileNames.SEGMENTS)) {
foundSegmentFiles++;
if (file.equals(si.getSegmentsFileName()) == false) {
directory.deleteFile(file); // remove all segment_N files except of the one we wanna keep
}
}
}
assert SegmentInfos.getLastCommitSegmentsFileName(directory).equals(segmentsFileName);
if (foundSegmentFiles == 0) {
throw new IllegalStateException("no commit found in the directory");
}
if (lastSegmentsFile.equals(si.getSegmentsFileName())) {
break;
}
directory.deleteFile(lastSegmentsFile);
}
final CommitPoint cp = new CommitPoint(si, directory);
try (IndexWriter _ = new IndexWriter(directory, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)
.setIndexCommit(cp)