View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.xdbm.search.impl;
21  
22  
23  import org.apache.directory.shared.ldap.filter.EqualityNode;
24  import org.apache.directory.shared.ldap.schema.AttributeType;
25  import org.apache.directory.shared.ldap.schema.ByteArrayComparator;
26  import org.apache.directory.shared.ldap.schema.MatchingRule;
27  import org.apache.directory.shared.ldap.schema.NoOpNormalizer;
28  import org.apache.directory.shared.ldap.schema.Normalizer;
29  import org.apache.directory.shared.ldap.entry.Value;
30  import org.apache.directory.server.xdbm.IndexEntry;
31  import org.apache.directory.server.xdbm.Store;
32  import org.apache.directory.server.xdbm.Index;
33  import org.apache.directory.server.xdbm.search.Evaluator;
34  import org.apache.directory.server.schema.registries.Registries;
35  import org.apache.directory.server.core.entry.ServerEntry;
36  import org.apache.directory.server.core.entry.ServerAttribute;
37  
38  import java.util.Iterator;
39  import java.util.Comparator;
40  
41  
42  /**
43   * An Evaluator which determines if candidates are matched by GreaterEqNode
44   * assertions.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev$
48   */
49  public class EqualityEvaluator<T> implements Evaluator<EqualityNode<T>, ServerEntry>
50  {
51      private final EqualityNode<T> node;
52      private final Store<ServerEntry> db;
53      private final Registries registries;
54      private final AttributeType type;
55      private final Normalizer normalizer;
56      private final Comparator comparator;
57      private final Index<T,ServerEntry> idx;
58  
59  
60      public EqualityEvaluator( EqualityNode<T> node, Store<ServerEntry> db, Registries registries )
61          throws Exception
62      {
63          this.db = db;
64          this.node = node;
65          this.registries = registries;
66  
67          if ( db.hasUserIndexOn( node.getAttribute() ) )
68          {
69              //noinspection unchecked
70              idx = ( Index<T, ServerEntry> ) db.getUserIndex( node.getAttribute() );
71              type = null;
72              normalizer = null;
73              comparator = null;
74          }
75          else
76          {
77              idx = null;
78              type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
79  
80              MatchingRule mr = type.getEquality();
81  
82              if ( mr == null )
83              {
84                  normalizer = NoOpNormalizer.INSTANCE;
85                  comparator = ByteArrayComparator.INSTANCE;
86              }
87              else
88              {
89                  normalizer = mr.getNormalizer();
90                  comparator = mr.getComparator();
91              }
92          }
93      }
94  
95  
96      public EqualityNode<T> getExpression()
97      {
98          return node;
99      }
100 
101 
102     public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
103     {
104         if ( idx != null )
105         {
106             return idx.forward( node.getValue().get(), indexEntry.getId() );
107         }
108 
109         ServerEntry entry = indexEntry.getObject();
110 
111         // resuscitate the entry if it has not been and set entry in IndexEntry
112         if ( null == entry )
113         {
114             entry = db.lookup( indexEntry.getId() );
115             indexEntry.setObject( entry );
116         }
117 
118         return evaluate( entry );
119     }
120 
121 
122     public boolean evaluate( ServerEntry entry ) throws Exception
123     {
124         // get the attribute
125         ServerAttribute attr = ( ServerAttribute ) entry.get( type );
126 
127         // if the attribute does not exist just return false
128         if ( attr != null && evaluate( attr ) )
129         {
130             return true;
131         }
132 
133         // If we do not have the attribute, loop through the sub classes of
134         // the attributeType.  Perhaps the entry has an attribute value of a
135         // subtype (descendant) that will produce a match
136         if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
137         {
138             // TODO check to see if descendant handling is necessary for the
139             // index so we can match properly even when for example a name
140             // attribute is used instead of more specific commonName
141             Iterator<AttributeType> descendants =
142                 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
143 
144             while ( descendants.hasNext() )
145             {
146                 AttributeType descendant = descendants.next();
147 
148                 attr = ( ServerAttribute ) entry.get( descendant );
149 
150                 if ( attr != null && evaluate( attr ) )
151                 {
152                     return true;
153                 }
154             }
155         }
156 
157         // we fell through so a match was not found - assertion was false.
158         return false;
159     }
160 
161 
162     public boolean evaluate( Long id ) throws Exception
163     {
164         if ( idx != null )
165         {
166             return idx.reverse( id );
167         }
168 
169         return evaluate ( db.lookup( id ) );
170     }
171 
172 
173     // TODO - determine if comaparator and index entry should have the Value
174     // wrapper or the raw normalized value
175     private boolean evaluate( ServerAttribute attribute ) throws Exception
176     {
177         /*
178          * Cycle through the attribute values testing normalized version
179          * obtained from using the ordering or equality matching rule's
180          * normalizer.  The test uses the comparator obtained from the
181          * appropriate matching rule to perform the check.
182          */
183         for ( Value value : attribute )
184         {
185             value.normalize( normalizer );
186 
187             //noinspection unchecked
188             if ( comparator.compare( value.getNormalizedValue(), node.getValue().getNormalizedValue() ) == 0 )
189             {
190                 return true;
191             }
192         }
193 
194         return false;
195     }
196 }