|
1 |
| package photospace.search; |
|
2 |
| |
|
3 |
| import java.io.*; |
|
4 |
| import org.apache.commons.io.*; |
|
5 |
| import org.apache.lucene.index.*; |
|
6 |
| |
|
7 |
| public class SearchIndex |
|
8 |
| { |
|
9 |
| File index; |
|
10 |
| IndexReader reader; |
|
11 |
| |
|
12 |
14
| public SearchIndex(File index)
|
|
13 |
| throws IOException |
|
14 |
| { |
|
15 |
14
| this.index = index.getCanonicalFile();
|
|
16 |
| } |
|
17 |
| |
|
18 |
63
| public IndexReader getReader() throws IOException
|
|
19 |
| { |
|
20 |
63
| synchronized(this)
|
|
21 |
| { |
|
22 |
1
| if (!index.exists()) throw new IllegalStateException("Search index has not been created yet");
|
|
23 |
| |
|
24 |
62
| if (reader == null)
|
|
25 |
| { |
|
26 |
15
| try
|
|
27 |
| { |
|
28 |
15
| reader = IndexReader.open(index);
|
|
29 |
| } |
|
30 |
| catch (IOException e) |
|
31 |
| { |
|
32 |
0
| IndexWriter writer = new IndexWriter(index, new MetaAnalyzer(), true);
|
|
33 |
0
| writer.close();
|
|
34 |
0
| reader = IndexReader.open(index);
|
|
35 |
| } |
|
36 |
| } |
|
37 |
62
| return reader;
|
|
38 |
| } |
|
39 |
| } |
|
40 |
| |
|
41 |
45
| public void closeReader() throws IOException
|
|
42 |
| { |
|
43 |
45
| synchronized(this)
|
|
44 |
| { |
|
45 |
45
| if (reader != null)
|
|
46 |
| { |
|
47 |
15
| reader.close();
|
|
48 |
15
| reader = null;
|
|
49 |
| } |
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
14
| public void update(File updated) throws IOException
|
|
54 |
| { |
|
55 |
14
| synchronized(this)
|
|
56 |
| { |
|
57 |
14
| closeReader();
|
|
58 |
14
| FileUtils.deleteDirectory(index);
|
|
59 |
0
| if (!updated.renameTo(index)) throw new IOException("Unable to move temporary index " + updated + " to " + index);
|
|
60 |
| } |
|
61 |
| } |
|
62 |
| |
|
63 |
1
| public File getIndex()
|
|
64 |
| { |
|
65 |
1
| return index;
|
|
66 |
| } |
|
67 |
| } |