1 17 package org.apache.ldap.server.db; 18 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.NoSuchElementException ; 25 26 27 34 public class IndexAssertionEnumeration 35 implements NamingEnumeration 36 { 37 38 private final IndexRecord prefetched = new IndexRecord(); 39 40 private final IndexRecord candidate = new IndexRecord(); 41 42 private final NamingEnumeration underlying; 43 44 private final Map candidates; 45 46 private final IndexAssertion assertion; 47 48 private final boolean checkDups; 49 50 private boolean hasMore = true; 51 52 53 57 58 65 public IndexAssertionEnumeration( NamingEnumeration underlying, 66 IndexAssertion assertion ) throws NamingException 67 { 68 this.underlying = underlying; 69 candidates = null; 70 this.assertion = assertion; 71 checkDups = false; 72 prefetch(); 73 } 74 75 76 84 public IndexAssertionEnumeration( NamingEnumeration underlying, 85 IndexAssertion assertion, boolean enableDupCheck ) 86 throws NamingException 87 { 88 this.underlying = underlying; 89 candidates = new HashMap (); 90 this.assertion = assertion; 91 checkDups = enableDupCheck; 92 prefetch(); 93 } 94 95 96 100 101 104 public Object nextElement() 105 { 106 try 107 { 108 return next(); 109 } 110 catch ( NamingException e ) 111 { 112 throw new NoSuchElementException (); 113 } 114 } 115 116 117 120 public boolean hasMoreElements() 121 { 122 return hasMore; 123 } 124 125 126 130 131 134 public Object next() throws NamingException 135 { 136 candidate.copy( prefetched ); 137 prefetch(); 138 return candidate; 139 } 140 141 142 145 public boolean hasMore() 146 { 147 return hasMore; 148 } 149 150 151 154 public void close() throws NamingException 155 { 156 hasMore = false; 157 underlying.close(); 158 } 159 160 161 165 166 171 private void prefetch() throws NamingException 172 { 173 IndexRecord rec = null; 174 175 179 while ( underlying.hasMore() ) 180 { 181 rec = ( IndexRecord ) underlying.next(); 182 183 if ( assertion.assertCandidate( rec ) ) 185 { 186 if ( checkDups ) 187 { 188 boolean dup = candidates.containsKey( rec.getEntryId() ); 189 190 if ( dup ) 191 { 192 196 continue; 197 } 198 else 199 { 200 206 prefetched.copy( rec ); 207 candidates.put( rec.getEntryId(), rec.getEntryId() ); 208 return; 209 } 210 } 211 212 prefetched.copy( rec ); 213 return; 214 } 215 } 216 217 close(); 219 } 220 } 221 | Popular Tags |