Mercurial Hosting > luan
annotate src/goodjava/lucene/logging/LogOutputStream.java @ 1490:9a2a2181a58f
FixedLengthInputStream
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 02 May 2020 20:42:28 -0600 |
parents | 2469aa31f31b |
children | 117ce8645b7f |
rev | line source |
---|---|
1461 | 1 package goodjava.lucene.logging; |
2 | |
1481 | 3 import java.io.OutputStream; |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
4 import java.io.DataOutputStream; |
1461 | 5 import java.io.RandomAccessFile; |
6 import java.io.IOException; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 import org.apache.lucene.index.Term; | |
10 import org.apache.lucene.search.Query; | |
11 import org.apache.lucene.search.MatchAllDocsQuery; | |
12 import org.apache.lucene.search.TermQuery; | |
13 import org.apache.lucene.search.PrefixQuery; | |
14 import org.apache.lucene.search.WildcardQuery; | |
15 import org.apache.lucene.search.TermRangeQuery; | |
16 import org.apache.lucene.search.PhraseQuery; | |
17 import org.apache.lucene.search.NumericRangeQuery; | |
18 import org.apache.lucene.search.BooleanQuery; | |
19 import org.apache.lucene.search.BooleanClause; | |
20 import org.apache.lucene.util.BytesRef; | |
1465 | 21 import goodjava.logging.Logger; |
22 import goodjava.logging.LoggerFactory; | |
1461 | 23 |
24 | |
1486 | 25 public class LogOutputStream extends DataOutputStream { |
26 private static final Logger logger = LoggerFactory.getLogger(LogOutputStream.class); | |
27 public final LogFile logFile; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
28 private final RandomAccessFile raf; |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
29 |
1486 | 30 protected LogOutputStream(LogFile logFile,RandomAccessFile raf,OutputStream out) throws IOException { |
31 super(out); | |
32 this.logFile = logFile; | |
33 this.raf = raf; | |
34 raf.seek(logFile.end); | |
1461 | 35 } |
36 | |
37 public void commit() throws IOException { | |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
38 flush(); |
1486 | 39 long end = raf.getFilePointer(); |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
40 raf.seek(0L); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
41 raf.writeLong(end); |
1486 | 42 logFile.end = end; |
1481 | 43 raf.seek(end); |
1461 | 44 } |
45 | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
46 public void rollback() throws IOException { |
1484
1fa6e8ec2d53
lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1481
diff
changeset
|
47 flush(); |
1486 | 48 raf.seek(logFile.end); |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
49 } |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
50 |
1461 | 51 public void writeObject(Object obj) throws IOException { |
52 if( obj==null ) { | |
1486 | 53 writeByte(LogFile.TYPE_NULL); |
1461 | 54 return; |
55 } | |
56 if( obj instanceof String ) { | |
1486 | 57 writeByte(LogFile.TYPE_STRING); |
1461 | 58 writeUTF((String)obj); |
59 return; | |
60 } | |
61 if( obj instanceof Integer ) { | |
1486 | 62 writeByte(LogFile.TYPE_INT); |
1461 | 63 writeInt((Integer)obj); |
64 return; | |
65 } | |
66 if( obj instanceof Long ) { | |
1486 | 67 writeByte(LogFile.TYPE_LONG); |
1461 | 68 writeLong((Long)obj); |
69 return; | |
70 } | |
71 if( obj instanceof Float ) { | |
1486 | 72 writeByte(LogFile.TYPE_FLOAT); |
1461 | 73 writeFloat((Float)obj); |
74 return; | |
75 } | |
76 if( obj instanceof Double ) { | |
1486 | 77 writeByte(LogFile.TYPE_DOUBLE); |
1461 | 78 writeDouble((Double)obj); |
79 return; | |
80 } | |
81 if( obj instanceof byte[] ) { | |
1486 | 82 writeByte(LogFile.TYPE_BYTES); |
1461 | 83 writeByteArray((byte[])obj); |
84 return; | |
85 } | |
86 if( obj instanceof List ) { | |
1486 | 87 writeByte(LogFile.TYPE_LIST); |
1461 | 88 writeList((List)obj); |
89 return; | |
90 } | |
91 if( obj instanceof MatchAllDocsQuery ) { | |
1486 | 92 writeByte(LogFile.TYPE_QUERY_MATCH_ALL_DOCS); |
1461 | 93 return; |
94 } | |
95 if( obj instanceof TermQuery ) { | |
1486 | 96 writeByte(LogFile.TYPE_QUERY_TERM); |
1461 | 97 TermQuery query = (TermQuery)obj; |
98 writeTerm( query.getTerm() ); | |
99 return; | |
100 } | |
101 if( obj instanceof PrefixQuery ) { | |
1486 | 102 writeByte(LogFile.TYPE_QUERY_PREFIX); |
1461 | 103 PrefixQuery query = (PrefixQuery)obj; |
104 writeTerm( query.getPrefix() ); | |
105 return; | |
106 } | |
107 if( obj instanceof WildcardQuery ) { | |
1486 | 108 writeByte(LogFile.TYPE_QUERY_TERM_RANGE); |
1461 | 109 WildcardQuery query = (WildcardQuery)obj; |
110 writeTerm( query.getTerm() ); | |
111 return; | |
112 } | |
113 if( obj instanceof TermRangeQuery ) { | |
1486 | 114 writeByte(LogFile.TYPE_QUERY_TERM_RANGE); |
1461 | 115 TermRangeQuery query = (TermRangeQuery)obj; |
116 writeUTF( query.getField() ); | |
117 writeBytesRef( query.getLowerTerm() ); | |
118 writeBytesRef( query.getUpperTerm() ); | |
119 writeBoolean( query.includesLower() ); | |
120 writeBoolean( query.includesUpper() ); | |
121 return; | |
122 } | |
123 if( obj instanceof PhraseQuery ) { | |
1486 | 124 writeByte(LogFile.TYPE_QUERY_PHRASE); |
1461 | 125 PhraseQuery query = (PhraseQuery)obj; |
126 Term[] terms = query.getTerms(); | |
127 int[] positions = query.getPositions(); | |
128 if( terms.length != positions.length ) | |
129 throw new RuntimeException(); | |
130 writeInt( terms.length ); | |
131 for( int i=0; i<terms.length; i++ ) { | |
132 writeTerm( terms[i] ); | |
133 writeInt( positions[i] ); | |
134 } | |
135 return; | |
136 } | |
137 if( obj instanceof NumericRangeQuery ) { | |
1486 | 138 writeByte(LogFile.TYPE_QUERY_NUMERIC_RANGE); |
1461 | 139 NumericRangeQuery query = (NumericRangeQuery)obj; |
140 writeUTF( query.getField() ); | |
141 writeObject( query.getMin() ); | |
142 writeObject( query.getMax() ); | |
143 writeBoolean( query.includesMin() ); | |
144 writeBoolean( query.includesMax() ); | |
145 return; | |
146 } | |
147 if( obj instanceof BooleanQuery ) { | |
1486 | 148 writeByte(LogFile.TYPE_QUERY_BOOLEAN); |
1461 | 149 BooleanQuery query = (BooleanQuery)obj; |
150 BooleanClause[] a = query.getClauses(); | |
151 writeInt(a.length); | |
152 for( BooleanClause bc : a ) { | |
153 writeQuery( bc.getQuery() ); | |
154 writeUTF( bc.getOccur().name() ); | |
155 } | |
156 return; | |
157 } | |
158 throw new IllegalArgumentException("invalid type for "+obj); | |
159 } | |
160 | |
161 public void writeByteArray(byte[] bytes) throws IOException { | |
162 writeInt(bytes.length); | |
163 write(bytes); | |
164 } | |
165 | |
166 public void writeList(List list) throws IOException { | |
167 writeInt(list.size()); | |
168 for( Object obj : list ) { | |
169 writeObject(obj); | |
170 } | |
171 } | |
172 | |
173 public void writeMap(Map map) throws IOException { | |
174 writeInt(map.size()); | |
175 for( Object obj : map.entrySet() ) { | |
176 Map.Entry entry = (Map.Entry)obj; | |
177 writeObject( entry.getKey() ); | |
178 writeObject( entry.getValue() ); | |
179 } | |
180 } | |
181 | |
182 public void writeQuery(Query query) throws IOException { | |
183 writeObject(query); | |
184 } | |
185 | |
186 public void writeBytesRef(BytesRef br) throws IOException { | |
187 writeInt(br.length); | |
188 write(br.bytes,0,br.length); | |
189 } | |
190 | |
191 public void writeTerm(Term term) throws IOException { | |
192 writeUTF(term.field()); | |
193 writeBytesRef( term.bytes() ); | |
194 } | |
195 | |
196 } |