annotate src/goodjava/lucene/logging/LogInputStream.java @ 1492:aaac1d29edea

better io
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 22:25:56 -0600
parents 7d145095cc0b
children 117ce8645b7f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.logging;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.InputStream;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.DataInputStream;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.List;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.ArrayList;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.Map;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.LinkedHashMap;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import org.apache.lucene.index.Term;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import org.apache.lucene.search.Query;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import org.apache.lucene.search.MatchAllDocsQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import org.apache.lucene.search.TermQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import org.apache.lucene.search.PrefixQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import org.apache.lucene.search.WildcardQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 import org.apache.lucene.search.TermRangeQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 import org.apache.lucene.search.PhraseQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 import org.apache.lucene.search.NumericRangeQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 import org.apache.lucene.search.BooleanQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import org.apache.lucene.search.BooleanClause;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import org.apache.lucene.util.BytesRef;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import goodjava.logging.Logger;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import goodjava.logging.LoggerFactory;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 public class LogInputStream extends DataInputStream {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 private static final Logger logger = LoggerFactory.getLogger(LogInputStream.class);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 public LogInputStream(InputStream in) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 super(in);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 public Object readObject() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 int type = readByte();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 return readObject(type);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 protected Object readObject(int type) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 switch(type) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 case LogFile.TYPE_NULL:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 return null;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 case LogFile.TYPE_STRING:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 return readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 case LogFile.TYPE_INT:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 return readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 case LogFile.TYPE_LONG:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return readLong();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 case LogFile.TYPE_FLOAT:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 return readFloat();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 case LogFile.TYPE_DOUBLE:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return readDouble();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 case LogFile.TYPE_BYTES:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 return readByteArray();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 case LogFile.TYPE_LIST:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return readList();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 case LogFile.TYPE_QUERY_MATCH_ALL_DOCS:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 return new MatchAllDocsQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 case LogFile.TYPE_QUERY_TERM:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 return new TermQuery( readTerm() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 case LogFile.TYPE_QUERY_PREFIX:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return new PrefixQuery( readTerm() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 case LogFile.TYPE_QUERY_WILDCARD:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 return new WildcardQuery( readTerm() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 case LogFile.TYPE_QUERY_TERM_RANGE:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 String field = readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 BytesRef lowerTerm = readBytesRef();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 BytesRef upperTerm = readBytesRef();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 boolean includeLower = readBoolean();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 boolean includeUpper = readBoolean();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 return new TermRangeQuery(field,lowerTerm,upperTerm,includeLower,includeUpper);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 case LogFile.TYPE_QUERY_PHRASE:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 PhraseQuery query = new PhraseQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 int n = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 for( int i=0; i<n; i++ ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 Term term = readTerm();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 int position = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 query.add(term,position);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 return query;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 case LogFile.TYPE_QUERY_NUMERIC_RANGE:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 String field = readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 Number min = (Number)readObject();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 Number max = (Number)readObject();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 boolean minInclusive = readBoolean();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 boolean maxInclusive = readBoolean();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 Number n = min!=null ? min : max;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 if( n instanceof Integer )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 return NumericRangeQuery.newIntRange(field,(Integer)min,(Integer)max,minInclusive,maxInclusive);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 if( n instanceof Long )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 return NumericRangeQuery.newLongRange(field,(Long)min,(Long)max,minInclusive,maxInclusive);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 if( n instanceof Float )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 return NumericRangeQuery.newFloatRange(field,(Float)min,(Float)max,minInclusive,maxInclusive);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 if( n instanceof Double )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 return NumericRangeQuery.newDoubleRange(field,(Double)min,(Double)max,minInclusive,maxInclusive);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 throw new RuntimeException("bad numeric type for "+n);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 case LogFile.TYPE_QUERY_BOOLEAN:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 BooleanQuery query = new BooleanQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 int n = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 for( int i=0; i<n; i++ ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 Query subquery = readQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 BooleanClause.Occur occur = BooleanClause.Occur.valueOf( readUTF() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 query.add(subquery,occur);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return query;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 default:
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 throw new RuntimeException("invalid type "+type);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 public byte[] readByteArray() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 int len = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 byte[] bytes = new byte[len];
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 readFully(bytes);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 return bytes;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 public List readList() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 final int size = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 List list = new ArrayList(size);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 for( int i=0; i<size; i++ ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 list.add( readObject() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 return list;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 public Map readMap() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 final int size = readInt();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 Map map = new LinkedHashMap();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 for( int i=0; i<size; i++ ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 Object key = readObject();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 Object value = readObject();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 map.put(key,value);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 return map;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 public Query readQuery() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 return (Query)readObject();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 public BytesRef readBytesRef() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 return new BytesRef( readByteArray() );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 public Term readTerm() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 String key = readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 BytesRef value = readBytesRef();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 return new Term(key,value);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 }