1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.xdbm.search.impl;
21
22
23 import org.apache.directory.shared.ldap.filter.GreaterEqNode;
24 import org.apache.directory.shared.ldap.schema.AttributeType;
25 import org.apache.directory.shared.ldap.schema.MatchingRule;
26 import org.apache.directory.shared.ldap.schema.Normalizer;
27 import org.apache.directory.shared.ldap.entry.Value;
28 import org.apache.directory.server.xdbm.IndexEntry;
29 import org.apache.directory.server.xdbm.Store;
30 import org.apache.directory.server.xdbm.Index;
31 import org.apache.directory.server.xdbm.search.Evaluator;
32 import org.apache.directory.server.schema.registries.Registries;
33 import org.apache.directory.server.core.entry.ServerEntry;
34 import org.apache.directory.server.core.entry.ServerAttribute;
35
36 import java.util.Iterator;
37 import java.util.Comparator;
38
39
40
41
42
43
44
45
46
47 public class GreaterEqEvaluator implements Evaluator<GreaterEqNode, ServerEntry>
48 {
49 private final GreaterEqNode node;
50 private final Store<ServerEntry> db;
51 private final Registries registries;
52 private final AttributeType type;
53 private final Normalizer normalizer;
54 private final Comparator comparator;
55 private final Index<Object,ServerEntry> idx;
56
57
58 public GreaterEqEvaluator( GreaterEqNode node, Store<ServerEntry> db, Registries registries )
59 throws Exception
60 {
61 this.db = db;
62 this.node = node;
63 this.registries = registries;
64 this.type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
65
66 if ( db.hasUserIndexOn( node.getAttribute() ) )
67 {
68
69 idx = ( Index<Object,ServerEntry> ) db.getUserIndex( node.getAttribute() );
70 }
71 else
72 {
73 idx = null;
74 }
75
76
77
78
79
80
81
82 MatchingRule mr = type.getOrdering();
83
84 if ( mr == null )
85 {
86 mr = type.getEquality();
87 }
88
89 if ( mr == null )
90 {
91 throw new IllegalStateException(
92 "Could not find matchingRule to use for GreaterEqNode evaluation: " + node );
93 }
94
95 normalizer = mr.getNormalizer();
96 comparator = mr.getComparator();
97 }
98
99
100 public GreaterEqNode getExpression()
101 {
102 return node;
103 }
104
105
106 public AttributeType getAttributeType()
107 {
108 return type;
109 }
110
111
112 public Normalizer getNormalizer()
113 {
114 return normalizer;
115 }
116
117
118 public Comparator getComparator()
119 {
120 return comparator;
121 }
122
123
124 public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
125 {
126 if ( idx != null )
127 {
128 return idx.reverseGreaterOrEq( indexEntry.getId(), node.getValue().get() );
129 }
130
131 ServerEntry entry = indexEntry.getObject();
132
133
134 if ( null == entry )
135 {
136 entry = db.lookup( indexEntry.getId() );
137 indexEntry.setObject( entry );
138 }
139
140
141
142
143
144
145
146
147
148
149 ServerAttribute attr = ( ServerAttribute ) entry.get( type );
150
151
152
153 if ( attr != null && evaluate( ( IndexEntry<Object,ServerEntry> ) indexEntry, attr ) )
154 {
155 return true;
156 }
157
158
159
160
161 if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
162 {
163
164
165
166 Iterator<AttributeType> descendants =
167 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
168
169 while ( descendants.hasNext() )
170 {
171 AttributeType descendant = descendants.next();
172
173 attr = ( ServerAttribute ) entry.get( descendant );
174
175
176 if ( attr != null && evaluate( ( IndexEntry<Object, ServerEntry> ) indexEntry, attr ) )
177 {
178 return true;
179 }
180 }
181 }
182
183
184 return false;
185 }
186
187
188 public boolean evaluate( Long id ) throws Exception
189 {
190 if ( idx != null )
191 {
192 return idx.reverseGreaterOrEq( id, node.getValue().get() );
193 }
194
195 return evaluate ( db.lookup( id ) );
196 }
197
198
199 public boolean evaluate( ServerEntry entry ) throws Exception
200 {
201
202 ServerAttribute attr = ( ServerAttribute ) entry.get( type );
203
204
205 if ( attr != null && evaluate( null, attr ) )
206 {
207 return true;
208 }
209
210
211
212
213 if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
214 {
215
216
217
218 Iterator<AttributeType> descendants =
219 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
220
221 while ( descendants.hasNext() )
222 {
223 AttributeType descendant = descendants.next();
224
225 attr = ( ServerAttribute ) entry.get( descendant );
226
227 if ( attr != null && evaluate( null, attr ) )
228 {
229 return true;
230 }
231 }
232 }
233
234
235 return false;
236 }
237
238
239
240
241 private boolean evaluate( IndexEntry<Object,ServerEntry> indexEntry, ServerAttribute attribute ) throws Exception
242 {
243
244
245
246
247
248
249 for ( Value value : attribute )
250 {
251 value.normalize( normalizer );
252
253
254 if ( comparator.compare( value.getNormalizedValue(), node.getValue().getNormalizedValue() ) >= 0 )
255 {
256 if ( indexEntry != null )
257 {
258 indexEntry.setValue( value.getNormalizedValue() );
259 }
260 return true;
261 }
262 }
263
264 return false;
265 }
266 }