Skip to content

Commit 767f9f7

Browse files
author
Vladimir Kotal
committed
close directory
1 parent 4d2d051 commit 767f9f7

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package org.opengrok.indexer.index;
2525

2626
import java.io.File;
27-
import java.io.IOException;
2827
import java.util.List;
2928
import java.util.logging.Level;
3029
import java.util.logging.Logger;
@@ -74,29 +73,23 @@ public static void check(List<String> subFilesList) throws Exception {
7473
LOGGER.log(Level.FINER,
7574
"Checking Lucene index version in project {0}",
7675
projectName);
77-
checkDir(getDirectory(new File(indexRoot, projectName)));
76+
checkDir(new File(indexRoot, projectName));
7877
}
7978
} else {
8079
if (env.isProjectsEnabled()) {
8180
for (String projectName : env.getProjects().keySet()) {
8281
LOGGER.log(Level.FINER,
8382
"Checking Lucene index version in project {0}",
8483
projectName);
85-
checkDir(getDirectory(new File(indexRoot, projectName)));
84+
checkDir(new File(indexRoot, projectName));
8685
}
8786
} else {
8887
LOGGER.log(Level.FINER, "Checking Lucene index version in {0}",
8988
indexRoot);
90-
checkDir(getDirectory(indexRoot));
89+
checkDir(indexRoot);
9190
}
9291
}
9392
}
94-
95-
private static Directory getDirectory(File indexDir) throws IOException {
96-
LockFactory lockfact = NativeFSLockFactory.INSTANCE;
97-
FSDirectory indexDirectory = FSDirectory.open(indexDir.toPath(), lockfact);
98-
return indexDirectory;
99-
}
10093

10194
/**
10295
* Check index version in given directory. It assumes that that all commits
@@ -105,13 +98,21 @@ private static Directory getDirectory(File indexDir) throws IOException {
10598
* @param dir directory with index
10699
* @thows IOException if the directory cannot be opened
107100
*/
108-
private static void checkDir(Directory dir) throws IOException, Exception {
101+
private static void checkDir(File dir) throws Exception {
102+
LockFactory lockfact = NativeFSLockFactory.INSTANCE;
109103
int segVersion;
110-
try {
111-
segVersion = SegmentInfos.readLatestCommit(dir).getIndexCreatedVersionMajor();
112-
} catch (IndexNotFoundException e) {
113-
return;
104+
105+
try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockfact)) {
106+
SegmentInfos segInfos = null;
107+
108+
try {
109+
segInfos = SegmentInfos.readLatestCommit(indexDirectory);
110+
segVersion = segInfos.getIndexCreatedVersionMajor();
111+
} catch (IndexNotFoundException e) {
112+
return;
113+
}
114114
}
115+
115116
if (segVersion != Version.LATEST.major) {
116117
throw new IndexVersionException(
117118
String.format("Directory %s has index of version %d and Lucene has %d",

0 commit comments

Comments
 (0)