comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 521:8a217fe5b4f3

cleaner LuanState.onClose()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 03:12:28 -0600
parents dae264ad6a7b
children 70bda9184158
comparison
equal deleted inserted replaced
520:fcb9b4c8e972 521:8a217fe5b4f3
29 import luan.LuanState; 29 import luan.LuanState;
30 import luan.LuanTable; 30 import luan.LuanTable;
31 import luan.LuanFunction; 31 import luan.LuanFunction;
32 import luan.LuanJavaFunction; 32 import luan.LuanJavaFunction;
33 import luan.LuanException; 33 import luan.LuanException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
34 36
35 37
36 public final class LuceneIndex { 38 public final class LuceneIndex {
39 private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class);
40
37 private static final String FLD_NEXT_ID = "nextId"; 41 private static final String FLD_NEXT_ID = "nextId";
38 42
39 final Lock writeLock = new ReentrantLock(); 43 final Lock writeLock = new ReentrantLock();
40 private final File indexDir; 44 private final File indexDir;
41 final SnapshotDeletionPolicy snapshotDeletionPolicy; 45 final SnapshotDeletionPolicy snapshotDeletionPolicy;
42 final IndexWriter writer; 46 final IndexWriter writer;
43 private DirectoryReader reader; 47 private DirectoryReader reader;
44 private LuceneSearcher searcher; 48 private LuceneSearcher searcher;
45 public final FieldTable fields = new FieldTable(); 49 public final FieldTable fields = new FieldTable();
50 private boolean isClosed = false;
46 51
47 public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException { 52 public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException {
48 File indexDir = new File(indexDirStr); 53 File indexDir = new File(indexDirStr);
49 this.indexDir = indexDir; 54 this.indexDir = indexDir;
50 Directory dir = FSDirectory.open(indexDir); 55 Directory dir = FSDirectory.open(indexDir);
54 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); 59 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
55 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); 60 conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
56 writer = new IndexWriter(dir,conf); 61 writer = new IndexWriter(dir,conf);
57 writer.commit(); // commit index creation 62 writer.commit(); // commit index creation
58 reader = DirectoryReader.open(dir); 63 reader = DirectoryReader.open(dir);
64 luan.onClose(new Runnable(){public void run() {
65 try {
66 close();
67 } catch(IOException e) {
68 logger.error("",e);
69 }
70 }});
59 searcher = new LuceneSearcher(this,reader); 71 searcher = new LuceneSearcher(this,reader);
60 initId(luan); 72 initId(luan);
61 } 73 }
62 74
63 Document toLucene(LuanState luan,LuanTable table) throws LuanException { 75 Document toLucene(LuanState luan,LuanTable table) throws LuanException {
185 searcher.close(); 197 searcher.close();
186 } 198 }
187 } 199 }
188 200
189 public void close() throws IOException { 201 public void close() throws IOException {
190 writer.close(); 202 if( !isClosed ) {
191 reader.close(); 203 writer.close();
204 reader.close();
205 isClosed = true;
206 }
207 }
208
209 protected void finalize() throws Throwable {
210 if( !isClosed ) {
211 logger.error("not closed");
212 close();
213 }
214 super.finalize();
192 } 215 }
193 216
194 217
195 public Query parse(String s) throws QueryNodeException { 218 public Query parse(String s) throws QueryNodeException {
196 StandardQueryParser qp = new StandardQueryParser(); 219 StandardQueryParser qp = new StandardQueryParser();