LUCENE-10024: remove non-existing path from history file

This commit is contained in:
Tomoko Uchida 2021-07-17 14:30:27 +09:00
parent 489ba3e4f9
commit 40038bcc92
3 changed files with 18 additions and 0 deletions

View File

@ -17,11 +17,13 @@
package org.apache.lucene.luke.app;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.NoSuchFileException;
import java.util.Objects;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.luke.app.desktop.PreferencesFactory;
import org.apache.lucene.luke.app.desktop.util.MessageUtils;
import org.apache.lucene.luke.models.LukeException;
import org.apache.lucene.luke.models.util.IndexUtils;
@ -74,6 +76,12 @@ public final class IndexHandler extends AbstractHandler<IndexObserver> {
reader = IndexUtils.openIndex(indexPath, dirImpl);
} catch (NoSuchFileException e) {
log.error("Error opening index", e);
try {
// remove the non-existing index path from history.
PreferencesFactory.getInstance().removeHistory(indexPath);
} catch (IOException ioe) {
log.error("Preference file is deleted?", ioe);
}
throw new LukeException(
MessageUtils.getLocalizedMessage(
"openindex.message.index_path_does_not_exist", indexPath),

View File

@ -28,6 +28,8 @@ public interface Preferences {
void addHistory(String indexPath) throws IOException;
void removeHistory(String indexPath) throws IOException;
boolean isReadOnly();
String getDirImpl();

View File

@ -76,6 +76,14 @@ public final class PreferencesImpl implements Preferences {
saveHistory();
}
@Override
public void removeHistory(String indexPath) throws IOException {
if (history.contains(indexPath)) {
history.remove(indexPath);
saveHistory();
}
}
private void saveHistory() throws IOException {
Files.write(historyFile(), history);
}