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.evaluator;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Entry;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.filter.ScopeNode;
26  import org.apache.directory.server.core.api.partition.PartitionTxn;
27  import org.apache.directory.server.i18n.I18n;
28  import org.apache.directory.server.xdbm.IndexEntry;
29  import org.apache.directory.server.xdbm.Store;
30  import org.apache.directory.server.xdbm.search.Evaluator;
31  
32  
33  /**
34   * Evaluates base level scope assertions on candidates using an entry database.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class BaseLevelScopeEvaluator<E> implements Evaluator<ScopeNode>
39  {
40      /** The ScopeNode containing initial search scope constraints */
41      private final ScopeNode node;
42  
43      /** The entry identifier of the scope base */
44      private final String baseId;
45  
46      /** True if the scope requires alias dereferencing while searching */
47      private final boolean dereferencing;
48  
49      /** the entry db storing entries */
50      private final Store db;
51  
52  
53      /**
54       * Creates a one level scope node Evaluator for search expressions.
55       *
56       * @param db the database used to evaluate scope node
57       * @param node the scope node
58       */
59      public BaseLevelScopeEvaluator( Store db, ScopeNode node )
60      {
61          this.node = node;
62  
63          this.db = db;
64          baseId = node.getBaseId();
65          dereferencing = node.getDerefAliases().isDerefInSearching() || node.getDerefAliases().isDerefAlways();
66      }
67  
68  
69      /**
70       * Asserts whether or not a candidate has one level scope while taking
71       * alias dereferencing into account.
72       *
73       * TODO - terribly inefficient - would benefit from exposing the id of an
74       * entry within the Entry
75       *
76       * {@inheritDoc}
77       */
78      public boolean evaluate( Entry candidate ) throws LdapException
79      {
80          throw new UnsupportedOperationException( I18n.err( I18n.ERR_721 ) );
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public boolean evaluate( PartitionTxn partitionTxn, IndexEntry<?, String> indexEntry ) throws LdapException
89      {
90          Entry entry = indexEntry.getEntry();
91  
92          // Fetch the entry
93          if ( null == entry )
94          {
95              entry = db.fetch( partitionTxn, indexEntry.getId() );
96  
97              if ( null == entry )
98              {
99                  // The entry is not anymore present : get out
100                 return false;
101             }
102 
103             indexEntry.setEntry( entry );
104         }
105 
106         return true;
107     }
108 
109 
110     public ScopeNode getExpression()
111     {
112         return node;
113     }
114 
115 
116     /**
117      * Gets the id of the search base associated with the ScopeNode expression.
118      *
119      * @return identifier of the search base
120      */
121     public String getBaseId()
122     {
123         return baseId;
124     }
125 
126 
127     /**
128      * Gets whether or not dereferencing is enabled for this evaluator.
129      *
130      * @return true if dereferencing is enabled, false otherwise
131      */
132     public boolean isDereferencing()
133     {
134         return dereferencing;
135     }
136 
137 
138     /**
139      * @see Object#toString()
140      */
141     public String toString( String tabs )
142     {
143         StringBuilder sb = new StringBuilder();
144 
145         sb.append( tabs ).append( "BaseLevelScopEvaluator : " ).append( node ).append( "\n" );
146 
147         return sb.toString();
148     }
149 
150 
151     /**
152      * @see Object#toString()
153      */
154     public String toString()
155     {
156         return toString( "" );
157     }
158 }