changeset 1539:c27dc6af87ca

remove nextId
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 06 Sep 2020 11:35:30 -0600
parents 634f6765830e
children 79f67662d3e7
files src/goodjava/lucene/api/GoodIndexWriter.java src/goodjava/lucene/api/LuceneIndexWriter.java src/goodjava/lucene/logging/LoggingIndexWriter.java src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/PostgresBackup.java
diffstat 6 files changed, 23 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/lucene/api/GoodIndexWriter.java	Fri Aug 07 21:42:16 2020 -0600
+++ b/src/goodjava/lucene/api/GoodIndexWriter.java	Sun Sep 06 11:35:30 2020 -0600
@@ -2,9 +2,8 @@
 
 import java.io.IOException;
 import java.util.Map;
-import org.apache.lucene.index.LiveIndexWriterConfig;
+import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.store.Directory;
 
 
 public interface GoodIndexWriter {
@@ -16,6 +15,5 @@
 	public void addDocument(Map<String,Object> storedFields) throws IOException;
 	public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException;
 	public void reindexDocuments(String keyFieldName,Query query) throws IOException;
-	public Directory getDirectory();
-	public LiveIndexWriterConfig getLuceneConfig();
+	public IndexWriter getLuceneIndexWriter();
 }
--- a/src/goodjava/lucene/api/LuceneIndexWriter.java	Fri Aug 07 21:42:16 2020 -0600
+++ b/src/goodjava/lucene/api/LuceneIndexWriter.java	Sun Sep 06 11:35:30 2020 -0600
@@ -46,12 +46,8 @@
 		luceneWriter.commit();  // commit index creation
 	}
 
-	public Directory getDirectory() {
-		return luceneWriter.getDirectory();
-	}
-
-	public LiveIndexWriterConfig getLuceneConfig() {
-		return luceneWriter.getConfig();
+	public IndexWriter getLuceneIndexWriter() {
+		return luceneWriter;
 	}
 
 	public void close() throws IOException {
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri Aug 07 21:42:16 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sun Sep 06 11:35:30 2020 -0600
@@ -17,8 +17,8 @@
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.DirectoryReader;
 import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.Term;
-import org.apache.lucene.index.LiveIndexWriterConfig;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.MatchAllDocsQuery;
@@ -80,12 +80,8 @@
 		newLogs();
 	}
 
-	public Directory getDirectory() {
-		return indexWriter.getDirectory();
-	}
-
-	public LiveIndexWriterConfig getLuceneConfig() {
-		return indexWriter.getLuceneConfig();
+	public IndexWriter getLuceneIndexWriter() {
+		return indexWriter.getLuceneIndexWriter();
 	}
 
 	private void setLog() throws IOException {
--- a/src/luan/modules/lucene/Lucene.luan	Fri Aug 07 21:42:16 2020 -0600
+++ b/src/luan/modules/lucene/Lucene.luan	Sun Sep 06 11:35:30 2020 -0600
@@ -70,7 +70,7 @@
 	index.is_in_transaction = java_index.is_in_transaction
 	index.run_in_transaction = java_index.run_in_transaction
 	index.ensure_open = java_index.ensure_open
-	index.next_id = java_index.nextId
+	--index.next_id = java_index.nextId
 	index.highlighter = java_index.highlighter
 	index.count_tokens = java_index.count_tokens
 	--index.close = java_index.close
--- 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 ) {
--- a/src/luan/modules/lucene/PostgresBackup.java	Fri Aug 07 21:42:16 2020 -0600
+++ b/src/luan/modules/lucene/PostgresBackup.java	Sun Sep 06 11:35:30 2020 -0600
@@ -169,17 +169,6 @@
 		stmt.close();
 	}
 
-	long maxId()
-		throws LuanException, IOException, SQLException
-	{
-		Statement stmt = con.createStatement();
-		ResultSet rs = stmt.executeQuery("select max(id) as m from lucene");
-		rs.next();
-		long m = rs.getLong("m");
-		stmt.close();
-		return m;
-	}
-
 	final class Checker {
 		private final Connection con;
 		private final PreparedStatement pstmt;