1461
|
1 package goodjava.lucene.logging;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.RandomAccessFile;
|
|
5 import java.io.IOException;
|
|
6 import java.util.List;
|
|
7 import java.util.ArrayList;
|
|
8 import java.util.Map;
|
|
9 import java.util.LinkedHashMap;
|
|
10 import org.apache.lucene.index.Term;
|
|
11 import org.apache.lucene.search.Query;
|
|
12 import org.apache.lucene.search.MatchAllDocsQuery;
|
|
13 import org.apache.lucene.search.TermQuery;
|
|
14 import org.apache.lucene.search.PrefixQuery;
|
|
15 import org.apache.lucene.search.WildcardQuery;
|
|
16 import org.apache.lucene.search.TermRangeQuery;
|
|
17 import org.apache.lucene.search.PhraseQuery;
|
|
18 import org.apache.lucene.search.NumericRangeQuery;
|
|
19 import org.apache.lucene.search.BooleanQuery;
|
|
20 import org.apache.lucene.search.BooleanClause;
|
|
21 import org.apache.lucene.util.BytesRef;
|
|
22
|
|
23
|
1462
|
24 public class LogFile extends RandomAccessFile {
|
1461
|
25 private long end;
|
|
26
|
|
27 public LogFile(File file,String mode) throws IOException {
|
|
28 super(file,mode);
|
|
29 init();
|
|
30 }
|
|
31
|
|
32 public LogFile(String file,String mode) throws IOException {
|
|
33 super(file,mode);
|
|
34 init();
|
|
35 }
|
|
36
|
|
37 private void init() throws IOException {
|
|
38 if( length() == 0 ) {
|
|
39 end = 8;
|
|
40 writeLong(end);
|
|
41 } else {
|
|
42 seek(0L);
|
|
43 end = readLong();
|
|
44 gotoEnd();
|
|
45 }
|
|
46 }
|
|
47
|
|
48 public void gotoStart() throws IOException {
|
|
49 seek(8L);
|
|
50 }
|
|
51
|
|
52 public void gotoEnd() throws IOException {
|
|
53 seek(end);
|
|
54 }
|
|
55
|
|
56 public void commit() throws IOException {
|
|
57 end = getFilePointer();
|
|
58 seek(0L);
|
|
59 writeLong(end);
|
|
60 gotoEnd();
|
|
61 }
|
|
62
|
|
63 public boolean hasMore() throws IOException {
|
|
64 return getFilePointer() < end;
|
|
65 }
|
|
66
|
|
67 private static final int TYPE_NULL = 0;
|
|
68 private static final int TYPE_STRING = 1;
|
|
69 private static final int TYPE_INT = 2;
|
|
70 private static final int TYPE_LONG = 3;
|
|
71 private static final int TYPE_FLOAT = 4;
|
|
72 private static final int TYPE_DOUBLE = 5;
|
|
73 private static final int TYPE_BYTES = 6;
|
|
74 private static final int TYPE_LIST = 7;
|
|
75 private static final int TYPE_QUERY_MATCH_ALL_DOCS = 8;
|
|
76 private static final int TYPE_QUERY_TERM = 9;
|
|
77 private static final int TYPE_QUERY_PREFIX = 10;
|
|
78 private static final int TYPE_QUERY_WILDCARD = 11;
|
|
79 private static final int TYPE_QUERY_TERM_RANGE = 12;
|
|
80 private static final int TYPE_QUERY_PHRASE = 13;
|
|
81 private static final int TYPE_QUERY_NUMERIC_RANGE = 14;
|
|
82 private static final int TYPE_QUERY_BOOLEAN = 15;
|
|
83
|
|
84 public void writeObject(Object obj) throws IOException {
|
|
85 if( obj==null ) {
|
|
86 writeByte(TYPE_NULL);
|
|
87 return;
|
|
88 }
|
|
89 if( obj instanceof String ) {
|
|
90 writeByte(TYPE_STRING);
|
|
91 writeUTF((String)obj);
|
|
92 return;
|
|
93 }
|
|
94 if( obj instanceof Integer ) {
|
|
95 writeByte(TYPE_INT);
|
|
96 writeInt((Integer)obj);
|
|
97 return;
|
|
98 }
|
|
99 if( obj instanceof Long ) {
|
|
100 writeByte(TYPE_LONG);
|
|
101 writeLong((Long)obj);
|
|
102 return;
|
|
103 }
|
|
104 if( obj instanceof Float ) {
|
|
105 writeByte(TYPE_FLOAT);
|
|
106 writeFloat((Float)obj);
|
|
107 return;
|
|
108 }
|
|
109 if( obj instanceof Double ) {
|
|
110 writeByte(TYPE_DOUBLE);
|
|
111 writeDouble((Double)obj);
|
|
112 return;
|
|
113 }
|
|
114 if( obj instanceof byte[] ) {
|
|
115 writeByte(TYPE_BYTES);
|
|
116 writeByteArray((byte[])obj);
|
|
117 return;
|
|
118 }
|
|
119 if( obj instanceof List ) {
|
|
120 writeByte(TYPE_LIST);
|
|
121 writeList((List)obj);
|
|
122 return;
|
|
123 }
|
|
124 if( obj instanceof MatchAllDocsQuery ) {
|
|
125 writeByte(TYPE_QUERY_MATCH_ALL_DOCS);
|
|
126 return;
|
|
127 }
|
|
128 if( obj instanceof TermQuery ) {
|
|
129 writeByte(TYPE_QUERY_TERM);
|
|
130 TermQuery query = (TermQuery)obj;
|
|
131 writeTerm( query.getTerm() );
|
|
132 return;
|
|
133 }
|
|
134 if( obj instanceof PrefixQuery ) {
|
|
135 writeByte(TYPE_QUERY_PREFIX);
|
|
136 PrefixQuery query = (PrefixQuery)obj;
|
|
137 writeTerm( query.getPrefix() );
|
|
138 return;
|
|
139 }
|
|
140 if( obj instanceof WildcardQuery ) {
|
|
141 writeByte(TYPE_QUERY_TERM_RANGE);
|
|
142 WildcardQuery query = (WildcardQuery)obj;
|
|
143 writeTerm( query.getTerm() );
|
|
144 return;
|
|
145 }
|
|
146 if( obj instanceof TermRangeQuery ) {
|
|
147 writeByte(TYPE_QUERY_TERM_RANGE);
|
|
148 TermRangeQuery query = (TermRangeQuery)obj;
|
|
149 writeUTF( query.getField() );
|
|
150 writeBytesRef( query.getLowerTerm() );
|
|
151 writeBytesRef( query.getUpperTerm() );
|
|
152 writeBoolean( query.includesLower() );
|
|
153 writeBoolean( query.includesUpper() );
|
|
154 return;
|
|
155 }
|
|
156 if( obj instanceof PhraseQuery ) {
|
|
157 writeByte(TYPE_QUERY_PHRASE);
|
|
158 PhraseQuery query = (PhraseQuery)obj;
|
|
159 Term[] terms = query.getTerms();
|
|
160 int[] positions = query.getPositions();
|
|
161 if( terms.length != positions.length )
|
|
162 throw new RuntimeException();
|
|
163 writeInt( terms.length );
|
|
164 for( int i=0; i<terms.length; i++ ) {
|
|
165 writeTerm( terms[i] );
|
|
166 writeInt( positions[i] );
|
|
167 }
|
|
168 return;
|
|
169 }
|
|
170 if( obj instanceof NumericRangeQuery ) {
|
|
171 writeByte(TYPE_QUERY_NUMERIC_RANGE);
|
|
172 NumericRangeQuery query = (NumericRangeQuery)obj;
|
|
173 writeUTF( query.getField() );
|
|
174 writeObject( query.getMin() );
|
|
175 writeObject( query.getMax() );
|
|
176 writeBoolean( query.includesMin() );
|
|
177 writeBoolean( query.includesMax() );
|
|
178 return;
|
|
179 }
|
|
180 if( obj instanceof BooleanQuery ) {
|
|
181 writeByte(TYPE_QUERY_BOOLEAN);
|
|
182 BooleanQuery query = (BooleanQuery)obj;
|
|
183 BooleanClause[] a = query.getClauses();
|
|
184 writeInt(a.length);
|
|
185 for( BooleanClause bc : a ) {
|
|
186 writeQuery( bc.getQuery() );
|
|
187 writeUTF( bc.getOccur().name() );
|
|
188 }
|
|
189 return;
|
|
190 }
|
|
191 throw new IllegalArgumentException("invalid type for "+obj);
|
|
192 }
|
|
193
|
|
194 public Object readObject() throws IOException {
|
|
195 int type = readByte();
|
1462
|
196 return readObject(type);
|
|
197 }
|
|
198
|
|
199 protected Object readObject(int type) throws IOException {
|
1461
|
200 switch(type) {
|
|
201 case TYPE_NULL:
|
|
202 return null;
|
|
203 case TYPE_STRING:
|
|
204 return readUTF();
|
|
205 case TYPE_INT:
|
|
206 return readInt();
|
|
207 case TYPE_LONG:
|
|
208 return readLong();
|
|
209 case TYPE_FLOAT:
|
|
210 return readFloat();
|
|
211 case TYPE_DOUBLE:
|
|
212 return readDouble();
|
|
213 case TYPE_BYTES:
|
|
214 return readByteArray();
|
|
215 case TYPE_LIST:
|
|
216 return readList();
|
|
217 case TYPE_QUERY_MATCH_ALL_DOCS:
|
|
218 return new MatchAllDocsQuery();
|
|
219 case TYPE_QUERY_TERM:
|
|
220 return new TermQuery( readTerm() );
|
|
221 case TYPE_QUERY_PREFIX:
|
|
222 return new PrefixQuery( readTerm() );
|
|
223 case TYPE_QUERY_WILDCARD:
|
|
224 return new WildcardQuery( readTerm() );
|
|
225 case TYPE_QUERY_TERM_RANGE:
|
|
226 {
|
|
227 String field = readUTF();
|
|
228 BytesRef lowerTerm = readBytesRef();
|
|
229 BytesRef upperTerm = readBytesRef();
|
|
230 boolean includeLower = readBoolean();
|
|
231 boolean includeUpper = readBoolean();
|
|
232 return new TermRangeQuery(field,lowerTerm,upperTerm,includeLower,includeUpper);
|
|
233 }
|
|
234 case TYPE_QUERY_PHRASE:
|
|
235 {
|
|
236 PhraseQuery query = new PhraseQuery();
|
|
237 int n = readInt();
|
|
238 for( int i=0; i<n; i++ ) {
|
|
239 Term term = readTerm();
|
|
240 int position = readInt();
|
|
241 query.add(term,position);
|
|
242 }
|
|
243 return query;
|
|
244 }
|
|
245 case TYPE_QUERY_NUMERIC_RANGE:
|
|
246 {
|
|
247 String field = readUTF();
|
|
248 Number min = (Number)readObject();
|
|
249 Number max = (Number)readObject();
|
|
250 boolean minInclusive = readBoolean();
|
|
251 boolean maxInclusive = readBoolean();
|
|
252 Number n = min!=null ? min : max;
|
|
253 if( n instanceof Integer )
|
|
254 return NumericRangeQuery.newIntRange(field,(Integer)min,(Integer)max,minInclusive,maxInclusive);
|
|
255 if( n instanceof Long )
|
|
256 return NumericRangeQuery.newLongRange(field,(Long)min,(Long)max,minInclusive,maxInclusive);
|
|
257 if( n instanceof Float )
|
|
258 return NumericRangeQuery.newFloatRange(field,(Float)min,(Float)max,minInclusive,maxInclusive);
|
|
259 if( n instanceof Double )
|
|
260 return NumericRangeQuery.newDoubleRange(field,(Double)min,(Double)max,minInclusive,maxInclusive);
|
|
261 throw new RuntimeException("bad numeric type for "+n);
|
|
262 }
|
|
263 case TYPE_QUERY_BOOLEAN:
|
|
264 {
|
|
265 BooleanQuery query = new BooleanQuery();
|
|
266 int n = readInt();
|
|
267 for( int i=0; i<n; i++ ) {
|
|
268 Query subquery = readQuery();
|
|
269 BooleanClause.Occur occur = BooleanClause.Occur.valueOf( readUTF() );
|
|
270 query.add(subquery,occur);
|
|
271 }
|
|
272 return query;
|
|
273 }
|
|
274 default:
|
|
275 throw new RuntimeException("invalid type "+type);
|
|
276 }
|
|
277 }
|
|
278
|
|
279 public void writeByteArray(byte[] bytes) throws IOException {
|
|
280 writeInt(bytes.length);
|
|
281 write(bytes);
|
|
282 }
|
|
283
|
|
284 public byte[] readByteArray() throws IOException {
|
|
285 int len = readInt();
|
|
286 byte[] bytes = new byte[len];
|
|
287 readFully(bytes);
|
|
288 return bytes;
|
|
289 }
|
|
290
|
|
291 public void writeList(List list) throws IOException {
|
|
292 writeInt(list.size());
|
|
293 for( Object obj : list ) {
|
|
294 writeObject(obj);
|
|
295 }
|
|
296 }
|
|
297
|
|
298 public List readList() throws IOException {
|
|
299 final int size = readInt();
|
|
300 List list = new ArrayList(size);
|
|
301 for( int i=0; i<size; i++ ) {
|
|
302 list.add( readObject() );
|
|
303 }
|
|
304 return list;
|
|
305 }
|
|
306
|
|
307 public void writeMap(Map map) throws IOException {
|
|
308 writeInt(map.size());
|
|
309 for( Object obj : map.entrySet() ) {
|
|
310 Map.Entry entry = (Map.Entry)obj;
|
|
311 writeObject( entry.getKey() );
|
|
312 writeObject( entry.getValue() );
|
|
313 }
|
|
314 }
|
|
315
|
|
316 public Map readMap() throws IOException {
|
|
317 final int size = readInt();
|
|
318 Map map = new LinkedHashMap();
|
|
319 for( int i=0; i<size; i++ ) {
|
|
320 Object key = readObject();
|
|
321 Object value = readObject();
|
|
322 map.put(key,value);
|
|
323 }
|
|
324 return map;
|
|
325 }
|
|
326
|
|
327 public void writeQuery(Query query) throws IOException {
|
|
328 writeObject(query);
|
|
329 }
|
|
330
|
|
331 public Query readQuery() throws IOException {
|
|
332 return (Query)readObject();
|
|
333 }
|
|
334
|
|
335 public void writeBytesRef(BytesRef br) throws IOException {
|
|
336 writeInt(br.length);
|
|
337 write(br.bytes,0,br.length);
|
|
338 }
|
|
339
|
|
340 public BytesRef readBytesRef() throws IOException {
|
|
341 return new BytesRef( readByteArray() );
|
|
342 }
|
|
343
|
|
344 public void writeTerm(Term term) throws IOException {
|
|
345 writeUTF(term.field());
|
|
346 writeBytesRef( term.bytes() );
|
|
347 }
|
|
348
|
|
349 public Term readTerm() throws IOException {
|
|
350 String key = readUTF();
|
|
351 BytesRef value = readBytesRef();
|
|
352 return new Term(key,value);
|
|
353 }
|
|
354
|
|
355 }
|