1551
|
1 package luan.modules.lucene;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.util.Map;
|
|
5 import org.apache.lucene.search.Query;
|
|
6 import goodjava.lucene.api.GoodIndexWriter;
|
|
7 import goodjava.lucene.logging.OpDoer;
|
|
8 import goodjava.lucene.logging.BasicOpDoer;
|
|
9 import luan.LuanFunction;
|
|
10 import luan.LuanException;
|
|
11 import luan.LuanRuntimeException;
|
|
12
|
|
13
|
|
14 final class LuanOpDoer implements OpDoer {
|
|
15 private final OpDoer opDoer;
|
|
16 private final LuanFunction fn;
|
|
17
|
|
18 LuanOpDoer(GoodIndexWriter writer,LuanFunction fn) {
|
|
19 this.opDoer = new BasicOpDoer(writer);
|
|
20 this.fn = fn;
|
|
21 }
|
|
22
|
|
23 public GoodIndexWriter writer() {
|
|
24 return opDoer.writer();
|
|
25 }
|
|
26
|
|
27 public void commit() throws IOException {
|
|
28 try {
|
|
29 fn.call(new CommitAction(opDoer));
|
|
30 } catch(LuanException e) {
|
|
31 throw new LuanRuntimeException(e);
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public void deleteAll(long time) throws IOException {
|
|
36 try {
|
|
37 fn.call(new DeleteAllAction(opDoer,time));
|
|
38 } catch(LuanException e) {
|
|
39 throw new LuanRuntimeException(e);
|
|
40 }
|
|
41 }
|
|
42
|
|
43 public void deleteDocuments(long time,Query query) throws IOException {
|
|
44 try {
|
|
45 fn.call(new DeleteDocumentsAction(opDoer,time,query));
|
|
46 } catch(LuanException e) {
|
|
47 throw new LuanRuntimeException(e);
|
|
48 }
|
|
49 }
|
|
50
|
|
51 public void addDocument(long time,Map<String,Object> storedFields) throws IOException {
|
|
52 try {
|
|
53 fn.call(new AddDocumentAction(opDoer,time,storedFields));
|
|
54 } catch(LuanException e) {
|
|
55 throw new LuanRuntimeException(e);
|
|
56 }
|
|
57 }
|
|
58
|
|
59 public void updateDocument(long time,String keyFieldName,Map<String,Object> storedFields) throws IOException {
|
|
60 try {
|
|
61 fn.call(new UpdateDocumentAction(opDoer,time,keyFieldName,storedFields));
|
|
62 } catch(LuanException e) {
|
|
63 throw new LuanRuntimeException(e);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 public void tag(long time,String tag) throws IOException {
|
|
68 try {
|
|
69 fn.call(new TagAction(opDoer,time,tag));
|
|
70 } catch(LuanException e) {
|
|
71 throw new LuanRuntimeException(e);
|
|
72 }
|
|
73 }
|
|
74
|
|
75 public static abstract class Action {
|
|
76 public final OpDoer opDoer;
|
|
77 public final String name;
|
|
78
|
|
79 private Action(OpDoer opDoer,String name) {
|
|
80 this.opDoer = opDoer;
|
|
81 this.name = name;
|
|
82 }
|
|
83
|
|
84 public abstract void run() throws IOException;
|
|
85 }
|
|
86
|
|
87 public static final class CommitAction extends Action {
|
|
88
|
|
89 private CommitAction(OpDoer opDoer) {
|
|
90 super(opDoer,"commit");
|
|
91 }
|
|
92
|
|
93 public void run() throws IOException {
|
|
94 opDoer.commit();
|
|
95 }
|
|
96 }
|
|
97
|
|
98 public static final class DeleteAllAction extends Action {
|
|
99 public final long time;
|
|
100
|
|
101 private DeleteAllAction(OpDoer opDoer,long time) {
|
|
102 super(opDoer,"deleteAll");
|
|
103 this.time = time;
|
|
104 }
|
|
105
|
|
106 public void run() throws IOException {
|
|
107 opDoer.deleteAll(time);
|
|
108 }
|
|
109 }
|
|
110
|
|
111 public static final class DeleteDocumentsAction extends Action {
|
|
112 public final long time;
|
|
113 public final Query query;
|
|
114
|
|
115 private DeleteDocumentsAction(OpDoer opDoer,long time,Query query) {
|
|
116 super(opDoer,"deleteDocuments");
|
|
117 this.time = time;
|
|
118 this.query = query;
|
|
119 }
|
|
120
|
|
121 public void run() throws IOException {
|
|
122 opDoer.deleteDocuments(time,query);
|
|
123 }
|
|
124 }
|
|
125
|
|
126 public static final class AddDocumentAction extends Action {
|
|
127 public final long time;
|
|
128 public final Map<String,Object> storedFields;
|
|
129
|
|
130 private AddDocumentAction(OpDoer opDoer,long time,Map<String,Object> storedFields) {
|
|
131 super(opDoer,"addDocument");
|
|
132 this.time = time;
|
|
133 this.storedFields = storedFields;
|
|
134 }
|
|
135
|
|
136 public void run() throws IOException {
|
|
137 opDoer.addDocument(time,storedFields);
|
|
138 }
|
|
139 }
|
|
140
|
|
141 public static final class UpdateDocumentAction extends Action {
|
|
142 public final long time;
|
|
143 public final String keyFieldName;
|
|
144 public final Map<String,Object> storedFields;
|
|
145
|
|
146 private UpdateDocumentAction(OpDoer opDoer,long time,String keyFieldName,Map<String,Object> storedFields) {
|
|
147 super(opDoer,"updateDocument");
|
|
148 this.time = time;
|
|
149 this.keyFieldName = keyFieldName;
|
|
150 this.storedFields = storedFields;
|
|
151 }
|
|
152
|
|
153 public void run() throws IOException {
|
|
154 opDoer.updateDocument(time,keyFieldName,storedFields);
|
|
155 }
|
|
156 }
|
|
157
|
|
158 public static final class TagAction extends Action {
|
|
159 public final long time;
|
|
160 public final String tag;
|
|
161
|
|
162 private TagAction(OpDoer opDoer,long time,String tag) {
|
|
163 super(opDoer,"tag");
|
|
164 this.time = time;
|
|
165 this.tag = tag;
|
|
166 }
|
|
167
|
|
168 public void run() throws IOException {
|
|
169 opDoer.tag(time,tag);
|
|
170 }
|
|
171 }
|
|
172
|
|
173 }
|