view src/luan/modules/lucene/LuanOpDoer.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents 52241b69c339
children
line wrap: on
line source

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.Luan;
import luan.LuanFunction;
import luan.LuanException;
import luan.LuanRuntimeException;


final class LuanOpDoer implements OpDoer {
	private final Luan luan;
	private final OpDoer opDoer;
	private final LuanFunction fn;

	LuanOpDoer(GoodIndexWriter writer,Luan luan,LuanFunction fn) {
		this.opDoer = new BasicOpDoer(writer);
		this.luan = luan;
		this.fn = fn;
	}

	@Override public void commit() throws IOException {
		try {
			fn.call(luan,new CommitAction(opDoer));
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	@Override public void deleteAll(long time) throws IOException {
		try {
			fn.call(luan,new DeleteAllAction(opDoer,time));
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	@Override public void deleteDocuments(long time,Query query) throws IOException {
		try {
			fn.call(luan,new DeleteDocumentsAction(opDoer,time,query));
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	@Override public void addDocument(long time,Map<String,Object> storedFields) throws IOException {
		try {
			fn.call(luan,new AddDocumentAction(opDoer,time,storedFields));
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	@Override public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException {
		try {
			fn.call(luan,new UpdateDocumentAction(opDoer,time,keyFieldName,storedFields));
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	@Override public void tag(long time,String tag) throws IOException {
		try {
			fn.call(luan,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);
		}
	}

}