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.List;
24  
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  
30  /**
31   * Interface for row and column filters directly applied within the regionserver.
32   *
33   * A filter can expect the following call sequence:
34   * <ul>
35   *   <li> {@link #reset()} : reset the filter state before filtering a new row. </li>
36   *   <li> {@link #filterAllRemaining()}: true means row scan is over; false means keep going. </li>
37   *   <li> {@link #filterRowKey(Cell)}: true means drop this row; false means include.</li>
38   *   <li> {@link #filterKeyValue(Cell)}: decides whether to include or exclude this Cell.
39   *        See {@link ReturnCode}. </li>
40   *   <li> {@link #transformCell(Cell)}: if the Cell is included, let the filter transform the
41   *        Cell. </li>
42   *   <li> {@link #filterRowCells(List)}: allows direct modification of the final list to be submitted
43   *   <li> {@link #filterRow()}: last chance to drop entire row based on the sequence of
44   *        filter calls. Eg: filter a row if it doesn't contain a specified column. </li>
45   * </ul>
46   *
47   * Filter instances are created one per region/scan.  This abstract class replaces
48   * the old RowFilterInterface.
49   *
50   * When implementing your own filters, consider inheriting {@link FilterBase} to help
51   * you reduce boilerplate.
52   *
53   * @see FilterBase
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Stable
57  public abstract class Filter {
58    protected transient boolean reversed;
59    /**
60     * Reset the state of the filter between rows.
61     * 
62     * Concrete implementers can signal a failure condition in their code by throwing an
63     * {@link IOException}.
64     * 
65     * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
66     */
67    abstract public void reset() throws IOException;
68  
69    /**
70     * Filters a row based on the row key. If this returns true, the entire row will be excluded. If
71     * false, each KeyValue in the row will be passed to {@link #filterKeyValue(Cell)} below.
72     * 
73     * Concrete implementers can signal a failure condition in their code by throwing an
74     * {@link IOException}.
75     * 
76     * @param buffer buffer containing row key
77     * @param offset offset into buffer where row key starts
78     * @param length length of the row key
79     * @return true, remove entire row, false, include the row (maybe).
80     * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
81     * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
82     *             Instead use {@link #filterRowKey(Cell)}
83     */
84    @Deprecated
85    abstract public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException;
86  
87    /**
88     * Filters a row based on the row key. If this returns true, the entire row will be excluded. If
89     * false, each KeyValue in the row will be passed to {@link #filterKeyValue(Cell)} below.
90     *
91     * Concrete implementers can signal a failure condition in their code by throwing an
92     * {@link IOException}.
93     *
94     * @param firstRowCell The first cell coming in the new row
95     * @return true, remove entire row, false, include the row (maybe).
96     * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
97     */
98    abstract public boolean filterRowKey(Cell firstRowCell) throws IOException;
99  
100   /**
101    * If this returns true, the scan will terminate.
102    * 
103    * Concrete implementers can signal a failure condition in their code by throwing an
104    * {@link IOException}.
105    * 
106    * @return true to end scan, false to continue.
107    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
108    */
109   abstract public boolean filterAllRemaining() throws IOException;
110 
111   /**
112    * A way to filter based on the column family, column qualifier and/or the column value. Return
113    * code is described below. This allows filters to filter only certain number of columns, then
114    * terminate without matching ever column.
115    * 
116    * If filterRowKey returns true, filterKeyValue needs to be consistent with it.
117    * 
118    * filterKeyValue can assume that filterRowKey has already been called for the row.
119    * 
120    * If your filter returns <code>ReturnCode.NEXT_ROW</code>, it should return
121    * <code>ReturnCode.NEXT_ROW</code> until {@link #reset()} is called just in case the caller calls
122    * for the next row.
123    * 
124    * Concrete implementers can signal a failure condition in their code by throwing an
125    * {@link IOException}.
126    * 
127    * @param v the Cell in question
128    * @return code as described below
129    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
130    * @see Filter.ReturnCode
131    */
132   abstract public ReturnCode filterKeyValue(final Cell v) throws IOException;
133 
134   /**
135    * Give the filter a chance to transform the passed KeyValue. If the Cell is changed a new
136    * Cell object must be returned.
137    * 
138    * @see org.apache.hadoop.hbase.KeyValue#shallowCopy()
139    *      The transformed KeyValue is what is eventually returned to the client. Most filters will
140    *      return the passed KeyValue unchanged.
141    * @see org.apache.hadoop.hbase.filter.KeyOnlyFilter#transformCell(Cell) for an example of a
142    *      transformation.
143    * 
144    *      Concrete implementers can signal a failure condition in their code by throwing an
145    *      {@link IOException}.
146    * 
147    * @param v the KeyValue in question
148    * @return the changed KeyValue
149    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
150    */
151   abstract public Cell transformCell(final Cell v) throws IOException;
152 
153   /**
154    * Return codes for filterValue().
155    */
156   @InterfaceAudience.Public
157   @InterfaceStability.Stable
158   public enum ReturnCode {
159     /**
160      * Include the Cell
161      */
162     INCLUDE,
163     /**
164      * Include the Cell and seek to the next column skipping older versions.
165      */
166     INCLUDE_AND_NEXT_COL,
167     /**
168      * Skip this Cell
169      */
170     SKIP,
171     /**
172      * Skip this column. Go to the next column in this row.
173      */
174     NEXT_COL,
175     /**
176      * Done with columns, skip to next row. Note that filterRow() will
177      * still be called.
178      */
179     NEXT_ROW,
180     /**
181      * Seek to next key which is given as hint by the filter.
182      */
183     SEEK_NEXT_USING_HINT,
184 }
185 
186   /**
187    * Chance to alter the list of Cells to be submitted. Modifications to the list will carry on
188    * 
189    * Concrete implementers can signal a failure condition in their code by throwing an
190    * {@link IOException}.
191    * 
192    * @param kvs the list of Cells to be filtered
193    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
194    */
195   abstract public void filterRowCells(List<Cell> kvs) throws IOException;
196 
197   /**
198    * Primarily used to check for conflicts with scans(such as scans that do not read a full row at a
199    * time).
200    * 
201    * @return True if this filter actively uses filterRowCells(List) or filterRow().
202    */
203   abstract public boolean hasFilterRow();
204 
205   /**
206    * Last chance to veto row based on previous {@link #filterKeyValue(Cell)} calls. The filter
207    * needs to retain state then return a particular value for this call if they wish to exclude a
208    * row if a certain column is missing (for example).
209    * 
210    * Concrete implementers can signal a failure condition in their code by throwing an
211    * {@link IOException}.
212    * 
213    * @return true to exclude row, false to include row.
214    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
215    */
216   abstract public boolean filterRow() throws IOException;
217 
218   /**
219    * If the filter returns the match code SEEK_NEXT_USING_HINT, then it should also tell which is
220    * the next key it must seek to. After receiving the match code SEEK_NEXT_USING_HINT, the
221    * QueryMatcher would call this function to find out which key it must next seek to.
222    * 
223    * Concrete implementers can signal a failure condition in their code by throwing an
224    * {@link IOException}.
225    * 
226    * @return KeyValue which must be next seeked. return null if the filter is not sure which key to
227    *         seek to next.
228    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
229    */
230   abstract public Cell getNextCellHint(final Cell currentCell) throws IOException;
231 
232   /**
233    * Check that given column family is essential for filter to check row. Most filters always return
234    * true here. But some could have more sophisticated logic which could significantly reduce
235    * scanning process by not even touching columns until we are 100% sure that it's data is needed
236    * in result.
237    * 
238    * Concrete implementers can signal a failure condition in their code by throwing an
239    * {@link IOException}.
240    * 
241    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
242    */
243   abstract public boolean isFamilyEssential(byte[] name) throws IOException;
244 
245   /**
246    * TODO: JAVADOC
247    * 
248    * Concrete implementers can signal a failure condition in their code by throwing an
249    * {@link IOException}.
250    * 
251    * @return The filter serialized using pb
252    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
253    */
254   abstract public byte[] toByteArray() throws IOException;
255 
256   /**
257    * 
258    * Concrete implementers can signal a failure condition in their code by throwing an
259    * {@link IOException}.
260    * 
261    * @param pbBytes A pb serialized {@link Filter} instance
262    * @return An instance of {@link Filter} made from <code>bytes</code>
263    * @throws DeserializationException
264    * @see #toByteArray
265    */
266   public static Filter parseFrom(final byte [] pbBytes) throws DeserializationException {
267     throw new DeserializationException(
268       "parseFrom called on base Filter, but should be called on derived type");
269   }
270 
271   /**
272    * Concrete implementers can signal a failure condition in their code by throwing an
273    * {@link IOException}.
274    * 
275    * @param other
276    * @return true if and only if the fields of the filter that are serialized are equal to the
277    *         corresponding fields in other. Used for testing.
278    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
279    */
280   abstract boolean areSerializedFieldsEqual(Filter other);
281 
282   /**
283    * alter the reversed scan flag
284    * @param reversed flag
285    */
286   public void setReversed(boolean reversed) {
287     this.reversed = reversed;
288   }
289 
290   public boolean isReversed() {
291     return this.reversed;
292   }
293 }