1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.search.lucene.query;
25
26 import java.io.IOException;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.FieldSelector;
29 import org.apache.lucene.document.FieldSelectorResult;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.search.Searcher;
33 import org.apache.lucene.search.Weight;
34 import org.modeshape.graph.property.Path;
35 import org.modeshape.graph.property.PathFactory;
36 import org.modeshape.graph.property.ValueComparators;
37 import org.modeshape.graph.property.ValueFactories;
38 import org.modeshape.graph.property.ValueFactory;
39 import org.modeshape.graph.query.model.Comparison;
40
41
42
43
44
45
46
47 public class CompareNameQuery extends CompareQuery<Path.Segment> {
48
49 private static final long serialVersionUID = 1L;
50 protected static final Evaluator<Path.Segment> IS_LESS_THAN = new Evaluator<Path.Segment>() {
51 private static final long serialVersionUID = 1L;
52
53 public boolean satisfiesConstraint( Path.Segment nodeValue,
54 Path.Segment constraintValue ) {
55 return ValueComparators.PATH_SEGMENT_COMPARATOR.compare(nodeValue, constraintValue) < 0;
56 }
57
58 @Override
59 public String toString() {
60 return " < ";
61 }
62 };
63 protected static final Evaluator<Path.Segment> IS_LESS_THAN_OR_EQUAL_TO = new Evaluator<Path.Segment>() {
64 private static final long serialVersionUID = 1L;
65
66 public boolean satisfiesConstraint( Path.Segment nodeValue,
67 Path.Segment constraintValue ) {
68 return ValueComparators.PATH_SEGMENT_COMPARATOR.compare(nodeValue, constraintValue) <= 0;
69 }
70
71 @Override
72 public String toString() {
73 return " <= ";
74 }
75 };
76 protected static final Evaluator<Path.Segment> IS_GREATER_THAN = new Evaluator<Path.Segment>() {
77 private static final long serialVersionUID = 1L;
78
79 public boolean satisfiesConstraint( Path.Segment nodeValue,
80 Path.Segment constraintValue ) {
81 return ValueComparators.PATH_SEGMENT_COMPARATOR.compare(nodeValue, constraintValue) > 0;
82 }
83
84 @Override
85 public String toString() {
86 return " > ";
87 }
88 };
89 protected static final Evaluator<Path.Segment> IS_GREATER_THAN_OR_EQUAL_TO = new Evaluator<Path.Segment>() {
90 private static final long serialVersionUID = 1L;
91
92 public boolean satisfiesConstraint( Path.Segment nodeValue,
93 Path.Segment constraintValue ) {
94 return ValueComparators.PATH_SEGMENT_COMPARATOR.compare(nodeValue, constraintValue) >= 0;
95 }
96
97 @Override
98 public String toString() {
99 return " >= ";
100 }
101 };
102
103 protected static final Evaluator<Path.Segment> IS_LESS_THAN_NO_SNS = new Evaluator<Path.Segment>() {
104 private static final long serialVersionUID = 1L;
105
106 public boolean satisfiesConstraint( Path.Segment nodeValue,
107 Path.Segment constraintValue ) {
108 return ValueComparators.PATH_SEGMENT_NAME_COMPARATOR.compare(nodeValue, constraintValue) < 0;
109 }
110
111 @Override
112 public String toString() {
113 return " < ";
114 }
115 };
116 protected static final Evaluator<Path.Segment> IS_LESS_THAN_OR_EQUAL_TO_NO_SNS = new Evaluator<Path.Segment>() {
117 private static final long serialVersionUID = 1L;
118
119 public boolean satisfiesConstraint( Path.Segment nodeValue,
120 Path.Segment constraintValue ) {
121 return ValueComparators.PATH_SEGMENT_NAME_COMPARATOR.compare(nodeValue, constraintValue) <= 0;
122 }
123
124 @Override
125 public String toString() {
126 return " <= ";
127 }
128 };
129 protected static final Evaluator<Path.Segment> IS_GREATER_THAN_NO_SNS = new Evaluator<Path.Segment>() {
130 private static final long serialVersionUID = 1L;
131
132 public boolean satisfiesConstraint( Path.Segment nodeValue,
133 Path.Segment constraintValue ) {
134 return ValueComparators.PATH_SEGMENT_NAME_COMPARATOR.compare(nodeValue, constraintValue) > 0;
135 }
136
137 @Override
138 public String toString() {
139 return " > ";
140 }
141 };
142 protected static final Evaluator<Path.Segment> IS_GREATER_THAN_OR_EQUAL_TO_NO_SNS = new Evaluator<Path.Segment>() {
143 private static final long serialVersionUID = 1L;
144
145 public boolean satisfiesConstraint( Path.Segment nodeValue,
146 Path.Segment constraintValue ) {
147 return ValueComparators.PATH_SEGMENT_NAME_COMPARATOR.compare(nodeValue, constraintValue) >= 0;
148 }
149
150 @Override
151 public String toString() {
152 return " >= ";
153 }
154 };
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public static CompareNameQuery createQueryForNodesWithNameGreaterThan( Path.Segment constraintValue,
170 String localNameField,
171 String snsIndexFieldName,
172 ValueFactories factories,
173 boolean caseSensitive,
174 boolean includeSns ) {
175 return new CompareNameQuery(localNameField, snsIndexFieldName, constraintValue, factories.getPathFactory(),
176 factories.getStringFactory(), factories.getLongFactory(),
177 includeSns ? IS_GREATER_THAN : IS_GREATER_THAN_NO_SNS, caseSensitive);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static CompareNameQuery createQueryForNodesWithNameGreaterThanOrEqualTo( Path.Segment constraintValue,
194 String localNameField,
195 String snsIndexFieldName,
196 ValueFactories factories,
197 boolean caseSensitive,
198 boolean includeSns ) {
199 return new CompareNameQuery(localNameField, snsIndexFieldName, constraintValue, factories.getPathFactory(),
200 factories.getStringFactory(), factories.getLongFactory(),
201 includeSns ? IS_GREATER_THAN_OR_EQUAL_TO : IS_GREATER_THAN_OR_EQUAL_TO_NO_SNS, caseSensitive);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public static CompareNameQuery createQueryForNodesWithNameLessThan( Path.Segment constraintValue,
218 String localNameField,
219 String snsIndexFieldName,
220 ValueFactories factories,
221 boolean caseSensitive,
222 boolean includeSns ) {
223 return new CompareNameQuery(localNameField, snsIndexFieldName, constraintValue, factories.getPathFactory(),
224 factories.getStringFactory(), factories.getLongFactory(),
225 includeSns ? IS_LESS_THAN : IS_LESS_THAN_NO_SNS, caseSensitive);
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 public static CompareNameQuery createQueryForNodesWithNameLessThanOrEqualTo( Path.Segment constraintValue,
242 String localNameField,
243 String snsIndexFieldName,
244 ValueFactories factories,
245 boolean caseSensitive,
246 boolean includeSns ) {
247 return new CompareNameQuery(localNameField, snsIndexFieldName, constraintValue, factories.getPathFactory(),
248 factories.getStringFactory(), factories.getLongFactory(),
249 includeSns ? IS_LESS_THAN_OR_EQUAL_TO : IS_LESS_THAN_OR_EQUAL_TO_NO_SNS, caseSensitive);
250 }
251
252 private final String snsIndexFieldName;
253 private final ValueFactory<Long> longFactory;
254 private final PathFactory pathFactory;
255 private final boolean caseSensitive;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 protected CompareNameQuery( final String localNameField,
272 final String snsIndexFieldName,
273 Path.Segment constraintValue,
274 PathFactory pathFactory,
275 ValueFactory<String> stringFactory,
276 ValueFactory<Long> longFactory,
277 Evaluator<Path.Segment> evaluator,
278 boolean caseSensitive ) {
279 super(localNameField, constraintValue, null, stringFactory, evaluator, new FieldSelector() {
280 private static final long serialVersionUID = 1L;
281
282 public FieldSelectorResult accept( String fieldName ) {
283 if (fieldName.equals(localNameField)) return FieldSelectorResult.LOAD;
284 if (fieldName.equals(snsIndexFieldName)) return FieldSelectorResult.LOAD;
285 return FieldSelectorResult.NO_LOAD;
286 }
287 });
288 this.snsIndexFieldName = snsIndexFieldName;
289 this.longFactory = longFactory;
290 this.pathFactory = pathFactory;
291 this.caseSensitive = caseSensitive;
292 assert this.snsIndexFieldName != null;
293 assert this.longFactory != null;
294 }
295
296
297
298
299
300
301 @Override
302 protected Path.Segment readFromDocument( IndexReader reader,
303 int docId ) throws IOException {
304 Document doc = reader.document(docId, fieldSelector);
305 String localName = doc.get(fieldName);
306 if (!caseSensitive) localName = localName.toLowerCase();
307 int sns = longFactory.create(doc.get(snsIndexFieldName)).intValue();
308 return pathFactory.createSegment(localName, sns);
309 }
310
311
312
313
314
315
316 @Override
317 public Object clone() {
318 return new CompareNameQuery(fieldName, snsIndexFieldName, constraintValue, pathFactory, stringFactory, longFactory,
319 evaluator, caseSensitive);
320 }
321 }