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