1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.ldap.server.partition.impl.btree;
28
29
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32
33 import org.apache.ldap.common.filter.ExprNode;
34 import org.apache.ldap.common.filter.SubstringNode;
35 import org.apache.ldap.common.schema.AttributeType;
36 import org.apache.ldap.common.schema.Normalizer;
37 import org.apache.ldap.server.schema.AttributeTypeRegistry;
38 import org.apache.regexp.RE;
39 import org.apache.regexp.RESyntaxException;
40
41
42 /***
43 * Enumerator that creates a NamingEnumeration over the set of candidates that
44 * satisfy a substring filter expression.
45 *
46 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47 * @version $Rev: 264732 $
48 */
49 public class SubstringEnumerator implements Enumerator
50 {
51 /*** Database used */
52 private final BTreeContextPartition db;
53 /*** Evaluator used is an Avalon dependent object */
54 private final SubstringEvaluator evaluator;
55 /*** the attribute type registry */
56 private final AttributeTypeRegistry attributeTypeRegistry;
57
58
59 /***
60 * Creates a SubstringEnumerator for a database.
61 *
62 * @param db the database
63 * @param evaluator a substring evaluator
64 */
65 public SubstringEnumerator( BTreeContextPartition db,
66 AttributeTypeRegistry attributeTypeRegistry,
67 SubstringEvaluator evaluator )
68 {
69 this.db = db;
70 this.evaluator = evaluator;
71 this.attributeTypeRegistry = attributeTypeRegistry;
72 }
73
74
75
76
77
78
79
80 /***
81 * @see Enumerator#enumerate(
82 * org.apache.ldap.common.filter.ExprNode)
83 */
84 public NamingEnumeration enumerate( final ExprNode node )
85 throws NamingException
86 {
87 RE regex = null;
88 Index idx = null;
89 final SubstringNode snode = ( SubstringNode ) node;
90 AttributeType type = attributeTypeRegistry.lookup( snode.getAttribute() );
91 Normalizer normalizer = type.getSubstr().getNormalizer();
92
93 if ( db.hasUserIndexOn( snode.getAttribute() ) )
94 {
95
96
97
98
99 try
100 {
101 regex = snode.getRegex( normalizer );
102 }
103 catch ( RESyntaxException e )
104 {
105 NamingException ne = new NamingException( "SubstringNode '"
106 + node + "' had incorrect syntax" );
107 ne.setRootCause( e );
108 throw ne;
109 }
110
111
112
113
114
115
116 idx = db.getUserIndex( snode.getAttribute() );
117 if ( null == snode.getInitial() )
118 {
119 return idx.listIndices( regex );
120 }
121 else
122 {
123 return idx.listIndices( regex, snode.getInitial() );
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136 NamingEnumeration underlying = db.getNdnIndex().listIndices();
137 IndexAssertion assertion = new IndexAssertion()
138 {
139 public boolean assertCandidate( final IndexRecord record ) throws NamingException
140 {
141 return evaluator.evaluate( node, record );
142 }
143 };
144
145 return new IndexAssertionEnumeration( underlying, assertion );
146 }
147 }