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