Mercurial Hosting > luan
annotate src/goodjava/lucene/logging/LogFile.java @ 1483:97740900c820
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 25 Apr 2020 10:31:49 -0600 |
parents | 3cff066f3bbc |
children | 1fa6e8ec2d53 |
rev | line source |
---|---|
1461 | 1 package goodjava.lucene.logging; |
2 | |
3 import java.io.File; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
4 import java.io.InputStream; |
1481 | 5 import java.io.OutputStream; |
6 import java.io.BufferedOutputStream; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
7 import java.io.DataOutputStream; |
1461 | 8 import java.io.RandomAccessFile; |
1480 | 9 import java.io.FileInputStream; |
1481 | 10 import java.io.FileOutputStream; |
1461 | 11 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; | |
1465 | 26 import goodjava.logging.Logger; |
27 import goodjava.logging.LoggerFactory; | |
1480 | 28 import goodjava.io.LimitedInputStream; |
29 import goodjava.io.BufferedInputStream; | |
1461 | 30 |
31 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
32 public class LogFile { |
1465 | 33 private static final Logger logger = LoggerFactory.getLogger(LogFile.class); |
34 public final File file; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
35 private final RandomAccessFile raf; |
1481 | 36 private final DataOutputStream out; |
1461 | 37 private long end; |
38 | |
39 public LogFile(File file,String mode) throws IOException { | |
1465 | 40 this.file = file; |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
41 this.raf = new RandomAccessFile(file,mode); |
1481 | 42 OutputStream out = new FileOutputStream(raf.getFD()); |
43 out = new BufferedOutputStream(out); | |
44 this.out = new DataOutputStream(out); | |
1461 | 45 init(); |
46 } | |
47 | |
48 private void init() throws IOException { | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
49 if( raf.length() == 0 ) { |
1461 | 50 end = 8; |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
51 raf.writeLong(end); |
1461 | 52 } else { |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
53 raf.seek(0L); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
54 end = raf.readLong(); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
55 raf.seek(end); |
1461 | 56 } |
57 } | |
58 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
59 private LogFile(LogFile lf) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
60 this.file = lf.file; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
61 this.raf = new RandomAccessFile(file,"r"); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
62 this.out = null; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
63 this.end = lf.end; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
64 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
65 |
1481 | 66 public void close() throws IOException { |
67 raf.close(); | |
68 } | |
69 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
70 public LogFile snapshot() throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
71 return new LogFile(this); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
72 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
73 |
1465 | 74 public String toString() { |
75 return "LogFile<" + file.getName() + ">"; | |
76 } | |
77 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
78 public long end() { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
79 return end; |
1461 | 80 } |
81 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
82 public LogInputStream input() throws IOException { |
1480 | 83 InputStream in = new FileInputStream(file); |
84 in = new LimitedInputStream(in,end); | |
85 in = new BufferedInputStream(in); | |
86 LogInputStream lis = newLogInputStream(in); | |
87 lis.readLong(); // skip end | |
88 return lis; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
89 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
90 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
91 protected LogInputStream newLogInputStream(InputStream in) { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
92 return new LogInputStream(in); |
1461 | 93 } |
94 | |
95 public void commit() throws IOException { | |
1481 | 96 out.flush(); |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
97 end = raf.getFilePointer(); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
98 raf.seek(0L); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
99 raf.writeLong(end); |
1481 | 100 raf.seek(end); |
1461 | 101 } |
102 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
103 public void rollback() throws IOException { |
1481 | 104 out.flush(); |
105 raf.seek(end); | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
106 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
107 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
108 static final int TYPE_NULL = 0; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
109 static final int TYPE_STRING = 1; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
110 static final int TYPE_INT = 2; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
111 static final int TYPE_LONG = 3; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
112 static final int TYPE_FLOAT = 4; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
113 static final int TYPE_DOUBLE = 5; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
114 static final int TYPE_BYTES = 6; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
115 static final int TYPE_LIST = 7; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
116 static final int TYPE_QUERY_MATCH_ALL_DOCS = 8; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
117 static final int TYPE_QUERY_TERM = 9; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
118 static final int TYPE_QUERY_PREFIX = 10; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
119 static final int TYPE_QUERY_WILDCARD = 11; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
120 static final int TYPE_QUERY_TERM_RANGE = 12; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
121 static final int TYPE_QUERY_PHRASE = 13; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
122 static final int TYPE_QUERY_NUMERIC_RANGE = 14; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
123 static final int TYPE_QUERY_BOOLEAN = 15; |
1461 | 124 |
125 public void writeObject(Object obj) throws IOException { | |
126 if( obj==null ) { | |
127 writeByte(TYPE_NULL); | |
128 return; | |
129 } | |
130 if( obj instanceof String ) { | |
131 writeByte(TYPE_STRING); | |
132 writeUTF((String)obj); | |
133 return; | |
134 } | |
135 if( obj instanceof Integer ) { | |
136 writeByte(TYPE_INT); | |
137 writeInt((Integer)obj); | |
138 return; | |
139 } | |
140 if( obj instanceof Long ) { | |
141 writeByte(TYPE_LONG); | |
142 writeLong((Long)obj); | |
143 return; | |
144 } | |
145 if( obj instanceof Float ) { | |
146 writeByte(TYPE_FLOAT); | |
147 writeFloat((Float)obj); | |
148 return; | |
149 } | |
150 if( obj instanceof Double ) { | |
151 writeByte(TYPE_DOUBLE); | |
152 writeDouble((Double)obj); | |
153 return; | |
154 } | |
155 if( obj instanceof byte[] ) { | |
156 writeByte(TYPE_BYTES); | |
157 writeByteArray((byte[])obj); | |
158 return; | |
159 } | |
160 if( obj instanceof List ) { | |
161 writeByte(TYPE_LIST); | |
162 writeList((List)obj); | |
163 return; | |
164 } | |
165 if( obj instanceof MatchAllDocsQuery ) { | |
166 writeByte(TYPE_QUERY_MATCH_ALL_DOCS); | |
167 return; | |
168 } | |
169 if( obj instanceof TermQuery ) { | |
170 writeByte(TYPE_QUERY_TERM); | |
171 TermQuery query = (TermQuery)obj; | |
172 writeTerm( query.getTerm() ); | |
173 return; | |
174 } | |
175 if( obj instanceof PrefixQuery ) { | |
176 writeByte(TYPE_QUERY_PREFIX); | |
177 PrefixQuery query = (PrefixQuery)obj; | |
178 writeTerm( query.getPrefix() ); | |
179 return; | |
180 } | |
181 if( obj instanceof WildcardQuery ) { | |
182 writeByte(TYPE_QUERY_TERM_RANGE); | |
183 WildcardQuery query = (WildcardQuery)obj; | |
184 writeTerm( query.getTerm() ); | |
185 return; | |
186 } | |
187 if( obj instanceof TermRangeQuery ) { | |
188 writeByte(TYPE_QUERY_TERM_RANGE); | |
189 TermRangeQuery query = (TermRangeQuery)obj; | |
190 writeUTF( query.getField() ); | |
191 writeBytesRef( query.getLowerTerm() ); | |
192 writeBytesRef( query.getUpperTerm() ); | |
193 writeBoolean( query.includesLower() ); | |
194 writeBoolean( query.includesUpper() ); | |
195 return; | |
196 } | |
197 if( obj instanceof PhraseQuery ) { | |
198 writeByte(TYPE_QUERY_PHRASE); | |
199 PhraseQuery query = (PhraseQuery)obj; | |
200 Term[] terms = query.getTerms(); | |
201 int[] positions = query.getPositions(); | |
202 if( terms.length != positions.length ) | |
203 throw new RuntimeException(); | |
204 writeInt( terms.length ); | |
205 for( int i=0; i<terms.length; i++ ) { | |
206 writeTerm( terms[i] ); | |
207 writeInt( positions[i] ); | |
208 } | |
209 return; | |
210 } | |
211 if( obj instanceof NumericRangeQuery ) { | |
212 writeByte(TYPE_QUERY_NUMERIC_RANGE); | |
213 NumericRangeQuery query = (NumericRangeQuery)obj; | |
214 writeUTF( query.getField() ); | |
215 writeObject( query.getMin() ); | |
216 writeObject( query.getMax() ); | |
217 writeBoolean( query.includesMin() ); | |
218 writeBoolean( query.includesMax() ); | |
219 return; | |
220 } | |
221 if( obj instanceof BooleanQuery ) { | |
222 writeByte(TYPE_QUERY_BOOLEAN); | |
223 BooleanQuery query = (BooleanQuery)obj; | |
224 BooleanClause[] a = query.getClauses(); | |
225 writeInt(a.length); | |
226 for( BooleanClause bc : a ) { | |
227 writeQuery( bc.getQuery() ); | |
228 writeUTF( bc.getOccur().name() ); | |
229 } | |
230 return; | |
231 } | |
232 throw new IllegalArgumentException("invalid type for "+obj); | |
233 } | |
234 | |
235 public void writeByteArray(byte[] bytes) throws IOException { | |
236 writeInt(bytes.length); | |
237 write(bytes); | |
238 } | |
239 | |
240 public void writeList(List list) throws IOException { | |
241 writeInt(list.size()); | |
242 for( Object obj : list ) { | |
243 writeObject(obj); | |
244 } | |
245 } | |
246 | |
247 public void writeMap(Map map) throws IOException { | |
248 writeInt(map.size()); | |
249 for( Object obj : map.entrySet() ) { | |
250 Map.Entry entry = (Map.Entry)obj; | |
251 writeObject( entry.getKey() ); | |
252 writeObject( entry.getValue() ); | |
253 } | |
254 } | |
255 | |
256 public void writeQuery(Query query) throws IOException { | |
257 writeObject(query); | |
258 } | |
259 | |
260 public void writeBytesRef(BytesRef br) throws IOException { | |
261 writeInt(br.length); | |
262 write(br.bytes,0,br.length); | |
263 } | |
264 | |
265 public void writeTerm(Term term) throws IOException { | |
266 writeUTF(term.field()); | |
267 writeBytesRef( term.bytes() ); | |
268 } | |
269 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
270 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
271 public void writeByte(int v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
272 out.writeByte(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
273 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
274 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
275 public void writeInt(int v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
276 out.writeInt(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
277 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
278 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
279 public void writeLong(long v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
280 out.writeLong(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
281 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
282 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
283 public void writeFloat(float v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
284 out.writeFloat(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
285 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
286 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
287 public void writeDouble(double v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
288 out.writeDouble(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
289 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
290 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
291 public void writeBoolean(boolean v) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
292 out.writeBoolean(v); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
293 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
294 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
295 public void writeUTF(String s) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
296 out.writeUTF(s); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
297 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
298 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
299 public void write(byte[] b) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
300 out.write(b); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
301 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
302 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
303 public void write(byte[] b, int off, int len) throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
304 out.write(b,off,len); |
1461 | 305 } |
306 | |
307 } |