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 org.apache.directory.server.xdbm.IndexEntry;
024    import org.apache.directory.server.xdbm.Store;
025    import org.apache.directory.server.xdbm.AbstractIndexCursor;
026    import org.apache.directory.server.xdbm.IndexCursor;
027    import org.apache.directory.server.core.entry.ServerEntry;
028    import org.apache.directory.shared.ldap.cursor.InvalidCursorPositionException;
029    import org.apache.directory.shared.ldap.schema.AttributeType;
030    
031    
032    /**
033     * A returning candidates satisfying an attribute presence expression.
034     *
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $$Rev$$
037     */
038    public class PresenceCursor extends AbstractIndexCursor<String, ServerEntry>
039    {
040        private static final String UNSUPPORTED_MSG =
041            "PresenceCursors do not support positioning by element without a user index on the presence attribute.";
042        private final IndexCursor<String,ServerEntry> ndnCursor;
043        private final IndexCursor<String,ServerEntry> presenceCursor;
044        private final PresenceEvaluator presenceEvaluator;
045        private boolean available = false;
046    
047    
048        public PresenceCursor( Store<ServerEntry> db, PresenceEvaluator presenceEvaluator ) throws Exception
049        {
050            this.presenceEvaluator = presenceEvaluator;
051            AttributeType type = presenceEvaluator.getAttributeType();
052    
053            if ( db.hasUserIndexOn( type.getOid() ) )
054            {
055                presenceCursor = db.getPresenceIndex().forwardCursor( type.getOid() );
056                ndnCursor = null;
057            }
058            else
059            {
060                presenceCursor = null;
061                ndnCursor = db.getNdnIndex().forwardCursor();
062            }
063        }
064    
065    
066        public boolean available()
067        {
068            if ( presenceCursor != null )
069            {
070                return presenceCursor.available();
071            }
072    
073            return available;
074        }
075    
076    
077        public void beforeValue( Long id, String value ) throws Exception
078        {
079            checkNotClosed( "beforeValue()" );
080            if ( presenceCursor != null )
081            {
082                presenceCursor.beforeValue( id, value );
083                return;
084            }
085    
086            throw new UnsupportedOperationException( UNSUPPORTED_MSG );
087        }
088    
089    
090        public void before( IndexEntry<String, ServerEntry> element ) throws Exception
091        {
092            checkNotClosed( "before()" );
093            if ( presenceCursor != null )
094            {
095                presenceCursor.before( element );
096                return;
097            }
098    
099            throw new UnsupportedOperationException( UNSUPPORTED_MSG );
100        }
101    
102    
103        public void afterValue( Long id, String value ) throws Exception
104        {
105            checkNotClosed( "afterValue()" );
106            if ( presenceCursor != null )
107            {
108                presenceCursor.afterValue( id, value );
109                return;
110            }
111    
112            throw new UnsupportedOperationException( UNSUPPORTED_MSG );
113        }
114    
115    
116        public void after( IndexEntry<String, ServerEntry> element ) throws Exception
117        {
118            checkNotClosed( "after()" );
119            if ( presenceCursor != null )
120            {
121                presenceCursor.after( element );
122                return;
123            }
124    
125            throw new UnsupportedOperationException( UNSUPPORTED_MSG );
126        }
127    
128    
129        public void beforeFirst() throws Exception
130        {
131            checkNotClosed( "beforeFirst()" );
132            if ( presenceCursor != null )
133            {
134                presenceCursor.beforeFirst();
135                return;
136            }
137    
138            ndnCursor.beforeFirst();
139            available = false;
140        }
141    
142    
143        public void afterLast() throws Exception
144        {
145            checkNotClosed( "afterLast()" );
146            if ( presenceCursor != null )
147            {
148                presenceCursor.afterLast();
149                return;
150            }
151    
152            ndnCursor.afterLast();
153            available = false;
154        }
155    
156    
157        public boolean first() throws Exception
158        {
159            checkNotClosed( "first()" );
160            if ( presenceCursor != null )
161            {
162                return presenceCursor.first();
163            }
164    
165            beforeFirst();
166            return next();
167        }
168    
169    
170        public boolean last() throws Exception
171        {
172            checkNotClosed( "last()" );
173            if ( presenceCursor != null )
174            {
175                return presenceCursor.last();
176            }
177    
178            afterLast();
179            return previous();
180        }
181    
182    
183        public boolean previous() throws Exception
184        {
185            checkNotClosed( "previous()" );
186            if ( presenceCursor != null )
187            {
188                return presenceCursor.previous();
189            }
190    
191            while ( ndnCursor.previous() )
192            {
193                checkNotClosed( "previous()" );
194                IndexEntry<?,ServerEntry> candidate = ndnCursor.get();
195                if ( presenceEvaluator.evaluate( candidate ) )
196                {
197                    return available = true;
198                }
199            }
200            
201            return available = false;
202        }
203    
204    
205        public boolean next() throws Exception
206        {
207            checkNotClosed( "next()" );
208            if ( presenceCursor != null )
209            {
210                return presenceCursor.next();
211            }
212    
213            while ( ndnCursor.next() )
214            {
215                checkNotClosed( "next()" );
216                IndexEntry<?,ServerEntry> candidate = ndnCursor.get();
217                if ( presenceEvaluator.evaluate( candidate ) )
218                {
219                    return available = true;
220                }
221            }
222    
223            return available = false;
224        }
225    
226    
227        public IndexEntry<String, ServerEntry> get() throws Exception
228        {
229            checkNotClosed( "get()" );
230            if ( presenceCursor != null )
231            {
232                if ( presenceCursor.available() )
233                {
234                    return presenceCursor.get();
235                }
236    
237                throw new InvalidCursorPositionException( "Cursor has not been positioned yet." );
238            }
239    
240            if ( available )
241            {
242                /*
243                 * The value of NDN indices is the normalized dn and we want the
244                 * value to be the value of the attribute in question.  So we will
245                 * set that accordingly here.
246                 */
247                IndexEntry<String, ServerEntry> indexEntry = ndnCursor.get();
248                indexEntry.setValue( presenceEvaluator.getAttributeType().getOid() );
249                return indexEntry;
250            }
251    
252            throw new InvalidCursorPositionException( "Cursor has not been positioned yet." );
253        }
254    
255    
256        public boolean isElementReused()
257        {
258            if ( presenceCursor != null )
259            {
260                return presenceCursor.isElementReused();
261            }
262    
263            return ndnCursor.isElementReused();
264        }
265    
266    
267        public void close() throws Exception
268        {
269            super.close();
270    
271            if ( presenceCursor != null )
272            {
273                presenceCursor.close();
274            }
275            else
276            {
277                ndnCursor.close();
278            }
279        }
280    }