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.EqualityNode;
24 import org.apache.directory.shared.ldap.schema.AttributeType;
25 import org.apache.directory.shared.ldap.schema.ByteArrayComparator;
26 import org.apache.directory.shared.ldap.schema.MatchingRule;
27 import org.apache.directory.shared.ldap.schema.NoOpNormalizer;
28 import org.apache.directory.shared.ldap.schema.Normalizer;
29 import org.apache.directory.shared.ldap.entry.Value;
30 import org.apache.directory.server.xdbm.IndexEntry;
31 import org.apache.directory.server.xdbm.Store;
32 import org.apache.directory.server.xdbm.Index;
33 import org.apache.directory.server.xdbm.search.Evaluator;
34 import org.apache.directory.server.schema.registries.Registries;
35 import org.apache.directory.server.core.entry.ServerEntry;
36 import org.apache.directory.server.core.entry.ServerAttribute;
37
38 import java.util.Iterator;
39 import java.util.Comparator;
40
41
42
43
44
45
46
47
48
49 public class EqualityEvaluator<T> implements Evaluator<EqualityNode<T>, ServerEntry>
50 {
51 private final EqualityNode<T> node;
52 private final Store<ServerEntry> db;
53 private final Registries registries;
54 private final AttributeType type;
55 private final Normalizer normalizer;
56 private final Comparator comparator;
57 private final Index<T,ServerEntry> idx;
58
59
60 public EqualityEvaluator( EqualityNode<T> node, Store<ServerEntry> db, Registries registries )
61 throws Exception
62 {
63 this.db = db;
64 this.node = node;
65 this.registries = registries;
66
67 if ( db.hasUserIndexOn( node.getAttribute() ) )
68 {
69
70 idx = ( Index<T, ServerEntry> ) db.getUserIndex( node.getAttribute() );
71 type = null;
72 normalizer = null;
73 comparator = null;
74 }
75 else
76 {
77 idx = null;
78 type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
79
80 MatchingRule mr = type.getEquality();
81
82 if ( mr == null )
83 {
84 normalizer = NoOpNormalizer.INSTANCE;
85 comparator = ByteArrayComparator.INSTANCE;
86 }
87 else
88 {
89 normalizer = mr.getNormalizer();
90 comparator = mr.getComparator();
91 }
92 }
93 }
94
95
96 public EqualityNode<T> getExpression()
97 {
98 return node;
99 }
100
101
102 public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
103 {
104 if ( idx != null )
105 {
106 return idx.forward( node.getValue().get(), indexEntry.getId() );
107 }
108
109 ServerEntry entry = indexEntry.getObject();
110
111
112 if ( null == entry )
113 {
114 entry = db.lookup( indexEntry.getId() );
115 indexEntry.setObject( entry );
116 }
117
118 return evaluate( entry );
119 }
120
121
122 public boolean evaluate( ServerEntry entry ) throws Exception
123 {
124
125 ServerAttribute attr = ( ServerAttribute ) entry.get( type );
126
127
128 if ( attr != null && evaluate( attr ) )
129 {
130 return true;
131 }
132
133
134
135
136 if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
137 {
138
139
140
141 Iterator<AttributeType> descendants =
142 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
143
144 while ( descendants.hasNext() )
145 {
146 AttributeType descendant = descendants.next();
147
148 attr = ( ServerAttribute ) entry.get( descendant );
149
150 if ( attr != null && evaluate( attr ) )
151 {
152 return true;
153 }
154 }
155 }
156
157
158 return false;
159 }
160
161
162 public boolean evaluate( Long id ) throws Exception
163 {
164 if ( idx != null )
165 {
166 return idx.reverse( id );
167 }
168
169 return evaluate ( db.lookup( id ) );
170 }
171
172
173
174
175 private boolean evaluate( ServerAttribute attribute ) throws Exception
176 {
177
178
179
180
181
182
183 for ( Value value : attribute )
184 {
185 value.normalize( normalizer );
186
187
188 if ( comparator.compare( value.getNormalizedValue(), node.getValue().getNormalizedValue() ) == 0 )
189 {
190 return true;
191 }
192 }
193
194 return false;
195 }
196 }