Skip to content

Commit 318c505

Browse files
authored
handle empty data directory in index check (#3981)
fixes #3980
1 parent 035a7b7 commit 318c505

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexCheck.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.index;
@@ -28,6 +28,8 @@
2828
import java.util.Collection;
2929
import java.util.logging.Level;
3030
import java.util.logging.Logger;
31+
32+
import org.apache.lucene.index.IndexNotFoundException;
3133
import org.apache.lucene.index.SegmentInfos;
3234
import org.apache.lucene.store.Directory;
3335
import org.apache.lucene.store.FSDirectory;
@@ -135,8 +137,13 @@ public static void checkDir(File dir) throws IndexVersionException, IOException
135137
int segVersion;
136138

137139
try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockFactory)) {
138-
SegmentInfos segInfos = SegmentInfos.readLatestCommit(indexDirectory);
139-
segVersion = segInfos.getIndexCreatedVersionMajor();
140+
try {
141+
SegmentInfos segInfos = SegmentInfos.readLatestCommit(indexDirectory);
142+
segVersion = segInfos.getIndexCreatedVersionMajor();
143+
} catch (IndexNotFoundException e) {
144+
LOGGER.log(Level.FINE, "no index found in ''{0}''", indexDirectory);
145+
return;
146+
}
140147
}
141148

142149
if (segVersion != Version.LATEST.major) {

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexCheckTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.index;
@@ -33,6 +33,7 @@
3333
import java.util.Arrays;
3434
import java.util.List;
3535

36+
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.junit.jupiter.api.Assertions.assertFalse;
3738
import static org.junit.jupiter.api.Assertions.assertNotNull;
3839
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -42,6 +43,7 @@
4243
import org.junit.jupiter.api.BeforeAll;
4344
import org.junit.jupiter.api.BeforeEach;
4445
import org.junit.jupiter.api.Test;
46+
import org.junit.jupiter.api.io.TempDir;
4547
import org.opengrok.indexer.configuration.Configuration;
4648
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4749
import org.opengrok.indexer.history.RepositoryFactory;
@@ -140,4 +142,10 @@ void testIndexVersionOldIndex() throws Exception {
140142

141143
assertThrows(IndexCheck.IndexVersionException.class, () -> IndexCheck.checkDir(indexDir));
142144
}
145+
146+
@Test
147+
void testEmptyDir(@TempDir Path tempDir) throws Exception {
148+
assertEquals(0, tempDir.toFile().list().length);
149+
IndexCheck.checkDir(tempDir.toFile());
150+
}
143151
}

0 commit comments

Comments
 (0)