View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.xdbm.search.cursor;
21  
22  
23  import java.io.IOException;
24  
25  import org.apache.directory.api.ldap.model.constants.Loggers;
26  import org.apache.directory.api.ldap.model.cursor.Cursor;
27  import org.apache.directory.api.ldap.model.cursor.CursorException;
28  import org.apache.directory.api.ldap.model.exception.LdapException;
29  import org.apache.directory.server.core.api.partition.PartitionTxn;
30  import org.apache.directory.server.core.partition.impl.btree.IndexCursorAdaptor;
31  import org.apache.directory.server.xdbm.AbstractIndexCursor;
32  import org.apache.directory.server.xdbm.IndexEntry;
33  import org.apache.directory.server.xdbm.Store;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * A Cursor over all entries in a partition which returns IndexEntries.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class AllEntriesCursor extends AbstractIndexCursor<String>
44  {
45      /** A dedicated log for cursors */
46      private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
47  
48      /** Speedup for logs */
49      private static final boolean IS_DEBUG = LOG_CURSOR.isDebugEnabled();
50  
51      /** The index entry we use to return entries one by one.  */
52      private IndexEntry<String, String> indexEntry = new IndexEntry<>();
53  
54      /** The cursor on the MsterTable index */
55      private final Cursor<IndexEntry<String, String>> wrapped;
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      protected String getUnsupportedMessage()
62      {
63          return UNSUPPORTED_MSG;
64      }
65  
66  
67      /**
68       * Creates a new instance of AllEntriesCursor
69       * 
70       * @param partitionTxn The transaction to use
71       * @param store The Store instance
72       * @throws LdapException If we weren't able to create an instance of AllEntriesCursor
73       */
74      public AllEntriesCursor( PartitionTxn partitionTxn, Store store ) throws LdapException
75      {
76          if ( IS_DEBUG )
77          {
78              LOG_CURSOR.debug( "Creating AllEntriesCursor {}", this );
79          }
80          
81          this.partitionTxn = partitionTxn;
82  
83          // Uses the MasterTable 
84          wrapped = new IndexCursorAdaptor( partitionTxn, store.getMasterTable().cursor(), true );
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public void after( IndexEntry<String, String> indexEntry ) throws LdapException, CursorException
93      {
94          checkNotClosed();
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     public void afterLast() throws LdapException, CursorException
102     {
103         checkNotClosed();
104 
105         wrapped.afterLast();
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public boolean available()
114     {
115         return wrapped.available();
116     }
117 
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void before( IndexEntry<String, String> indexEntry ) throws LdapException, CursorException
124     {
125         checkNotClosed();
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     public void beforeFirst() throws LdapException, CursorException
133     {
134         checkNotClosed();
135 
136         wrapped.beforeFirst();
137     }
138 
139 
140     /**
141      * {@inheritDoc}
142      */
143     public boolean first() throws LdapException, CursorException
144     {
145         checkNotClosed();
146 
147         return wrapped.first();
148     }
149 
150 
151     /**
152      * {@inheritDoc}
153      */
154     public IndexEntry<String, String> get() throws CursorException
155     {
156         checkNotClosed();
157 
158         // Create the returned IndexEntry, copying what we get from the wrapped cursor
159         // As we are using the MasterTable, we have to use the key as the 
160         // ID and value
161         IndexEntry<?, String> wrappedEntry = wrapped.get();
162         indexEntry.setId( ( String ) wrappedEntry.getKey() );
163         indexEntry.setKey( ( String ) wrappedEntry.getKey() );
164         indexEntry.setEntry( null );
165 
166         return indexEntry;
167     }
168 
169 
170     /**
171      * {@inheritDoc}
172      */
173     public boolean last() throws LdapException, CursorException
174     {
175         checkNotClosed();
176 
177         return wrapped.last();
178     }
179 
180 
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public boolean next() throws LdapException, CursorException
186     {
187         checkNotClosed();
188 
189         return wrapped.next();
190     }
191 
192 
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     public boolean previous() throws LdapException, CursorException
198     {
199         checkNotClosed();
200 
201         return wrapped.previous();
202     }
203 
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public void close() throws IOException
210     {
211         if ( IS_DEBUG )
212         {
213             LOG_CURSOR.debug( "Closing AllEntriesCursor {}", this );
214         }
215 
216         wrapped.close();
217     }
218 
219 
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     public void close( Exception cause ) throws IOException
225     {
226         if ( IS_DEBUG )
227         {
228             LOG_CURSOR.debug( "Closing AllEntriesCursor {}", this );
229         }
230 
231         wrapped.close( cause );
232     }
233 
234 
235     /**
236      * @see Object#toString()
237      */
238     @Override
239     public String toString( String tabs )
240     {
241         StringBuilder sb = new StringBuilder();
242 
243         sb.append( tabs ).append( "AllEntriesCursor (" );
244 
245         if ( available() )
246         {
247             sb.append( "available)" );
248         }
249         else
250         {
251             sb.append( "absent)" );
252         }
253 
254         sb.append( " :\n" );
255 
256         sb.append( wrapped.toString( tabs + "    " ) );
257 
258         return sb.toString();
259     }
260 
261 
262     /**
263      * @see Object#toString()
264      */
265     public String toString()
266     {
267         return toString( "" );
268     }
269 }