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      * Include KeyValue and done with row, seek to next.
186      */
187     INCLUDE_AND_SEEK_NEXT_ROW,
188 }
189 
190   /**
191    * Chance to alter the list of Cells to be submitted. Modifications to the list will carry on
192    * 
193    * Concrete implementers can signal a failure condition in their code by throwing an
194    * {@link IOException}.
195    * 
196    * @param kvs the list of Cells to be filtered
197    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
198    */
199   abstract public void filterRowCells(List<Cell> kvs) throws IOException;
200 
201   /**
202    * Primarily used to check for conflicts with scans(such as scans that do not read a full row at a
203    * time).
204    * 
205    * @return True if this filter actively uses filterRowCells(List) or filterRow().
206    */
207   abstract public boolean hasFilterRow();
208 
209   /**
210    * Last chance to veto row based on previous {@link #filterKeyValue(Cell)} calls. The filter
211    * needs to retain state then return a particular value for this call if they wish to exclude a
212    * row if a certain column is missing (for example).
213    * 
214    * Concrete implementers can signal a failure condition in their code by throwing an
215    * {@link IOException}.
216    * 
217    * @return true to exclude row, false to include row.
218    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
219    */
220   abstract public boolean filterRow() throws IOException;
221 
222   /**
223    * If the filter returns the match code SEEK_NEXT_USING_HINT, then it should also tell which is
224    * the next key it must seek to. After receiving the match code SEEK_NEXT_USING_HINT, the
225    * QueryMatcher would call this function to find out which key it must next seek to.
226    * 
227    * Concrete implementers can signal a failure condition in their code by throwing an
228    * {@link IOException}.
229    * 
230    * @return KeyValue which must be next seeked. return null if the filter is not sure which key to
231    *         seek to next.
232    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
233    */
234   abstract public Cell getNextCellHint(final Cell currentCell) throws IOException;
235 
236   /**
237    * Check that given column family is essential for filter to check row. Most filters always return
238    * true here. But some could have more sophisticated logic which could significantly reduce
239    * scanning process by not even touching columns until we are 100% sure that it's data is needed
240    * in result.
241    * 
242    * Concrete implementers can signal a failure condition in their code by throwing an
243    * {@link IOException}.
244    * 
245    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
246    */
247   abstract public boolean isFamilyEssential(byte[] name) throws IOException;
248 
249   /**
250    * TODO: JAVADOC
251    * 
252    * Concrete implementers can signal a failure condition in their code by throwing an
253    * {@link IOException}.
254    * 
255    * @return The filter serialized using pb
256    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
257    */
258   abstract public byte[] toByteArray() throws IOException;
259 
260   /**
261    * 
262    * Concrete implementers can signal a failure condition in their code by throwing an
263    * {@link IOException}.
264    * 
265    * @param pbBytes A pb serialized {@link Filter} instance
266    * @return An instance of {@link Filter} made from <code>bytes</code>
267    * @throws DeserializationException
268    * @see #toByteArray
269    */
270   public static Filter parseFrom(final byte [] pbBytes) throws DeserializationException {
271     throw new DeserializationException(
272       "parseFrom called on base Filter, but should be called on derived type");
273   }
274 
275   /**
276    * Concrete implementers can signal a failure condition in their code by throwing an
277    * {@link IOException}.
278    * 
279    * @param other
280    * @return true if and only if the fields of the filter that are serialized are equal to the
281    *         corresponding fields in other. Used for testing.
282    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
283    */
284   abstract boolean areSerializedFieldsEqual(Filter other);
285 
286   /**
287    * alter the reversed scan flag
288    * @param reversed flag
289    */
290   public void setReversed(boolean reversed) {
291     this.reversed = reversed;
292   }
293 
294   public boolean isReversed() {
295     return this.reversed;
296   }
297 }