Skip to content

Commit f44f45a

Browse files
committed
Use versioned index directories for repository indices.
Change from the index version of a repository index being stored in a config file to also using index directories with the version in the name. For that, `LuceneRepoIndexStore` is added, which adds the fixed `lucene` part to the path. It also gives out the location of the `lucene.conf` file, which is now stored in the index directory. This way it is automatically deleted when the directory is deleted. I believe that it should also provide means to store branch aliases and tips, i.e. hide the config file completely. But this isn't implemented with this commit, the `LuceneService` is still aware that a config file is used.
1 parent 71a27dd commit f44f45a

File tree

3 files changed

+339
-48
lines changed

3 files changed

+339
-48
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017 gitblit.com.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.gitblit.service;
18+
19+
import java.io.File;
20+
21+
import com.gitblit.utils.LuceneIndexStore;
22+
23+
/**
24+
* @author Florian Zschocke
25+
*
26+
* @since 1.9.0
27+
*/
28+
class LuceneRepoIndexStore extends LuceneIndexStore
29+
{
30+
31+
private static final String LUCENE_DIR = "lucene";
32+
private static final String CONF_FILE = "gb_lucene.conf";
33+
34+
35+
/**
36+
* @param repositoryFolder
37+
* The directory of the repository for this index
38+
* @param indexVersion
39+
* Version of the index definition
40+
*/
41+
public LuceneRepoIndexStore(File repositoryFolder, int indexVersion) {
42+
super(new File(repositoryFolder, LUCENE_DIR), indexVersion);
43+
}
44+
45+
46+
/**
47+
* Get the index config File.
48+
*
49+
* @return The index config File
50+
*/
51+
public File getConfigFile() {
52+
return new File(this.indexFolder, CONF_FILE);
53+
}
54+
55+
}

src/main/java/com/gitblit/service/LuceneService.java

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)