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.List;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.KeyValueUtil;
29  import org.apache.hadoop.hbase.exceptions.DeserializationException;
30  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
31  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32  
33  import com.google.protobuf.InvalidProtocolBufferException;
34  
35  /**
36   * This is a Filter wrapper class which is used in the server side. Some filter
37   * related hooks can be defined in this wrapper. The only way to create a
38   * FilterWrapper instance is passing a client side Filter instance through
39   * {@link org.apache.hadoop.hbase.client.Scan#getFilter()}.
40   * 
41   */
42  @InterfaceAudience.Private
43  final public class FilterWrapper extends Filter {
44    Filter filter = null;
45  
46    public FilterWrapper( Filter filter ) {
47      if (null == filter) {
48        // ensure the filter instance is not null
49        throw new NullPointerException("Cannot create FilterWrapper with null Filter");
50      }
51      this.filter = filter;
52    }
53  
54    /**
55     * @return The filter serialized using pb
56     */
57    public byte[] toByteArray() throws IOException {
58      FilterProtos.FilterWrapper.Builder builder =
59        FilterProtos.FilterWrapper.newBuilder();
60      builder.setFilter(ProtobufUtil.toFilter(this.filter));
61      return builder.build().toByteArray();
62    }
63  
64    /**
65     * @param pbBytes A pb serialized {@link FilterWrapper} instance
66     * @return An instance of {@link FilterWrapper} made from <code>bytes</code>
67     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
68     * @see #toByteArray
69     */
70    public static FilterWrapper parseFrom(final byte [] pbBytes)
71    throws DeserializationException {
72      FilterProtos.FilterWrapper proto;
73      try {
74        proto = FilterProtos.FilterWrapper.parseFrom(pbBytes);
75      } catch (InvalidProtocolBufferException e) {
76        throw new DeserializationException(e);
77      }
78      try {
79        return new FilterWrapper(ProtobufUtil.toFilter(proto.getFilter()));
80      } catch (IOException ioe) {
81        throw new DeserializationException(ioe);
82      }
83    }
84  
85    @Override
86    public void reset() throws IOException {
87      this.filter.reset();
88    }
89  
90    @Override
91    public boolean filterAllRemaining() throws IOException {
92      return this.filter.filterAllRemaining();
93    }
94  
95    @Override
96    public boolean filterRow() throws IOException {
97      return this.filter.filterRow();
98    }
99  
100   /**
101    * This method is deprecated and you should override Cell getNextKeyHint(Cell) instead.
102    */
103   @Override
104   @Deprecated
105   public KeyValue getNextKeyHint(KeyValue currentKV) throws IOException {
106     // This will never get called.
107     return KeyValueUtil.ensureKeyValue(this.filter.getNextCellHint((Cell)currentKV));
108   }
109 
110   @Override
111   public Cell getNextCellHint(Cell currentKV) throws IOException {
112     return this.filter.getNextCellHint(currentKV);
113   }
114 
115   @Override
116   public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException {
117     return this.filter.filterRowKey(buffer, offset, length);
118   }
119 
120   @Override
121   public ReturnCode filterKeyValue(Cell v) throws IOException {
122     return this.filter.filterKeyValue(v);
123   }
124 
125   @Override
126   public Cell transformCell(Cell v) throws IOException {
127     return this.filter.transformCell(v);
128   }
129 
130   /**
131    * WARNING: please to not override this method.  Instead override {@link #transformCell(Cell)}.
132    *
133    * This is for transition from 0.94 -&gt; 0.96
134    */
135   @Override
136   @Deprecated
137   public KeyValue transform(KeyValue currentKV) throws IOException {
138     // This will never get called.
139     return KeyValueUtil.ensureKeyValue(this.filter.transformCell(currentKV));
140   }
141 
142   @Override
143   public boolean hasFilterRow() {
144     return this.filter.hasFilterRow();
145   }
146 
147   @Override
148   public void filterRowCells(List<Cell> kvs) throws IOException {
149     filterRowCellsWithRet(kvs);
150   }
151 
152   public enum FilterRowRetCode {
153     NOT_CALLED,
154     INCLUDE,     // corresponds to filter.filterRow() returning false
155     EXCLUDE      // corresponds to filter.filterRow() returning true
156   }
157   public FilterRowRetCode filterRowCellsWithRet(List<Cell> kvs) throws IOException {
158     //To fix HBASE-6429,
159     //Filter with filterRow() returning true is incompatible with scan with limit
160     //1. hasFilterRow() returns true, if either filterRow() or filterRow(kvs) is implemented.
161     //2. filterRow() is merged with filterRow(kvs),
162     //so that to make all those row related filtering stuff in the same function.
163     this.filter.filterRowCells(kvs);
164     if (!kvs.isEmpty()) {
165       if (this.filter.filterRow()) {
166         kvs.clear();
167         return FilterRowRetCode.EXCLUDE;
168       }
169       return FilterRowRetCode.INCLUDE;
170     }
171     return FilterRowRetCode.NOT_CALLED;
172   }
173 
174   @Override
175   public boolean isFamilyEssential(byte[] name) throws IOException {
176     return filter.isFamilyEssential(name);
177   }
178 
179   /**
180    * @param other
181    * @return true if and only if the fields of the filter that are serialized
182    * are equal to the corresponding fields in other.  Used for testing.
183    */
184   boolean areSerializedFieldsEqual(Filter o) {
185     if (o == this) return true;
186     if (!(o instanceof FilterWrapper)) return false;
187 
188     FilterWrapper other = (FilterWrapper)o;
189     return this.filter.areSerializedFieldsEqual(other.filter);
190   }
191 }