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.classification.InterfaceAudience;
25  import org.apache.hadoop.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   * <pre>
44   * scan.setFilter(new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL,
45   *     new BinaryComparator(Bytes.toBytes(0))));
46   * </code>
47   * Any row which contained a column whose value was 0 will be filtered out
48   * (since ValueFilter will not pass that Cell).
49   * Without this filter, the other non-zero valued columns in the row would still
50   * be emitted.
51   */
52  @InterfaceAudience.Public
53  @InterfaceStability.Stable
54  public class SkipFilter extends FilterBase {
55    private boolean filterRow = false;
56    private Filter filter;
57  
58    public SkipFilter(Filter filter) {
59      this.filter = filter;
60    }
61  
62    public Filter getFilter() {
63      return filter;
64    }
65  
66    @Override
67    public void reset() throws IOException {
68      filter.reset();
69      filterRow = false;
70    }
71  
72    private void changeFR(boolean value) {
73      filterRow = filterRow || value;
74    }
75  
76    @Override
77    public ReturnCode filterKeyValue(Cell v) throws IOException {
78      ReturnCode c = filter.filterKeyValue(v);
79      changeFR(c != ReturnCode.INCLUDE);
80      return c;
81    }
82  
83    @Override
84    public Cell transformCell(Cell v) throws IOException {
85      return filter.transformCell(v);
86    }
87  
88    public boolean filterRow() {
89      return filterRow;
90    }
91      
92    public boolean hasFilterRow() {
93      return true;
94    }
95  
96    /**
97     * @return The filter serialized using pb
98     */
99    public byte[] toByteArray() throws IOException {
100     FilterProtos.SkipFilter.Builder builder =
101       FilterProtos.SkipFilter.newBuilder();
102     builder.setFilter(ProtobufUtil.toFilter(this.filter));
103     return builder.build().toByteArray();
104   }
105 
106   /**
107    * @param pbBytes A pb serialized {@link SkipFilter} instance
108    * @return An instance of {@link SkipFilter} made from <code>bytes</code>
109    * @throws DeserializationException
110    * @see #toByteArray
111    */
112   public static SkipFilter parseFrom(final byte [] pbBytes)
113   throws DeserializationException {
114     FilterProtos.SkipFilter proto;
115     try {
116       proto = FilterProtos.SkipFilter.parseFrom(pbBytes);
117     } catch (InvalidProtocolBufferException e) {
118       throw new DeserializationException(e);
119     }
120     try {
121       return new SkipFilter(ProtobufUtil.toFilter(proto.getFilter()));
122     } catch (IOException ioe) {
123       throw new DeserializationException(ioe);
124     }
125   }
126 
127   /**
128    * @param other
129    * @return true if and only if the fields of the filter that are serialized
130    * are equal to the corresponding fields in other.  Used for testing.
131    */
132   boolean areSerializedFieldsEqual(Filter o) {
133     if (o == this) return true;
134     if (!(o instanceof SkipFilter)) return false;
135 
136     SkipFilter other = (SkipFilter)o;
137     return getFilter().areSerializedFieldsEqual(other.getFilter());
138   }
139 
140   public boolean isFamilyEssential(byte[] name) throws IOException {
141     return filter.isFamilyEssential(name);
142   }
143 
144   @Override
145   public String toString() {
146     return this.getClass().getSimpleName() + " " + this.filter.toString();
147   }
148 }