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 java.util.List;
024 import java.util.ArrayList;
025
026 import org.apache.directory.server.xdbm.Store;
027 import org.apache.directory.server.xdbm.IndexCursor;
028 import org.apache.directory.server.xdbm.search.Evaluator;
029 import org.apache.directory.server.core.entry.ServerEntry;
030 import org.apache.directory.shared.ldap.NotImplementedException;
031 import org.apache.directory.shared.ldap.filter.*;
032
033
034 /**
035 * Builds Cursors over candidates that satisfy a filter expression.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev: 659774 $
039 */
040 public class CursorBuilder
041 {
042 /** The database used by this builder */
043 private Store<ServerEntry> db = null;
044
045 /** Evaluator dependency on a EvaluatorBuilder */
046 private EvaluatorBuilder evaluatorBuilder;
047
048
049 /**
050 * Creates an expression tree enumerator.
051 *
052 * @param db database used by this enumerator
053 * @param evaluatorBuilder the evaluator builder
054 */
055 public CursorBuilder( Store<ServerEntry> db, EvaluatorBuilder evaluatorBuilder )
056 {
057 this.db = db;
058 this.evaluatorBuilder = evaluatorBuilder;
059 }
060
061
062 public IndexCursor<?,ServerEntry> build( ExprNode node ) throws Exception
063 {
064 switch ( node.getAssertionType() )
065 {
066 /* ---------- LEAF NODE HANDLING ---------- */
067
068 case APPROXIMATE:
069 return new ApproximateCursor( db, ( ApproximateEvaluator ) evaluatorBuilder.build( node ) );
070 case EQUALITY:
071 return new EqualityCursor( db, ( EqualityEvaluator ) evaluatorBuilder.build( node ) );
072 case GREATEREQ:
073 return new GreaterEqCursor( db, ( GreaterEqEvaluator ) evaluatorBuilder.build( node ) );
074 case LESSEQ:
075 return new LessEqCursor( db, ( LessEqEvaluator ) evaluatorBuilder.build( node ) );
076 case PRESENCE:
077 return new PresenceCursor( db, ( PresenceEvaluator ) evaluatorBuilder.build( node ) );
078 case SCOPE:
079 if ( ( ( ScopeNode ) node ).getScope() == SearchScope.ONELEVEL )
080 {
081 return new OneLevelScopeCursor( db, ( OneLevelScopeEvaluator ) evaluatorBuilder.build( node ) );
082 }
083 else
084 {
085 return new SubtreeScopeCursor( db, ( SubtreeScopeEvaluator ) evaluatorBuilder.build( node ) );
086 }
087 case SUBSTRING:
088 return new SubstringCursor( db, ( SubstringEvaluator ) evaluatorBuilder.build( node ) );
089
090 /* ---------- LOGICAL OPERATORS ---------- */
091
092 case AND:
093 return buildAndCursor( ( AndNode ) node );
094 case NOT:
095 return new NotCursor( db, evaluatorBuilder.build( ( ( NotNode ) node).getFirstChild() ) );
096 case OR:
097 return buildOrCursor( ( OrNode ) node );
098
099 /* ---------- NOT IMPLEMENTED ---------- */
100
101 case ASSERTION:
102 case EXTENSIBLE:
103 throw new NotImplementedException();
104
105 default:
106 throw new IllegalStateException( "Unknown assertion type: " + node.getAssertionType() );
107 }
108 }
109
110
111 /**
112 * Creates a OrCursor over a disjunction expression branch node.
113 *
114 * @param node the disjunction expression branch node
115 * @return Cursor over candidates satisfying disjunction expression
116 * @throws Exception on db access failures
117 */
118 private IndexCursor<?,ServerEntry> buildOrCursor( OrNode node ) throws Exception
119 {
120 List<ExprNode> children = node.getChildren();
121 List<IndexCursor<?,ServerEntry>> childCursors = new ArrayList<IndexCursor<?,ServerEntry>>( children.size() );
122 List<Evaluator<? extends ExprNode, ServerEntry>> childEvaluators
123 = new ArrayList<Evaluator<? extends ExprNode, ServerEntry>>( children.size() );
124
125 // Recursively create Cursors and Evaluators for each child expression node
126 for ( ExprNode child : children )
127 {
128 childCursors.add( build( child ) );
129 childEvaluators.add( evaluatorBuilder.build( child ) );
130 }
131
132 //noinspection unchecked
133 return new OrCursor( childCursors, childEvaluators );
134 }
135
136
137 /**
138 * Creates an AndCursor over a conjunction expression branch node.
139 *
140 * @param node a conjunction expression branch node
141 * @return Cursor over the conjunction expression
142 * @throws Exception on db access failures
143 */
144 private IndexCursor<?,ServerEntry> buildAndCursor( AndNode node ) throws Exception
145 {
146 int minIndex = 0;
147 long minValue = Long.MAX_VALUE;
148 //noinspection UnusedAssignment
149 long value = Long.MAX_VALUE;
150
151 /*
152 * We scan the child nodes of a branch node searching for the child
153 * expression node with the smallest scan count. This is the child
154 * we will use for iteration by creating a Cursor over its expression.
155 */
156 final List<ExprNode> children = node.getChildren();
157
158 for ( int ii = 0; ii < children.size(); ii++ )
159 {
160 ExprNode child = children.get( ii );
161 Object count = child.get( "count" );
162 if( count == null )
163 {
164 continue;
165 }
166 value = ( Long ) count;
167 minValue = Math.min( minValue, value );
168
169 if ( minValue == value )
170 {
171 minIndex = ii;
172 }
173 }
174
175 // Once found we build the child Evaluators minus the one for the minChild
176 ExprNode minChild = children.get( minIndex );
177 List<Evaluator<? extends ExprNode, ServerEntry>> childEvaluators =
178 new ArrayList<Evaluator<? extends ExprNode, ServerEntry>>( children.size() - 1 );
179 for ( ExprNode child : children )
180 {
181 if ( child == minChild )
182 {
183 continue;
184 }
185
186 childEvaluators.add( evaluatorBuilder.build( child ) );
187 }
188
189 // Do recursive call to build min child Cursor then create AndCursor
190 IndexCursor<?,ServerEntry> childCursor = build( minChild );
191 return new AndCursor( childCursor, childEvaluators );
192 }
193 }