@@ -117,10 +117,6 @@ public class LuceneService implements Runnable {
117117 private static final String FIELD_DATE = "date" ;
118118 private static final String FIELD_TAG = "tag" ;
119119
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" ;
124120 private static final String CONF_ALIAS = "aliases" ;
125121 private static final String CONF_BRANCH = "branches" ;
126122
@@ -290,26 +286,13 @@ public synchronized void close() {
290286 * @return true, if successful
291287 */
292288 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 ();
313296 }
314297
315298 /**
@@ -383,29 +366,20 @@ private String getBranchKey(String branchName) {
383366 * @return a config object
384367 */
385368 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 ());
388371 return config ;
389372 }
390373
391374 /**
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.
395377 *
396378 * @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.
398380 */
399381 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 ());
409383 }
410384
411385
@@ -615,7 +589,6 @@ public int compare(RefModel ref1, RefModel ref2) {
615589 reader .close ();
616590
617591 // commit all changes and reset the searcher
618- config .setInt (CONF_INDEX , null , CONF_VERSION , INDEX_VERSION );
619592 config .save ();
620593 writer .commit ();
621594 resetIndexSearcher (model .name );
@@ -844,7 +817,6 @@ public int compare(RefModel ref1, RefModel ref2) {
844817 }
845818
846819 // update the config
847- config .setInt (CONF_INDEX , null , CONF_VERSION , INDEX_VERSION );
848820 config .setString (CONF_ALIAS , null , keyName , branchName );
849821 config .setString (CONF_BRANCH , null , keyName , branch .getObjectId ().getName ());
850822 config .save ();
@@ -962,14 +934,11 @@ private IndexSearcher getIndexSearcher(String repository) throws IOException {
962934 */
963935 private IndexWriter getIndexWriter (String repository ) throws IOException {
964936 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-
969937 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 ());
973942 StandardAnalyzer analyzer = new StandardAnalyzer ();
974943 IndexWriterConfig config = new IndexWriterConfig (analyzer );
975944 config .setOpenMode (OpenMode .CREATE_OR_APPEND );
0 commit comments