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 java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31  
32  import com.google.protobuf.InvalidProtocolBufferException;
33  
34  /**
35   * This filter is used to filter based on the key. It takes an operator
36   * (equal, greater, not equal, etc) and a byte [] comparator for the row,
37   * and column qualifier portions of a key.
38   * <p>
39   * This filter can be wrapped with {@link WhileMatchFilter} to add more control.
40   * <p>
41   * Multiple filters can be combined using {@link FilterList}.
42   * <p>
43   * If an already known row range needs to be scanned, use 
44   * {@link org.apache.hadoop.hbase.CellScanner} start
45   * and stop rows directly rather than a filter.
46   */
47  @InterfaceAudience.Public
48  @InterfaceStability.Stable
49  public class RowFilter extends CompareFilter {
50  
51    private boolean filterOutRow = false;
52  
53    /**
54     * Constructor.
55     * @param rowCompareOp the compare op for row matching
56     * @param rowComparator the comparator for row matching
57     */
58    public RowFilter(final CompareOp rowCompareOp,
59        final ByteArrayComparable rowComparator) {
60      super(rowCompareOp, rowComparator);
61    }
62  
63    @Override
64    public void reset() {
65      this.filterOutRow = false;
66    }
67  
68    @Override
69    public ReturnCode filterKeyValue(Cell v) {
70      if(this.filterOutRow) {
71        return ReturnCode.NEXT_ROW;
72      }
73      return ReturnCode.INCLUDE;
74    }
75  
76    @Override
77    public boolean filterRowKey(byte[] data, int offset, int length) {
78      if(doCompare(this.compareOp, this.comparator, data, offset, length)) {
79        this.filterOutRow = true;
80      }
81      return this.filterOutRow;
82    }
83  
84    @Override
85    public boolean filterRow() {
86      return this.filterOutRow;
87    }
88  
89    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
90      @SuppressWarnings("rawtypes") // for arguments
91      ArrayList arguments = CompareFilter.extractArguments(filterArguments);
92      CompareOp compareOp = (CompareOp)arguments.get(0);
93      ByteArrayComparable comparator = (ByteArrayComparable)arguments.get(1);
94      return new RowFilter(compareOp, comparator);
95    }
96  
97   /**
98    * @return The filter serialized using pb
99    */
100   public byte [] toByteArray() {
101     FilterProtos.RowFilter.Builder builder =
102       FilterProtos.RowFilter.newBuilder();
103     builder.setCompareFilter(super.convert());
104     return builder.build().toByteArray();
105   }
106 
107   /**
108    * @param pbBytes A pb serialized {@link RowFilter} instance
109    * @return An instance of {@link RowFilter} made from <code>bytes</code>
110    * @throws DeserializationException
111    * @see #toByteArray
112    */
113   public static RowFilter parseFrom(final byte [] pbBytes)
114   throws DeserializationException {
115     FilterProtos.RowFilter proto;
116     try {
117       proto = FilterProtos.RowFilter.parseFrom(pbBytes);
118     } catch (InvalidProtocolBufferException e) {
119       throw new DeserializationException(e);
120     }
121     final CompareOp valueCompareOp =
122       CompareOp.valueOf(proto.getCompareFilter().getCompareOp().name());
123     ByteArrayComparable valueComparator = null;
124     try {
125       if (proto.getCompareFilter().hasComparator()) {
126         valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
127       }
128     } catch (IOException ioe) {
129       throw new DeserializationException(ioe);
130     }
131     return new RowFilter(valueCompareOp,valueComparator);
132   }
133 
134   /**
135    * @param other
136    * @return true if and only if the fields of the filter that are serialized
137    * are equal to the corresponding fields in other.  Used for testing.
138    */
139   boolean areSerializedFieldsEqual(Filter o) {
140     if (o == this) return true;
141     if (!(o instanceof RowFilter)) return false;
142 
143     return super.areSerializedFieldsEqual(o);
144   }
145 }