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