Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 230:4438cb2e04d0
start lucene
git-svn-id: https://luan-java.googlecode.com/svn/trunk@231 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 30 Sep 2014 20:03:56 +0000 |
parents | |
children | 9ce18106f95a |
comparison
equal
deleted
inserted
replaced
229:2a54cb7d1cf4 | 230:4438cb2e04d0 |
---|---|
1 package luan.modules.lucene; | |
2 | |
3 import java.io.File; | |
4 import java.io.FileOutputStream; | |
5 import java.io.FileInputStream; | |
6 import java.io.IOException; | |
7 import java.util.concurrent.locks.Lock; | |
8 import java.util.concurrent.locks.ReentrantLock; | |
9 import java.util.zip.ZipOutputStream; | |
10 import java.util.zip.ZipEntry; | |
11 import org.apache.lucene.analysis.Analyzer; | |
12 import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
13 import org.apache.lucene.index.IndexWriter; | |
14 import org.apache.lucene.index.IndexWriterConfig; | |
15 import org.apache.lucene.index.DirectoryReader; | |
16 import org.apache.lucene.index.Term; | |
17 import org.apache.lucene.index.SnapshotDeletionPolicy; | |
18 import org.apache.lucene.store.Directory; | |
19 import org.apache.lucene.store.FSDirectory; | |
20 import org.apache.lucene.util.Version; | |
21 import org.apache.lucene.search.TermQuery; | |
22 import org.apache.lucene.search.TopDocs; | |
23 import luan.modules.Utils; | |
24 import luan.Luan; | |
25 import luan.LuanState; | |
26 import luan.LuanTable; | |
27 import luan.LuanFunction; | |
28 import luan.LuanJavaFunction; | |
29 import luan.LuanException; | |
30 | |
31 | |
32 public final class LuceneIndex { | |
33 private static final String FLD_TYPE = LuceneWriter.FLD_TYPE; | |
34 private static final String FLD_NEXT_ID = "nextId"; | |
35 | |
36 final Lock writeLock = new ReentrantLock(); | |
37 private final File indexDir; | |
38 final SnapshotDeletionPolicy snapshotDeletionPolicy; | |
39 final IndexWriter writer; | |
40 private DirectoryReader reader; | |
41 private LuceneSearcher searcher; | |
42 | |
43 public LuceneIndex(String indexDirStr) { | |
44 try { | |
45 File indexDir = new File(indexDirStr); | |
46 this.indexDir = indexDir; | |
47 Directory dir = FSDirectory.open(indexDir); | |
48 Version version = Version.LUCENE_4_9; | |
49 Analyzer analyzer = new StandardAnalyzer(version); | |
50 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); | |
51 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); | |
52 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); | |
53 writer = new IndexWriter(dir,conf); | |
54 writer.commit(); // commit index creation | |
55 reader = DirectoryReader.open(dir); | |
56 searcher = new LuceneSearcher(reader); | |
57 initId(); | |
58 } catch(IOException e) { | |
59 throw new RuntimeException(e); | |
60 } | |
61 } | |
62 | |
63 public LuceneWriter openWriter() { | |
64 return new LuceneWriter(this); | |
65 } | |
66 | |
67 public synchronized LuceneSearcher openSearcher() { | |
68 try { | |
69 DirectoryReader newReader = DirectoryReader.openIfChanged(reader); | |
70 if( newReader != null ) { | |
71 reader.decRef(); | |
72 reader = newReader; | |
73 searcher = new LuceneSearcher(reader); | |
74 } | |
75 reader.incRef(); | |
76 return searcher; | |
77 } catch(IOException e) { | |
78 throw new RuntimeException(e); | |
79 } | |
80 } | |
81 | |
82 public LuceneSnapshot openSnapshot() { | |
83 return new LuceneSnapshot(this); | |
84 } | |
85 | |
86 | |
87 private long id = 0; | |
88 private long idLim = 0; | |
89 private final int idBatch = 10; | |
90 | |
91 private void initId() { | |
92 TopDocs td = searcher.search(new TermQuery(new Term(FLD_TYPE,"next_id")),1); | |
93 switch(td.totalHits) { | |
94 case 0: | |
95 break; // do nothing | |
96 case 1: | |
97 LuanTable doc = searcher.doc(td.scoreDocs[0].doc); | |
98 idLim = (Long)doc.get(FLD_NEXT_ID); | |
99 id = idLim; | |
100 break; | |
101 default: | |
102 throw new RuntimeException(); | |
103 } | |
104 } | |
105 | |
106 synchronized String nextId() { | |
107 try { | |
108 String rtn = Long.toString(++id); | |
109 if( id > idLim ) { | |
110 idLim += idBatch; | |
111 LuanTable doc = Luan.newTable(); | |
112 doc.put( FLD_TYPE, "next_id" ); | |
113 doc.put( FLD_NEXT_ID, idLim ); | |
114 writer.updateDocument(new Term(FLD_TYPE,"next_id"),LuceneDocument.toLucene(doc)); | |
115 } | |
116 return rtn; | |
117 } catch(IOException e) { | |
118 throw new RuntimeException(e); | |
119 } | |
120 } | |
121 | |
122 public LuanTable getDocument(String id) { | |
123 return getDocument(new Term(LuceneWriter.FLD_ID,id)); | |
124 } | |
125 | |
126 public LuanTable getDocument(Term term) { | |
127 LuceneSearcher searcher = openSearcher(); | |
128 try { | |
129 TopDocs td = searcher.search(new TermQuery(term),1); | |
130 switch(td.totalHits) { | |
131 case 0: | |
132 return null; | |
133 case 1: | |
134 return searcher.doc(td.scoreDocs[0].doc); | |
135 default: | |
136 throw new RuntimeException(); | |
137 } | |
138 } finally { | |
139 searcher.close(); | |
140 } | |
141 } | |
142 | |
143 | |
144 public void backup(String zipFile) { | |
145 if( !zipFile.endsWith(".zip") ) | |
146 throw new RuntimeException("file "+zipFile+" doesn't end with '.zip'"); | |
147 LuceneSnapshot snapshot = openSnapshot(); | |
148 try { | |
149 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); | |
150 for( String fileName : snapshot.getFileNames() ) { | |
151 out.putNextEntry(new ZipEntry(fileName)); | |
152 FileInputStream in = new FileInputStream(new File(indexDir,fileName)); | |
153 Utils.copyAll(in,out); | |
154 in.close(); | |
155 out.closeEntry(); | |
156 } | |
157 out.close(); | |
158 } catch(IOException e) { | |
159 throw new RuntimeException(e); | |
160 } finally { | |
161 snapshot.close(); | |
162 } | |
163 } | |
164 | |
165 | |
166 | |
167 // luan | |
168 | |
169 public String to_string() { | |
170 return writer.getDirectory().toString(); | |
171 } | |
172 | |
173 public void Writer(LuanState luan,LuanFunction fn) throws LuanException, IOException { | |
174 LuceneWriter writer = openWriter(); | |
175 try { | |
176 luan.call( fn, new Object[]{writer.table()} ); | |
177 writer.commit(); | |
178 } finally { | |
179 writer.close(); | |
180 } | |
181 } | |
182 | |
183 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | |
184 t.put( method, new LuanJavaFunction(LuceneIndex.class.getMethod(method,parameterTypes),this) ); | |
185 } | |
186 | |
187 public LuanTable table() { | |
188 LuanTable tbl = Luan.newTable(); | |
189 try { | |
190 add( tbl, "to_string" ); | |
191 add( tbl, "backup", String.class ); | |
192 } catch(NoSuchMethodException e) { | |
193 throw new RuntimeException(e); | |
194 } | |
195 return tbl; | |
196 } | |
197 | |
198 } |