Mercurial Hosting > luan
annotate src/goodjava/lucene/logging/LogOutputStream.java @ 1579:dd881eb03d87
yourkit link
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 10 Feb 2021 22:49:47 -0700 |
parents | 117ce8645b7f |
children |
rev | line source |
---|---|
1461 | 1 package goodjava.lucene.logging; |
2 | |
1481 | 3 import java.io.OutputStream; |
1557
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
4 import goodjava.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 ) { | |
1557
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
57 String s = (String)obj; |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
58 if( s.length() <= 65535 ) { |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
59 writeByte(LogFile.TYPE_STRING); |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
60 writeUTF(s); |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
61 } else { |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
62 writeByte(LogFile.TYPE_LONG_STRING); |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
63 writeString(s); |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1486
diff
changeset
|
64 } |
1461 | 65 return; |
66 } | |
67 if( obj instanceof Integer ) { | |
1486 | 68 writeByte(LogFile.TYPE_INT); |
1461 | 69 writeInt((Integer)obj); |
70 return; | |
71 } | |
72 if( obj instanceof Long ) { | |
1486 | 73 writeByte(LogFile.TYPE_LONG); |
1461 | 74 writeLong((Long)obj); |
75 return; | |
76 } | |
77 if( obj instanceof Float ) { | |
1486 | 78 writeByte(LogFile.TYPE_FLOAT); |
1461 | 79 writeFloat((Float)obj); |
80 return; | |
81 } | |
82 if( obj instanceof Double ) { | |
1486 | 83 writeByte(LogFile.TYPE_DOUBLE); |
1461 | 84 writeDouble((Double)obj); |
85 return; | |
86 } | |
87 if( obj instanceof byte[] ) { | |
1486 | 88 writeByte(LogFile.TYPE_BYTES); |
1461 | 89 writeByteArray((byte[])obj); |
90 return; | |
91 } | |
92 if( obj instanceof List ) { | |
1486 | 93 writeByte(LogFile.TYPE_LIST); |
1461 | 94 writeList((List)obj); |
95 return; | |
96 } | |
97 if( obj instanceof MatchAllDocsQuery ) { | |
1486 | 98 writeByte(LogFile.TYPE_QUERY_MATCH_ALL_DOCS); |
1461 | 99 return; |
100 } | |
101 if( obj instanceof TermQuery ) { | |
1486 | 102 writeByte(LogFile.TYPE_QUERY_TERM); |
1461 | 103 TermQuery query = (TermQuery)obj; |
104 writeTerm( query.getTerm() ); | |
105 return; | |
106 } | |
107 if( obj instanceof PrefixQuery ) { | |
1486 | 108 writeByte(LogFile.TYPE_QUERY_PREFIX); |
1461 | 109 PrefixQuery query = (PrefixQuery)obj; |
110 writeTerm( query.getPrefix() ); | |
111 return; | |
112 } | |
113 if( obj instanceof WildcardQuery ) { | |
1486 | 114 writeByte(LogFile.TYPE_QUERY_TERM_RANGE); |
1461 | 115 WildcardQuery query = (WildcardQuery)obj; |
116 writeTerm( query.getTerm() ); | |
117 return; | |
118 } | |
119 if( obj instanceof TermRangeQuery ) { | |
1486 | 120 writeByte(LogFile.TYPE_QUERY_TERM_RANGE); |
1461 | 121 TermRangeQuery query = (TermRangeQuery)obj; |
122 writeUTF( query.getField() ); | |
123 writeBytesRef( query.getLowerTerm() ); | |
124 writeBytesRef( query.getUpperTerm() ); | |
125 writeBoolean( query.includesLower() ); | |
126 writeBoolean( query.includesUpper() ); | |
127 return; | |
128 } | |
129 if( obj instanceof PhraseQuery ) { | |
1486 | 130 writeByte(LogFile.TYPE_QUERY_PHRASE); |
1461 | 131 PhraseQuery query = (PhraseQuery)obj; |
132 Term[] terms = query.getTerms(); | |
133 int[] positions = query.getPositions(); | |
134 if( terms.length != positions.length ) | |
135 throw new RuntimeException(); | |
136 writeInt( terms.length ); | |
137 for( int i=0; i<terms.length; i++ ) { | |
138 writeTerm( terms[i] ); | |
139 writeInt( positions[i] ); | |
140 } | |
141 return; | |
142 } | |
143 if( obj instanceof NumericRangeQuery ) { | |
1486 | 144 writeByte(LogFile.TYPE_QUERY_NUMERIC_RANGE); |
1461 | 145 NumericRangeQuery query = (NumericRangeQuery)obj; |
146 writeUTF( query.getField() ); | |
147 writeObject( query.getMin() ); | |
148 writeObject( query.getMax() ); | |
149 writeBoolean( query.includesMin() ); | |
150 writeBoolean( query.includesMax() ); | |
151 return; | |
152 } | |
153 if( obj instanceof BooleanQuery ) { | |
1486 | 154 writeByte(LogFile.TYPE_QUERY_BOOLEAN); |
1461 | 155 BooleanQuery query = (BooleanQuery)obj; |
156 BooleanClause[] a = query.getClauses(); | |
157 writeInt(a.length); | |
158 for( BooleanClause bc : a ) { | |
159 writeQuery( bc.getQuery() ); | |
160 writeUTF( bc.getOccur().name() ); | |
161 } | |
162 return; | |
163 } | |
164 throw new IllegalArgumentException("invalid type for "+obj); | |
165 } | |
166 | |
167 public void writeByteArray(byte[] bytes) throws IOException { | |
168 writeInt(bytes.length); | |
169 write(bytes); | |
170 } | |
171 | |
172 public void writeList(List list) throws IOException { | |
173 writeInt(list.size()); | |
174 for( Object obj : list ) { | |
175 writeObject(obj); | |
176 } | |
177 } | |
178 | |
179 public void writeMap(Map map) throws IOException { | |
180 writeInt(map.size()); | |
181 for( Object obj : map.entrySet() ) { | |
182 Map.Entry entry = (Map.Entry)obj; | |
183 writeObject( entry.getKey() ); | |
184 writeObject( entry.getValue() ); | |
185 } | |
186 } | |
187 | |
188 public void writeQuery(Query query) throws IOException { | |
189 writeObject(query); | |
190 } | |
191 | |
192 public void writeBytesRef(BytesRef br) throws IOException { | |
193 writeInt(br.length); | |
194 write(br.bytes,0,br.length); | |
195 } | |
196 | |
197 public void writeTerm(Term term) throws IOException { | |
198 writeUTF(term.field()); | |
199 writeBytesRef( term.bytes() ); | |
200 } | |
201 | |
202 } |