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.util.Iterator;
21  
22  import javax.naming.NamingException;
23  
24  import org.apache.ldap.common.filter.BranchNode;
25  import org.apache.ldap.common.filter.ExprNode;
26  import org.apache.ldap.server.schema.AttributeTypeRegistry;
27  import org.apache.ldap.server.schema.OidRegistry;
28  
29  
30  /***
31   * Top level filter expression evaluator implemenation.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 264732 $
35   */
36  public class ExpressionEvaluator implements Evaluator
37  {
38      /*** Leaf Evaluator flyweight use for leaf filter assertions */
39      private LeafEvaluator leafEvaluator;
40  
41  
42      // ------------------------------------------------------------------------
43      // C O N S T R U C T O R S
44      // ------------------------------------------------------------------------
45  
46  
47      /***
48       * Creates a top level Evaluator where leaves are delegated to a leaf node
49       * evaluator which is already provided.
50       *
51       * @param leafEvaluator handles leaf node evaluation.
52       */
53      public ExpressionEvaluator( LeafEvaluator leafEvaluator )
54      {
55          this.leafEvaluator = leafEvaluator;
56      }
57  
58  
59      /***
60       * Creates a top level Evaluator where leaves are delegated to a leaf node
61       * evaluator which will be created.
62       *
63       * @param db the database this evaluator operates upon
64       * @param oidRegistry the oid reg used for attrID to oid resolution
65       * @param attributeTypeRegistry the attribtype reg used for value comparison
66       */
67      public ExpressionEvaluator( BTreeContextPartition db,
68                                  OidRegistry oidRegistry,
69                                  AttributeTypeRegistry attributeTypeRegistry )
70      {
71          ScopeEvaluator scopeEvaluator = null;
72          SubstringEvaluator substringEvaluator = null;
73  
74          scopeEvaluator = new ScopeEvaluator( db );
75          substringEvaluator = new SubstringEvaluator( db, oidRegistry,
76              attributeTypeRegistry );
77          leafEvaluator = new LeafEvaluator( db, oidRegistry,
78              attributeTypeRegistry, scopeEvaluator, substringEvaluator );
79      }
80  
81  
82      /***
83       * Gets the leaf evaluator used by this top level expression evaluator.
84       *
85       * @return the leaf evaluator used by this top level expression evaluator
86       */
87      public LeafEvaluator getLeafEvaluator()
88      {
89          return leafEvaluator;
90      }
91  
92  
93      // ------------------------------------------------------------------------
94      // Evaluator.evaluate() implementation
95      // ------------------------------------------------------------------------
96  
97  
98      /***
99       * @see org.apache.ldap.server.partition.impl.btree.Evaluator#evaluate(ExprNode, IndexRecord)
100      */
101     public boolean evaluate( ExprNode node, IndexRecord record )
102         throws NamingException
103     {
104         if ( node.isLeaf() ) 
105         {
106             return leafEvaluator.evaluate( node, record );
107         }
108 
109         BranchNode bnode = ( BranchNode ) node;
110 
111         switch( bnode.getOperator() )
112         {
113         case( BranchNode.OR ):
114             Iterator children = bnode.getChildren().iterator();
115             
116             while ( children.hasNext() ) 
117             {
118                 ExprNode child = ( ExprNode ) children.next();
119                 
120                 if ( evaluate( child, record ) ) 
121                 {
122                     return true;
123                 }
124             }
125 
126             return false;
127         case( BranchNode.AND ):
128             children = bnode.getChildren().iterator();
129             while ( children.hasNext() ) 
130             {
131                 ExprNode child = ( ExprNode ) children.next();
132 
133                 if ( ! evaluate( child, record ) ) 
134                 {
135                     return false;
136                 }
137             }
138 
139             return true;
140         case( BranchNode.NOT ):
141             if ( null != bnode.getChild() )
142             {
143                 return ! evaluate( bnode.getChild(), record );
144             }
145 
146             throw new NamingException( "Negation has no child: " + node );
147         default:
148             throw new NamingException( "Unrecognized branch node operator: "
149                 + bnode.getOperator() );
150         }
151     }
152 }