001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.xdbm.search.impl;
021    
022    
023    import org.apache.directory.shared.ldap.filter.EqualityNode;
024    import org.apache.directory.shared.ldap.schema.AttributeType;
025    import org.apache.directory.shared.ldap.schema.MatchingRule;
026    import org.apache.directory.shared.ldap.schema.Normalizer;
027    import org.apache.directory.shared.ldap.schema.comparators.ByteArrayComparator;
028    import org.apache.directory.shared.ldap.schema.comparators.StringComparator;
029    import org.apache.directory.shared.ldap.schema.normalizers.NoOpNormalizer;
030    import org.apache.directory.shared.ldap.util.StringTools;
031    import org.apache.directory.shared.ldap.entry.Value;
032    import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
033    import org.apache.directory.server.xdbm.IndexEntry;
034    import org.apache.directory.server.xdbm.Store;
035    import org.apache.directory.server.xdbm.Index;
036    import org.apache.directory.server.xdbm.search.Evaluator;
037    import org.apache.directory.server.schema.registries.Registries;
038    import org.apache.directory.server.core.entry.ServerBinaryValue;
039    import org.apache.directory.server.core.entry.ServerEntry;
040    import org.apache.directory.server.core.entry.ServerAttribute;
041    import org.apache.directory.server.core.entry.ServerStringValue;
042    
043    import java.util.Iterator;
044    import java.util.Comparator;
045    
046    
047    /**
048     * An Evaluator which determines if candidates are matched by GreaterEqNode
049     * assertions.
050     *
051     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
052     * @version $Rev$
053     */
054    public class EqualityEvaluator<T> implements Evaluator<EqualityNode<T>, ServerEntry>
055    {
056        private final EqualityNode<T> node;
057        private final Store<ServerEntry> db;
058        private final Registries registries;
059        private final AttributeType type;
060        private final Normalizer normalizer;
061        
062        /** The comparator to use */
063        private final Comparator comparator;
064        
065        /** The default byte[] comparator if no comparator has been defined */
066        private static final Comparator<byte[]> BINARY_COMPARATOR = ByteArrayComparator.INSTANCE;
067        
068        /** The default String comparator if no comparator has been defined */
069        private static final Comparator<String> STRING_COMPARATOR = StringComparator.INSTANCE;
070        
071        private final Index<T,ServerEntry> idx;
072    
073    
074        public EqualityEvaluator( EqualityNode<T> node, Store<ServerEntry> db, Registries registries )
075            throws Exception
076        {
077            this.db = db;
078            this.node = node;
079            this.registries = registries;
080    
081            if ( db.hasUserIndexOn( node.getAttribute() ) )
082            {
083                //noinspection unchecked
084                idx = ( Index<T, ServerEntry> ) db.getUserIndex( node.getAttribute() );
085                type = null;
086                normalizer = null;
087                comparator = null;
088            }
089            else
090            {
091                idx = null;
092                type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
093    
094                MatchingRule mr = type.getEquality();
095    
096                if ( mr == null )
097                {
098                    normalizer = NoOpNormalizer.INSTANCE;
099                    comparator = null;
100                }
101                else
102                {
103                    normalizer = mr.getNormalizer();
104                    comparator = mr.getComparator();
105                }
106            }
107        }
108    
109    
110        public EqualityNode<T> getExpression()
111        {
112            return node;
113        }
114    
115    
116        public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
117        {
118            if ( idx != null )
119            {
120                return idx.forward( node.getValue().get(), indexEntry.getId() );
121            }
122    
123            ServerEntry entry = indexEntry.getObject();
124    
125            // resuscitate the entry if it has not been and set entry in IndexEntry
126            if ( null == entry )
127            {
128                entry = db.lookup( indexEntry.getId() );
129                indexEntry.setObject( entry );
130            }
131    
132            return evaluate( entry );
133        }
134    
135    
136        public boolean evaluate( ServerEntry entry ) throws Exception
137        {
138            // get the attribute
139            ServerAttribute attr = ( ServerAttribute ) entry.get( type );
140    
141            // if the attribute does not exist just return false
142            if ( attr != null && evaluate( attr ) )
143            {
144                return true;
145            }
146    
147            // If we do not have the attribute, loop through the sub classes of
148            // the attributeType.  Perhaps the entry has an attribute value of a
149            // subtype (descendant) that will produce a match
150            if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
151            {
152                // TODO check to see if descendant handling is necessary for the
153                // index so we can match properly even when for example a name
154                // attribute is used instead of more specific commonName
155                Iterator<AttributeType> descendants =
156                    registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
157    
158                while ( descendants.hasNext() )
159                {
160                    AttributeType descendant = descendants.next();
161    
162                    attr = ( ServerAttribute ) entry.get( descendant );
163    
164                    if ( attr != null && evaluate( attr ) )
165                    {
166                        return true;
167                    }
168                }
169            }
170    
171            // we fell through so a match was not found - assertion was false.
172            return false;
173        }
174    
175    
176        public boolean evaluate( Long id ) throws Exception
177        {
178            if ( idx != null )
179            {
180                return idx.reverse( id );
181            }
182    
183            return evaluate ( db.lookup( id ) );
184        }
185        
186        
187        // TODO - determine if comparator and index entry should have the Value
188        // wrapper or the raw normalized value
189        private boolean evaluate( ServerAttribute attribute ) throws Exception
190        {
191            /*
192             * Cycle through the attribute values testing normalized version
193             * obtained from using the ordering or equality matching rule's
194             * normalizer.  The test uses the comparator obtained from the
195             * appropriate matching rule to perform the check.
196             */
197            for ( Value<?> value : attribute )
198            {
199                value.normalize( normalizer );
200                
201                //noinspection unchecked
202                if ( value.isBinary() )
203                {
204                    // Deal with a binary value
205                    byte[] serverValue = ((Value<byte[]>)value).getNormalizedValue();
206                    byte[] nodeValue = ((Value<byte[]>)node.getValue()).getNormalizedValue();
207                    
208                    if ( comparator != null )
209                    {
210                        if ( ( comparator.compare( serverValue, nodeValue ) == 0 ) )
211                        {
212                            return true;
213                        }
214                    }
215                    else
216                    {
217                        if ( BINARY_COMPARATOR.compare( serverValue, nodeValue ) == 0 )
218                        {
219                            return true;
220                        }
221                    }
222                }
223                else
224                {
225                    // Deal with a String value
226                    String serverValue = ((Value<String>)value).getNormalizedValue();
227                    String nodeValue = null;
228                    
229                    if ( node.getValue().isBinary() )
230                    {
231                        nodeValue = StringTools.utf8ToString( ((Value<byte[]>)node.getValue()).getNormalizedValue() );
232                    }
233                    else
234                    {
235                        nodeValue = ((Value<String>)node.getValue()).getNormalizedValue();
236                    }
237                    
238                    if ( comparator != null )
239                    {
240                        if ( comparator.compare( serverValue, nodeValue ) == 0 )
241                        {
242                            return true;
243                        }
244                    }
245                    else
246                    {
247                        if ( STRING_COMPARATOR.compare( serverValue, nodeValue ) == 0 )
248                        {
249                            return true;
250                        }
251                    }
252                }
253            }
254    
255            return false;
256        }
257    }