Mercurial Hosting > luan
comparison src/goodjava/lucene/logging/LogFile.java @ 1481:3cff066f3bbc
output buffering
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 24 Apr 2020 15:57:30 -0600 |
parents | 1f41e5921090 |
children | 1fa6e8ec2d53 |
comparison
equal
deleted
inserted
replaced
1480:1f41e5921090 | 1481:3cff066f3bbc |
---|---|
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; | 4 import java.io.InputStream; |
5 import java.io.DataOutput; | 5 import java.io.OutputStream; |
6 import java.io.BufferedOutputStream; | |
6 import java.io.DataOutputStream; | 7 import java.io.DataOutputStream; |
7 import java.io.RandomAccessFile; | 8 import java.io.RandomAccessFile; |
8 import java.io.ByteArrayOutputStream; | |
9 import java.io.FileInputStream; | 9 import java.io.FileInputStream; |
10 import java.io.FileOutputStream; | |
10 import java.io.IOException; | 11 import java.io.IOException; |
11 import java.util.List; | 12 import java.util.List; |
12 import java.util.Map; | 13 import java.util.Map; |
13 import org.apache.lucene.index.Term; | 14 import org.apache.lucene.index.Term; |
14 import org.apache.lucene.search.Query; | 15 import org.apache.lucene.search.Query; |
30 | 31 |
31 public class LogFile { | 32 public class LogFile { |
32 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); | 33 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); |
33 public final File file; | 34 public final File file; |
34 private final RandomAccessFile raf; | 35 private final RandomAccessFile raf; |
35 private ByteArrayOutputStream baos = new ByteArrayOutputStream(); | 36 private final DataOutputStream out; |
36 private DataOutput out; | |
37 private long end; | 37 private long end; |
38 | 38 |
39 public LogFile(File file,String mode) throws IOException { | 39 public LogFile(File file,String mode) throws IOException { |
40 this.file = file; | 40 this.file = file; |
41 this.raf = new RandomAccessFile(file,mode); | 41 this.raf = new RandomAccessFile(file,mode); |
42 this.out = new DataOutputStream(baos); | 42 OutputStream out = new FileOutputStream(raf.getFD()); |
43 out = new BufferedOutputStream(out); | |
44 this.out = new DataOutputStream(out); | |
43 init(); | 45 init(); |
44 } | 46 } |
45 | 47 |
46 private void init() throws IOException { | 48 private void init() throws IOException { |
47 if( raf.length() == 0 ) { | 49 if( raf.length() == 0 ) { |
59 this.raf = new RandomAccessFile(file,"r"); | 61 this.raf = new RandomAccessFile(file,"r"); |
60 this.out = null; | 62 this.out = null; |
61 this.end = lf.end; | 63 this.end = lf.end; |
62 } | 64 } |
63 | 65 |
66 public void close() throws IOException { | |
67 raf.close(); | |
68 } | |
69 | |
64 public LogFile snapshot() throws IOException { | 70 public LogFile snapshot() throws IOException { |
65 return new LogFile(this); | 71 return new LogFile(this); |
66 } | 72 } |
67 | 73 |
68 public String toString() { | 74 public String toString() { |
85 protected LogInputStream newLogInputStream(InputStream in) { | 91 protected LogInputStream newLogInputStream(InputStream in) { |
86 return new LogInputStream(in); | 92 return new LogInputStream(in); |
87 } | 93 } |
88 | 94 |
89 public void commit() throws IOException { | 95 public void commit() throws IOException { |
90 raf.seek(end); | 96 out.flush(); |
91 raf.write(baos.toByteArray()); | |
92 //logger.info("size "+baos.size()); | |
93 if( baos.size() < 10000 ) { | |
94 baos.reset(); | |
95 } else { | |
96 baos = new ByteArrayOutputStream(); | |
97 out = new DataOutputStream(baos); | |
98 } | |
99 end = raf.getFilePointer(); | 97 end = raf.getFilePointer(); |
100 raf.seek(0L); | 98 raf.seek(0L); |
101 raf.writeLong(end); | 99 raf.writeLong(end); |
100 raf.seek(end); | |
102 } | 101 } |
103 | 102 |
104 public void rollback() throws IOException { | 103 public void rollback() throws IOException { |
105 baos.reset(); | 104 out.flush(); |
105 raf.seek(end); | |
106 } | 106 } |
107 | 107 |
108 static final int TYPE_NULL = 0; | 108 static final int TYPE_NULL = 0; |
109 static final int TYPE_STRING = 1; | 109 static final int TYPE_STRING = 1; |
110 static final int TYPE_INT = 2; | 110 static final int TYPE_INT = 2; |