1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45 public class EntryCursorAdaptor extends AbstractCursor<Entry>
46 {
47
48 private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
49
50
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
71
72 public void after( Entry element ) throws LdapException, CursorException, IOException
73 {
74 throw new UnsupportedOperationException();
75 }
76
77
78
79
80
81 public void afterLast() throws LdapException, CursorException, IOException
82 {
83 this.indexCursor.afterLast();
84 }
85
86
87
88
89
90 public boolean available()
91 {
92 return indexCursor.available();
93 }
94
95
96
97
98
99 public void before( Entry element ) throws LdapException, CursorException, IOException
100 {
101 throw new UnsupportedOperationException();
102 }
103
104
105
106
107
108 public void beforeFirst() throws LdapException, CursorException, IOException
109 {
110 indexCursor.beforeFirst();
111 }
112
113
114
115
116
117 public final void setClosureMonitor( ClosureMonitor monitor )
118 {
119 indexCursor.setClosureMonitor( monitor );
120 }
121
122
123
124
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
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
153
154 public boolean first() throws LdapException, CursorException, IOException
155 {
156 return indexCursor.first();
157 }
158
159
160
161
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
192
193 public boolean isClosed()
194 {
195 return indexCursor.isClosed();
196 }
197
198
199
200
201
202 public boolean last() throws LdapException, CursorException, IOException
203 {
204 return indexCursor.last();
205 }
206
207
208
209
210
211 public boolean next() throws LdapException, CursorException, IOException
212 {
213 return indexCursor.next();
214 }
215
216
217
218
219
220 public boolean previous() throws LdapException, CursorException, IOException
221 {
222 return indexCursor.previous();
223 }
224
225
226
227
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
253
254 public String toString()
255 {
256 return toString( "" );
257 }
258 }