24
24
package org .opengrok .indexer .index ;
25
25
26
26
import java .io .File ;
27
- import java .io .IOException ;
28
27
import java .util .List ;
29
28
import java .util .logging .Level ;
30
29
import java .util .logging .Logger ;
@@ -74,29 +73,23 @@ public static void check(List<String> subFilesList) throws Exception {
74
73
LOGGER .log (Level .FINER ,
75
74
"Checking Lucene index version in project {0}" ,
76
75
projectName );
77
- checkDir (getDirectory ( new File (indexRoot , projectName ) ));
76
+ checkDir (new File (indexRoot , projectName ));
78
77
}
79
78
} else {
80
79
if (env .isProjectsEnabled ()) {
81
80
for (String projectName : env .getProjects ().keySet ()) {
82
81
LOGGER .log (Level .FINER ,
83
82
"Checking Lucene index version in project {0}" ,
84
83
projectName );
85
- checkDir (getDirectory ( new File (indexRoot , projectName ) ));
84
+ checkDir (new File (indexRoot , projectName ));
86
85
}
87
86
} else {
88
87
LOGGER .log (Level .FINER , "Checking Lucene index version in {0}" ,
89
88
indexRoot );
90
- checkDir (getDirectory ( indexRoot ) );
89
+ checkDir (indexRoot );
91
90
}
92
91
}
93
92
}
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
- }
100
93
101
94
/**
102
95
* Check index version in given directory. It assumes that that all commits
@@ -105,13 +98,21 @@ private static Directory getDirectory(File indexDir) throws IOException {
105
98
* @param dir directory with index
106
99
* @thows IOException if the directory cannot be opened
107
100
*/
108
- private static void checkDir (Directory dir ) throws IOException , Exception {
101
+ private static void checkDir (File dir ) throws Exception {
102
+ LockFactory lockfact = NativeFSLockFactory .INSTANCE ;
109
103
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
+ }
114
114
}
115
+
115
116
if (segVersion != Version .LATEST .major ) {
116
117
throw new IndexVersionException (
117
118
String .format ("Directory %s has index of version %d and Lucene has %d" ,
0 commit comments