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.AbstractIndexCursor;
024    import org.apache.directory.server.xdbm.ForwardIndexEntry;
025    import org.apache.directory.server.xdbm.IndexCursor;
026    import org.apache.directory.server.xdbm.IndexEntry;
027    import org.apache.directory.server.xdbm.Store;
028    import org.apache.directory.shared.ldap.entry.ServerEntry;
029    
030    
031    /**
032     * A Cursor over all entries in a partition which returns IndexEntries.
033     *
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class AllEntriesCursor<ID> extends AbstractIndexCursor<ID, ServerEntry, ID>
038    {
039        private IndexEntry<ID, ServerEntry, ID> indexEntry = new ForwardIndexEntry<ID, ServerEntry, ID>();
040        private final IndexCursor<String, ServerEntry, ID> wrapped;
041    
042    
043        public AllEntriesCursor( Store<ServerEntry, ID> db ) throws Exception
044        {
045            // Get a reverse cursor because we want to sort by ID
046            wrapped = db.getNdnIndex().reverseCursor();
047        }
048    
049    
050        /* 
051         * @see org.apache.directory.server.xdbm.IndexCursor#afterValue(Long, Object)
052         */
053        public void afterValue( ID key, ID value ) throws Exception
054        {
055            checkNotClosed( "afterValue()" );
056            wrapped.afterValue( key, null );
057        }
058    
059    
060        /* 
061         * @see org.apache.directory.server.xdbm.IndexCursor#beforeValue(java.lang.Long, java.lang.Object)
062         */
063        public void beforeValue( ID id, ID value ) throws Exception
064        {
065            checkNotClosed( "beforeValue()" );
066            wrapped.beforeValue( id, null );
067        }
068    
069    
070        /* 
071         * @see org.apache.directory.server.core.cursor.Cursor#after(java.lang.Object)
072         */
073        public void after( IndexEntry<ID, ServerEntry, ID> indexEntry ) throws Exception
074        {
075            checkNotClosed( "after()" );
076            wrapped.afterValue( indexEntry.getId(), null );
077        }
078    
079    
080        /* 
081         * @see org.apache.directory.server.core.cursor.Cursor#afterLast()
082         */
083        public void afterLast() throws Exception
084        {
085            checkNotClosed( "afterLast()" );
086            wrapped.afterLast();
087        }
088    
089    
090        /* 
091         * @see org.apache.directory.server.core.cursor.Cursor#available()
092         */
093        public boolean available()
094        {
095            return wrapped.available();
096        }
097    
098    
099        /* 
100         * @see org.apache.directory.server.core.cursor.Cursor#before(java.lang.Object)
101         */
102        public void before( IndexEntry<ID, ServerEntry, ID> indexEntry ) throws Exception
103        {
104            checkNotClosed( "before()" );
105            wrapped.beforeValue( indexEntry.getId(), null );
106        }
107    
108    
109        /* 
110         * @see org.apache.directory.server.core.cursor.Cursor#beforeFirst()
111         */
112        public void beforeFirst() throws Exception
113        {
114            checkNotClosed( "beforeFirst()" );
115            wrapped.beforeFirst();
116        }
117    
118    
119        /* 
120         * @see org.apache.directory.server.core.cursor.Cursor#first()
121         */
122        public boolean first() throws Exception
123        {
124            checkNotClosed( "first()" );
125            return wrapped.first();
126        }
127    
128    
129        /* 
130         * @see org.apache.directory.server.core.cursor.Cursor#get()
131         */
132        public IndexEntry<ID, ServerEntry, ID> get() throws Exception
133        {
134            checkNotClosed( "get()" );
135            IndexEntry<String, ServerEntry, ID> wrappedEntry = wrapped.get();
136            indexEntry.setId( wrappedEntry.getId() );
137            indexEntry.setValue( wrappedEntry.getId() );
138            indexEntry.setObject( wrappedEntry.getObject() );
139            return indexEntry;
140        }
141    
142    
143        /* 
144         * @see org.apache.directory.server.core.cursor.Cursor#isElementReused()
145         */
146        public boolean isElementReused()
147        {
148            return true;
149        }
150    
151    
152        /* 
153         * @see org.apache.directory.server.core.cursor.Cursor#last()
154         */
155        public boolean last() throws Exception
156        {
157            checkNotClosed( "last()" );
158            return wrapped.last();
159        }
160    
161    
162        /* 
163         * @see org.apache.directory.server.core.cursor.Cursor#next()
164         */
165        public boolean next() throws Exception
166        {
167            checkNotClosed( "next()" );
168            return wrapped.next();
169        }
170    
171    
172        /* 
173         * @see org.apache.directory.server.core.cursor.Cursor#previous()
174         */
175        public boolean previous() throws Exception
176        {
177            checkNotClosed( "previous()" );
178            return wrapped.previous();
179        }
180    }