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 org.apache.directory.shared.ldap.NotImplementedException;
24 import org.apache.directory.server.xdbm.ForwardIndexEntry;
25 import org.apache.directory.server.xdbm.IndexEntry;
26 import org.apache.directory.server.xdbm.Tuple;
27
28 import java.util.NoSuchElementException;
29 import java.util.regex.Pattern;
30
31 import javax.naming.NamingEnumeration;
32 import javax.naming.NamingException;
33
34
35
36
37
38
39
40
41 public class IndexEnumeration<T> implements NamingEnumeration<IndexEntry>
42 {
43
44 private final Pattern re;
45
46 private final ForwardIndexEntry tmp = new ForwardIndexEntry();
47
48 private final ForwardIndexEntry returned = new ForwardIndexEntry();
49
50 private final ForwardIndexEntry prefetched = new ForwardIndexEntry();
51
52 private final boolean swapKeyVal;
53
54 private final NamingEnumeration<Tuple> underlying;
55
56
57 private boolean hasMore = true;
58
59
60
61
62
63
64
65 public IndexEnumeration( NamingEnumeration<Tuple> list ) throws NamingException
66 {
67 this( list, false, null );
68 }
69
70
71 public IndexEnumeration( NamingEnumeration<Tuple> list, boolean swapKeyVal ) throws NamingException
72 {
73 this( list, swapKeyVal, null );
74 }
75
76
77 public IndexEnumeration( NamingEnumeration<Tuple> list, boolean swapKeyVal, Pattern regex )
78 throws NamingException
79 {
80 re = regex;
81 underlying = list;
82 this.swapKeyVal = swapKeyVal;
83
84 if ( !underlying.hasMore() )
85 {
86 hasMore = false;
87 return;
88 }
89
90 prefetch();
91 }
92
93
94
95
96
97
98
99
100
101 public IndexEntry next() throws NamingException
102 {
103 returned.copy( prefetched );
104 prefetch();
105 return returned;
106 }
107
108
109
110
111
112 public IndexEntry nextElement()
113 {
114 try
115 {
116 return next();
117 }
118 catch ( NamingException ne )
119 {
120 throw new NoSuchElementException();
121 }
122 }
123
124
125
126
127
128 public boolean hasMore()
129 {
130 return hasMore;
131 }
132
133
134
135
136
137 public boolean hasMoreElements()
138 {
139 return hasMore;
140 }
141
142
143
144
145
146 public void close() throws NamingException
147 {
148 hasMore = false;
149 underlying.close();
150 }
151
152
153
154
155
156
157
158 private void prefetch() throws NamingException
159 {
160 while ( underlying.hasMore() )
161 {
162 Tuple tuple = underlying.next();
163
164 if ( swapKeyVal )
165 {
166 throw new NotImplementedException();
167
168 }
169 else
170 {
171 tmp.setTuple( tuple, null );
172 }
173
174
175
176
177 if ( null == re || re.matcher( ( String ) tmp.getValue() ).matches() )
178 {
179 prefetched.copy( tmp );
180 return;
181 }
182 }
183
184
185 hasMore = false;
186 }
187 }