Mercurial Hosting > luan
annotate src/goodjava/lucene/logging/LogFile.java @ 1485:6a24c8b33d6b
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2020 11:23:29 -0600 |
parents | 1fa6e8ec2d53 |
children | 2469aa31f31b |
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 | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
32 public class LogFile extends DataOutputStream { |
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; |
1461 | 36 private long end; |
37 | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
38 public static LogFile newLogFile(File file) throws IOException { |
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
39 RandomAccessFile raf = new RandomAccessFile(file,"rwd"); |
1481 | 40 OutputStream out = new FileOutputStream(raf.getFD()); |
41 out = new BufferedOutputStream(out); | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
42 return new LogFile(file,raf,out); |
1461 | 43 } |
44 | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
45 protected LogFile(File file,RandomAccessFile raf,OutputStream out) throws IOException { |
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
46 super(out); |
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
47 this.file = file; |
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
48 this.raf = raf; |
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
49 |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
50 if( raf.length() == 0 ) { |
1461 | 51 end = 8; |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
52 raf.writeLong(end); |
1461 | 53 } else { |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
54 raf.seek(0L); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
55 end = raf.readLong(); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
56 raf.seek(end); |
1461 | 57 } |
58 } | |
59 | |
1465 | 60 public String toString() { |
61 return "LogFile<" + file.getName() + ">"; | |
62 } | |
63 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
64 public long end() { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
65 return end; |
1461 | 66 } |
67 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
68 public LogInputStream input() throws IOException { |
1480 | 69 InputStream in = new FileInputStream(file); |
70 in = new LimitedInputStream(in,end); | |
71 in = new BufferedInputStream(in); | |
72 LogInputStream lis = newLogInputStream(in); | |
73 lis.readLong(); // skip end | |
74 return lis; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
75 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
76 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
77 protected LogInputStream newLogInputStream(InputStream in) { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
78 return new LogInputStream(in); |
1461 | 79 } |
80 | |
81 public void commit() throws IOException { | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
82 flush(); |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
83 end = raf.getFilePointer(); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
84 raf.seek(0L); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
85 raf.writeLong(end); |
1481 | 86 raf.seek(end); |
1461 | 87 } |
88 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
89 public void rollback() throws IOException { |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
90 flush(); |
1481 | 91 raf.seek(end); |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
92 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
93 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
94 static final int TYPE_NULL = 0; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
95 static final int TYPE_STRING = 1; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
96 static final int TYPE_INT = 2; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
97 static final int TYPE_LONG = 3; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
98 static final int TYPE_FLOAT = 4; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
99 static final int TYPE_DOUBLE = 5; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
100 static final int TYPE_BYTES = 6; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
101 static final int TYPE_LIST = 7; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
102 static final int TYPE_QUERY_MATCH_ALL_DOCS = 8; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
103 static final int TYPE_QUERY_TERM = 9; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
104 static final int TYPE_QUERY_PREFIX = 10; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
105 static final int TYPE_QUERY_WILDCARD = 11; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
106 static final int TYPE_QUERY_TERM_RANGE = 12; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
107 static final int TYPE_QUERY_PHRASE = 13; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
108 static final int TYPE_QUERY_NUMERIC_RANGE = 14; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
109 static final int TYPE_QUERY_BOOLEAN = 15; |
1461 | 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 } |