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.PresenceNode;
24  import org.apache.directory.shared.ldap.schema.AttributeType;
25  import org.apache.directory.server.xdbm.IndexEntry;
26  import org.apache.directory.server.xdbm.Store;
27  import org.apache.directory.server.xdbm.Index;
28  import org.apache.directory.server.xdbm.search.Evaluator;
29  import org.apache.directory.server.schema.registries.Registries;
30  import org.apache.directory.server.core.entry.ServerEntry;
31  import org.apache.directory.server.core.entry.ServerAttribute;
32  
33  import java.util.Iterator;
34  
35  
36  /**
37   * An Evaluator which determines if candidates are matched by GreaterEqNode
38   * assertions.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev$
42   */
43  public class PresenceEvaluator implements Evaluator<PresenceNode, ServerEntry>
44  {
45      private final PresenceNode node;
46      private final Store<ServerEntry> db;
47      private final Registries registries;
48      private final AttributeType type;
49      private final Index<String,ServerEntry> idx;
50  
51  
52      public PresenceEvaluator( PresenceNode node, Store<ServerEntry> db, Registries registries )
53          throws Exception
54      {
55          this.db = db;
56          this.node = node;
57          this.registries = registries;
58          this.type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
59  
60          if ( db.hasUserIndexOn( node.getAttribute() ) )
61          {
62              idx = db.getPresenceIndex();
63          }
64          else
65          {
66              idx = null;
67          }
68      }
69  
70  
71      public PresenceNode getExpression()
72      {
73          return node;
74      }
75  
76  
77      public AttributeType getAttributeType()
78      {
79          return type;
80      }
81  
82  
83      // TODO - determine if comaparator and index entry should have the Value
84      // wrapper or the raw normalized value
85      public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
86      {
87          if ( idx != null )
88          {
89              return idx.forward( type.getOid(), indexEntry.getId() );
90          }
91  
92          ServerEntry entry = indexEntry.getObject();
93  
94          // resuscitate the entry if it has not been and set entry in IndexEntry
95          if ( null == entry )
96          {
97              entry = db.lookup( indexEntry.getId() );
98              indexEntry.setObject( entry );
99          }
100 
101         return evaluate( entry );
102     }
103 
104 
105     // TODO - determine if comaparator and index entry should have the Value
106     // wrapper or the raw normalized value
107     public boolean evaluate( Long id ) throws Exception
108     {
109         if ( idx != null )
110         {
111             return idx.forward( type.getOid(), id );
112         }
113 
114         return evaluate( db.lookup( id ) );
115     }
116 
117 
118     // TODO - determine if comaparator and index entry should have the Value
119     // wrapper or the raw normalized value
120     public boolean evaluate( ServerEntry entry ) throws Exception
121     {
122         // get the attribute
123         ServerAttribute attr = ( ServerAttribute ) entry.get( type );
124 
125         // if the attribute exists just return true
126         if ( attr != null )
127         {
128             return true;
129         }
130 
131         // If we do not have the attribute, loop through the sub classes of
132         // the attributeType.  Perhaps the entry has an attribute value of a
133         // subtype (descendant) that will produce a match
134         if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
135         {
136             // TODO check to see if descendant handling is necessary for the
137             // index so we can match properly even when for example a name
138             // attribute is used instead of more specific commonName
139             Iterator<AttributeType> descendants =
140                 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
141 
142             do
143             {
144                 AttributeType descendant = descendants.next();
145 
146                 attr = ( ServerAttribute ) entry.get( descendant );
147 
148                 if ( attr != null )
149                 {
150                     return true;
151                 }
152             }
153             while ( descendants.hasNext() );
154         }
155 
156         // we fell through so a match was not found - assertion was false.
157         return false;
158     }
159 }