Skip to content

Commit 71a27dd

Browse files
committed
Introduce an index version for the ticket index
In order to be able to update the index definition, the ticket index is assigned a version number, 2. This way the definiton can be updated and compatability with existing index files can be checked. The actual index is stored in a directory of name `indexVersion_codecVersion`. This wayit is veriy easy to check if an index of a certain version exists on the filesystem. It allows to have multiple indexes of different versions present, so that a downgrade of the software is possible without having to reindex again. Of coure, this is only possible if no new tickets were created since these would be missing in the old index. A new class `LuceneIndexStore` is introduced, which abstracts away the versioned index directory. The idea is, that this provides one place to keep the Lucene codec version and to allow to code compatibility rules into this class, so that older indices can still be used if they are compatible.
1 parent bf1b35a commit 71a27dd

File tree

3 files changed

+352
-9
lines changed

3 files changed

+352
-9
lines changed

src/main/java/com/gitblit/tickets/TicketIndexer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
import com.gitblit.models.TicketModel.Attachment;
6363
import com.gitblit.models.TicketModel.Patchset;
6464
import com.gitblit.models.TicketModel.Status;
65-
import com.gitblit.utils.FileUtils;
65+
import com.gitblit.utils.LuceneIndexStore;
6666
import com.gitblit.utils.StringUtils;
6767

6868
/**
@@ -110,6 +110,8 @@ public static enum Lucene {
110110
priority(Type.INT),
111111
severity(Type.INT);
112112

113+
final static int INDEX_VERSION = 2;
114+
113115
final Type fieldType;
114116

115117
Lucene(Type fieldType) {
@@ -169,14 +171,15 @@ public static Lucene fromString(String value) {
169171

170172
private final Logger log = LoggerFactory.getLogger(getClass());
171173

172-
private final File luceneDir;
174+
private final LuceneIndexStore indexStore;
173175

174176
private IndexWriter writer;
175177

176178
private IndexSearcher searcher;
177179

178180
public TicketIndexer(IRuntimeManager runtimeManager) {
179-
this.luceneDir = runtimeManager.getFileOrFolder(Keys.tickets.indexFolder, "${baseFolder}/tickets/lucene");
181+
File luceneDir = runtimeManager.getFileOrFolder(Keys.tickets.indexFolder, "${baseFolder}/tickets/lucene");
182+
this.indexStore = new LuceneIndexStore(luceneDir, Lucene.INDEX_VERSION);
180183
}
181184

182185
/**
@@ -192,7 +195,7 @@ public void close() {
192195
*/
193196
public void deleteAll() {
194197
close();
195-
FileUtils.delete(luceneDir);
198+
indexStore.delete();
196199
}
197200

198201
/**
@@ -441,12 +444,9 @@ public List<QueryResult> queryFor(String queryText, int page, int pageSize, Stri
441444

442445
private IndexWriter getWriter() throws IOException {
443446
if (writer == null) {
444-
Directory directory = FSDirectory.open(luceneDir.toPath());
445-
446-
if (!luceneDir.exists()) {
447-
luceneDir.mkdirs();
448-
}
447+
indexStore.create();
449448

449+
Directory directory = FSDirectory.open(indexStore.getPath());
450450
StandardAnalyzer analyzer = new StandardAnalyzer();
451451
IndexWriterConfig config = new IndexWriterConfig(analyzer);
452452
config.setOpenMode(OpenMode.CREATE_OR_APPEND);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.utils;
18+
19+
import java.io.File;
20+
import java.nio.file.Path;
21+
22+
/**
23+
* @author Florian Zschocke
24+
*
25+
* @since 1.9.0
26+
*/
27+
public class LuceneIndexStore
28+
{
29+
30+
public static final int LUCENE_CODEC_VERSION = 54;
31+
32+
protected File indexFolder;
33+
34+
/**
35+
* Constructor for a base folder that contains the version specific index folders
36+
* and an index version.
37+
*
38+
* @param luceneFolder
39+
* Path to the base folder for the Lucene indices, i.e. the common "lucene" directory.
40+
* @param indexVersion
41+
* Version of the index definition
42+
*/
43+
public LuceneIndexStore(File luceneFolder, int indexVersion)
44+
{
45+
this.indexFolder = new File(luceneFolder, indexVersion + "_" + LUCENE_CODEC_VERSION);
46+
}
47+
48+
49+
50+
/**
51+
* Create the Lucene index directory for this index version and Lucene codec version
52+
*/
53+
public void create()
54+
{
55+
if (! indexFolder.exists()) {
56+
indexFolder.mkdirs();
57+
}
58+
}
59+
60+
61+
/**
62+
* Delete the Lucene index directory for this index version and Lucene codec version
63+
*
64+
* @return True if the directory could successfully be deleted.
65+
*/
66+
public boolean delete()
67+
{
68+
if (indexFolder.exists()) {
69+
return FileUtils.delete(indexFolder);
70+
}
71+
return true;
72+
}
73+
74+
75+
76+
/**
77+
* @return The Path to the index folder
78+
*/
79+
public Path getPath()
80+
{
81+
return indexFolder.toPath();
82+
}
83+
84+
85+
86+
/**
87+
* Check if an index of the respective version, or compatible, already exists.
88+
*
89+
* @return True if an index exists, False otherwise
90+
*/
91+
public boolean hasIndex()
92+
{
93+
return indexFolder.exists() &&
94+
indexFolder.isDirectory() &&
95+
(indexFolder.list().length > 1);
96+
}
97+
98+
}

0 commit comments

Comments
 (0)