Mercurial Hosting > luan
annotate src/goodjava/lucene/logging/LogInputStream.java @ 1810:3c43b07e12b7
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 12 May 2024 22:00:42 -0600 |
parents | 117ce8645b7f |
children |
rev | line source |
---|---|
1476 | 1 package goodjava.lucene.logging; |
2 | |
3 import java.io.InputStream; | |
1557
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1476
diff
changeset
|
4 import goodjava.io.DataInputStream; |
1476 | 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(); | |
1557
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1476
diff
changeset
|
44 case LogFile.TYPE_LONG_STRING: |
117ce8645b7f
lucene logging - add long string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1476
diff
changeset
|
45 return readString(); |
1476 | 46 case LogFile.TYPE_INT: |
47 return readInt(); | |
48 case LogFile.TYPE_LONG: | |
49 return readLong(); | |
50 case LogFile.TYPE_FLOAT: | |
51 return readFloat(); | |
52 case LogFile.TYPE_DOUBLE: | |
53 return readDouble(); | |
54 case LogFile.TYPE_BYTES: | |
55 return readByteArray(); | |
56 case LogFile.TYPE_LIST: | |
57 return readList(); | |
58 case LogFile.TYPE_QUERY_MATCH_ALL_DOCS: | |
59 return new MatchAllDocsQuery(); | |
60 case LogFile.TYPE_QUERY_TERM: | |
61 return new TermQuery( readTerm() ); | |
62 case LogFile.TYPE_QUERY_PREFIX: | |
63 return new PrefixQuery( readTerm() ); | |
64 case LogFile.TYPE_QUERY_WILDCARD: | |
65 return new WildcardQuery( readTerm() ); | |
66 case LogFile.TYPE_QUERY_TERM_RANGE: | |
67 { | |
68 String field = readUTF(); | |
69 BytesRef lowerTerm = readBytesRef(); | |
70 BytesRef upperTerm = readBytesRef(); | |
71 boolean includeLower = readBoolean(); | |
72 boolean includeUpper = readBoolean(); | |
73 return new TermRangeQuery(field,lowerTerm,upperTerm,includeLower,includeUpper); | |
74 } | |
75 case LogFile.TYPE_QUERY_PHRASE: | |
76 { | |
77 PhraseQuery query = new PhraseQuery(); | |
78 int n = readInt(); | |
79 for( int i=0; i<n; i++ ) { | |
80 Term term = readTerm(); | |
81 int position = readInt(); | |
82 query.add(term,position); | |
83 } | |
84 return query; | |
85 } | |
86 case LogFile.TYPE_QUERY_NUMERIC_RANGE: | |
87 { | |
88 String field = readUTF(); | |
89 Number min = (Number)readObject(); | |
90 Number max = (Number)readObject(); | |
91 boolean minInclusive = readBoolean(); | |
92 boolean maxInclusive = readBoolean(); | |
93 Number n = min!=null ? min : max; | |
94 if( n instanceof Integer ) | |
95 return NumericRangeQuery.newIntRange(field,(Integer)min,(Integer)max,minInclusive,maxInclusive); | |
96 if( n instanceof Long ) | |
97 return NumericRangeQuery.newLongRange(field,(Long)min,(Long)max,minInclusive,maxInclusive); | |
98 if( n instanceof Float ) | |
99 return NumericRangeQuery.newFloatRange(field,(Float)min,(Float)max,minInclusive,maxInclusive); | |
100 if( n instanceof Double ) | |
101 return NumericRangeQuery.newDoubleRange(field,(Double)min,(Double)max,minInclusive,maxInclusive); | |
102 throw new RuntimeException("bad numeric type for "+n); | |
103 } | |
104 case LogFile.TYPE_QUERY_BOOLEAN: | |
105 { | |
106 BooleanQuery query = new BooleanQuery(); | |
107 int n = readInt(); | |
108 for( int i=0; i<n; i++ ) { | |
109 Query subquery = readQuery(); | |
110 BooleanClause.Occur occur = BooleanClause.Occur.valueOf( readUTF() ); | |
111 query.add(subquery,occur); | |
112 } | |
113 return query; | |
114 } | |
115 default: | |
116 throw new RuntimeException("invalid type "+type); | |
117 } | |
118 } | |
119 | |
120 public byte[] readByteArray() throws IOException { | |
121 int len = readInt(); | |
122 byte[] bytes = new byte[len]; | |
123 readFully(bytes); | |
124 return bytes; | |
125 } | |
126 | |
127 public List readList() throws IOException { | |
128 final int size = readInt(); | |
129 List list = new ArrayList(size); | |
130 for( int i=0; i<size; i++ ) { | |
131 list.add( readObject() ); | |
132 } | |
133 return list; | |
134 } | |
135 | |
136 public Map readMap() throws IOException { | |
137 final int size = readInt(); | |
138 Map map = new LinkedHashMap(); | |
139 for( int i=0; i<size; i++ ) { | |
140 Object key = readObject(); | |
141 Object value = readObject(); | |
142 map.put(key,value); | |
143 } | |
144 return map; | |
145 } | |
146 | |
147 public Query readQuery() throws IOException { | |
148 return (Query)readObject(); | |
149 } | |
150 | |
151 public BytesRef readBytesRef() throws IOException { | |
152 return new BytesRef( readByteArray() ); | |
153 } | |
154 | |
155 public Term readTerm() throws IOException { | |
156 String key = readUTF(); | |
157 BytesRef value = readBytesRef(); | |
158 return new Term(key,value); | |
159 } | |
160 | |
161 } |