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.AndNode;
24 import org.apache.directory.shared.ldap.filter.ExprNode;
25 import org.apache.directory.server.xdbm.IndexEntry;
26 import org.apache.directory.server.xdbm.search.Evaluator;
27 import org.apache.directory.server.core.entry.ServerEntry;
28
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Comparator;
33
34
35 /**
36 * An Evaluator for logical conjunction (AND) expressions.
37 *
38 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39 * @version $$Rev$$
40 */
41 public class AndEvaluator implements Evaluator<AndNode, ServerEntry>
42 {
43 private final List<Evaluator<? extends ExprNode,ServerEntry>> evaluators;
44
45 private final AndNode node;
46
47
48 public AndEvaluator( AndNode node, List<Evaluator<? extends ExprNode, ServerEntry>> evaluators )
49 {
50 this.node = node;
51 this.evaluators = optimize( evaluators );
52 }
53
54
55 /**
56 * Takes a set of Evaluators and copies then sorts them in a new list with
57 * increasing scan counts on their expression nodes. This is done to have
58 * the Evaluators with the least scan count which have the highest
59 * probability of rejecting a candidate first. That will increase the
60 * chance of shorting the checks on evaluators early so extra lookups and
61 * comparisons are avoided.
62 *
63 * @param unoptimized the unoptimized list of Evaluators
64 * @return optimized Evaluator list with increasing scan count ordering
65 */
66 private List<Evaluator<? extends ExprNode,ServerEntry>>
67 optimize( List<Evaluator<? extends ExprNode,ServerEntry>> unoptimized )
68 {
69 List<Evaluator<? extends ExprNode,ServerEntry>> optimized =
70 new ArrayList<Evaluator<? extends ExprNode,ServerEntry>>( unoptimized.size() );
71 optimized.addAll( unoptimized );
72 Collections.sort( optimized, new Comparator<Evaluator<?,ServerEntry>>()
73 {
74 public int compare( Evaluator<?, ServerEntry> e1, Evaluator<?, ServerEntry> e2 )
75 {
76 long scanCount1 = ( Long ) e1.getExpression().get( "count" );
77 long scanCount2 = ( Long ) e2.getExpression().get( "count" );
78
79 if ( scanCount1 == scanCount2 )
80 {
81 return 0;
82 }
83
84 /*
85 * We want the Evaluator with the smallest scan count first
86 * since this node has the highest probability of failing, or
87 * rather the least probability of succeeding. That way we
88 * can short the sub-expression evaluation process.
89 */
90 if ( scanCount1 < scanCount2 )
91 {
92 return -1;
93 }
94
95 return 1;
96 }
97 });
98
99 return optimized;
100 }
101
102
103 public boolean evaluate( Long id ) throws Exception
104 {
105 for ( Evaluator<?,ServerEntry> evaluator : evaluators )
106 {
107 if ( ! evaluator.evaluate( id ) )
108 {
109 return false;
110 }
111 }
112
113 return true;
114 }
115
116
117 public boolean evaluate( ServerEntry entry ) throws Exception
118 {
119 for ( Evaluator<?,ServerEntry> evaluator : evaluators )
120 {
121 if ( ! evaluator.evaluate( entry ) )
122 {
123 return false;
124 }
125 }
126
127 return true;
128 }
129
130
131 public boolean evaluate( IndexEntry<?, ServerEntry> indexEntry ) throws Exception
132 {
133 for ( Evaluator<?,ServerEntry> evaluator : evaluators )
134 {
135 if ( ! evaluator.evaluate( indexEntry ) )
136 {
137 return false;
138 }
139 }
140
141 return true;
142 }
143
144
145 public AndNode getExpression()
146 {
147 return node;
148 }
149 }