Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 754:1a101ac9ea46
add lucene restore
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 17 Jul 2016 19:21:52 -0600 |
parents | 01e68da6983b |
children | 9092e52f94eb |
comparison
equal
deleted
inserted
replaced
753:5e3970ccd86a | 754:1a101ac9ea46 |
---|---|
81 private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class); | 81 private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class); |
82 | 82 |
83 private static final String FLD_NEXT_ID = "nextId"; | 83 private static final String FLD_NEXT_ID = "nextId"; |
84 public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); | 84 public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); |
85 | 85 |
86 private static final Version version = Version.LUCENE_4_9; | |
86 private final ReentrantLock writeLock = new ReentrantLock(); | 87 private final ReentrantLock writeLock = new ReentrantLock(); |
87 private final File indexDir; | 88 private final File indexDir; |
88 final SnapshotDeletionPolicy snapshotDeletionPolicy; | 89 private SnapshotDeletionPolicy snapshotDeletionPolicy; |
89 private final IndexWriter writer; | 90 private IndexWriter writer; |
90 private DirectoryReader reader; | 91 private DirectoryReader reader; |
91 private IndexSearcher searcher; | 92 private IndexSearcher searcher; |
92 private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>(); | 93 private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>(); |
93 private boolean isClosed = false; | 94 private boolean isClosed = true; |
94 private final MultiFieldParser mfp; | 95 private final MultiFieldParser mfp; |
95 public final LuanTable indexed_only_fields = new LuanTable(); | 96 public final LuanTable indexed_only_fields = new LuanTable(); |
96 private final Analyzer analyzer; | 97 private final Analyzer analyzer; |
97 | 98 |
98 private static ConcurrentMap<File,AtomicInteger> globalWriteCounters = new ConcurrentHashMap<File,AtomicInteger>(); | 99 private static ConcurrentMap<File,AtomicInteger> globalWriteCounters = new ConcurrentHashMap<File,AtomicInteger>(); |
99 private final File fileDir; | 100 private File fileDir; |
100 private int writeCount; | 101 private int writeCount; |
101 | 102 |
102 public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException { | 103 public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException { |
103 mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields); | 104 mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields); |
104 mfp.fields.put( "type", STRING_FIELD_PARSER ); | 105 mfp.fields.put( "type", STRING_FIELD_PARSER ); |
105 mfp.fields.put( "id", NumberFieldParser.LONG ); | 106 mfp.fields.put( "id", NumberFieldParser.LONG ); |
106 File indexDir = new File(indexDirStr); | 107 File indexDir = new File(indexDirStr); |
107 this.indexDir = indexDir; | 108 this.indexDir = indexDir; |
108 FSDirectory dir = FSDirectory.open(indexDir); | |
109 fileDir = dir.getDirectory(); | |
110 globalWriteCounters.putIfAbsent(fileDir,new AtomicInteger()); | |
111 Version version = Version.LUCENE_4_9; | |
112 Analyzer analyzer = STRING_FIELD_PARSER.analyzer; | 109 Analyzer analyzer = STRING_FIELD_PARSER.analyzer; |
113 if( defaultFieldParser instanceof StringFieldParser ) { | 110 if( defaultFieldParser instanceof StringFieldParser ) { |
114 StringFieldParser sfp = (StringFieldParser)defaultFieldParser; | 111 StringFieldParser sfp = (StringFieldParser)defaultFieldParser; |
115 analyzer = sfp.analyzer; | 112 analyzer = sfp.analyzer; |
116 } | 113 } |
117 this.analyzer = analyzer; | 114 this.analyzer = analyzer; |
115 luan.onClose(this); | |
116 reopen(); | |
117 } | |
118 | |
119 public void reopen() throws LuanException, IOException { | |
120 if( !isClosed ) throw new RuntimeException(); | |
121 isClosed = false; | |
118 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); | 122 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); |
119 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); | 123 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); |
120 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); | 124 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); |
125 FSDirectory dir = FSDirectory.open(indexDir); | |
126 fileDir = dir.getDirectory(); | |
127 globalWriteCounters.putIfAbsent(fileDir,new AtomicInteger()); | |
121 writer = new IndexWriter(dir,conf); | 128 writer = new IndexWriter(dir,conf); |
122 writer.commit(); // commit index creation | 129 writer.commit(); // commit index creation |
123 reader = DirectoryReader.open(dir); | 130 reader = DirectoryReader.open(dir); |
124 luan.onClose(this); | |
125 searcher = new IndexSearcher(reader); | 131 searcher = new IndexSearcher(reader); |
126 initId(); | 132 initId(); |
127 } | 133 } |
128 | 134 |
129 private int globalWriteCount() { | 135 private int globalWriteCount() { |
230 writeLock.unlock(); | 236 writeLock.unlock(); |
231 } | 237 } |
232 } | 238 } |
233 | 239 |
234 | 240 |
235 private long id = 0; | 241 private long id; |
236 private long idLim = 0; | 242 private long idLim; |
237 private final int idBatch = 10; | 243 private final int idBatch = 10; |
238 | 244 |
239 private void initId() throws LuanException, IOException { | 245 private void initId() throws LuanException, IOException { |
240 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); | 246 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); |
241 /* | |
242 // tmp hack | |
243 if( td.totalHits == 0 ) { | |
244 td = searcher.search(new TermQuery(new Term("type index","next_id")),1); | |
245 if( td.totalHits == 1 ) { | |
246 long idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); | |
247 LuanTable doc = new LuanTable(); | |
248 doc.rawPut( "type", "next_id" ); | |
249 doc.rawPut( FLD_NEXT_ID, idLim ); | |
250 writer.addDocument(toLucene(luan,doc)); | |
251 writer.commit(); | |
252 } | |
253 } | |
254 */ | |
255 switch(td.totalHits) { | 247 switch(td.totalHits) { |
256 case 0: | 248 case 0: |
257 break; // do nothing | 249 id = 0; |
250 idLim = 0; | |
251 break; | |
258 case 1: | 252 case 1: |
259 idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); | 253 idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); |
260 id = idLim; | 254 id = idLim; |
261 break; | 255 break; |
262 default: | 256 default: |