001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.xdbm.search.impl;
021
022
023 import org.apache.directory.shared.ldap.filter.ApproximateNode;
024 import org.apache.directory.shared.ldap.schema.AttributeType;
025 import org.apache.directory.shared.ldap.schema.MatchingRule;
026 import org.apache.directory.shared.ldap.schema.Normalizer;
027 import org.apache.directory.shared.ldap.entry.Value;
028 import org.apache.directory.server.xdbm.IndexEntry;
029 import org.apache.directory.server.xdbm.Store;
030 import org.apache.directory.server.xdbm.Index;
031 import org.apache.directory.server.xdbm.search.Evaluator;
032 import org.apache.directory.server.schema.registries.Registries;
033 import org.apache.directory.server.core.entry.ServerEntry;
034 import org.apache.directory.server.core.entry.ServerAttribute;
035
036 import java.util.Iterator;
037 import java.util.Comparator;
038
039
040 /**
041 * An Evaluator which determines if candidates are matched by ApproximateNode
042 * assertions. Same as equality for now.
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 * @version $Rev$
046 */
047 public class ApproximateEvaluator implements Evaluator<ApproximateNode, ServerEntry>
048 {
049 private final ApproximateNode<Object> node;
050 private final Store<ServerEntry> db;
051 private final Registries registries;
052 private final AttributeType type;
053 private final Normalizer normalizer;
054 private final Comparator comparator;
055 private final Index<Object,ServerEntry> idx;
056
057
058 public ApproximateEvaluator( ApproximateNode node, Store<ServerEntry> db, Registries registries )
059 throws Exception
060 {
061 this.db = db;
062 this.node = node;
063 this.registries = registries;
064
065 if ( db.hasUserIndexOn( node.getAttribute() ) )
066 {
067 //noinspection unchecked
068 idx = ( Index<Object,ServerEntry> ) db.getUserIndex( node.getAttribute() );
069 type = null;
070 normalizer = null;
071 comparator = null;
072 }
073 else
074 {
075 idx = null;
076 type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
077
078 MatchingRule mr = type.getEquality();
079
080 if ( mr == null )
081 {
082 throw new IllegalStateException(
083 "Could not find matchingRule to use for EqualityNode evaluation: " + node );
084 }
085
086 normalizer = mr.getNormalizer();
087 comparator = mr.getComparator();
088 }
089 }
090
091
092 public ApproximateNode getExpression()
093 {
094 return node;
095 }
096
097
098 public boolean evaluate( ServerEntry entry ) throws Exception
099 {
100 // get the attribute
101 ServerAttribute attr = ( ServerAttribute ) entry.get( type );
102
103 // if the attribute does not exist just return false
104 if ( attr != null && evaluate( attr ) )
105 {
106 return true;
107 }
108
109 // If we do not have the attribute, loop through the sub classes of
110 // the attributeType. Perhaps the entry has an attribute value of a
111 // subtype (descendant) that will produce a match
112 if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
113 {
114 // TODO check to see if descendant handling is necessary for the
115 // index so we can match properly even when for example a name
116 // attribute is used instead of more specific commonName
117 Iterator<AttributeType> descendants =
118 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
119
120 while ( descendants.hasNext() )
121 {
122 AttributeType descendant = descendants.next();
123
124 attr = ( ServerAttribute ) entry.get( descendant );
125
126 if ( attr != null && evaluate( attr ) )
127 {
128 return true;
129 }
130 }
131 }
132
133 // we fell through so a match was not found - assertion was false.
134 return false;
135 }
136
137
138 public boolean evaluate( Long id ) throws Exception
139 {
140 if ( idx != null )
141 {
142 return idx.reverse( id );
143 }
144
145 return evaluate( db.lookup( id ) );
146 }
147
148
149 public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
150 {
151 if ( idx != null )
152 {
153 return idx.forward( node.getValue().get(), indexEntry.getId() );
154 }
155
156 ServerEntry entry = indexEntry.getObject();
157
158 // resuscitate the entry if it has not been and set entry in IndexEntry
159 if ( null == entry )
160 {
161 entry = db.lookup( indexEntry.getId() );
162 indexEntry.setObject( entry );
163 }
164
165 return evaluate( entry );
166 }
167
168
169 // TODO - determine if comaparator and index entry should have the Value
170 // wrapper or the raw normalized value
171 private boolean evaluate( ServerAttribute attribute ) throws Exception
172 {
173 /*
174 * Cycle through the attribute values testing normalized version
175 * obtained from using the ordering or equality matching rule's
176 * normalizer. The test uses the comparator obtained from the
177 * appropriate matching rule to perform the check.
178 */
179
180 for ( Value value : attribute )
181 {
182 value.normalize( normalizer );
183
184 //noinspection unchecked
185 if ( comparator.compare( value.getNormalizedValue(), node.getValue().getNormalizedValue() ) == 0 )
186 {
187 return true;
188 }
189 }
190
191 return false;
192 }
193 }