View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import com.google.common.base.Preconditions;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
27  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
28  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.CompareType;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import java.util.ArrayList;
32  /**
33   * This is a generic filter to be used to filter by comparison.  It takes an
34   * operator (equal, greater, not equal, etc) and a byte [] comparator.
35   * <p>
36   * To filter by row key, use {@link RowFilter}.
37   * <p>
38   * To filter by column qualifier, use {@link QualifierFilter}.
39   * <p>
40   * To filter by value, use {@link SingleColumnValueFilter}.
41   * <p>
42   * These filters can be wrapped with {@link SkipFilter} and {@link WhileMatchFilter}
43   * to add more control.
44   * <p>
45   * Multiple filters can be combined using {@link FilterList}.
46   */
47  @InterfaceAudience.Public
48  @InterfaceStability.Stable
49  public abstract class CompareFilter extends FilterBase {
50  
51    /** Comparison operators. */
52    public enum CompareOp {
53      /** less than */
54      LESS,
55      /** less than or equal to */
56      LESS_OR_EQUAL,
57      /** equals */
58      EQUAL,
59      /** not equal */
60      NOT_EQUAL,
61      /** greater than or equal to */
62      GREATER_OR_EQUAL,
63      /** greater than */
64      GREATER,
65      /** no operation */
66      NO_OP,
67    }
68  
69    protected CompareOp compareOp;
70    protected ByteArrayComparable comparator;
71  
72    /**
73     * Constructor.
74     * @param compareOp the compare op for row matching
75     * @param comparator the comparator for row matching
76     */
77    public CompareFilter(final CompareOp compareOp,
78        final ByteArrayComparable comparator) {
79      this.compareOp = compareOp;
80      this.comparator = comparator;
81    }
82  
83    /**
84     * @return operator
85     */
86    public CompareOp getOperator() {
87      return compareOp;
88    }
89  
90    /**
91     * @return the comparator
92     */
93    public ByteArrayComparable getComparator() {
94      return comparator;
95    }
96  
97    protected boolean doCompare(final CompareOp compareOp,
98        final ByteArrayComparable comparator, final byte [] data,
99        final int offset, final int length) {
100     if (compareOp == CompareOp.NO_OP) {
101       return true;
102     }
103     int compareResult = comparator.compareTo(data, offset, length);
104     switch (compareOp) {
105       case LESS:
106         return compareResult <= 0;
107       case LESS_OR_EQUAL:
108         return compareResult < 0;
109       case EQUAL:
110         return compareResult != 0;
111       case NOT_EQUAL:
112         return compareResult == 0;
113       case GREATER_OR_EQUAL:
114         return compareResult > 0;
115       case GREATER:
116         return compareResult >= 0;
117       default:
118         throw new RuntimeException("Unknown Compare op " +
119           compareOp.name());
120     }
121   }
122 
123   public static ArrayList extractArguments(ArrayList<byte []> filterArguments) {
124     Preconditions.checkArgument(filterArguments.size() == 2,
125                                 "Expected 2 but got: %s", filterArguments.size());
126     CompareOp compareOp = ParseFilter.createCompareOp(filterArguments.get(0));
127     ByteArrayComparable comparator = ParseFilter.createComparator(
128       ParseFilter.removeQuotesFromByteArray(filterArguments.get(1)));
129 
130     if (comparator instanceof RegexStringComparator ||
131         comparator instanceof SubstringComparator) {
132       if (compareOp != CompareOp.EQUAL &&
133           compareOp != CompareOp.NOT_EQUAL) {
134         throw new IllegalArgumentException ("A regexstring comparator and substring comparator" +
135                                             " can only be used with EQUAL and NOT_EQUAL");
136       }
137     }
138     ArrayList arguments = new ArrayList();
139     arguments.add(compareOp);
140     arguments.add(comparator);
141     return arguments;
142   }
143 
144   /**
145    * @return A pb instance to represent this instance.
146    */
147   FilterProtos.CompareFilter convert() {
148     FilterProtos.CompareFilter.Builder builder =
149       FilterProtos.CompareFilter.newBuilder();
150     HBaseProtos.CompareType compareOp = CompareType.valueOf(this.compareOp.name());
151     builder.setCompareOp(compareOp);
152     if (this.comparator != null) builder.setComparator(ProtobufUtil.toComparator(this.comparator));
153     return builder.build();
154   }
155 
156   /**
157    *
158    * @param o
159    * @return true if and only if the fields of the filter that are serialized
160    * are equal to the corresponding fields in other.  Used for testing.
161    */
162   boolean areSerializedFieldsEqual(Filter o) {
163     if (o == this) return true;
164     if (!(o instanceof CompareFilter)) return false;
165 
166     CompareFilter other = (CompareFilter)o;
167     return this.getOperator().equals(other.getOperator()) &&
168       (this.getComparator() == other.getComparator()
169         || this.getComparator().areSerializedFieldsEqual(other.getComparator()));
170   }
171 
172   @Override
173   public String toString() {
174     return String.format("%s (%s, %s)",
175         this.getClass().getSimpleName(),
176         this.compareOp.name(),
177         Bytes.toStringBinary(this.comparator.getValue()));
178   }
179 }