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