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.LessEqNode;
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.entry.Value;
028    import org.apache.directory.server.xdbm.IndexEntry;
029    import org.apache.directory.server.xdbm.Store;
030    import org.apache.directory.server.xdbm.Index;
031    import org.apache.directory.server.xdbm.search.Evaluator;
032    import org.apache.directory.server.schema.registries.Registries;
033    import org.apache.directory.server.core.entry.ServerEntry;
034    import org.apache.directory.server.core.entry.ServerAttribute;
035    
036    import java.util.Iterator;
037    import java.util.Comparator;
038    
039    
040    /**
041     * An Evaluator which determines if candidates are matched by LessEqNode
042     * assertions.
043     *
044     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045     * @version $Rev$
046     */
047    public class LessEqEvaluator implements Evaluator<LessEqNode, ServerEntry>
048    {
049        private final LessEqNode node;
050        private final Store<ServerEntry> db;
051        private final Registries registries;
052        private final AttributeType type;
053        private final Normalizer normalizer;
054        private final Comparator comparator;
055        private final Index<Object,ServerEntry> idx;
056    
057    
058        public LessEqEvaluator( LessEqNode node, Store<ServerEntry> db, Registries registries )
059            throws Exception
060        {
061            this.db = db;
062            this.node = node;
063            this.registries = registries;
064            this.type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
065    
066            if ( db.hasUserIndexOn( node.getAttribute() ) )
067            {
068                //noinspection unchecked
069                idx = ( Index<Object,ServerEntry> ) db.getUserIndex( node.getAttribute() );
070            }
071            else
072            {
073                idx = null;
074            }
075    
076            /*
077             * We prefer matching using the Normalizer and Comparator pair from
078             * the ordering matchingRule if one is available.  It may very well
079             * not be.  If so then we resort to using the Normalizer and
080             * Comparator from the equality matchingRule as a last resort.
081             */
082            MatchingRule mr = type.getOrdering();
083    
084            if ( mr == null )
085            {
086                mr = type.getEquality();
087            }
088    
089            if ( mr == null )
090            {
091                throw new IllegalStateException(
092                    "Could not find matchingRule to use for LessEqNode evaluation: " + node );
093            }
094    
095            normalizer = mr.getNormalizer();
096            comparator = mr.getComparator();
097        }
098    
099    
100        public LessEqNode 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( Long id ) throws Exception
125        {
126            if ( idx != null )
127            {
128                return idx.reverseLessOrEq( id, node.getValue().get() );
129            }
130    
131            return evaluate( db.lookup( id ) );
132        }
133    
134    
135        public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
136        {
137            if ( idx != null )
138            {
139                return idx.reverseLessOrEq( indexEntry.getId(), node.getValue().get() );
140            }
141    
142            ServerEntry entry = indexEntry.getObject();
143    
144            // resuscitate the entry if it has not been and set entry in IndexEntry
145            if ( null == entry )
146            {
147                entry = db.lookup( indexEntry.getId() );
148                indexEntry.setObject( entry );
149            }
150            
151            if ( null == entry )
152            {
153                return false;
154            }
155    
156            // get the attribute
157            ServerAttribute attr = ( ServerAttribute ) entry.get( type );
158    
159            // if the attribute does not exist just return false
160            //noinspection unchecked
161            if ( attr != null && evaluate( ( IndexEntry<Object,ServerEntry> ) indexEntry, attr ) )
162            {
163                return true;
164            }
165    
166            // If we do not have the attribute, loop through the sub classes of
167            // the attributeType.  Perhaps the entry has an attribute value of a
168            // subtype (descendant) that will produce a match
169            if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
170            {
171                // TODO check to see if descendant handling is necessary for the
172                // index so we can match properly even when for example a name
173                // attribute is used instead of more specific commonName
174                Iterator<AttributeType> descendants =
175                    registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
176    
177                while ( descendants.hasNext() )
178                {
179                    AttributeType descendant = descendants.next();
180    
181                    attr = ( ServerAttribute ) entry.get( descendant );
182    
183                    //noinspection unchecked
184                    if ( attr != null && evaluate( ( IndexEntry<Object,ServerEntry> ) indexEntry, attr ) )
185                    {
186                        return true;
187                    }
188                }
189            }
190    
191            // we fell through so a match was not found - assertion was false.
192            return false;
193        }
194    
195    
196        public boolean evaluate( ServerEntry entry ) throws Exception
197        {
198            // get the attribute
199            ServerAttribute attr = ( ServerAttribute ) entry.get( type );
200    
201            // if the attribute does not exist just return false
202            if ( attr != null && evaluate( null, attr ) )
203            {
204                return true;
205            }
206    
207            // If we do not have the attribute, loop through the sub classes of
208            // the attributeType.  Perhaps the entry has an attribute value of a
209            // subtype (descendant) that will produce a match
210            if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
211            {
212                // TODO check to see if descendant handling is necessary for the
213                // index so we can match properly even when for example a name
214                // attribute is used instead of more specific commonName
215                Iterator<AttributeType> descendants =
216                    registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
217    
218                while ( descendants.hasNext() )
219                {
220                    AttributeType descendant = descendants.next();
221    
222                    attr = ( ServerAttribute ) entry.get( descendant );
223    
224                    if ( attr != null && evaluate( null, attr ) )
225                    {
226                        return true;
227                    }
228                }
229            }
230    
231            // we fell through so a match was not found - assertion was false.
232            return false;
233        }
234    
235    
236        // TODO - determine if comaparator and index entry should have the Value
237        // wrapper or the raw normalized value
238        private boolean evaluate( IndexEntry<Object,ServerEntry> indexEntry, ServerAttribute attribute ) throws Exception
239        {
240            /*
241             * Cycle through the attribute values testing normalized version
242             * obtained from using the ordering or equality matching rule's
243             * normalizer.  The test uses the comparator obtained from the
244             * appropriate matching rule to perform the check.
245             */
246            for ( Value value : attribute )
247            {
248                value.normalize( normalizer );
249    
250                //noinspection unchecked
251                if ( comparator.compare( value.getNormalizedValue(), node.getValue().getNormalizedValue() ) <= 0 )
252                {
253                    if ( indexEntry != null )
254                    {
255                        indexEntry.setValue( value.getNormalizedValue() );
256                    }
257                    return true;
258                }
259            }
260    
261            return false;
262        }
263    }