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.Objects;
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   * A wrapper filter that filters an entire row if any of the Cell checks do
36   * not pass.
37   * <p>
38   * For example, if all columns in a row represent weights of different things,
39   * with the values being the actual weights, and we want to filter out the
40   * entire row if any of its weights are zero.  In this case, we want to prevent
41   * rows from being emitted if a single key is filtered.  Combine this filter
42   * with a {@link ValueFilter}:
43   * </p>
44   * <p>
45   * <code>
46   * scan.setFilter(new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL,
47   *     new BinaryComparator(Bytes.toBytes(0))));
48   * </code>
49   * Any row which contained a column whose value was 0 will be filtered out
50   * (since ValueFilter will not pass that Cell).
51   * Without this filter, the other non-zero valued columns in the row would still
52   * be emitted.
53   * </p>
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Stable
57  public class SkipFilter extends FilterBase {
58    private boolean filterRow = false;
59    private Filter filter;
60  
61    public SkipFilter(Filter filter) {
62      this.filter = filter;
63    }
64  
65    public Filter getFilter() {
66      return filter;
67    }
68  
69    @Override
70    public void reset() throws IOException {
71      filter.reset();
72      filterRow = false;
73    }
74  
75    private void changeFR(boolean value) {
76      filterRow = filterRow || value;
77    }
78  
79    @Override
80    public ReturnCode filterKeyValue(Cell v) throws IOException {
81      ReturnCode c = filter.filterKeyValue(v);
82      changeFR(c != ReturnCode.INCLUDE);
83      return c;
84    }
85  
86    @Override
87    public Cell transformCell(Cell v) throws IOException {
88      return filter.transformCell(v);
89    }
90  
91    @Override
92    public boolean filterRow() {
93      return filterRow;
94    }
95      
96    @Override
97    public boolean hasFilterRow() {
98      return true;
99    }
100 
101   /**
102    * @return The filter serialized using pb
103    */
104   @Override
105   public byte[] toByteArray() throws IOException {
106     FilterProtos.SkipFilter.Builder builder =
107       FilterProtos.SkipFilter.newBuilder();
108     builder.setFilter(ProtobufUtil.toFilter(this.filter));
109     return builder.build().toByteArray();
110   }
111 
112   /**
113    * @param pbBytes A pb serialized {@link SkipFilter} instance
114    * @return An instance of {@link SkipFilter} made from <code>bytes</code>
115    * @throws DeserializationException
116    * @see #toByteArray
117    */
118   public static SkipFilter parseFrom(final byte [] pbBytes)
119   throws DeserializationException {
120     FilterProtos.SkipFilter proto;
121     try {
122       proto = FilterProtos.SkipFilter.parseFrom(pbBytes);
123     } catch (InvalidProtocolBufferException e) {
124       throw new DeserializationException(e);
125     }
126     try {
127       return new SkipFilter(ProtobufUtil.toFilter(proto.getFilter()));
128     } catch (IOException ioe) {
129       throw new DeserializationException(ioe);
130     }
131   }
132 
133   /**
134    * @param other
135    * @return true if and only if the fields of the filter that are serialized
136    * are equal to the corresponding fields in other.  Used for testing.
137    */
138   @Override
139   boolean areSerializedFieldsEqual(Filter o) {
140     if (o == this) return true;
141     if (!(o instanceof SkipFilter)) return false;
142 
143     SkipFilter other = (SkipFilter)o;
144     return getFilter().areSerializedFieldsEqual(other.getFilter());
145   }
146 
147   @Override
148   public boolean isFamilyEssential(byte[] name) throws IOException {
149     return filter.isFamilyEssential(name);
150   }
151 
152   @Override
153   public String toString() {
154     return this.getClass().getSimpleName() + " " + this.filter.toString();
155   }
156 
157   @Override
158   public boolean equals(Object obj) {
159     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
160   }
161 
162   @Override
163   public int hashCode() {
164     return Objects.hash(this.filter);
165   }
166 }