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