View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.filter;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.io.HbaseObjectWritable;
25  
26  import java.io.DataInput;
27  import java.io.DataOutput;
28  import java.io.IOException;
29  import java.util.Arrays;
30  
31  /**
32   * This is a generic filter to be used to filter by comparison.  It takes an
33   * operator (equal, greater, not equal, etc) and a byte [] comparator.
34   * <p>
35   * To filter by row key, use {@link RowFilter}.
36   * <p>
37   * To filter by column qualifier, use {@link QualifierFilter}.
38   * <p>
39   * To filter by value, use {@link SingleColumnValueFilter}.
40   * <p>
41   * These filters can be wrapped with {@link SkipFilter} and {@link WhileMatchFilter}
42   * to add more control.
43   * <p>
44   * Multiple filters can be combined using {@link FilterList}.
45   */
46  public abstract class CompareFilter extends FilterBase {
47  
48    /** Comparison operators. */
49    public enum CompareOp {
50      /** less than */
51      LESS,
52      /** less than or equal to */
53      LESS_OR_EQUAL,
54      /** equals */
55      EQUAL,
56      /** not equal */
57      NOT_EQUAL,
58      /** greater than or equal to */
59      GREATER_OR_EQUAL,
60      /** greater than */
61      GREATER,
62      /** no operation */
63      NO_OP,
64    }
65  
66    protected CompareOp compareOp;
67    protected WritableByteArrayComparable comparator;
68  
69    /**
70     * Writable constructor, do not use.
71     */
72    public CompareFilter() {
73    }
74  
75    /**
76     * Constructor.
77     * @param compareOp the compare op for row matching
78     * @param comparator the comparator for row matching
79     */
80    public CompareFilter(final CompareOp compareOp,
81        final WritableByteArrayComparable comparator) {
82      this.compareOp = compareOp;
83      this.comparator = comparator;
84    }
85  
86    /**
87     * @return operator
88     */
89    public CompareOp getOperator() {
90      return compareOp;
91    }
92  
93    /**
94     * @return the comparator
95     */
96    public WritableByteArrayComparable getComparator() {
97      return comparator;
98    }
99  
100   protected boolean doCompare(final CompareOp compareOp,
101       final WritableByteArrayComparable comparator, final byte [] data,
102       final int offset, final int length) {
103       if (compareOp == CompareOp.NO_OP) {
104 	  return true;
105       }
106     int compareResult =
107       comparator.compareTo(Arrays.copyOfRange(data, offset,
108         offset + length));
109     switch (compareOp) {
110       case LESS:
111         return compareResult <= 0;
112       case LESS_OR_EQUAL:
113         return compareResult < 0;
114       case EQUAL:
115         return compareResult != 0;
116       case NOT_EQUAL:
117         return compareResult == 0;
118       case GREATER_OR_EQUAL:
119         return compareResult > 0;
120       case GREATER:
121         return compareResult >= 0;
122       default:
123         throw new RuntimeException("Unknown Compare op " +
124           compareOp.name());
125     }
126   }
127 
128   public void readFields(DataInput in) throws IOException {
129     compareOp = CompareOp.valueOf(in.readUTF());
130     comparator = (WritableByteArrayComparable)
131       HbaseObjectWritable.readObject(in, null);
132   }
133 
134   public void write(DataOutput out) throws IOException {
135     out.writeUTF(compareOp.name());
136     HbaseObjectWritable.writeObject(out, comparator,
137       WritableByteArrayComparable.class, null);
138   }
139 }