@@ -117,10 +117,6 @@ public class LuceneService implements Runnable {
117
117
private static final String FIELD_DATE = "date" ;
118
118
private static final String FIELD_TAG = "tag" ;
119
119
120
- private static final String CONF_FILE = "lucene.conf" ;
121
- private static final String LUCENE_DIR = "lucene" ;
122
- private static final String CONF_INDEX = "index" ;
123
- private static final String CONF_VERSION = "version" ;
124
120
private static final String CONF_ALIAS = "aliases" ;
125
121
private static final String CONF_BRANCH = "branches" ;
126
122
@@ -290,26 +286,13 @@ public synchronized void close() {
290
286
* @return true, if successful
291
287
*/
292
288
public boolean deleteIndex (String repositoryName ) {
293
- try {
294
- // close any open writer/searcher
295
- close (repositoryName );
296
-
297
- // delete the index folder
298
- File repositoryFolder = FileKey .resolve (new File (repositoriesFolder , repositoryName ), FS .DETECTED );
299
- File luceneIndex = new File (repositoryFolder , LUCENE_DIR );
300
- if (luceneIndex .exists ()) {
301
- org .eclipse .jgit .util .FileUtils .delete (luceneIndex ,
302
- org .eclipse .jgit .util .FileUtils .RECURSIVE );
303
- }
304
- // delete the config file
305
- File luceneConfig = new File (repositoryFolder , CONF_FILE );
306
- if (luceneConfig .exists ()) {
307
- luceneConfig .delete ();
308
- }
309
- return true ;
310
- } catch (IOException e ) {
311
- throw new RuntimeException (e );
312
- }
289
+ // close any open writer/searcher
290
+ close (repositoryName );
291
+
292
+ // delete the index folder
293
+ File repositoryFolder = FileKey .resolve (new File (repositoriesFolder , repositoryName ), FS .DETECTED );
294
+ LuceneRepoIndexStore luceneIndex = new LuceneRepoIndexStore (repositoryFolder , INDEX_VERSION );
295
+ return luceneIndex .delete ();
313
296
}
314
297
315
298
/**
@@ -383,29 +366,20 @@ private String getBranchKey(String branchName) {
383
366
* @return a config object
384
367
*/
385
368
private FileBasedConfig getConfig (Repository repository ) {
386
- File file = new File (repository .getDirectory (), CONF_FILE );
387
- FileBasedConfig config = new FileBasedConfig (file , FS .detect ());
369
+ LuceneRepoIndexStore luceneIndex = new LuceneRepoIndexStore (repository .getDirectory (), INDEX_VERSION );
370
+ FileBasedConfig config = new FileBasedConfig (luceneIndex . getConfigFile () , FS .detect ());
388
371
return config ;
389
372
}
390
373
391
374
/**
392
- * Reads the Lucene config file for the repository to check the index
393
- * version. If the index version is different, then rebuild the repository
394
- * index.
375
+ * Checks if an index exists for the repository, that is compatible with
376
+ * INDEX_VERSION and the Lucene version.
395
377
*
396
378
* @param repository
397
- * @return true of the on-disk index format is different than INDEX_VERSION
379
+ * @return true if no index is found for the repository, false otherwise.
398
380
*/
399
381
private boolean shouldReindex (Repository repository ) {
400
- try {
401
- FileBasedConfig config = getConfig (repository );
402
- config .load ();
403
- int indexVersion = config .getInt (CONF_INDEX , CONF_VERSION , 0 );
404
- // reindex if versions do not match
405
- return indexVersion != INDEX_VERSION ;
406
- } catch (Throwable t ) {
407
- }
408
- return true ;
382
+ return ! (new LuceneRepoIndexStore (repository .getDirectory (), INDEX_VERSION ).hasIndex ());
409
383
}
410
384
411
385
@@ -615,7 +589,6 @@ public int compare(RefModel ref1, RefModel ref2) {
615
589
reader .close ();
616
590
617
591
// commit all changes and reset the searcher
618
- config .setInt (CONF_INDEX , null , CONF_VERSION , INDEX_VERSION );
619
592
config .save ();
620
593
writer .commit ();
621
594
resetIndexSearcher (model .name );
@@ -844,7 +817,6 @@ public int compare(RefModel ref1, RefModel ref2) {
844
817
}
845
818
846
819
// update the config
847
- config .setInt (CONF_INDEX , null , CONF_VERSION , INDEX_VERSION );
848
820
config .setString (CONF_ALIAS , null , keyName , branchName );
849
821
config .setString (CONF_BRANCH , null , keyName , branch .getObjectId ().getName ());
850
822
config .save ();
@@ -962,14 +934,11 @@ private IndexSearcher getIndexSearcher(String repository) throws IOException {
962
934
*/
963
935
private IndexWriter getIndexWriter (String repository ) throws IOException {
964
936
IndexWriter indexWriter = writers .get (repository );
965
- File repositoryFolder = FileKey .resolve (new File (repositoriesFolder , repository ), FS .DETECTED );
966
- File indexFolder = new File (repositoryFolder , LUCENE_DIR );
967
- Directory directory = FSDirectory .open (indexFolder .toPath ());
968
-
969
937
if (indexWriter == null ) {
970
- if (!indexFolder .exists ()) {
971
- indexFolder .mkdirs ();
972
- }
938
+ File repositoryFolder = FileKey .resolve (new File (repositoriesFolder , repository ), FS .DETECTED );
939
+ LuceneRepoIndexStore indexStore = new LuceneRepoIndexStore (repositoryFolder , INDEX_VERSION );
940
+ indexStore .create ();
941
+ Directory directory = FSDirectory .open (indexStore .getPath ());
973
942
StandardAnalyzer analyzer = new StandardAnalyzer ();
974
943
IndexWriterConfig config = new IndexWriterConfig (analyzer );
975
944
config .setOpenMode (OpenMode .CREATE_OR_APPEND );
0 commit comments