1 17 package org.apache.ldap.server.db; 18 19 20 import org.apache.ldap.common.filter.ExprNode; 21 import org.apache.ldap.common.filter.ScopeNode; 22 import org.apache.ldap.common.util.SingletonEnumeration; 23 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.SearchControls ; 27 import java.math.BigInteger ; 28 29 30 36 public class ScopeEnumerator implements Enumerator 37 { 38 39 private Database db = null; 40 41 private ScopeEvaluator evaluator = null; 42 43 44 public ScopeEnumerator( Database db, ScopeEvaluator evaluator ) 45 { 46 this.db = db; 47 this.evaluator = evaluator; 48 } 49 50 51 60 public NamingEnumeration enumerate( ExprNode node ) throws NamingException 61 { 62 final ScopeNode snode = ( ScopeNode ) node; 63 final BigInteger id = db.getEntryId( snode.getBaseDn() ); 64 65 switch( snode.getScope() ) 66 { 67 case( SearchControls.OBJECT_SCOPE ): 68 final IndexRecord record = new IndexRecord(); 69 record.setEntryId( id ); 70 record.setIndexKey( snode.getBaseDn() ); 71 return new SingletonEnumeration( record ); 72 case( SearchControls.ONELEVEL_SCOPE ): 73 return enumerateChildren( snode.getBaseDn(), 74 snode.getDerefAliases().derefInSearching() ); 75 case( SearchControls.SUBTREE_SCOPE ): 76 return enumerateDescendants( snode ); 77 default: 78 throw new NamingException ( "Unrecognized search scope!" ); 79 } 80 } 81 82 83 94 private NamingEnumeration enumerateChildren( String dn, boolean deref ) 95 throws NamingException 96 { 97 Index idx = db.getHierarchyIndex(); 98 final BigInteger id = db.getEntryId( dn ); 99 final NamingEnumeration children = idx.listIndices( id ); 100 101 105 if ( ! deref ) 106 { 107 return children; 108 } 109 110 120 121 idx = db.getOneAliasIndex(); 123 NamingEnumeration aliasIntroduced = idx.listIndices( id ); 124 125 NamingEnumeration nonAliasChildren = new IndexAssertionEnumeration( 127 children, new AssertNotAlias() ); 128 129 NamingEnumeration [] all = {nonAliasChildren, aliasIntroduced}; 131 return new DisjunctionEnumeration( all ); 132 } 133 134 135 145 private NamingEnumeration enumerateDescendants( final ScopeNode node ) 146 throws NamingException 147 { 148 Index idx = null; 149 150 154 if ( ! node.getDerefAliases().derefInSearching() ) 155 { 156 idx = db.getNdnIndex(); 158 NamingEnumeration underlying = idx.listIndices(); 159 return new IndexAssertionEnumeration( underlying, 160 new AssertDescendant( node ) ); 161 } 162 163 IndexAssertion assertion = new IndexAssertion() 165 { 166 public boolean assertCandidate( IndexRecord rec ) 167 throws NamingException 168 { 169 return evaluator.evaluate( node, rec ); 170 } 171 }; 172 173 idx = db.getNdnIndex(); 175 NamingEnumeration underlying = idx.listIndices(); 176 return new IndexAssertionEnumeration( underlying, assertion ); 177 } 178 179 180 183 class AssertDescendant implements IndexAssertion 184 { 185 186 private final ScopeNode scope; 187 188 189 194 AssertDescendant( final ScopeNode node ) 195 { 196 scope = node; 197 } 198 199 200 206 public boolean assertCandidate( IndexRecord record ) throws NamingException 207 { 208 String dn = db.getEntryDn( record.getEntryId() ); 209 return dn.endsWith( scope.getBaseDn() ); 210 } 211 } 212 213 214 217 class AssertNotAlias implements IndexAssertion 218 { 219 224 public boolean assertCandidate( IndexRecord record ) throws NamingException 225 { 226 Index aliasIdx = db.getAliasIndex(); 227 228 if ( null == aliasIdx.reverseLookup( record.getEntryId() ) ) 229 { 230 return true; 231 } 232 233 return false; 234 } 235 } 236 } 237 | Popular Tags |