comparison src/goodjava/lucene/logging/LogFile.java @ 1484:1fa6e8ec2d53

lucene.logging cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 25 Apr 2020 11:14:25 -0600
parents 3cff066f3bbc
children 2469aa31f31b
comparison
equal deleted inserted replaced
1483:97740900c820 1484:1fa6e8ec2d53
27 import goodjava.logging.LoggerFactory; 27 import goodjava.logging.LoggerFactory;
28 import goodjava.io.LimitedInputStream; 28 import goodjava.io.LimitedInputStream;
29 import goodjava.io.BufferedInputStream; 29 import goodjava.io.BufferedInputStream;
30 30
31 31
32 public class LogFile { 32 public class LogFile extends DataOutputStream {
33 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); 33 private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
34 public final File file; 34 public final File file;
35 private final RandomAccessFile raf; 35 private final RandomAccessFile raf;
36 private final DataOutputStream out;
37 private long end; 36 private long end;
38 37
39 public LogFile(File file,String mode) throws IOException { 38 public static LogFile newLogFile(File file) throws IOException {
40 this.file = file; 39 RandomAccessFile raf = new RandomAccessFile(file,"rwd");
41 this.raf = new RandomAccessFile(file,mode);
42 OutputStream out = new FileOutputStream(raf.getFD()); 40 OutputStream out = new FileOutputStream(raf.getFD());
43 out = new BufferedOutputStream(out); 41 out = new BufferedOutputStream(out);
44 this.out = new DataOutputStream(out); 42 return new LogFile(file,raf,out);
45 init(); 43 }
46 } 44
47 45 protected LogFile(File file,RandomAccessFile raf,OutputStream out) throws IOException {
48 private void init() throws IOException { 46 super(out);
47 this.file = file;
48 this.raf = raf;
49
49 if( raf.length() == 0 ) { 50 if( raf.length() == 0 ) {
50 end = 8; 51 end = 8;
51 raf.writeLong(end); 52 raf.writeLong(end);
52 } else { 53 } else {
53 raf.seek(0L); 54 raf.seek(0L);
54 end = raf.readLong(); 55 end = raf.readLong();
55 raf.seek(end); 56 raf.seek(end);
56 } 57 }
57 } 58 }
58 59
59 private LogFile(LogFile lf) throws IOException {
60 this.file = lf.file;
61 this.raf = new RandomAccessFile(file,"r");
62 this.out = null;
63 this.end = lf.end;
64 }
65
66 public void close() throws IOException {
67 raf.close();
68 }
69
70 public LogFile snapshot() throws IOException {
71 return new LogFile(this);
72 }
73
74 public String toString() { 60 public String toString() {
75 return "LogFile<" + file.getName() + ">"; 61 return "LogFile<" + file.getName() + ">";
76 } 62 }
77 63
78 public long end() { 64 public long end() {
91 protected LogInputStream newLogInputStream(InputStream in) { 77 protected LogInputStream newLogInputStream(InputStream in) {
92 return new LogInputStream(in); 78 return new LogInputStream(in);
93 } 79 }
94 80
95 public void commit() throws IOException { 81 public void commit() throws IOException {
96 out.flush(); 82 flush();
97 end = raf.getFilePointer(); 83 end = raf.getFilePointer();
98 raf.seek(0L); 84 raf.seek(0L);
99 raf.writeLong(end); 85 raf.writeLong(end);
100 raf.seek(end); 86 raf.seek(end);
101 } 87 }
102 88
103 public void rollback() throws IOException { 89 public void rollback() throws IOException {
104 out.flush(); 90 flush();
105 raf.seek(end); 91 raf.seek(end);
106 } 92 }
107 93
108 static final int TYPE_NULL = 0; 94 static final int TYPE_NULL = 0;
109 static final int TYPE_STRING = 1; 95 static final int TYPE_STRING = 1;
265 public void writeTerm(Term term) throws IOException { 251 public void writeTerm(Term term) throws IOException {
266 writeUTF(term.field()); 252 writeUTF(term.field());
267 writeBytesRef( term.bytes() ); 253 writeBytesRef( term.bytes() );
268 } 254 }
269 255
270
271 public void writeByte(int v) throws IOException {
272 out.writeByte(v);
273 }
274
275 public void writeInt(int v) throws IOException {
276 out.writeInt(v);
277 }
278
279 public void writeLong(long v) throws IOException {
280 out.writeLong(v);
281 }
282
283 public void writeFloat(float v) throws IOException {
284 out.writeFloat(v);
285 }
286
287 public void writeDouble(double v) throws IOException {
288 out.writeDouble(v);
289 }
290
291 public void writeBoolean(boolean v) throws IOException {
292 out.writeBoolean(v);
293 }
294
295 public void writeUTF(String s) throws IOException {
296 out.writeUTF(s);
297 }
298
299 public void write(byte[] b) throws IOException {
300 out.write(b);
301 }
302
303 public void write(byte[] b, int off, int len) throws IOException {
304 out.write(b,off,len);
305 }
306
307 } 256 }