View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.partition.impl.btree;
18  
19  
20  import java.math.BigInteger;
21  import java.util.Map;
22  
23  import javax.naming.Name;
24  import javax.naming.NamingEnumeration;
25  import javax.naming.NamingException;
26  import javax.naming.directory.SearchControls;
27  
28  import org.apache.ldap.common.filter.AbstractExprNode;
29  import org.apache.ldap.common.filter.BranchNode;
30  import org.apache.ldap.common.filter.ExprNode;
31  import org.apache.ldap.common.filter.ScopeNode;
32  import org.apache.ldap.common.message.DerefAliasesEnum;
33  import org.apache.ldap.common.name.LdapName;
34  
35  
36  /***
37   * Given a search filter and a scope the search engine identifies valid
38   * candidate entries returning their ids.
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev: 264732 $
42   */
43  public class DefaultSearchEngine implements SearchEngine
44  {
45      /*** the Optimizer used by this DefaultSearchEngine */
46      private final Optimizer optimizer;
47      /*** the Database this DefaultSearchEngine operates on */
48      private BTreeContextPartition db;
49      /*** Evaluator flyweight used for filter expression assertions */
50      private ExpressionEvaluator evaluator;
51      /*** Enumerator flyweight that creates enumerations on filter expressions */
52      private ExpressionEnumerator enumerator;
53      
54  
55      // ------------------------------------------------------------------------
56      // C O N S T R U C T O R S
57      // ------------------------------------------------------------------------
58  
59  
60      /***
61       * Creates a DefaultSearchEngine for searching a Database without setting
62       * up the database.
63       */
64      public DefaultSearchEngine( BTreeContextPartition db,
65                                  ExpressionEvaluator evaluator,
66                                  ExpressionEnumerator enumerator )
67      {
68          this.db = db;
69          this.evaluator = evaluator;
70          this.enumerator = enumerator;
71          optimizer = new DefaultOptimizer( db );
72      }
73  
74  
75      /***
76       * Gets the optimizer for this DefaultSearchEngine.
77       *
78       * @return the optimizer
79       */
80      public Optimizer getOptimizer()
81      {
82          return optimizer;
83      }
84      
85      
86      /***
87       * @see SearchEngine#search(Name, Map, ExprNode,SearchControls)
88       */
89      public NamingEnumeration search( Name base, Map env, ExprNode filter,
90                                       SearchControls searchCtls )
91          throws NamingException
92      {
93          Name effectiveBase = null;
94          BigInteger baseId = db.getEntryId( base.toString() );
95          String aliasedBase = ( String ) db.getAliasIndex().reverseLookup( baseId );
96          DerefAliasesEnum mode = DerefAliasesEnum.getEnum( env );
97  
98  
99          // --------------------------------------------------------------------
100         // Determine the eective base with aliases
101         // --------------------------------------------------------------------
102 
103 
104         /*
105          * If the base is not an alias or if alias dereerencing does not
106          * occur on finding the base then we set the effective base to the
107          * given base.
108          */
109         if ( null == aliasedBase || ! mode.derefFindingBase() )
110         {
111             effectiveBase = base;
112         }
113         /*
114          * I the base is an alias and alias dereerencing does occur on
115          * inding the base then we set the eective base to the alias target
116          * gotten rom the alias index.
117          */
118         else if ( null != aliasedBase ) // mode = FINDING || ALWAYS
119         {
120             effectiveBase = new LdapName( aliasedBase );
121         }
122         /*
123          * I the base not an alias the we just set the base to the given base
124          */
125         else
126         {
127             effectiveBase = base;
128         }
129 
130         // Add the scope node using the eective base to the ilter
131         BranchNode root = new BranchNode( AbstractExprNode.AND );
132         ExprNode node = new ScopeNode( env, effectiveBase.toString(),
133             searchCtls.getSearchScope() );
134         root.getChildren().add( node );
135         root.getChildren().add( filter );
136 
137         // Annotate the node with the optimizer and return search enumeration.
138         optimizer.annotate( root );
139         return enumerator.enumerate( root );
140     }
141 
142 
143     /***
144      * @see SearchEngine#evaluate(ExprNode, BigInteger)
145      */
146     public boolean evaluate( ExprNode ilter, BigInteger id ) throws NamingException
147     {
148         IndexRecord rec = new IndexRecord();
149         rec.setEntryId( id );
150         return evaluator.evaluate( ilter, rec );
151     }
152 }