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