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 */
020package org.apache.directory.server.xdbm.search.evaluator;
021
022
023import org.apache.directory.api.ldap.model.entry.Entry;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.filter.ScopeNode;
026import org.apache.directory.api.ldap.model.message.SearchScope;
027import org.apache.directory.api.ldap.model.name.Dn;
028import org.apache.directory.server.core.api.partition.Partition;
029import org.apache.directory.server.core.api.partition.PartitionTxn;
030import org.apache.directory.server.i18n.I18n;
031import org.apache.directory.server.xdbm.IndexEntry;
032import org.apache.directory.server.xdbm.ParentIdAndRdn;
033import org.apache.directory.server.xdbm.Store;
034import org.apache.directory.server.xdbm.search.Evaluator;
035
036
037/**
038 * Evaluates ScopeNode assertions with subtree scope on candidates using an
039 * entry database.
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class SubtreeScopeEvaluator implements Evaluator<ScopeNode>
044{
045    /** The ScopeNode containing initial search scope constraints */
046    private final ScopeNode node;
047
048    /** The entry identifier of the scope base */
049    private final String baseId;
050
051    /** 
052     * Whether or not to accept all candidates.  If this evaluator's baseId is
053     * set to the context entry's id, then obviously all candidates will be 
054     * subordinate to this root ancestor or in subtree scope.  This check is 
055     * done on  initialization and used there after.  One reason we need do 
056     * this is because the subtree scope index (sub level index) does not map 
057     * the values for the context entry id to it's subordinates since it would 
058     * have to include all entries.  This is a waste of space and lookup time
059     * since we know all entries will be subordinates in this case.
060     */
061    private final boolean baseIsContextEntry;
062
063    /** True if the scope requires alias dereferencing while searching */
064    private final boolean dereferencing;
065
066    /** The entry database/store */
067    private final Store db;
068
069
070    /**
071     * Creates a subtree scope node evaluator for search expressions.
072     *
073     * @param partitionTxn The transaction to use
074     * @param db the database used to evaluate scope node
075     * @param node the scope node
076     * @throws LdapException on db access failure
077     */
078    public SubtreeScopeEvaluator( PartitionTxn partitionTxn, Store db, ScopeNode node ) throws LdapException
079    {
080        this.db = db;
081        this.node = node;
082
083        if ( node.getScope() != SearchScope.SUBTREE )
084        {
085            throw new IllegalStateException( I18n.err( I18n.ERR_727 ) );
086        }
087
088        baseId = node.getBaseId();
089        
090        baseIsContextEntry = db.getSuffixId( partitionTxn ) == baseId;
091
092        dereferencing = node.getDerefAliases().isDerefInSearching() || node.getDerefAliases().isDerefAlways();
093    }
094
095
096    /**
097     * Tells if a candidate is a descendant of the base ID. We have to fetch all 
098     * the parentIdAndRdn up to the baseId. If we terminate on the context entry without 
099     * having found the baseId, then the candidate is not a descendant.
100     */
101    private boolean isDescendant( PartitionTxn partitionTxn, String candidateId ) throws LdapException
102    {
103        String tmp = candidateId;
104
105        while ( true )
106        {
107            ParentIdAndRdn parentIdAndRdn = db.getRdnIndex().reverseLookup( partitionTxn, tmp );
108
109            if ( parentIdAndRdn == null )
110            {
111                return false;
112            }
113
114            tmp = parentIdAndRdn.getParentId();
115
116            if ( tmp.equals( Partition.ROOT_ID ) )
117            {
118                return false;
119            }
120
121            if ( tmp.equals( baseId ) )
122            {
123                return true;
124            }
125        }
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public boolean evaluate( PartitionTxn partitionTxn, IndexEntry<?, String> indexEntry ) throws LdapException
134    {
135        String id = indexEntry.getId();
136        Entry entry = indexEntry.getEntry();
137
138        // Fetch the entry
139        if ( null == entry )
140        {
141            entry = db.fetch( partitionTxn, indexEntry.getId() );
142
143            if ( null == entry )
144            {
145                // The entry is not anymore present : get out
146                return false;
147            }
148
149            indexEntry.setEntry( entry );
150        }
151
152        /*
153         * This condition catches situations where the candidate is equal to 
154         * the base entry and when the base entry is the context entry.  Note
155         * we do not store a mapping in the subtree index of the context entry
156         * to all it's subordinates since that would be the entire set of 
157         * entries in the db.
158         */
159        boolean isDescendant = baseIsContextEntry || baseId.equals( id ) || entry.getDn().isDescendantOf( node.getBaseDn() );
160
161        /*
162         * The candidate id could be any entry in the db.  If search
163         * dereferencing is not enabled then we return the results of the
164         * descendant test.
165         */
166        if ( !isDereferencing() )
167        {
168            return isDescendant;
169        }
170
171        /*
172         * From here down alias dereferencing is enabled.  We determine if the
173         * candidate id is an alias, if so we reject it since aliases should
174         * not be returned.
175         */
176        if ( db.getAliasCache() != null )
177        {
178            Dn dn = db.getAliasCache().getIfPresent( id );
179            
180            if ( dn != null )
181            {
182                return false;
183            }
184        }
185        else if ( null != db.getAliasIndex().reverseLookup( partitionTxn, id ) )
186        {
187            return false;
188        }
189
190        /*
191         * The candidate is NOT an alias at this point.  So if it is a
192         * descendant we just return true since it is in normal subtree scope.
193         */
194        if ( isDescendant )
195        {
196            return true;
197        }
198
199        /*
200         * At this point the candidate is not a descendant and it is not an
201         * alias.  We need to check if the candidate is in extended subtree
202         * scope by performing a lookup on the subtree alias index.  This index
203         * stores a tuple mapping the baseId to the ids of objects brought
204         * into subtree scope of the base by an alias:
205         *
206         * ( baseId, aliasedObjId )
207         *
208         * If the candidate id is an object brought into subtree scope then
209         * the lookup returns true accepting the candidate.  Otherwise the
210         * candidate is rejected with a false return because it is not in scope.
211         */
212        return db.getSubAliasIndex().forward( partitionTxn, baseId, id );
213    }
214
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    public boolean evaluate( Entry candidate ) throws LdapException
221    {
222        throw new UnsupportedOperationException( I18n.err( I18n.ERR_721 ) );
223    }
224
225
226    /**
227     * {@inheritDoc}
228     */
229    @Override
230    public ScopeNode getExpression()
231    {
232        return node;
233    }
234
235
236    /**
237     * @return The base ID
238     */
239    public String getBaseId()
240    {
241        return baseId;
242    }
243
244
245    /**
246     * @return <tt>true</tt> if dereferencing
247     */
248    public boolean isDereferencing()
249    {
250        return dereferencing;
251    }
252
253
254    /**
255     * @see Object#toString()
256     */
257    public String toString( String tabs )
258    {
259        StringBuilder sb = new StringBuilder();
260
261        sb.append( tabs ).append( "SubstreeScopeEvaluator : " ).append( node ).append( '\n' );
262
263        return sb.toString();
264    }
265
266
267    /**
268     * @see Object#toString()
269     */
270    public String toString()
271    {
272        return toString( "" );
273    }
274}