comparison src/goodjava/lucene/logging/LogOutputStream.java @ 1486:2469aa31f31b

LogOutputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2020 16:09:35 -0600
parents src/goodjava/lucene/logging/LogFile.java@1fa6e8ec2d53
children 117ce8645b7f
comparison
equal deleted inserted replaced
1485:6a24c8b33d6b 1486:2469aa31f31b
1 package goodjava.lucene.logging;
2
3 import java.io.OutputStream;
4 import java.io.DataOutputStream;
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;
21 import goodjava.logging.Logger;
22 import goodjava.logging.LoggerFactory;
23
24
25 public class LogOutputStream extends DataOutputStream {
26 private static final Logger logger = LoggerFactory.getLogger(LogOutputStream.class);
27 public final LogFile logFile;
28 private final RandomAccessFile raf;
29
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);
35 }
36
37 public void commit() throws IOException {
38 flush();
39 long end = raf.getFilePointer();
40 raf.seek(0L);
41 raf.writeLong(end);
42 logFile.end = end;
43 raf.seek(end);
44 }
45
46 public void rollback() throws IOException {
47 flush();
48 raf.seek(logFile.end);
49 }
50
51 public void writeObject(Object obj) throws IOException {
52 if( obj==null ) {
53 writeByte(LogFile.TYPE_NULL);
54 return;
55 }
56 if( obj instanceof String ) {
57 writeByte(LogFile.TYPE_STRING);
58 writeUTF((String)obj);
59 return;
60 }
61 if( obj instanceof Integer ) {
62 writeByte(LogFile.TYPE_INT);
63 writeInt((Integer)obj);
64 return;
65 }
66 if( obj instanceof Long ) {
67 writeByte(LogFile.TYPE_LONG);
68 writeLong((Long)obj);
69 return;
70 }
71 if( obj instanceof Float ) {
72 writeByte(LogFile.TYPE_FLOAT);
73 writeFloat((Float)obj);
74 return;
75 }
76 if( obj instanceof Double ) {
77 writeByte(LogFile.TYPE_DOUBLE);
78 writeDouble((Double)obj);
79 return;
80 }
81 if( obj instanceof byte[] ) {
82 writeByte(LogFile.TYPE_BYTES);
83 writeByteArray((byte[])obj);
84 return;
85 }
86 if( obj instanceof List ) {
87 writeByte(LogFile.TYPE_LIST);
88 writeList((List)obj);
89 return;
90 }
91 if( obj instanceof MatchAllDocsQuery ) {
92 writeByte(LogFile.TYPE_QUERY_MATCH_ALL_DOCS);
93 return;
94 }
95 if( obj instanceof TermQuery ) {
96 writeByte(LogFile.TYPE_QUERY_TERM);
97 TermQuery query = (TermQuery)obj;
98 writeTerm( query.getTerm() );
99 return;
100 }
101 if( obj instanceof PrefixQuery ) {
102 writeByte(LogFile.TYPE_QUERY_PREFIX);
103 PrefixQuery query = (PrefixQuery)obj;
104 writeTerm( query.getPrefix() );
105 return;
106 }
107 if( obj instanceof WildcardQuery ) {
108 writeByte(LogFile.TYPE_QUERY_TERM_RANGE);
109 WildcardQuery query = (WildcardQuery)obj;
110 writeTerm( query.getTerm() );
111 return;
112 }
113 if( obj instanceof TermRangeQuery ) {
114 writeByte(LogFile.TYPE_QUERY_TERM_RANGE);
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 ) {
124 writeByte(LogFile.TYPE_QUERY_PHRASE);
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 ) {
138 writeByte(LogFile.TYPE_QUERY_NUMERIC_RANGE);
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 ) {
148 writeByte(LogFile.TYPE_QUERY_BOOLEAN);
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 }