Mercurial Hosting > luan
diff src/luan/modules/lucene/LuanOpDoer.java @ 1551:9cc4cee39b8b
add LuanOpDoer
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Oct 2020 16:29:54 -0600 |
parents | |
children | 52241b69c339 |
line wrap: on
line diff
--- /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); + } + } + +}