Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java Fri Jul 15 17:35:50 2016 -0600 +++ b/lucene/src/luan/modules/lucene/LuceneIndex.java Sun Jul 17 19:21:52 2016 -0600 @@ -83,20 +83,21 @@ private static final String FLD_NEXT_ID = "nextId"; public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); + private static final Version version = Version.LUCENE_4_9; private final ReentrantLock writeLock = new ReentrantLock(); private final File indexDir; - final SnapshotDeletionPolicy snapshotDeletionPolicy; - private final IndexWriter writer; + private SnapshotDeletionPolicy snapshotDeletionPolicy; + private IndexWriter writer; private DirectoryReader reader; private IndexSearcher searcher; private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>(); - private boolean isClosed = false; + private boolean isClosed = true; private final MultiFieldParser mfp; public final LuanTable indexed_only_fields = new LuanTable(); private final Analyzer analyzer; private static ConcurrentMap<File,AtomicInteger> globalWriteCounters = new ConcurrentHashMap<File,AtomicInteger>(); - private final File fileDir; + private File fileDir; private int writeCount; public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException { @@ -105,23 +106,28 @@ mfp.fields.put( "id", NumberFieldParser.LONG ); File indexDir = new File(indexDirStr); this.indexDir = indexDir; - FSDirectory dir = FSDirectory.open(indexDir); - fileDir = dir.getDirectory(); - globalWriteCounters.putIfAbsent(fileDir,new AtomicInteger()); - Version version = Version.LUCENE_4_9; Analyzer analyzer = STRING_FIELD_PARSER.analyzer; if( defaultFieldParser instanceof StringFieldParser ) { StringFieldParser sfp = (StringFieldParser)defaultFieldParser; analyzer = sfp.analyzer; } this.analyzer = analyzer; + luan.onClose(this); + reopen(); + } + + public void reopen() throws LuanException, IOException { + if( !isClosed ) throw new RuntimeException(); + isClosed = false; IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); conf.setIndexDeletionPolicy(snapshotDeletionPolicy); + FSDirectory dir = FSDirectory.open(indexDir); + fileDir = dir.getDirectory(); + globalWriteCounters.putIfAbsent(fileDir,new AtomicInteger()); writer = new IndexWriter(dir,conf); writer.commit(); // commit index creation reader = DirectoryReader.open(dir); - luan.onClose(this); searcher = new IndexSearcher(reader); initId(); } @@ -232,29 +238,17 @@ } - private long id = 0; - private long idLim = 0; + private long id; + private long idLim; private final int idBatch = 10; private void initId() throws LuanException, IOException { TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); -/* - // tmp hack - if( td.totalHits == 0 ) { - td = searcher.search(new TermQuery(new Term("type index","next_id")),1); - if( td.totalHits == 1 ) { - long idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); - LuanTable doc = new LuanTable(); - doc.rawPut( "type", "next_id" ); - doc.rawPut( FLD_NEXT_ID, idLim ); - writer.addDocument(toLucene(luan,doc)); - writer.commit(); - } - } -*/ switch(td.totalHits) { case 0: - break; // do nothing + id = 0; + idLim = 0; + break; case 1: idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); id = idLim;