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.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
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(byte[],int,int)}: 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 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     */
82    abstract public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException;
83  
84    /**
85     * If this returns true, the scan will terminate.
86     * 
87     * Concrete implementers can signal a failure condition in their code by throwing an
88     * {@link IOException}.
89     * 
90     * @return true to end scan, false to continue.
91     * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
92     */
93    abstract public boolean filterAllRemaining() throws IOException;
94  
95    /**
96     * A way to filter based on the column family, column qualifier and/or the column value. Return
97     * code is described below. This allows filters to filter only certain number of columns, then
98     * terminate without matching ever column.
99     * 
100    * If filterRowKey returns true, filterKeyValue needs to be consistent with it.
101    * 
102    * filterKeyValue can assume that filterRowKey has already been called for the row.
103    * 
104    * If your filter returns <code>ReturnCode.NEXT_ROW</code>, it should return
105    * <code>ReturnCode.NEXT_ROW</code> until {@link #reset()} is called just in case the caller calls
106    * for the next row.
107    * 
108    * Concrete implementers can signal a failure condition in their code by throwing an
109    * {@link IOException}.
110    * 
111    * @param v the Cell in question
112    * @return code as described below
113    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
114    * @see Filter.ReturnCode
115    */
116   abstract public ReturnCode filterKeyValue(final Cell v) throws IOException;
117 
118   /**
119    * Give the filter a chance to transform the passed KeyValue. If the Cell is changed a new
120    * Cell object must be returned.
121    * 
122    * @see org.apache.hadoop.hbase.KeyValue#shallowCopy()
123    *      The transformed KeyValue is what is eventually returned to the client. Most filters will
124    *      return the passed KeyValue unchanged.
125    * @see org.apache.hadoop.hbase.filter.KeyOnlyFilter#transformCell(Cell) for an example of a
126    *      transformation.
127    * 
128    *      Concrete implementers can signal a failure condition in their code by throwing an
129    *      {@link IOException}.
130    * 
131    * @param v the KeyValue in question
132    * @return the changed KeyValue
133    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
134    */
135   abstract public Cell transformCell(final Cell v) throws IOException;
136 
137   /**
138    * Return codes for filterValue().
139    */
140   @InterfaceAudience.Public
141   @InterfaceStability.Stable
142   public enum ReturnCode {
143     /**
144      * Include the Cell
145      */
146     INCLUDE,
147     /**
148      * Include the Cell and seek to the next column skipping older versions.
149      */
150     INCLUDE_AND_NEXT_COL,
151     /**
152      * Skip this Cell
153      */
154     SKIP,
155     /**
156      * Skip this column. Go to the next column in this row.
157      */
158     NEXT_COL,
159     /**
160      * Done with columns, skip to next row. Note that filterRow() will
161      * still be called.
162      */
163     NEXT_ROW,
164     /**
165      * Seek to next key which is given as hint by the filter.
166      */
167     SEEK_NEXT_USING_HINT,
168 }
169 
170   /**
171    * Chance to alter the list of Cells to be submitted. Modifications to the list will carry on
172    * 
173    * Concrete implementers can signal a failure condition in their code by throwing an
174    * {@link IOException}.
175    * 
176    * @param kvs the list of Cells to be filtered
177    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
178    */
179   abstract public void filterRowCells(List<Cell> kvs) throws IOException;
180 
181   /**
182    * Primarily used to check for conflicts with scans(such as scans that do not read a full row at a
183    * time).
184    * 
185    * @return True if this filter actively uses filterRowCells(List) or filterRow().
186    */
187   abstract public boolean hasFilterRow();
188 
189   /**
190    * Last chance to veto row based on previous {@link #filterKeyValue(Cell)} calls. The filter
191    * needs to retain state then return a particular value for this call if they wish to exclude a
192    * row if a certain column is missing (for example).
193    * 
194    * Concrete implementers can signal a failure condition in their code by throwing an
195    * {@link IOException}.
196    * 
197    * @return true to exclude row, false to include row.
198    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
199    */
200   abstract public boolean filterRow() throws IOException;
201 
202   /**
203    * If the filter returns the match code SEEK_NEXT_USING_HINT, then it should also tell which is
204    * the next key it must seek to. After receiving the match code SEEK_NEXT_USING_HINT, the
205    * QueryMatcher would call this function to find out which key it must next seek to.
206    * 
207    * Concrete implementers can signal a failure condition in their code by throwing an
208    * {@link IOException}.
209    * 
210    * @return KeyValue which must be next seeked. return null if the filter is not sure which key to
211    *         seek to next.
212    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
213    */
214   abstract public Cell getNextCellHint(final Cell currentCell) throws IOException;
215 
216   /**
217    * Check that given column family is essential for filter to check row. Most filters always return
218    * true here. But some could have more sophisticated logic which could significantly reduce
219    * scanning process by not even touching columns until we are 100% sure that it's data is needed
220    * in result.
221    * 
222    * Concrete implementers can signal a failure condition in their code by throwing an
223    * {@link IOException}.
224    * 
225    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
226    */
227   abstract public boolean isFamilyEssential(byte[] name) throws IOException;
228 
229   /**
230    * TODO: JAVADOC
231    * 
232    * Concrete implementers can signal a failure condition in their code by throwing an
233    * {@link IOException}.
234    * 
235    * @return The filter serialized using pb
236    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
237    */
238   abstract public byte[] toByteArray() throws IOException;
239 
240   /**
241    * 
242    * Concrete implementers can signal a failure condition in their code by throwing an
243    * {@link IOException}.
244    * 
245    * @param pbBytes A pb serialized {@link Filter} instance
246    * @return An instance of {@link Filter} made from <code>bytes</code>
247    * @throws DeserializationException
248    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
249    * @see #toByteArray
250    */
251   public static Filter parseFrom(final byte [] pbBytes) throws DeserializationException {
252     throw new DeserializationException(
253       "parseFrom called on base Filter, but should be called on derived type");
254   }
255 
256   /**
257    * Concrete implementers can signal a failure condition in their code by throwing an
258    * {@link IOException}.
259    * 
260    * @param other
261    * @return true if and only if the fields of the filter that are serialized are equal to the
262    *         corresponding fields in other. Used for testing.
263    * @throws IOException in case an I/O or an filter specific failure needs to be signaled.
264    */
265   abstract boolean areSerializedFieldsEqual(Filter other);
266 
267   /**
268    * alter the reversed scan flag
269    * @param reversed flag
270    */
271   public void setReversed(boolean reversed) {
272     this.reversed = reversed;
273   }
274 
275   public boolean isReversed() {
276     return this.reversed;
277   }
278 }