Mercurial Hosting > luan
comparison src/goodjava/lucene/logging/LogFile.java @ 1486:2469aa31f31b
LogOutputStream
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2020 16:09:35 -0600 |
parents | 1fa6e8ec2d53 |
children | 9a2a2181a58f |
comparison
equal
deleted
inserted
replaced
1485:6a24c8b33d6b | 1486:2469aa31f31b |
---|---|
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.OutputStream; | 5 import java.io.OutputStream; |
6 import java.io.BufferedOutputStream; | 6 import java.io.BufferedOutputStream; |
7 import java.io.DataOutputStream; | |
8 import java.io.RandomAccessFile; | 7 import java.io.RandomAccessFile; |
9 import java.io.FileInputStream; | 8 import java.io.FileInputStream; |
10 import java.io.FileOutputStream; | 9 import java.io.FileOutputStream; |
11 import java.io.IOException; | 10 import java.io.IOException; |
12 import java.util.List; | |
13 import java.util.Map; | |
14 import org.apache.lucene.index.Term; | |
15 import org.apache.lucene.search.Query; | |
16 import org.apache.lucene.search.MatchAllDocsQuery; | |
17 import org.apache.lucene.search.TermQuery; | |
18 import org.apache.lucene.search.PrefixQuery; | |
19 import org.apache.lucene.search.WildcardQuery; | |
20 import org.apache.lucene.search.TermRangeQuery; | |
21 import org.apache.lucene.search.PhraseQuery; | |
22 import org.apache.lucene.search.NumericRangeQuery; | |
23 import org.apache.lucene.search.BooleanQuery; | |
24 import org.apache.lucene.search.BooleanClause; | |
25 import org.apache.lucene.util.BytesRef; | |
26 import goodjava.logging.Logger; | 11 import goodjava.logging.Logger; |
27 import goodjava.logging.LoggerFactory; | 12 import goodjava.logging.LoggerFactory; |
28 import goodjava.io.LimitedInputStream; | 13 import goodjava.io.LimitedInputStream; |
29 import goodjava.io.BufferedInputStream; | 14 import goodjava.io.BufferedInputStream; |
30 | 15 |
31 | 16 |
32 public class LogFile extends DataOutputStream { | 17 public class LogFile { |
33 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); | 18 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); |
34 public final File file; | 19 public final File file; |
35 private final RandomAccessFile raf; | 20 long end; |
36 private long end; | |
37 | 21 |
38 public static LogFile newLogFile(File file) throws IOException { | 22 public LogFile(File file) throws IOException { |
23 this.file = file; | |
39 RandomAccessFile raf = new RandomAccessFile(file,"rwd"); | 24 RandomAccessFile raf = new RandomAccessFile(file,"rwd"); |
40 OutputStream out = new FileOutputStream(raf.getFD()); | |
41 out = new BufferedOutputStream(out); | |
42 return new LogFile(file,raf,out); | |
43 } | |
44 | |
45 protected LogFile(File file,RandomAccessFile raf,OutputStream out) throws IOException { | |
46 super(out); | |
47 this.file = file; | |
48 this.raf = raf; | |
49 | |
50 if( raf.length() == 0 ) { | 25 if( raf.length() == 0 ) { |
51 end = 8; | 26 end = 8; |
52 raf.writeLong(end); | 27 raf.writeLong(end); |
53 } else { | 28 } else { |
54 raf.seek(0L); | 29 raf.seek(0L); |
55 end = raf.readLong(); | 30 end = raf.readLong(); |
56 raf.seek(end); | 31 raf.seek(end); |
57 } | 32 } |
33 raf.close(); | |
58 } | 34 } |
59 | 35 |
60 public String toString() { | 36 public String toString() { |
61 return "LogFile<" + file.getName() + ">"; | 37 return "LogFile<" + file.getName() + ">"; |
62 } | 38 } |
63 | 39 |
64 public long end() { | 40 public long end() { |
65 return end; | 41 return end; |
42 } | |
43 | |
44 public LogOutputStream output() throws IOException { | |
45 RandomAccessFile raf = new RandomAccessFile(file,"rwd"); | |
46 OutputStream out = new FileOutputStream(raf.getFD()); | |
47 out = new BufferedOutputStream(out); | |
48 return newLogOutputStream(raf,out); | |
49 } | |
50 | |
51 protected LogOutputStream newLogOutputStream(RandomAccessFile raf,OutputStream out) throws IOException { | |
52 return new LogOutputStream(this,raf,out); | |
66 } | 53 } |
67 | 54 |
68 public LogInputStream input() throws IOException { | 55 public LogInputStream input() throws IOException { |
69 InputStream in = new FileInputStream(file); | 56 InputStream in = new FileInputStream(file); |
70 in = new LimitedInputStream(in,end); | 57 in = new LimitedInputStream(in,end); |
74 return lis; | 61 return lis; |
75 } | 62 } |
76 | 63 |
77 protected LogInputStream newLogInputStream(InputStream in) { | 64 protected LogInputStream newLogInputStream(InputStream in) { |
78 return new LogInputStream(in); | 65 return new LogInputStream(in); |
79 } | |
80 | |
81 public void commit() throws IOException { | |
82 flush(); | |
83 end = raf.getFilePointer(); | |
84 raf.seek(0L); | |
85 raf.writeLong(end); | |
86 raf.seek(end); | |
87 } | |
88 | |
89 public void rollback() throws IOException { | |
90 flush(); | |
91 raf.seek(end); | |
92 } | 66 } |
93 | 67 |
94 static final int TYPE_NULL = 0; | 68 static final int TYPE_NULL = 0; |
95 static final int TYPE_STRING = 1; | 69 static final int TYPE_STRING = 1; |
96 static final int TYPE_INT = 2; | 70 static final int TYPE_INT = 2; |
105 static final int TYPE_QUERY_WILDCARD = 11; | 79 static final int TYPE_QUERY_WILDCARD = 11; |
106 static final int TYPE_QUERY_TERM_RANGE = 12; | 80 static final int TYPE_QUERY_TERM_RANGE = 12; |
107 static final int TYPE_QUERY_PHRASE = 13; | 81 static final int TYPE_QUERY_PHRASE = 13; |
108 static final int TYPE_QUERY_NUMERIC_RANGE = 14; | 82 static final int TYPE_QUERY_NUMERIC_RANGE = 14; |
109 static final int TYPE_QUERY_BOOLEAN = 15; | 83 static final int TYPE_QUERY_BOOLEAN = 15; |
110 | |
111 public void writeObject(Object obj) throws IOException { | |
112 if( obj==null ) { | |
113 writeByte(TYPE_NULL); | |
114 return; | |
115 } | |
116 if( obj instanceof String ) { | |
117 writeByte(TYPE_STRING); | |
118 writeUTF((String)obj); | |
119 return; | |
120 } | |
121 if( obj instanceof Integer ) { | |
122 writeByte(TYPE_INT); | |
123 writeInt((Integer)obj); | |
124 return; | |
125 } | |
126 if( obj instanceof Long ) { | |
127 writeByte(TYPE_LONG); | |
128 writeLong((Long)obj); | |
129 return; | |
130 } | |
131 if( obj instanceof Float ) { | |
132 writeByte(TYPE_FLOAT); | |
133 writeFloat((Float)obj); | |
134 return; | |
135 } | |
136 if( obj instanceof Double ) { | |
137 writeByte(TYPE_DOUBLE); | |
138 writeDouble((Double)obj); | |
139 return; | |
140 } | |
141 if( obj instanceof byte[] ) { | |
142 writeByte(TYPE_BYTES); | |
143 writeByteArray((byte[])obj); | |
144 return; | |
145 } | |
146 if( obj instanceof List ) { | |
147 writeByte(TYPE_LIST); | |
148 writeList((List)obj); | |
149 return; | |
150 } | |
151 if( obj instanceof MatchAllDocsQuery ) { | |
152 writeByte(TYPE_QUERY_MATCH_ALL_DOCS); | |
153 return; | |
154 } | |
155 if( obj instanceof TermQuery ) { | |
156 writeByte(TYPE_QUERY_TERM); | |
157 TermQuery query = (TermQuery)obj; | |
158 writeTerm( query.getTerm() ); | |
159 return; | |
160 } | |
161 if( obj instanceof PrefixQuery ) { | |
162 writeByte(TYPE_QUERY_PREFIX); | |
163 PrefixQuery query = (PrefixQuery)obj; | |
164 writeTerm( query.getPrefix() ); | |
165 return; | |
166 } | |
167 if( obj instanceof WildcardQuery ) { | |
168 writeByte(TYPE_QUERY_TERM_RANGE); | |
169 WildcardQuery query = (WildcardQuery)obj; | |
170 writeTerm( query.getTerm() ); | |
171 return; | |
172 } | |
173 if( obj instanceof TermRangeQuery ) { | |
174 writeByte(TYPE_QUERY_TERM_RANGE); | |
175 TermRangeQuery query = (TermRangeQuery)obj; | |
176 writeUTF( query.getField() ); | |
177 writeBytesRef( query.getLowerTerm() ); | |
178 writeBytesRef( query.getUpperTerm() ); | |
179 writeBoolean( query.includesLower() ); | |
180 writeBoolean( query.includesUpper() ); | |
181 return; | |
182 } | |
183 if( obj instanceof PhraseQuery ) { | |
184 writeByte(TYPE_QUERY_PHRASE); | |
185 PhraseQuery query = (PhraseQuery)obj; | |
186 Term[] terms = query.getTerms(); | |
187 int[] positions = query.getPositions(); | |
188 if( terms.length != positions.length ) | |
189 throw new RuntimeException(); | |
190 writeInt( terms.length ); | |
191 for( int i=0; i<terms.length; i++ ) { | |
192 writeTerm( terms[i] ); | |
193 writeInt( positions[i] ); | |
194 } | |
195 return; | |
196 } | |
197 if( obj instanceof NumericRangeQuery ) { | |
198 writeByte(TYPE_QUERY_NUMERIC_RANGE); | |
199 NumericRangeQuery query = (NumericRangeQuery)obj; | |
200 writeUTF( query.getField() ); | |
201 writeObject( query.getMin() ); | |
202 writeObject( query.getMax() ); | |
203 writeBoolean( query.includesMin() ); | |
204 writeBoolean( query.includesMax() ); | |
205 return; | |
206 } | |
207 if( obj instanceof BooleanQuery ) { | |
208 writeByte(TYPE_QUERY_BOOLEAN); | |
209 BooleanQuery query = (BooleanQuery)obj; | |
210 BooleanClause[] a = query.getClauses(); | |
211 writeInt(a.length); | |
212 for( BooleanClause bc : a ) { | |
213 writeQuery( bc.getQuery() ); | |
214 writeUTF( bc.getOccur().name() ); | |
215 } | |
216 return; | |
217 } | |
218 throw new IllegalArgumentException("invalid type for "+obj); | |
219 } | |
220 | |
221 public void writeByteArray(byte[] bytes) throws IOException { | |
222 writeInt(bytes.length); | |
223 write(bytes); | |
224 } | |
225 | |
226 public void writeList(List list) throws IOException { | |
227 writeInt(list.size()); | |
228 for( Object obj : list ) { | |
229 writeObject(obj); | |
230 } | |
231 } | |
232 | |
233 public void writeMap(Map map) throws IOException { | |
234 writeInt(map.size()); | |
235 for( Object obj : map.entrySet() ) { | |
236 Map.Entry entry = (Map.Entry)obj; | |
237 writeObject( entry.getKey() ); | |
238 writeObject( entry.getValue() ); | |
239 } | |
240 } | |
241 | |
242 public void writeQuery(Query query) throws IOException { | |
243 writeObject(query); | |
244 } | |
245 | |
246 public void writeBytesRef(BytesRef br) throws IOException { | |
247 writeInt(br.length); | |
248 write(br.bytes,0,br.length); | |
249 } | |
250 | |
251 public void writeTerm(Term term) throws IOException { | |
252 writeUTF(term.field()); | |
253 writeBytesRef( term.bytes() ); | |
254 } | |
255 | |
256 } | 84 } |