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