changeset 1551:9cc4cee39b8b

add LuanOpDoer
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Oct 2020 16:29:54 -0600
parents 0dc3be25ad20
children 46d4baaad64d
files src/goodjava/lucene/logging/BasicOpDoer.java src/goodjava/lucene/logging/LoggingIndexWriter.java src/goodjava/lucene/logging/OpDoer.java src/luan/modules/lucene/LuanOpDoer.java src/luan/modules/lucene/LuceneIndex.java
diffstat 5 files changed, 244 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
diff -r 0dc3be25ad20 -r 9cc4cee39b8b src/goodjava/lucene/logging/BasicOpDoer.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/logging/BasicOpDoer.java	Sun Oct 04 16:29:54 2020 -0600
@@ -0,0 +1,53 @@
+package goodjava.lucene.logging;
+
+import java.io.IOException;
+import java.util.Map;
+import org.apache.lucene.search.Query;
+import goodjava.lucene.api.GoodIndexWriter;
+
+
+public class BasicOpDoer implements OpDoer {
+	protected final GoodIndexWriter writer;
+	protected boolean isActive = true;
+
+	public BasicOpDoer(GoodIndexWriter writer) {
+		this.writer = writer;
+	}
+
+	public GoodIndexWriter writer() {
+		return writer;
+	}
+
+	public void commit() throws IOException {
+		if( !isActive )
+			return;
+		writer.commit();
+	}
+
+	public void deleteAll(long time) throws IOException {
+		if( !isActive )
+			return;
+		writer.deleteAll();
+	}
+
+	public void deleteDocuments(long time,Query query) throws IOException {
+		if( !isActive )
+			return;
+		writer.deleteDocuments(query);
+	}
+
+	public void addDocument(long time,Map<String,Object> storedFields) throws IOException {
+		if( !isActive )
+			return;
+		writer.addDocument(storedFields);
+	}
+
+	public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException {
+		if( !isActive )
+			return;
+		writer.updateDocument(keyFieldName,storedFields);
+	}
+
+	public void tag(long time,String tag) throws IOException {}
+
+}
diff -r 0dc3be25ad20 -r 9cc4cee39b8b src/goodjava/lucene/logging/LoggingIndexWriter.java
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sat Oct 03 23:08:36 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sun Oct 04 16:29:54 2020 -0600
@@ -223,7 +223,7 @@
 			throw new RuntimeException();
 		Directory dir = FSDirectory.open(dirFile);
 		LuceneIndexWriter mergeWriter = new LuceneIndexWriter( dir, indexWriter.goodConfig );
-		OpDoer opDoer = new OpDoer(mergeWriter);
+		OpDoer opDoer = new BasicOpDoer(mergeWriter);
 		playLog( first.input(), opDoer );
 		playLog( second.input(), opDoer );
 		mergeWriter.commit();
@@ -300,7 +300,7 @@
 			IoUtils.deleteRecursively(dirFile);
 			Directory dir = FSDirectory.open(dirFile);
 			LuceneIndexWriter checkWriter = new LuceneIndexWriter( dir, indexWriter.goodConfig );
-			playLogs(logReaders,new OpDoer(checkWriter));
+			playLogs(logReaders,new BasicOpDoer(checkWriter));
 			//logger.info("check lucene");
 			IndexReader checkReader = checkWriter.openReader();
 			int nCheck = checkReader.numDocs();
@@ -448,10 +448,9 @@
 		log.writeByte(op);
 	}
 
-	// return whether stopped at tag
 	public synchronized void playLogs(OpDoer opDoer) throws IOException {
 		if( opDoer == null )
-			opDoer = new OpDoer(indexWriter);
+			opDoer = new BasicOpDoer(indexWriter);
 		playLogs( logReaders(logs), opDoer );
 	}
 
