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.Cell;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
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 boolean filterRowKey(Cell cell) throws IOException {
80      // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
81      return false;
82    }
83  
84    @Override
85    public ReturnCode filterKeyValue(Cell v) throws IOException {
86      ReturnCode c = filter.filterKeyValue(v);
87      changeFR(c != ReturnCode.INCLUDE);
88      return c;
89    }
90  
91    @Override
92    public Cell transformCell(Cell v) throws IOException {
93      return filter.transformCell(v);
94    }
95  
96    public boolean filterRow() {
97      return filterRow;
98    }
99      
100   public boolean hasFilterRow() {
101     return true;
102   }
103 
104   /**
105    * @return The filter serialized using pb
106    */
107   public byte[] toByteArray() throws IOException {
108     FilterProtos.SkipFilter.Builder builder =
109       FilterProtos.SkipFilter.newBuilder();
110     builder.setFilter(ProtobufUtil.toFilter(this.filter));
111     return builder.build().toByteArray();
112   }
113 
114   /**
115    * @param pbBytes A pb serialized {@link SkipFilter} instance
116    * @return An instance of {@link SkipFilter} made from <code>bytes</code>
117    * @throws DeserializationException
118    * @see #toByteArray
119    */
120   public static SkipFilter parseFrom(final byte [] pbBytes)
121   throws DeserializationException {
122     FilterProtos.SkipFilter proto;
123     try {
124       proto = FilterProtos.SkipFilter.parseFrom(pbBytes);
125     } catch (InvalidProtocolBufferException e) {
126       throw new DeserializationException(e);
127     }
128     try {
129       return new SkipFilter(ProtobufUtil.toFilter(proto.getFilter()));
130     } catch (IOException ioe) {
131       throw new DeserializationException(ioe);
132     }
133   }
134 
135   /**
136    * @param other
137    * @return true if and only if the fields of the filter that are serialized
138    * are equal to the corresponding fields in other.  Used for testing.
139    */
140   boolean areSerializedFieldsEqual(Filter o) {
141     if (o == this) return true;
142     if (!(o instanceof SkipFilter)) return false;
143 
144     SkipFilter other = (SkipFilter)o;
145     return getFilter().areSerializedFieldsEqual(other.getFilter());
146   }
147 
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 }