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

View File

@ -152,23 +152,25 @@ public class Lucene {
*/ */
public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Directory directory) throws IOException { public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Directory directory) throws IOException {
final SegmentInfos si = readSegmentInfos(segmentsFileName, directory); 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 * 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" * 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 * 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. * 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. * We also want the caller to know if we were not able to remove a segments_N file.
*
*/ */
String lastSegmentsFile = SegmentInfos.getLastCommitSegmentsFileName(directory); if (file.startsWith(IndexFileNames.SEGMENTS)) {
if (lastSegmentsFile == null) { foundSegmentFiles++;
throw new IllegalStateException("no commit found in the directory"); if (file.equals(si.getSegmentsFileName()) == false) {
directory.deleteFile(file); // remove all segment_N files except of the one we wanna keep
}
} }
if (lastSegmentsFile.equals(si.getSegmentsFileName())) { }
break; assert SegmentInfos.getLastCommitSegmentsFileName(directory).equals(segmentsFileName);
} if (foundSegmentFiles == 0) {
directory.deleteFile(lastSegmentsFile); throw new IllegalStateException("no commit found in the directory");
} }
final CommitPoint cp = new CommitPoint(si, directory); final CommitPoint cp = new CommitPoint(si, directory);
try (IndexWriter _ = new IndexWriter(directory, new IndexWriterConfig(Lucene.STANDARD_ANALYZER) try (IndexWriter _ = new IndexWriter(directory, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)