1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.core.indexer; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.core.data.Value; |
25 |
| |
26 |
| import java.util.Arrays; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class IndexQuery { |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| private static final Log log = LogFactory.getLog(IndexQuery.class); |
41 |
| |
42 |
| |
43 |
| public static final int ANY = 0; |
44 |
| |
45 |
| |
46 |
| public static final int EQ = 1; |
47 |
| public static final int NEQ = -1; |
48 |
| public static final int GT = 2; |
49 |
| public static final int LEQ = -2; |
50 |
| public static final int LT = 3; |
51 |
| public static final int GEQ = -3; |
52 |
| |
53 |
| |
54 |
| public static final int BW = 4; |
55 |
| public static final int NBW = -4; |
56 |
| public static final int BWX = 5; |
57 |
| public static final int NBWX = -5; |
58 |
| |
59 |
| |
60 |
| public static final int IN = 6; |
61 |
| public static final int NIN = -6; |
62 |
| |
63 |
| |
64 |
| public static final int SW = 7; |
65 |
| public static final int NSW = -7; |
66 |
| |
67 |
| public static final int TQ = 8; |
68 |
| |
69 |
| |
70 |
| protected final IndexPattern pattern; |
71 |
| protected final int op; |
72 |
| protected final Value[] vals; |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
0
| public IndexQuery(IndexPattern pattern) {
|
79 |
0
| this.pattern = pattern;
|
80 |
0
| this.op = ANY;
|
81 |
0
| this.vals = null;
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public IndexQuery(IndexPattern pattern, Value[] vals) {
|
88 |
0
| this(pattern, IN, vals);
|
89 |
| } |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
82
| public IndexQuery(IndexPattern pattern, int op, Value[] vals) {
|
95 |
82
| this.pattern = pattern;
|
96 |
82
| this.op = op;
|
97 |
82
| this.vals = vals;
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
275
| public IndexQuery(IndexPattern pattern, int op, Value val) {
|
104 |
275
| this.pattern = pattern;
|
105 |
275
| this.op = op;
|
106 |
| |
107 |
275
| if (op == SW || op == NSW) {
|
108 |
| |
109 |
| |
110 |
102
| byte[] b = new byte[val.getLength() + 1];
|
111 |
102
| val.copyTo(b, 0);
|
112 |
102
| b[b.length - 1] = Byte.MAX_VALUE;
|
113 |
102
| Value val2 = new Value(b);
|
114 |
102
| this.vals = new Value[]{ val, val2 };
|
115 |
| } else { |
116 |
173
| this.vals = new Value[]{ val };
|
117 |
| } |
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
0
| public IndexQuery(IndexPattern pattern, Value val1) {
|
124 |
0
| this(pattern, EQ, val1);
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
0
| public IndexQuery(IndexPattern pattern, int op, Value val1, Value val2) {
|
131 |
0
| this.pattern = pattern;
|
132 |
0
| this.op = op;
|
133 |
0
| this.vals = new Value[]{val1, val2};
|
134 |
| } |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
0
| public IndexQuery(IndexPattern pattern, Value val1, Value val2) {
|
140 |
0
| this(pattern, IN, val1, val2);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
181
| public IndexQuery(IndexPattern pattern, int op, String val1) {
|
147 |
181
| this(pattern, op, new Value(val1));
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
0
| public IndexQuery(IndexPattern pattern, String val1) {
|
154 |
0
| this(pattern, new Value(val1));
|
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
0
| public IndexQuery(IndexPattern pattern, int op, String val1, String val2) {
|
161 |
0
| this(pattern, op, new Value(val1), new Value(val2));
|
162 |
| } |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
0
| public IndexQuery(IndexPattern pattern, String val1, String val2) {
|
168 |
0
| this(pattern, new Value(val1), new Value(val2));
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
392
| public IndexPattern getPattern() {
|
178 |
392
| return this.pattern;
|
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
111
| public int getOperator() {
|
187 |
111
| return this.op;
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
6
| public final Value getValue(int index) {
|
197 |
6
| return this.vals[index];
|
198 |
| } |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
162
| public Value[] getValues() {
|
206 |
162
| return this.vals;
|
207 |
| } |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
0
| public final int getLength() {
|
216 |
0
| return this.vals.length;
|
217 |
| } |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
64
| public boolean testValue(Value value) {
|
229 |
64
| switch (this.op) {
|
230 |
| |
231 |
0
| case ANY:
|
232 |
0
| return true;
|
233 |
| |
234 |
| |
235 |
0
| case EQ:
|
236 |
0
| return value.equals(vals[0]);
|
237 |
0
| case NEQ:
|
238 |
0
| return !value.equals(vals[0]);
|
239 |
9
| case GT:
|
240 |
9
| return value.compareTo(vals[0]) > 0;
|
241 |
4
| case LEQ:
|
242 |
4
| return value.compareTo(vals[0]) <= 0;
|
243 |
14
| case LT:
|
244 |
14
| return value.compareTo(vals[0]) < 0;
|
245 |
3
| case GEQ:
|
246 |
3
| return value.compareTo(vals[0]) >= 0;
|
247 |
| |
248 |
| |
249 |
5
| case BW:
|
250 |
5
| return value.compareTo(vals[0]) >= 0 && value.compareTo(vals[1]) <= 0;
|
251 |
3
| case NBW:
|
252 |
3
| return value.compareTo(vals[0]) <= 0 || value.compareTo(vals[1]) >= 0;
|
253 |
3
| case BWX:
|
254 |
3
| return value.compareTo(vals[0]) > 0 && value.compareTo(vals[1]) < 0;
|
255 |
4
| case NBWX:
|
256 |
4
| return value.compareTo(vals[0]) < 0 || value.compareTo(vals[1]) > 0;
|
257 |
| |
258 |
| |
259 |
3
| case IN:
|
260 |
4
| case NIN:
|
261 |
7
| return Arrays.binarySearch(vals, value) >= 0 ? op == IN
|
262 |
| : op == NIN; |
263 |
| |
264 |
| |
265 |
12
| case SW:
|
266 |
0
| case NSW:
|
267 |
12
| return value.startsWith(vals[0]) ? op == SW : op == NSW;
|
268 |
0
| default:
|
269 |
0
| if (log.isWarnEnabled()) {
|
270 |
0
| log.warn("invalid operation : " + op);
|
271 |
| } |
272 |
| } |
273 |
| |
274 |
0
| return false;
|
275 |
| } |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
0
| public final boolean testValue(String value) {
|
286 |
0
| return testValue(new Value(value));
|
287 |
| } |
288 |
| } |