Mercurial Hosting > luan
diff src/luan/modules/lucene/LuceneIndex.java @ 1539:c27dc6af87ca
remove nextId
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 06 Sep 2020 11:35:30 -0600 |
parents | 634f6765830e |
children | 79f67662d3e7 |
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java Fri Aug 07 21:42:16 2020 -0600 +++ b/src/luan/modules/lucene/LuceneIndex.java Sun Sep 06 11:35:30 2020 -0600 @@ -51,6 +51,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; @@ -118,11 +119,12 @@ } private static final Version luceneVersion = Version.LUCENE_4_9; - private static final String FLD_NEXT_ID = "nextId"; +// private static final String FLD_NEXT_ID = "nextId"; public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); public static final StringFieldParser LOWERCASE_FIELD_PARSER = new StringFieldParser(new LowercaseAnalyzer(luceneVersion)); public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(luceneVersion)); private static final SortField ID_SORT = new SortField("id",SortField.Type.LONG); + private static final SortField ID_DESC_SORT = new SortField("id",SortField.Type.LONG,true); private final Object version; @@ -206,7 +208,7 @@ writeLock.lock(); try { writer.deleteAll(); - id = idLim = 0; + id = 0; if( postgresBackup != null ) postgresBackup.deleteAll(); if(commit) writer.commit(); @@ -296,7 +298,7 @@ writeLock.lock(); try { if( id == null ) { - id = nextId(); + id = ++this.id; doc.put("id",id); if( postgresBackup != null ) postgresBackup.add(doc); @@ -363,41 +365,22 @@ private long id; - private long idLim; - private final int idBatch = 10; private void initId() throws IOException { - TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); - switch(td.totalHits) { +// TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); + TopDocs td = searcher.search(new MatchAllDocsQuery(),1,new Sort(ID_DESC_SORT)); + switch(td.scoreDocs.length) { case 0: id = 0; - idLim = 0; break; case 1: - idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); - id = idLim; + id = (Long)searcher.doc(td.scoreDocs[0].doc).getField("id").numericValue(); break; default: throw new RuntimeException(); } } - private void saveNextId(long nextId) throws LuanException, IOException { - Map<String,Object> doc = new HashMap(); - doc.put( "type", "next_id" ); - doc.put( FLD_NEXT_ID, idLim ); - writer.updateDocument("type",doc); - } - - public synchronized long nextId() throws LuanException, IOException { - if( ++id > idLim ) { - idLim += idBatch; - saveNextId(idLim); - wrote(); - } - return id; - } - /* public void backup(String zipFile) throws LuanException, IOException { if( !zipFile.endsWith(".zip") ) @@ -419,7 +402,7 @@ } */ public SnapshotDeletionPolicy snapshotDeletionPolicy() { - return (SnapshotDeletionPolicy)writer.getLuceneConfig().getIndexDeletionPolicy(); + return (SnapshotDeletionPolicy)writer.getLuceneIndexWriter().getConfig().getIndexDeletionPolicy(); } public Object snapshot(LuanFunction fn) throws LuanException, IOException { @@ -437,7 +420,7 @@ public String to_string() { - return writer.getDirectory().toString(); + return writer.getLuceneIndexWriter().getDirectory().toString(); } protected void finalize() throws Throwable { @@ -506,6 +489,7 @@ if( writeCount != gwc ) { writeCount = gwc; DirectoryReader newReader = DirectoryReader.openIfChanged(reader); +// DirectoryReader newReader = DirectoryReader.openIfChanged(reader,writer.getLuceneIndexWriter(),true); if( newReader != null ) { reader.decRef(); reader = newReader; @@ -754,12 +738,12 @@ boolean ok = false; try { writer.deleteAll(); - long nextId = postgresBackup.maxId() + 1; postgresBackup.restoreLucene(this); - id = idLim = nextId; - saveNextId(nextId); ok = true; writer.commit(); + wrote(); + ensure_open(); // refresh searcher + initId(); wasCreated = false; } finally { if( !ok ) {