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  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  
32  /**
33   * Abstract base class to help you implement new Filters.  Common "ignore" or NOOP type
34   * methods can go here, helping to reduce boiler plate in an ever-expanding filter
35   * library.
36   *
37   * If you could instantiate FilterBase, it would end up being a "null" filter -
38   * that is one that never filters anything.
39   */
40  @InterfaceAudience.Private // TODO add filter limited private level
41  public abstract class FilterBase extends Filter {
42  
43    /**
44     * Filters that are purely stateless and do nothing in their reset() methods can inherit
45     * this null/empty implementation.
46     *
47     * @inheritDoc
48     */
49    @Override
50    public void reset() throws IOException {
51    }
52  
53    /**
54     * Filters that do not filter by row key can inherit this implementation that
55     * never filters anything. (ie: returns false).
56     *
57     * @inheritDoc
58     */
59    @Override
60    public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException {
61      return false;
62    }
63  
64    /**
65     * Filters that never filter all remaining can inherit this implementation that
66     * never stops the filter early.
67     *
68     * @inheritDoc
69     */
70    @Override
71    public boolean filterAllRemaining() throws IOException {
72      return false;
73    }
74  
75    /**
76     * By default no transformation takes place
77     *
78     * @inheritDoc
79     */
80    @Override
81    public Cell transformCell(Cell v) throws IOException {
82      // Old filters based off of this class will override KeyValue transform(KeyValue).
83      // Thus to maintain compatibility we need to call the old version.
84      return transform(KeyValueUtil.ensureKeyValue(v));
85    }
86  
87    /**
88     * WARNING: please to not override this method.  Instead override {@link #transformCell(Cell)}.
89     *
90     * This is for transition from 0.94 -> 0.96
91     */
92    @Override
93    @Deprecated
94    public KeyValue transform(KeyValue currentKV) throws IOException {
95      return currentKV;
96    }
97  
98    /**
99     * Filters that never filter by modifying the returned List of Cells can
100    * inherit this implementation that does nothing.
101    *
102    * @inheritDoc
103    */
104   @Override
105   public void filterRowCells(List<Cell> ignored) throws IOException {
106   }
107 
108   /**
109    * Fitlers that never filter by modifying the returned List of Cells can
110    * inherit this implementation that does nothing.
111    *
112    * @inheritDoc
113    */
114   @Override
115   public boolean hasFilterRow() {
116     return false;
117   }
118 
119   /**
120    * Filters that never filter by rows based on previously gathered state from
121    * {@link #filterKeyValue(Cell)} can inherit this implementation that
122    * never filters a row.
123    *
124    * @inheritDoc
125    */
126   @Override
127   public boolean filterRow() throws IOException {
128     return false;
129   }
130 
131   /**
132    * This method is deprecated and you should override Cell getNextKeyHint(Cell) instead.
133    */
134   @Override
135   @Deprecated
136   public KeyValue getNextKeyHint(KeyValue currentKV) throws IOException {
137     return null;
138   }
139   
140   /**
141    * Filters that are not sure which key must be next seeked to, can inherit
142    * this implementation that, by default, returns a null Cell.
143    *
144    * @inheritDoc
145    */
146   public Cell getNextCellHint(Cell currentKV) throws IOException {
147     // Old filters based off of this class will override KeyValue getNextKeyHint(KeyValue).
148     // Thus to maintain compatibility we need to call the old version.
149     return getNextKeyHint(KeyValueUtil.ensureKeyValue(currentKV));
150   }
151 
152   /**
153    * By default, we require all scan's column families to be present. Our
154    * subclasses may be more precise.
155    *
156    * @inheritDoc
157    */
158   public boolean isFamilyEssential(byte[] name) throws IOException {
159     return true;
160   }
161 
162   /**
163    * Given the filter's arguments it constructs the filter
164    * <p>
165    * @param filterArguments the filter's arguments
166    * @return constructed filter object
167    */
168   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
169     throw new IllegalArgumentException("This method has not been implemented");
170   }
171 
172   /**
173    * Return filter's info for debugging and logging purpose.
174    */
175   public String toString() {
176     return this.getClass().getSimpleName();
177   }
178 
179   /**
180    * Return length 0 byte array for Filters that don't require special serialization
181    */
182   public byte[] toByteArray() throws IOException {
183     return new byte[0];
184   }
185 
186   /**
187    * Default implementation so that writers of custom filters aren't forced to implement.
188    *
189    * @param other
190    * @return true if and only if the fields of the filter that are serialized
191    * are equal to the corresponding fields in other.  Used for testing.
192    */
193   boolean areSerializedFieldsEqual(Filter other) {
194     return true;
195   }
196 }