comparison src/goodjava/lucene/logging/LogFile.java @ 1476:7d145095cc0b

lucene.logging check
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Apr 2020 20:42:26 -0600
parents 5e3870618377
children 1f41e5921090
comparison
equal deleted inserted replaced
1475:c7b86342857f 1476:7d145095cc0b
1 package goodjava.lucene.logging; 1 package goodjava.lucene.logging;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.DataOutput;
6 import java.io.DataOutputStream;
4 import java.io.RandomAccessFile; 7 import java.io.RandomAccessFile;
8 import java.io.ByteArrayOutputStream;
9 import java.io.ByteArrayInputStream;
5 import java.io.IOException; 10 import java.io.IOException;
6 import java.util.List; 11 import java.util.List;
7 import java.util.ArrayList;
8 import java.util.Map; 12 import java.util.Map;
9 import java.util.LinkedHashMap;
10 import org.apache.lucene.index.Term; 13 import org.apache.lucene.index.Term;
11 import org.apache.lucene.search.Query; 14 import org.apache.lucene.search.Query;
12 import org.apache.lucene.search.MatchAllDocsQuery; 15 import org.apache.lucene.search.MatchAllDocsQuery;
13 import org.apache.lucene.search.TermQuery; 16 import org.apache.lucene.search.TermQuery;
14 import org.apache.lucene.search.PrefixQuery; 17 import org.apache.lucene.search.PrefixQuery;
21 import org.apache.lucene.util.BytesRef; 24 import org.apache.lucene.util.BytesRef;
22 import goodjava.logging.Logger; 25 import goodjava.logging.Logger;
23 import goodjava.logging.LoggerFactory; 26 import goodjava.logging.LoggerFactory;
24 27
25 28
26 public class LogFile extends RandomAccessFile { 29 public class LogFile {
27 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); 30 private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
28 public final File file; 31 public final File file;
32 private final RandomAccessFile raf;
33 private ByteArrayOutputStream baos = new ByteArrayOutputStream();
34 private DataOutput out;
29 private long end; 35 private long end;
30 36
31 public LogFile(File file,String mode) throws IOException { 37 public LogFile(File file,String mode) throws IOException {
32 super(file,mode);
33 this.file = file; 38 this.file = file;
39 this.raf = new RandomAccessFile(file,mode);
40 this.out = new DataOutputStream(baos);
34 init(); 41 init();
35 } 42 }
36 43
37 private void init() throws IOException { 44 private void init() throws IOException {
38 if( length() == 0 ) { 45 if( raf.length() == 0 ) {
39 end = 8; 46 end = 8;
40 writeLong(end); 47 raf.writeLong(end);
41 } else { 48 } else {
42 seek(0L); 49 raf.seek(0L);
43 end = readLong(); 50 end = raf.readLong();
44 gotoEnd(); 51 raf.seek(end);
45 } 52 }
53 }
54
55 private LogFile(LogFile lf) throws IOException {
56 this.file = lf.file;
57 this.raf = new RandomAccessFile(file,"r");
58 this.out = null;
59 this.end = lf.end;
60 }
61
62 public LogFile snapshot() throws IOException {
63 return new LogFile(this);
46 } 64 }
47 65
48 public String toString() { 66 public String toString() {
49 return "LogFile<" + file.getName() + ">"; 67 return "LogFile<" + file.getName() + ">";
50 } 68 }
51 69
52 public void gotoStart() throws IOException { 70 public long end() {
53 seek(8L); 71 return end;
54 } 72 }
55 73
56 public void gotoEnd() throws IOException { 74 public LogInputStream input() throws IOException {
57 seek(end); 75 byte[] a = new byte[(int)end - 8];
76 raf.seek(8L);
77 raf.readFully(a);
78 return newLogInputStream(new ByteArrayInputStream(a));
79 }
80
81 protected LogInputStream newLogInputStream(InputStream in) {
82 return new LogInputStream(in);
58 } 83 }
59 84
60 public void commit() throws IOException { 85 public void commit() throws IOException {
61 end = getFilePointer(); 86 raf.seek(end);
62 seek(0L); 87 raf.write(baos.toByteArray());
63 writeLong(end); 88 //logger.info("size "+baos.size());
64 gotoEnd(); 89 if( baos.size() < 10000 ) {
65 } 90 baos.reset();
66 91 } else {
67 public boolean hasMore() throws IOException { 92 baos = new ByteArrayOutputStream();
68 return getFilePointer() < end; 93 out = new DataOutputStream(baos);
69 } 94 }
70 95 end = raf.getFilePointer();
71 private static final int TYPE_NULL = 0; 96 raf.seek(0L);
72 private static final int TYPE_STRING = 1; 97 raf.writeLong(end);
73 private static final int TYPE_INT = 2; 98 }
74 private static final int TYPE_LONG = 3; 99
75 private static final int TYPE_FLOAT = 4; 100 public void rollback() throws IOException {
76 private static final int TYPE_DOUBLE = 5; 101 baos.reset();
77 private static final int TYPE_BYTES = 6; 102 }
78 private static final int TYPE_LIST = 7; 103
79 private static final int TYPE_QUERY_MATCH_ALL_DOCS = 8; 104 static final int TYPE_NULL = 0;
80 private static final int TYPE_QUERY_TERM = 9; 105 static final int TYPE_STRING = 1;
81 private static final int TYPE_QUERY_PREFIX = 10; 106 static final int TYPE_INT = 2;
82 private static final int TYPE_QUERY_WILDCARD = 11; 107 static final int TYPE_LONG = 3;
83 private static final int TYPE_QUERY_TERM_RANGE = 12; 108 static final int TYPE_FLOAT = 4;
84 private static final int TYPE_QUERY_PHRASE = 13; 109 static final int TYPE_DOUBLE = 5;
85 private static final int TYPE_QUERY_NUMERIC_RANGE = 14; 110 static final int TYPE_BYTES = 6;
86 private static final int TYPE_QUERY_BOOLEAN = 15; 111 static final int TYPE_LIST = 7;
112 static final int TYPE_QUERY_MATCH_ALL_DOCS = 8;
113 static final int TYPE_QUERY_TERM = 9;
114 static final int TYPE_QUERY_PREFIX = 10;
115 static final int TYPE_QUERY_WILDCARD = 11;
116 static final int TYPE_QUERY_TERM_RANGE = 12;
117 static final int TYPE_QUERY_PHRASE = 13;
118 static final int TYPE_QUERY_NUMERIC_RANGE = 14;
119 static final int TYPE_QUERY_BOOLEAN = 15;
87 120
88 public void writeObject(Object obj) throws IOException { 121 public void writeObject(Object obj) throws IOException {
89 if( obj==null ) { 122 if( obj==null ) {
90 writeByte(TYPE_NULL); 123 writeByte(TYPE_NULL);
91 return; 124 return;
193 return; 226 return;
194 } 227 }
195 throw new IllegalArgumentException("invalid type for "+obj); 228 throw new IllegalArgumentException("invalid type for "+obj);
196 } 229 }
197 230
198 public Object readObject() throws IOException {
199 int type = readByte();
200 return readObject(type);
201 }
202
203 protected Object readObject(int type) throws IOException {
204 switch(type) {
205 case TYPE_NULL:
206 return null;
207 case TYPE_STRING:
208 return readUTF();
209 case TYPE_INT:
210 return readInt();
211 case TYPE_LONG:
212 return readLong();
213 case TYPE_FLOAT:
214 return readFloat();
215 case TYPE_DOUBLE:
216 return readDouble();
217 case TYPE_BYTES:
218 return readByteArray();
219 case TYPE_LIST:
220 return readList();
221 case TYPE_QUERY_MATCH_ALL_DOCS:
222 return new MatchAllDocsQuery();
223 case TYPE_QUERY_TERM:
224 return new TermQuery( readTerm() );
225 case TYPE_QUERY_PREFIX:
226 return new PrefixQuery( readTerm() );
227 case TYPE_QUERY_WILDCARD:
228 return new WildcardQuery( readTerm() );
229 case TYPE_QUERY_TERM_RANGE:
230 {
231 String field = readUTF();
232 BytesRef lowerTerm = readBytesRef();
233 BytesRef upperTerm = readBytesRef();
234 boolean includeLower = readBoolean();
235 boolean includeUpper = readBoolean();
236 return new TermRangeQuery(field,lowerTerm,upperTerm,includeLower,includeUpper);
237 }
238 case TYPE_QUERY_PHRASE:
239 {
240 PhraseQuery query = new PhraseQuery();
241 int n = readInt();
242 for( int i=0; i<n; i++ ) {
243 Term term = readTerm();
244 int position = readInt();
245 query.add(term,position);
246 }
247 return query;
248 }
249 case TYPE_QUERY_NUMERIC_RANGE:
250 {
251 String field = readUTF();
252 Number min = (Number)readObject();
253 Number max = (Number)readObject();
254 boolean minInclusive = readBoolean();
255 boolean maxInclusive = readBoolean();
256 Number n = min!=null ? min : max;
257 if( n instanceof Integer )
258 return NumericRangeQuery.newIntRange(field,(Integer)min,(Integer)max,minInclusive,maxInclusive);
259 if( n instanceof Long )
260 return NumericRangeQuery.newLongRange(field,(Long)min,(Long)max,minInclusive,maxInclusive);
261 if( n instanceof Float )
262 return NumericRangeQuery.newFloatRange(field,(Float)min,(Float)max,minInclusive,maxInclusive);
263 if( n instanceof Double )
264 return NumericRangeQuery.newDoubleRange(field,(Double)min,(Double)max,minInclusive,maxInclusive);
265 throw new RuntimeException("bad numeric type for "+n);
266 }
267 case TYPE_QUERY_BOOLEAN:
268 {
269 BooleanQuery query = new BooleanQuery();
270 int n = readInt();
271 for( int i=0; i<n; i++ ) {
272 Query subquery = readQuery();
273 BooleanClause.Occur occur = BooleanClause.Occur.valueOf( readUTF() );
274 query.add(subquery,occur);
275 }
276 return query;
277 }
278 default:
279 throw new RuntimeException("invalid type "+type);
280 }
281 }
282
283 public void writeByteArray(byte[] bytes) throws IOException { 231 public void writeByteArray(byte[] bytes) throws IOException {
284 writeInt(bytes.length); 232 writeInt(bytes.length);
285 write(bytes); 233 write(bytes);
286 }
287
288 public byte[] readByteArray() throws IOException {
289 int len = readInt();
290 byte[] bytes = new byte[len];
291 readFully(bytes);
292 return bytes;
293 } 234 }
294 235
295 public void writeList(List list) throws IOException { 236 public void writeList(List list) throws IOException {
296 writeInt(list.size()); 237 writeInt(list.size());
297 for( Object obj : list ) { 238 for( Object obj : list ) {
298 writeObject(obj); 239 writeObject(obj);
299 } 240 }
300 }
301
302 public List readList() throws IOException {
303 final int size = readInt();
304 List list = new ArrayList(size);
305 for( int i=0; i<size; i++ ) {
306 list.add( readObject() );
307 }
308 return list;
309 } 241 }
310 242
311 public void writeMap(Map map) throws IOException { 243 public void writeMap(Map map) throws IOException {
312 writeInt(map.size()); 244 writeInt(map.size());
313 for( Object obj : map.entrySet() ) { 245 for( Object obj : map.entrySet() ) {
315 writeObject( entry.getKey() ); 247 writeObject( entry.getKey() );
316 writeObject( entry.getValue() ); 248 writeObject( entry.getValue() );
317 } 249 }
318 } 250 }
319 251
320 public Map readMap() throws IOException {
321 final int size = readInt();
322 Map map = new LinkedHashMap();
323 for( int i=0; i<size; i++ ) {
324 Object key = readObject();
325 Object value = readObject();
326 map.put(key,value);
327 }
328 return map;
329 }
330
331 public void writeQuery(Query query) throws IOException { 252 public void writeQuery(Query query) throws IOException {
332 writeObject(query); 253 writeObject(query);
333 }
334
335 public Query readQuery() throws IOException {
336 return (Query)readObject();
337 } 254 }
338 255
339 public void writeBytesRef(BytesRef br) throws IOException { 256 public void writeBytesRef(BytesRef br) throws IOException {
340 writeInt(br.length); 257 writeInt(br.length);
341 write(br.bytes,0,br.length); 258 write(br.bytes,0,br.length);
342 } 259 }
343 260
344 public BytesRef readBytesRef() throws IOException {
345 return new BytesRef( readByteArray() );
346 }
347
348 public void writeTerm(Term term) throws IOException { 261 public void writeTerm(Term term) throws IOException {
349 writeUTF(term.field()); 262 writeUTF(term.field());
350 writeBytesRef( term.bytes() ); 263 writeBytesRef( term.bytes() );
351 } 264 }
352 265
353 public Term readTerm() throws IOException { 266
354 String key = readUTF(); 267 public void writeByte(int v) throws IOException {
355 BytesRef value = readBytesRef(); 268 out.writeByte(v);
356 return new Term(key,value); 269 }
270
271 public void writeInt(int v) throws IOException {
272 out.writeInt(v);
273 }
274
275 public void writeLong(long v) throws IOException {
276 out.writeLong(v);
277 }
278
279 public void writeFloat(float v) throws IOException {
280 out.writeFloat(v);
281 }
282
283 public void writeDouble(double v) throws IOException {
284 out.writeDouble(v);
285 }
286
287 public void writeBoolean(boolean v) throws IOException {
288 out.writeBoolean(v);
289 }
290
291 public void writeUTF(String s) throws IOException {
292 out.writeUTF(s);
293 }
294
295 public void write(byte[] b) throws IOException {
296 out.write(b);
297 }
298
299 public void write(byte[] b, int off, int len) throws IOException {
300 out.write(b,off,len);
357 } 301 }
358 302
359 } 303 }