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 javax.naming.directory.SearchControls;
024
025 import org.apache.directory.shared.ldap.filter.AndNode;
026 import org.apache.directory.shared.ldap.filter.BranchNode;
027 import org.apache.directory.shared.ldap.filter.ExprNode;
028 import org.apache.directory.shared.ldap.filter.ScopeNode;
029 import org.apache.directory.shared.ldap.filter.SearchScope;
030 import org.apache.directory.shared.ldap.message.AliasDerefMode;
031 import org.apache.directory.shared.ldap.name.LdapDN;
032 import org.apache.directory.server.xdbm.*;
033 import org.apache.directory.server.xdbm.search.Optimizer;
034 import org.apache.directory.server.xdbm.search.SearchEngine;
035 import org.apache.directory.server.xdbm.search.Evaluator;
036 import org.apache.directory.server.core.entry.ServerEntry;
037
038
039 /**
040 * Given a search filter and a scope the search engine identifies valid
041 * candidate entries returning their ids.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 * @version $Rev: 697059 $
045 */
046 public class DefaultSearchEngine implements SearchEngine<ServerEntry>
047 {
048 /** the Optimizer used by this DefaultSearchEngine */
049 private final Optimizer optimizer;
050 /** the Database this DefaultSearchEngine operates on */
051 private final Store<ServerEntry> db;
052 /** creates Cursors over entries satisfying filter expressions */
053 private final CursorBuilder cursorBuilder;
054 /** creates evaluators which check to see if candidates satisfy a filter expression */
055 private final EvaluatorBuilder evaluatorBuilder;
056
057
058 // ------------------------------------------------------------------------
059 // C O N S T R U C T O R S
060 // ------------------------------------------------------------------------
061
062
063 /**
064 * Creates a DefaultSearchEngine for searching a Database without setting
065 * up the database.
066 * @param db the btree based partition
067 * @param cursorBuilder an expression cursor builder
068 * @param evaluatorBuilder an expression evaluator builder
069 * @param optimizer an optimizer to use during search
070 */
071 public DefaultSearchEngine( Store<ServerEntry> db,
072 CursorBuilder cursorBuilder, EvaluatorBuilder evaluatorBuilder, Optimizer optimizer )
073 {
074 this.db = db;
075 this.optimizer = optimizer;
076 this.cursorBuilder = cursorBuilder;
077 this.evaluatorBuilder = evaluatorBuilder;
078 }
079
080
081 /**
082 * Gets the optimizer for this DefaultSearchEngine.
083 *
084 * @return the optimizer
085 */
086 public Optimizer getOptimizer()
087 {
088 return optimizer;
089 }
090
091
092 /**
093 * @see SearchEngine#cursor(LdapDN, AliasDerefMode, ExprNode, SearchControls)
094 */
095 public IndexCursor<Long,ServerEntry> cursor( LdapDN base, AliasDerefMode aliasDerefMode, ExprNode filter,
096 SearchControls searchCtls ) throws Exception
097 {
098 LdapDN effectiveBase;
099 Long baseId = db.getEntryId( base.toString() );
100
101 // Check that we have an entry, otherwise we can immediately get out
102 if ( baseId == null )
103 {
104 // The entry is not found : ciao !
105 return new EmptyIndexCursor<Long, ServerEntry>();
106 }
107
108 String aliasedBase = db.getAliasIndex().reverseLookup( baseId );
109
110 // --------------------------------------------------------------------
111 // Determine the effective base with aliases
112 // --------------------------------------------------------------------
113
114 /*
115 * If the base is not an alias or if alias dereferencing does not
116 * occur on finding the base then we set the effective base to the
117 * given base.
118 */
119 if ( ( null == aliasedBase ) || ! aliasDerefMode.isDerefFindingBase() )
120 {
121 effectiveBase = base;
122 }
123
124 /*
125 * If the base is an alias and alias dereferencing does occur on
126 * finding the base then we set the effective base to the alias target
127 * got from the alias index.
128 */
129 else
130 {
131 effectiveBase = new LdapDN( aliasedBase );
132 }
133
134 // --------------------------------------------------------------------
135 // Specifically Handle Object Level Scope
136 // --------------------------------------------------------------------
137
138 if ( searchCtls.getSearchScope() == SearchControls.OBJECT_SCOPE )
139 {
140 Long effectiveBaseId = baseId;
141 if ( effectiveBase != base )
142 {
143 effectiveBaseId = db.getEntryId( effectiveBase.toNormName() );
144 }
145
146 IndexEntry<Long, ServerEntry> indexEntry = new ForwardIndexEntry<Long, ServerEntry>();
147 indexEntry.setId( effectiveBaseId );
148 optimizer.annotate( filter );
149 Evaluator<? extends ExprNode, ServerEntry> evaluator = evaluatorBuilder.build( filter );
150
151 if ( evaluator.evaluate( indexEntry ) )
152 {
153 return new SingletonIndexCursor<Long,ServerEntry>( indexEntry );
154 }
155 else
156 {
157 return new EmptyIndexCursor<Long, ServerEntry>();
158 }
159 }
160
161 // Add the scope node using the effective base to the filter
162 BranchNode root = new AndNode();
163 ExprNode node = new ScopeNode( aliasDerefMode, effectiveBase.toString(),
164 SearchScope.getSearchScope( searchCtls ) );
165 root.getChildren().add( node );
166 root.getChildren().add( filter );
167
168 // Annotate the node with the optimizer and return search enumeration.
169 optimizer.annotate( root );
170 return ( IndexCursor<Long, ServerEntry> ) cursorBuilder.build( root );
171 }
172
173
174 /**
175 * @see SearchEngine#evaluator(ExprNode)
176 */
177 public Evaluator<? extends ExprNode, ServerEntry> evaluator( ExprNode filter )
178 throws Exception
179 {
180 return evaluatorBuilder.build( filter );
181 }
182 }