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.server.xdbm.ForwardIndexEntry;
24 import org.apache.directory.server.xdbm.IndexEntry;
25
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.NoSuchElementException;
29
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32
33
34
35
36
37
38
39
40
41 public class IndexAssertionEnumeration implements NamingEnumeration<IndexEntry>
42 {
43
44 private final ForwardIndexEntry prefetched = new ForwardIndexEntry();
45
46 private final ForwardIndexEntry candidate = new ForwardIndexEntry();
47
48 private final NamingEnumeration<ForwardIndexEntry> underlying;
49
50 private final Map<Object,Object> candidates;
51
52 private final IndexAssertion assertion;
53
54 private final boolean checkDups;
55
56 private boolean hasMore = true;
57
58
59
60
61
62
63
64 public IndexAssertionEnumeration( NamingEnumeration<ForwardIndexEntry> underlying, IndexAssertion assertion )
65 throws NamingException
66 {
67 this.underlying = underlying;
68 candidates = null;
69 this.assertion = assertion;
70 checkDups = false;
71 prefetch();
72 }
73
74
75 public IndexAssertionEnumeration( NamingEnumeration<ForwardIndexEntry> underlying, IndexAssertion assertion,
76 boolean enableDupCheck ) throws NamingException
77 {
78 this.underlying = underlying;
79 candidates = new HashMap<Object,Object>();
80 this.assertion = assertion;
81 checkDups = enableDupCheck;
82 prefetch();
83 }
84
85
86
87
88
89
90
91
92
93 public IndexEntry nextElement()
94 {
95 try
96 {
97 return next();
98 }
99 catch ( NamingException e )
100 {
101 throw new NoSuchElementException();
102 }
103 }
104
105
106
107
108
109 public boolean hasMoreElements()
110 {
111 return hasMore;
112 }
113
114
115
116
117
118
119
120
121
122 public IndexEntry next() throws NamingException
123 {
124 candidate.copy( prefetched );
125 prefetch();
126 return candidate;
127 }
128
129
130
131
132
133 public boolean hasMore()
134 {
135 return hasMore;
136 }
137
138
139
140
141
142 public void close() throws NamingException
143 {
144 hasMore = false;
145 underlying.close();
146 }
147
148
149
150
151
152
153 private void prefetch() throws NamingException
154 {
155 IndexEntry rec = null;
156
157
158
159
160
161 while ( underlying.hasMore() )
162 {
163 rec = underlying.next();
164
165
166 try
167 {
168 if ( assertion.assertCandidate( rec ) )
169 {
170 if ( checkDups )
171 {
172 boolean dup = candidates.containsKey( rec.getId() );
173
174 if ( dup )
175 {
176
177
178
179
180 continue;
181 }
182 else
183 {
184
185
186
187
188
189
190 prefetched.copy( rec );
191 candidates.put( rec.getId(), rec.getId() );
192 return;
193 }
194 }
195
196 prefetched.copy( rec );
197 return;
198 }
199 }
200 catch ( Exception e )
201 {
202 NamingException ne = new NamingException();
203 ne.setRootCause( e );
204 throw ne;
205 }
206 }
207
208
209 close();
210 }
211 }