KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > DefaultSearchEngine


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.db;
18
19
20 import org.apache.ldap.common.filter.AbstractExprNode;
21 import org.apache.ldap.common.filter.BranchNode;
22 import org.apache.ldap.common.filter.ExprNode;
23 import org.apache.ldap.common.filter.ScopeNode;
24 import org.apache.ldap.common.message.DerefAliasesEnum;
25 import org.apache.ldap.common.name.LdapName;
26
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.directory.SearchControls JavaDoc;
31 import java.math.BigInteger JavaDoc;
32 import java.util.Map JavaDoc;
33
34
35 /**
36  * Given a search filter and a scope the search engine identifies valid
37  * candidate entries returning their ids.
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev: 169198 $
41  */

42 public class DefaultSearchEngine implements SearchEngine
43 {
44     /** the Optimizer used by this DefaultSearchEngine */
45     private final Optimizer optimizer;
46     /** the Database this DefaultSearchEngine operates on */
47     private Database db;
48     /** Evaluator flyweight used for filter expression assertions */
49     private ExpressionEvaluator evaluator;
50     /** Enumerator flyweight that creates enumerations on filter expressions */
51     private ExpressionEnumerator enumerator;
52     
53
54     // ------------------------------------------------------------------------
55
// C O N S T R U C T O R S
56
// ------------------------------------------------------------------------
57

58
59     /**
60      * Creates a DefaultSearchEngine for searching a Database without setting
61      * up the database.
62      */

63     public DefaultSearchEngine( Database db,
64                                 ExpressionEvaluator evaluator,
65                                 ExpressionEnumerator enumerator )
66     {
67         this.db = db;
68         this.evaluator = evaluator;
69         this.enumerator = enumerator;
70         optimizer = new DefaultOptimizer( db );
71     }
72
73
74     /**
75      * Gets the optimizer for this DefaultSearchEngine.
76      *
77      * @return the optimizer
78      */

79     public Optimizer getOptimizer()
80     {
81         return optimizer;
82     }
83     
84     
85     /**
86      * @see SearchEngine#search(Name, Map, ExprNode,SearchControls)
87      */

88     public NamingEnumeration JavaDoc search( Name base, Map JavaDoc env, ExprNode filter,
89                                      SearchControls JavaDoc searchCtls )
90         throws NamingException JavaDoc
91     {
92         Name effectiveBase = null;
93         BigInteger JavaDoc baseId = db.getEntryId( base.toString() );
94         String JavaDoc aliasedBase = ( String JavaDoc ) db.getAliasIndex().reverseLookup( baseId );
95         DerefAliasesEnum mode = DerefAliasesEnum.getEnum( env );
96
97
98         // --------------------------------------------------------------------
99
// Determine the eective base with aliases
100
// --------------------------------------------------------------------
101

102
103         /*
104          * If the base is not an alias or if alias dereerencing does not
105          * occur on finding the base then we set the effective base to the
106          * given base.
107          */

108         if ( null == aliasedBase || ! mode.derefFindingBase() )
109         {
110             effectiveBase = base;
111         }
112         /*
113          * I the base is an alias and alias dereerencing does occur on
114          * inding the base then we set the eective base to the alias target
115          * gotten rom the alias index.
116          */

117         else if ( null != aliasedBase ) // mode = FINDING || ALWAYS
118
{
119             effectiveBase = new LdapName( aliasedBase );
120         }
121         /*
122          * I the base not an alias the we just set the base to the given base
123          */

124         else
125         {
126             effectiveBase = base;
127         }
128
129         // Add the scope node using the eective base to the ilter
130
BranchNode root = new BranchNode( AbstractExprNode.AND );
131         ExprNode node = new ScopeNode( env, effectiveBase.toString(),
132             searchCtls.getSearchScope() );
133         root.getChildren().add( node );
134         root.getChildren().add( filter );
135
136         // Annotate the node with the optimizer and return search enumeration.
137
optimizer.annotate( root );
138         return enumerator.enumerate( root );
139     }
140
141
142     /**
143      * @see SearchEngine#evaluate(ExprNode, BigInteger)
144      */

145     public boolean evaluate( ExprNode ilter, BigInteger JavaDoc id ) throws NamingException JavaDoc
146     {
147         IndexRecord rec = new IndexRecord();
148         rec.setEntryId( id );
149         return evaluator.evaluate( ilter, rec );
150     }
151 }
152
Popular Tags