Mercurial Hosting > luan
changeset 704:90c89138c234
optimize lucene
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 13 May 2016 17:13:13 -0600 |
parents | 6e6e9e73abaa |
children | 52ecb629a634 |
files | lucene/src/luan/modules/lucene/LuceneIndex.java |
diffstat | 1 files changed, 30 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java Fri May 13 14:51:05 2016 -0600 +++ b/lucene/src/luan/modules/lucene/LuceneIndex.java Fri May 13 17:13:13 2016 -0600 @@ -12,6 +12,9 @@ import java.util.Set; import java.util.HashSet; import java.util.Collections; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.zip.ZipOutputStream; @@ -92,13 +95,19 @@ 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 int writeCount; + public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException { mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields); mfp.fields.put( "type", STRING_FIELD_PARSER ); mfp.fields.put( "id", NumberFieldParser.LONG ); File indexDir = new File(indexDirStr); this.indexDir = indexDir; - Directory dir = FSDirectory.open(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 ) { @@ -117,7 +126,13 @@ initId(); } + private int globalWriteCount() { + return globalWriteCounters.get(fileDir).get(); + } + private void wrote() { + globalWriteCounters.get(fileDir).incrementAndGet(); + } public void delete_all() throws IOException { boolean commit = !writeLock.isHeldByCurrentThread(); @@ -127,6 +142,7 @@ id = idLim = 0; if(commit) writer.commit(); } finally { + wrote(); writeLock.unlock(); } } @@ -146,6 +162,7 @@ writer.deleteDocuments(query); if(commit) writer.commit(); } finally { + wrote(); writeLock.unlock(); } } @@ -197,6 +214,7 @@ } if(commit) writer.commit(); } finally { + wrote(); writeLock.unlock(); } } @@ -208,6 +226,7 @@ fn.call(luan); if(commit) writer.commit(); } finally { + wrote(); writeLock.unlock(); } } @@ -252,6 +271,7 @@ doc.rawPut( "type", "next_id" ); doc.rawPut( FLD_NEXT_ID, idLim ); writer.updateDocument(new Term("type","next_id"),toLucene(luan,doc,Collections.EMPTY_SET)); + wrote(); } return id; } @@ -331,11 +351,15 @@ } private synchronized IndexSearcher openSearcher() throws IOException { - DirectoryReader newReader = DirectoryReader.openIfChanged(reader); - if( newReader != null ) { - reader.decRef(); - reader = newReader; - searcher = new IndexSearcher(reader); + int gwc = globalWriteCount(); + if( writeCount != gwc ) { + writeCount = gwc; + DirectoryReader newReader = DirectoryReader.openIfChanged(reader); + if( newReader != null ) { + reader.decRef(); + reader = newReader; + searcher = new IndexSearcher(reader); + } } reader.incRef(); return searcher;