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.core.partition.impl.btree;
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.AbstractCursor;
27  import org.apache.directory.api.ldap.model.cursor.ClosureMonitor;
28  import org.apache.directory.api.ldap.model.cursor.Cursor;
29  import org.apache.directory.api.ldap.model.cursor.CursorException;
30  import org.apache.directory.api.ldap.model.entry.Entry;
31  import org.apache.directory.api.ldap.model.exception.LdapException;
32  import org.apache.directory.api.ldap.model.filter.ExprNode;
33  import org.apache.directory.server.xdbm.IndexEntry;
34  import org.apache.directory.server.xdbm.search.Evaluator;
35  import org.apache.directory.server.xdbm.search.PartitionSearchResult;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * Adapts index cursors to return just Entry objects.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class EntryCursorAdaptor extends AbstractCursor<Entry>
46  {
47      /** A dedicated log for cursors */
48      private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
49  
50      /** Speedup for logs */
51      private static final boolean IS_DEBUG = LOG_CURSOR.isDebugEnabled();
52  
53      private final Cursor<IndexEntry<String, String>> indexCursor;
54      private final Evaluator<? extends ExprNode> evaluator;
55  
56  
57      public EntryCursorAdaptor( AbstractBTreePartition db, PartitionSearchResult searchResult )
58      {
59          if ( IS_DEBUG )
60          {
61              LOG_CURSOR.debug( "Creating EntryCursorAdaptor {}", this );
62          }
63  
64          indexCursor = searchResult.getResultSet();
65          evaluator = searchResult.getEvaluator();
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void after( Entry element ) throws LdapException, CursorException, IOException
73      {
74          throw new UnsupportedOperationException();
75      }
76  
77  
78      /**
79       * {@inheritDoc}
80       */
81      public void afterLast() throws LdapException, CursorException, IOException
82      {
83          this.indexCursor.afterLast();
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      public boolean available()
91      {
92          return indexCursor.available();
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void before( Entry element ) throws LdapException, CursorException, IOException
100     {
101         throw new UnsupportedOperationException();
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     public void beforeFirst() throws LdapException, CursorException, IOException
109     {
110         indexCursor.beforeFirst();
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public final void setClosureMonitor( ClosureMonitor monitor )
118     {
119         indexCursor.setClosureMonitor( monitor );
120     }
121 
122 
123     /**
124      * {@inheritDoc}}
125      */
126     public void close()
127     {
128         if ( IS_DEBUG )
129         {
130             LOG_CURSOR.debug( "Closing EntryCursorAdaptor {}", this );
131         }
132 
133         indexCursor.close();
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     public void close( Exception cause )
141     {
142         if ( IS_DEBUG )
143         {
144             LOG_CURSOR.debug( "Closing EntryCursorAdaptor {}", this );
145         }
146 
147         indexCursor.close( cause );
148     }
149 
150 
151     /**
152      * {@inheritDoc}
153      */
154     public boolean first() throws LdapException, CursorException, IOException
155     {
156         return indexCursor.first();
157     }
158 
159 
160     /**
161      * {@inheritDoc}
162      */
163     public Entry get() throws CursorException, IOException
164     {
165         IndexEntry<String, String> indexEntry = indexCursor.get();
166 
167         try
168         {
169             if ( evaluator.evaluate( indexEntry ) )
170             {
171                 Entry entry = indexEntry.getEntry();
172                 indexEntry.setEntry( null );
173 
174                 return entry;
175             }
176             else
177             {
178                 indexEntry.setEntry( null );
179             }
180 
181             return null;
182         }
183         catch ( Exception e )
184         {
185             throw new CursorException( e.getMessage(), e );
186         }
187     }
188 
189 
190     /**
191      * {@inheritDoc}
192      */
193     public boolean isClosed()
194     {
195         return indexCursor.isClosed();
196     }
197 
198 
199     /**
200      * {@inheritDoc}
201      */
202     public boolean last() throws LdapException, CursorException, IOException
203     {
204         return indexCursor.last();
205     }
206 
207 
208     /**
209      * {@inheritDoc}
210      */
211     public boolean next() throws LdapException, CursorException, IOException
212     {
213         return indexCursor.next();
214     }
215 
216 
217     /**
218      * {@inheritDoc}
219      */
220     public boolean previous() throws LdapException, CursorException, IOException
221     {
222         return indexCursor.previous();
223     }
224 
225 
226     /**
227      * @see Object#toString()
228      */
229     public String toString( String tabs )
230     {
231         StringBuilder sb = new StringBuilder();
232 
233         sb.append( tabs ).append( "EntryCursorAdaptor\n" );
234 
235         if ( indexCursor != null )
236         {
237             sb.append( tabs ).append( "    " ).append( "IndexCursor : \n" );
238             sb.append( indexCursor.toString( tabs + "        " ) );
239         }
240 
241         if ( evaluator != null )
242         {
243             sb.append( tabs ).append( "    " ).append( "Evaluator : \n" );
244             sb.append( evaluator.toString( tabs + "        " ) );
245         }
246 
247         return sb.toString();
248     }
249 
250 
251     /**
252      * @see Object#toString()
253      */
254     public String toString()
255     {
256         return toString( "" );
257     }
258 }