1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.db;
18
19
20 import org.apache.regexp.RE;
21
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24 import java.util.NoSuchElementException;
25
26
27 /***
28 * A NamingEnumeration over an Index which returns IndexRecords.
29 *
30 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31 * @version $Rev: 159259 $
32 */
33 public class IndexEnumeration
34 implements NamingEnumeration
35 {
36 /*** */
37 private final RE re;
38 /*** */
39 private final IndexRecord tmp = new IndexRecord();
40 /*** */
41 private final IndexRecord returned = new IndexRecord();
42 /*** */
43 private final IndexRecord prefetched = new IndexRecord();
44 /*** */
45 private final boolean swapKeyVal;
46 /*** */
47 private final NamingEnumeration underlying;
48
49 /*** */
50 private boolean hasMore = true;
51
52
53
54
55
56
57
58 /***
59 * TODO Domument me!
60 *
61 * @param list TODO
62 * @throws NamingException TODO
63 */
64 public IndexEnumeration( NamingEnumeration list ) throws NamingException
65 {
66 this( list, false, null );
67 }
68
69
70 /***
71 * TODO Domument me!
72 *
73 * @param list TODO
74 * @param swapKeyVal TODO
75 * @throws NamingException TODO
76 */
77 public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal )
78 throws NamingException
79 {
80 this( list, swapKeyVal, null );
81 }
82
83
84 /***
85 * TODO Domument me!
86 *
87 * @param list TODO
88 * @param swapKeyVal TODO
89 * @param regex TODO
90 * @throws NamingException TODO
91 */
92 public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal,
93 RE regex )
94 throws NamingException
95 {
96 re = regex;
97 underlying = list;
98 this.swapKeyVal = swapKeyVal;
99
100 if ( ! underlying.hasMore() )
101 {
102 hasMore = false;
103 return;
104 }
105
106 prefetch();
107 }
108
109
110
111
112
113
114
115 /***
116 * @see javax.naming.NamingEnumeration#next()
117 */
118 public Object next()
119 throws NamingException
120 {
121 returned.copy( prefetched );
122 prefetch();
123 return returned;
124 }
125
126
127 /***
128 * @see java.util.Enumeration#nextElement()
129 */
130 public Object nextElement()
131 {
132 try
133 {
134 return next();
135 }
136 catch ( NamingException ne )
137 {
138 throw new NoSuchElementException();
139 }
140 }
141
142
143 /***
144 * @see javax.naming.NamingEnumeration#hasMore()
145 */
146 public boolean hasMore()
147 {
148 return hasMore;
149 }
150
151
152 /***
153 * @see javax.naming.NamingEnumeration#hasMoreElements()
154 */
155 public boolean hasMoreElements()
156 {
157 return hasMore;
158 }
159
160
161 /***
162 * @see javax.naming.NamingEnumeration#close()
163 */
164 public void close() throws NamingException
165 {
166 hasMore = false;
167 underlying.close();
168 }
169
170
171
172
173
174
175
176 /***
177 * TODO Document me!
178 *
179 * @throws NamingException TODO
180 */
181 private void prefetch() throws NamingException
182 {
183 while ( underlying.hasMore() )
184 {
185 Tuple tuple = ( Tuple ) underlying.next();
186
187 if ( swapKeyVal )
188 {
189 tmp.setSwapped( tuple, null );
190 }
191 else
192 {
193 tmp.setTuple( tuple, null );
194 }
195
196
197
198
199 if ( null == re || re.match( ( String ) tmp.getIndexKey() ) )
200 {
201 prefetched.copy( tmp );
202 return;
203 }
204 }
205
206
207 hasMore = false;
208 }
209 }