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.ArrayList;
024 import java.util.HashSet;
025 import java.util.List;
026 import java.util.Set;
027
028 import org.apache.directory.server.core.entry.ServerEntry;
029 import org.apache.directory.server.xdbm.IndexEntry;
030 import org.apache.directory.server.xdbm.AbstractIndexCursor;
031 import org.apache.directory.server.xdbm.IndexCursor;
032 import org.apache.directory.server.xdbm.search.Evaluator;
033 import org.apache.directory.shared.ldap.cursor.Cursor;
034 import org.apache.directory.shared.ldap.cursor.InvalidCursorPositionException;
035 import org.apache.directory.shared.ldap.filter.ExprNode;
036
037
038
039 /**
040 * A Cursor returning candidates satisfying a logical disjunction expression.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 * @version $Rev$
044 */
045 public class OrCursor<V> extends AbstractIndexCursor<V, ServerEntry>
046 {
047 private static final String UNSUPPORTED_MSG =
048 "OrCursors are not ordered and do not support positioning by element.";
049 private final List<IndexCursor<V,ServerEntry>> cursors;
050 private final List<Evaluator<? extends ExprNode,ServerEntry>> evaluators;
051 private final List<Set<Long>> blacklists;
052 private int cursorIndex = -1;
053 private boolean available = false;
054
055
056 // TODO - do same evaluator fail fast optimization that we do in AndCursor
057 public OrCursor( List<IndexCursor<V, ServerEntry>> cursors, List<Evaluator<? extends ExprNode,ServerEntry>> evaluators )
058 {
059 if ( cursors.size() <= 1 )
060 {
061 throw new IllegalArgumentException(
062 "Must have 2 or more sub-expression Cursors for a disjunction" );
063 }
064
065 this.cursors = cursors;
066 this.evaluators = evaluators;
067 this.blacklists = new ArrayList<Set<Long>>();
068
069 for ( int ii = 0; ii < cursors.size(); ii++ )
070 {
071 this.blacklists.add( new HashSet<Long>() );
072 }
073 this.cursorIndex = 0;
074 }
075
076
077 public boolean available()
078 {
079 return available;
080 }
081
082
083 public void before( IndexEntry<V, ServerEntry> element ) throws Exception
084 {
085 throw new UnsupportedOperationException( UNSUPPORTED_MSG );
086 }
087
088
089 public void after( IndexEntry<V, ServerEntry> element ) throws Exception
090 {
091 throw new UnsupportedOperationException( UNSUPPORTED_MSG );
092 }
093
094
095 public void beforeValue( Long id, V value ) throws Exception
096 {
097 throw new UnsupportedOperationException( UNSUPPORTED_MSG );
098 }
099
100
101 public void afterValue( Long id, V value ) throws Exception
102 {
103 throw new UnsupportedOperationException( UNSUPPORTED_MSG );
104 }
105
106
107 public void beforeFirst() throws Exception
108 {
109 checkNotClosed( "beforeFirst()" );
110 cursorIndex = 0;
111 cursors.get( cursorIndex ).beforeFirst();
112 available = false;
113 }
114
115
116 public void afterLast() throws Exception
117 {
118 checkNotClosed( "afterLast()" );
119 cursorIndex = cursors.size() - 1;
120 cursors.get( cursorIndex ).afterLast();
121 available = false;
122 }
123
124
125 public boolean first() throws Exception
126 {
127 beforeFirst();
128 return available = next();
129 }
130
131
132 public boolean last() throws Exception
133 {
134 afterLast();
135 return available = previous();
136 }
137
138
139 private boolean isBlackListed( Long id )
140 {
141 return blacklists.get( cursorIndex ).contains( id );
142 }
143
144
145 /**
146 * The first sub-expression Cursor to advance to an entry adds the entry
147 * to the blacklists of other Cursors that might return that entry.
148 *
149 * @param indexEntry the index entry to blacklist
150 * @throws Exception if there are problems accessing underlying db
151 */
152 private void blackListIfDuplicate( IndexEntry<?, ServerEntry> indexEntry ) throws Exception
153 {
154 for ( int ii = 0; ii < evaluators.size(); ii++ )
155 {
156 if ( ii == cursorIndex )
157 {
158 continue;
159 }
160
161 if ( evaluators.get( ii ).evaluate( indexEntry ) )
162 {
163 blacklists.get( ii ).add( indexEntry.getId() );
164 }
165 }
166 }
167
168
169 public boolean previous() throws Exception
170 {
171 while ( cursors.get( cursorIndex ).previous() )
172 {
173 checkNotClosed( "previous()" );
174 IndexEntry<?,ServerEntry> candidate = cursors.get( cursorIndex ).get();
175 if ( ! isBlackListed( candidate.getId() ) )
176 {
177 blackListIfDuplicate( candidate );
178 return available = true;
179 }
180 }
181
182 while ( cursorIndex > 0 )
183 {
184 checkNotClosed( "previous()" );
185 cursorIndex--;
186 cursors.get( cursorIndex ).afterLast();
187
188 while ( cursors.get( cursorIndex ).previous() )
189 {
190 checkNotClosed( "previous()" );
191 IndexEntry<?,ServerEntry> candidate = cursors.get( cursorIndex ).get();
192 if ( ! isBlackListed( candidate.getId() ) )
193 {
194 blackListIfDuplicate( candidate );
195 return available = true;
196 }
197 }
198 }
199
200 return available = false;
201 }
202
203
204 public boolean next() throws Exception
205 {
206 while ( cursors.get( cursorIndex ).next() )
207 {
208 checkNotClosed( "next()" );
209 IndexEntry<?,ServerEntry> candidate = cursors.get( cursorIndex ).get();
210 if ( ! isBlackListed( candidate.getId() ) )
211 {
212 blackListIfDuplicate( candidate );
213 return available = true;
214 }
215 }
216
217 while ( cursorIndex < cursors.size() - 1 )
218 {
219 checkNotClosed( "previous()" );
220 cursorIndex++;
221 cursors.get( cursorIndex ).beforeFirst();
222
223 while ( cursors.get( cursorIndex ).next() )
224 {
225 checkNotClosed( "previous()" );
226 IndexEntry<?,ServerEntry> candidate = cursors.get( cursorIndex ).get();
227 if ( ! isBlackListed( candidate.getId() ) )
228 {
229 blackListIfDuplicate( candidate );
230 return available = true;
231 }
232 }
233 }
234
235 return available = false;
236 }
237
238
239 public IndexEntry<V, ServerEntry> get() throws Exception
240 {
241 checkNotClosed( "get()" );
242 if ( available )
243 {
244 return cursors.get( cursorIndex ).get();
245 }
246
247 throw new InvalidCursorPositionException( "Cursor has not been positioned yet." );
248 }
249
250
251 public boolean isElementReused()
252 {
253 return cursors.get( cursorIndex ).isElementReused();
254 }
255
256
257 public void close() throws Exception
258 {
259 super.close();
260 for ( Cursor<?> cursor : cursors )
261 {
262 cursor.close();
263 }
264 }
265 }