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 */ 020package org.apache.directory.server.xdbm.search.impl; 021 022 023import java.util.Comparator; 024 025import org.apache.directory.server.xdbm.search.Evaluator; 026 027 028/** 029 * A helper class used to compare scan counts. 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 */ 033public class ScanCountComparator implements Comparator<Evaluator<?>> 034{ 035 /** 036 * Compare two scan counts frpm two evaluators 037 */ 038 public int compare( Evaluator<?> e1, Evaluator<?> e2 ) 039 { 040 Object count1 = e1.getExpression().get( DefaultOptimizer.COUNT_ANNOTATION ); 041 Object count2 = e2.getExpression().get( DefaultOptimizer.COUNT_ANNOTATION ); 042 long scanCount1 = Long.MAX_VALUE; 043 long scanCount2 = Long.MAX_VALUE; 044 045 if ( count1 != null ) 046 { 047 scanCount1 = ( Long ) e1.getExpression().get( DefaultOptimizer.COUNT_ANNOTATION ); 048 } 049 050 if ( count2 != null ) 051 { 052 scanCount2 = ( Long ) e2.getExpression().get( DefaultOptimizer.COUNT_ANNOTATION ); 053 } 054 055 if ( scanCount1 == scanCount2 ) 056 { 057 return 0; 058 } 059 060 /* 061 * We want the Evaluator with the smallest scan count first 062 * since this node has the highest probability of failing, or 063 * rather the least probability of succeeding. That way we 064 * can short the sub-expression evaluation process. 065 */ 066 if ( scanCount1 < scanCount2 ) 067 { 068 return -1; 069 } 070 071 return 1; 072 } 073}