View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.KeyValueUtil;
31  import org.apache.hadoop.hbase.exceptions.DeserializationException;
32  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
33  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
34  import org.apache.zookeeper.KeeperException.UnimplementedException;
35  
36  import com.google.protobuf.InvalidProtocolBufferException;
37  
38  /**
39   * This is a Filter wrapper class which is used in the server side. Some filter
40   * related hooks can be defined in this wrapper. The only way to create a
41   * FilterWrapper instance is passing a client side Filter instance through
42   * {@link org.apache.hadoop.hbase.client.Scan#getFilter()}.
43   * 
44   */
45  @InterfaceAudience.Private
46  final public class FilterWrapper extends Filter {
47    Filter filter = null;
48  
49    public FilterWrapper( Filter filter ) {
50      if (null == filter) {
51        // ensure the filter instance is not null
52        throw new NullPointerException("Cannot create FilterWrapper with null Filter");
53      }
54      this.filter = filter;
55    }
56  
57    /**
58     * @return The filter serialized using pb
59     */
60    public byte[] toByteArray() throws IOException {
61      FilterProtos.FilterWrapper.Builder builder =
62        FilterProtos.FilterWrapper.newBuilder();
63      builder.setFilter(ProtobufUtil.toFilter(this.filter));
64      return builder.build().toByteArray();
65    }
66  
67    /**
68     * @param pbBytes A pb serialized {@link FilterWrapper} instance
69     * @return An instance of {@link FilterWrapper} made from <code>bytes</code>
70     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
71     * @see #toByteArray
72     */
73    public static FilterWrapper parseFrom(final byte [] pbBytes)
74    throws DeserializationException {
75      FilterProtos.FilterWrapper proto;
76      try {
77        proto = FilterProtos.FilterWrapper.parseFrom(pbBytes);
78      } catch (InvalidProtocolBufferException e) {
79        throw new DeserializationException(e);
80      }
81      try {
82        return new FilterWrapper(ProtobufUtil.toFilter(proto.getFilter()));
83      } catch (IOException ioe) {
84        throw new DeserializationException(ioe);
85      }
86    }
87  
88    @Override
89    public void reset() throws IOException {
90      this.filter.reset();
91    }
92  
93    @Override
94    public boolean filterAllRemaining() throws IOException {
95      return this.filter.filterAllRemaining();
96    }
97  
98    @Override
99    public boolean filterRow() throws IOException {
100     return this.filter.filterRow();
101   }
102 
103   /**
104    * This method is deprecated and you should override Cell getNextKeyHint(Cell) instead.
105    */
106   @Override
107   @Deprecated
108   public KeyValue getNextKeyHint(KeyValue currentKV) throws IOException {
109     return KeyValueUtil.ensureKeyValue(this.filter.getNextCellHint((Cell)currentKV));
110   }
111 
112   /**
113    * Old filter wrapper descendants will implement KV getNextKeyHint(KV) so we should call it.
114    */
115   @Override
116   public Cell getNextCellHint(Cell currentKV) throws IOException {
117     // Old filters based off of this class will override KeyValue getNextKeyHint(KeyValue).
118     // Thus to maintain compatibility we need to call the old version.
119     return this.getNextKeyHint(KeyValueUtil.ensureKeyValue(currentKV));
120   }
121 
122   @Override
123   public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException {
124     return this.filter.filterRowKey(buffer, offset, length);
125   }
126 
127   @Override
128   public ReturnCode filterKeyValue(Cell v) throws IOException {
129     return this.filter.filterKeyValue(v);
130   }
131 
132   @Override
133   public Cell transformCell(Cell v) throws IOException {
134     // Old filters based off of this class will override KeyValue transform(KeyValue).
135     // Thus to maintain compatibility we need to call the old version.
136     return transform(KeyValueUtil.ensureKeyValue(v));
137   }
138 
139   /**
140    * WARNING: please to not override this method.  Instead override {@link #transformCell(Cell)}.
141    *
142    * This is for transition from 0.94 -> 0.96
143    */
144   @Override
145   @Deprecated
146   public KeyValue transform(KeyValue currentKV) throws IOException {
147     return KeyValueUtil.ensureKeyValue(this.filter.transformCell(currentKV));
148   }
149 
150   @Override
151   public boolean hasFilterRow() {
152     return this.filter.hasFilterRow();
153   }
154 
155   @Override
156   public void filterRowCells(List<Cell> kvs) throws IOException {
157     //To fix HBASE-6429,
158     //Filter with filterRow() returning true is incompatible with scan with limit
159     //1. hasFilterRow() returns true, if either filterRow() or filterRow(kvs) is implemented.
160     //2. filterRow() is merged with filterRow(kvs),
161     //so that to make all those row related filtering stuff in the same function.
162     this.filter.filterRowCells(kvs);
163     if (!kvs.isEmpty() && this.filter.filterRow()) {
164       kvs.clear();
165     }
166   }
167 
168   /**
169    * WARNING: please to not override this method.  Instead override {@link #transformCell(Cell)}.
170    *
171    * This is for transition from 0.94 -> 0.96
172    */
173   @Override
174   @Deprecated
175   public void filterRow(List<KeyValue> kvs) throws IOException {
176     // This is only used internally, marked InterfaceAudience.private, and not used anywhere.
177     // We can get away with not implementing this.
178     throw new UnsupportedOperationException("filterRow(List<KeyValue>) should never be called");
179   }
180 
181   @Override
182   public boolean isFamilyEssential(byte[] name) throws IOException {
183     return filter.isFamilyEssential(name);
184   }
185 
186   /**
187    * @param other
188    * @return true if and only if the fields of the filter that are serialized
189    * are equal to the corresponding fields in other.  Used for testing.
190    */
191   boolean areSerializedFieldsEqual(Filter o) {
192     if (o == this) return true;
193     if (!(o instanceof FilterWrapper)) return false;
194 
195     FilterWrapper other = (FilterWrapper)o;
196     return this.filter.areSerializedFieldsEqual(other.filter);
197   }
198 }