@@ -466,7 +465,7 @@
 	private static void playLogs(LogInputStream[] logReaders,OpDoer opDoer)
 		throws IOException
 	{
-		if( numDocs(opDoer.writer) != 0 )
+		if( numDocs(opDoer.writer()) != 0 )
 			throw new RuntimeException ("not empty");
 		for( LogInputStream reader : logReaders ) {
 			playLog(reader,opDoer);
diff -r 0dc3be25ad20 -r 9cc4cee39b8b src/goodjava/lucene/logging/OpDoer.java
--- a/src/goodjava/lucene/logging/OpDoer.java	Sat Oct 03 23:08:36 2020 -0600
+++ b/src/goodjava/lucene/logging/OpDoer.java	Sun Oct 04 16:29:54 2020 -0600
@@ -3,52 +3,15 @@
 import java.io.IOException;
 import java.util.Map;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.index.IndexReader;
 import goodjava.lucene.api.GoodIndexWriter;
 
 
-public class OpDoer {
-	protected final GoodIndexWriter writer;
-	protected boolean isActive = true;
-
-	protected OpDoer(GoodIndexWriter writer) {
-		this.writer = writer;
-	}
-
-	public IndexReader openReader() throws IOException {
-		return writer.openReader();
-	}
-
-	public void commit() throws IOException {
-		if( !isActive )
-			return;
-		writer.commit();
-	}
-
-	public void deleteAll(long time) throws IOException {
-		if( !isActive )
-			return;
-		writer.deleteAll();
-	}
-
-	public void deleteDocuments(long time,Query query) throws IOException {
-		if( !isActive )
-			return;
-		writer.deleteDocuments(query);
-	}
-
-	public void addDocument(long time,Map<String,Object> storedFields) throws IOException {
-		if( !isActive )
-			return;
-		writer.addDocument(storedFields);
-	}
-
-	public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException {
-		if( !isActive )
-			return;
-		writer.updateDocument(keyFieldName,storedFields);
-	}
-
-	public void tag(long time,String tag) throws IOException {}
-
+public interface OpDoer {
+	public GoodIndexWriter writer();
+	public void commit() throws IOException;
+	public void deleteAll(long time) throws IOException;
+	public void deleteDocuments(long time,Query query) throws IOException;
+	public void addDocument(long time,Map<String,Object> storedFields) throws IOException;
+	public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException;
+	public void tag(long time,String tag) throws IOException;
 }
diff -r 0dc3be25ad20 -r 9cc4cee39b8b src/luan/modules/lucene/LuanOpDoer.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/lucene/LuanOpDoer.java	Sun Oct 04 16:29:54 2020 -0600
@@ -0,0 +1,173 @@
+package luan.modules.lucene;
+
+import java.io.IOException;
+import java.util.Map;
+import org.apache.lucene.search.Query;
+import goodjava.lucene.api.GoodIndexWriter;
+import goodjava.lucene.logging.OpDoer;
+import goodjava.lucene.logging.BasicOpDoer;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanRuntimeException;
+
+
+final class LuanOpDoer implements OpDoer {
+	private final OpDoer opDoer;
+	private final LuanFunction fn;
+
+	LuanOpDoer(GoodIndexWriter writer,LuanFunction fn) {
+		this.opDoer = new BasicOpDoer(writer);
+		this.fn = fn;
+	}
+
+	public GoodIndexWriter writer() {
+		return opDoer.writer();
+	}
+
+	public void commit() throws IOException {
+		try {
+			fn.call(new CommitAction(opDoer));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public void deleteAll(long time) throws IOException {
+		try {
+			fn.call(new DeleteAllAction(opDoer,time));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public void deleteDocuments(long time,Query query) throws IOException {
+		try {
+			fn.call(new DeleteDocumentsAction(opDoer,time,query));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public void addDocument(long time,Map<String,Object> storedFields) throws IOException {
+		try {
+			fn.call(new AddDocumentAction(opDoer,time,storedFields));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException {
+		try {
+			fn.call(new UpdateDocumentAction(opDoer,time,keyFieldName,storedFields));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public void tag(long time,String tag) throws IOException {
+		try {
+			fn.call(new TagAction(opDoer,time,tag));
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+
+	public static abstract class Action {
+		public final OpDoer opDoer;
+		public final String name;
+
+		private Action(OpDoer opDoer,String name) {
+			this.opDoer = opDoer;
+			this.name = name;
+		}
+
+		public abstract void run() throws IOException;
+	}
+
+	public static final class CommitAction extends Action {
+
+		private CommitAction(OpDoer opDoer) {
+			super(opDoer,"commit");
+		}
+
+		public void run() throws IOException {
+			opDoer.commit();
+		}
+	}
+
+	public static final class DeleteAllAction extends Action {
+		public final long time;
+
+		private DeleteAllAction(OpDoer opDoer,long time) {
+			super(opDoer,"deleteAll");
+			this.time = time;
+		}
+
+		public void run() throws IOException {
+			opDoer.deleteAll(time);
+		}
+	}
+
+	public static final class DeleteDocumentsAction extends Action {
+		public final long time;
+		public final Query query;
+
+		private DeleteDocumentsAction(OpDoer opDoer,long time,Query query) {
+			super(opDoer,"deleteDocuments");
+			this.time = time;
+			this.query = query;
+		}
+
+		public void run() throws IOException {
+			opDoer.deleteDocuments(time,query);
+		}
+	}
+
+	public static final class AddDocumentAction extends Action {
+		public final long time;
+		public final Map<String,Object> storedFields;
+
+		private AddDocumentAction(OpDoer opDoer,long time,Map<String,Object> storedFields) {
+			super(opDoer,"addDocument");
+			this.time = time;
+			this.storedFields = storedFields;
+		}
+
+		public void run() throws IOException {
+			opDoer.addDocument(time,storedFields);
+		}
+	}
+
+	public static final class UpdateDocumentAction extends Action {
+		public final long time;
+		public final String keyFieldName;
+		public final Map<String,Object> storedFields;
+
+		private UpdateDocumentAction(OpDoer opDoer,long time,String keyFieldName,Map<String,Object> storedFields) {
+			super(opDoer,"updateDocument");
+			this.time = time;
+			this.keyFieldName = keyFieldName;
+			this.storedFields = storedFields;
+		}
+
+		public void run() throws IOException {
+			opDoer.updateDocument(time,keyFieldName,storedFields);
+		}
+	}
+
+	public static final class TagAction extends Action {
+		public final long time;
+		public final String tag;
+
+		private TagAction(OpDoer opDoer,long time,String tag) {
+			super(opDoer,"tag");
+			this.time = time;
+			this.tag = tag;
+		}
+
+		public void run() throws IOException {
+			opDoer.tag(time,tag);
+		}
+	}
+
+}
diff -r 0dc3be25ad20 -r 9cc4cee39b8b src/luan/modules/lucene/LuceneIndex.java
--- a/src/luan/modules/lucene/LuceneIndex.java	Sat Oct 03 23:08:36 2020 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Sun Oct 04 16:29:54 2020 -0600
@@ -80,6 +80,7 @@
 import goodjava.lucene.api.GoodIndexWriterConfig;
 import goodjava.lucene.api.LuceneUtils;
 import goodjava.lucene.logging.LoggingIndexWriter;
+import goodjava.lucene.logging.OpDoer;
 import goodjava.parser.ParseException;
 import luan.modules.Utils;
 import luan.Luan;
@@ -769,29 +770,30 @@
 		writer.addDocument(toLucene(doc));
 	}
 
-	public void restore_from_log()
+	public void restore_from_log(LuanFunction handler)
 		throws IOException, LuanException, SQLException, ParseException
 	{
 		LoggingIndexWriter loggingWriter = (LoggingIndexWriter)writer;
 		if( wasCreated && !loggingWriter.wasCreated ) {
 			logger.error("restoring from log");
-			force_restore_from_log();
+			force_restore_from_log(handler);
 		}
 	}
 
-	public void force_restore_from_log()
+	public void force_restore_from_log(LuanFunction handler)
 		throws IOException
 	{
 		logger.warn("start force_restore_from_log");
 		if( writeLock.isHeldByCurrentThread() )
 			throw new RuntimeException();
+		OpDoer opDoer = handler==null ? null : new LuanOpDoer(writer,handler);
 		writeLock.lock();
 		boolean ok = false;
 		try {
 			LoggingIndexWriter loggingWriter = (LoggingIndexWriter)writer;
 			IndexWriter iw = writer.getLuceneIndexWriter();
 			iw.deleteAll();
-			loggingWriter.playLogs(null);
+			loggingWriter.playLogs(opDoer);
 			ok = true;
 			wrote();
 			ensure_open();  // refresh searcher