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 com.google.common.base.Preconditions;
23  import com.google.protobuf.ByteString;
24  import com.google.protobuf.InvalidProtocolBufferException;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.classification.InterfaceStability;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.client.Scan;
31  import org.apache.hadoop.hbase.exceptions.DeserializationException;
32  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
35  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
36  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.CompareType;
37  import org.apache.hadoop.hbase.util.Bytes;
38  
39  import java.io.IOException;
40  import java.util.ArrayList;
41  
42  /**
43   * This filter is used to filter cells based on value. It takes a {@link CompareFilter.CompareOp}
44   * operator (equal, greater, not equal, etc), and either a byte [] value or
45   * a ByteArrayComparable.
46   * <p>
47   * If we have a byte [] value then we just do a lexicographic compare. For
48   * example, if passed value is 'b' and cell has 'a' and the compare operator
49   * is LESS, then we will filter out this cell (return true).  If this is not
50   * sufficient (eg you want to deserialize a long and then compare it to a fixed
51   * long value), then you can pass in your own comparator instead.
52   * <p>
53   * You must also specify a family and qualifier.  Only the value of this column
54   * will be tested. When using this filter on a {@link Scan} with specified
55   * inputs, the column to be tested should also be added as input (otherwise
56   * the filter will regard the column as missing).
57   * <p>
58   * To prevent the entire row from being emitted if the column is not found
59   * on a row, use {@link #setFilterIfMissing}.
60   * Otherwise, if the column is found, the entire row will be emitted only if
61   * the value passes.  If the value fails, the row will be filtered out.
62   * <p>
63   * In order to test values of previous versions (timestamps), set
64   * {@link #setLatestVersionOnly} to false. The default is true, meaning that
65   * only the latest version's value is tested and all previous versions are ignored.
66   * <p>
67   * To filter based on the value of all scanned columns, use {@link ValueFilter}.
68   */
69  @InterfaceAudience.Public
70  @InterfaceStability.Stable
71  public class SingleColumnValueFilter extends FilterBase {
72    static final Log LOG = LogFactory.getLog(SingleColumnValueFilter.class);
73  
74    protected byte [] columnFamily;
75    protected byte [] columnQualifier;
76    protected CompareOp compareOp;
77    protected ByteArrayComparable comparator;
78    protected boolean foundColumn = false;
79    protected boolean matchedColumn = false;
80    protected boolean filterIfMissing = false;
81    protected boolean latestVersionOnly = true;
82  
83    /**
84     * Constructor for binary compare of the value of a single column.  If the
85     * column is found and the condition passes, all columns of the row will be
86     * emitted.  If the condition fails, the row will not be emitted.
87     * <p>
88     * Use the filterIfColumnMissing flag to set whether the rest of the columns
89     * in a row will be emitted if the specified column to check is not found in
90     * the row.
91     *
92     * @param family name of column family
93     * @param qualifier name of column qualifier
94     * @param compareOp operator
95     * @param value value to compare column values against
96     */
97    public SingleColumnValueFilter(final byte [] family, final byte [] qualifier,
98        final CompareOp compareOp, final byte[] value) {
99      this(family, qualifier, compareOp, new BinaryComparator(value));
100   }
101 
102   /**
103    * Constructor for binary compare of the value of a single column.  If the
104    * column is found and the condition passes, all columns of the row will be
105    * emitted.  If the condition fails, the row will not be emitted.
106    * <p>
107    * Use the filterIfColumnMissing flag to set whether the rest of the columns
108    * in a row will be emitted if the specified column to check is not found in
109    * the row.
110    *
111    * @param family name of column family
112    * @param qualifier name of column qualifier
113    * @param compareOp operator
114    * @param comparator Comparator to use.
115    */
116   public SingleColumnValueFilter(final byte [] family, final byte [] qualifier,
117       final CompareOp compareOp, final ByteArrayComparable comparator) {
118     this.columnFamily = family;
119     this.columnQualifier = qualifier;
120     this.compareOp = compareOp;
121     this.comparator = comparator;
122   }
123 
124   /**
125    * Constructor for protobuf deserialization only.
126    * @param family
127    * @param qualifier
128    * @param compareOp
129    * @param comparator
130    * @param filterIfMissing
131    * @param latestVersionOnly
132    */
133   protected SingleColumnValueFilter(final byte[] family, final byte[] qualifier,
134       final CompareOp compareOp, ByteArrayComparable comparator, final boolean filterIfMissing,
135       final boolean latestVersionOnly) {
136     this(family, qualifier, compareOp, comparator);
137     this.filterIfMissing = filterIfMissing;
138     this.latestVersionOnly = latestVersionOnly;
139   }
140 
141   /**
142    * @return operator
143    */
144   public CompareOp getOperator() {
145     return compareOp;
146   }
147 
148   /**
149    * @return the comparator
150    */
151   public ByteArrayComparable getComparator() {
152     return comparator;
153   }
154 
155   /**
156    * @return the family
157    */
158   public byte[] getFamily() {
159     return columnFamily;
160   }
161 
162   /**
163    * @return the qualifier
164    */
165   public byte[] getQualifier() {
166     return columnQualifier;
167   }
168 
169   public ReturnCode filterKeyValue(KeyValue keyValue) {
170     // System.out.println("REMOVE KEY=" + keyValue.toString() + ", value=" + Bytes.toString(keyValue.getValue()));
171     if (this.matchedColumn) {
172       // We already found and matched the single column, all keys now pass
173       return ReturnCode.INCLUDE;
174     } else if (this.latestVersionOnly && this.foundColumn) {
175       // We found but did not match the single column, skip to next row
176       return ReturnCode.NEXT_ROW;
177     }
178     if (!keyValue.matchingColumn(this.columnFamily, this.columnQualifier)) {
179       return ReturnCode.INCLUDE;
180     }
181     foundColumn = true;
182     if (filterColumnValue(keyValue.getBuffer(),
183         keyValue.getValueOffset(), keyValue.getValueLength())) {
184       return this.latestVersionOnly? ReturnCode.NEXT_ROW: ReturnCode.INCLUDE;
185     }
186     this.matchedColumn = true;
187     return ReturnCode.INCLUDE;
188   }
189 
190   private boolean filterColumnValue(final byte [] data, final int offset,
191       final int length) {
192     int compareResult = this.comparator.compareTo(data, offset, length);
193     switch (this.compareOp) {
194     case LESS:
195       return compareResult <= 0;
196     case LESS_OR_EQUAL:
197       return compareResult < 0;
198     case EQUAL:
199       return compareResult != 0;
200     case NOT_EQUAL:
201       return compareResult == 0;
202     case GREATER_OR_EQUAL:
203       return compareResult > 0;
204     case GREATER:
205       return compareResult >= 0;
206     default:
207       throw new RuntimeException("Unknown Compare op " + compareOp.name());
208     }
209   }
210 
211   public boolean filterRow() {
212     // If column was found, return false if it was matched, true if it was not
213     // If column not found, return true if we filter if missing, false if not
214     return this.foundColumn? !this.matchedColumn: this.filterIfMissing;
215   }
216   
217   public boolean hasFilterRow() {
218     return true;
219   }
220 
221   public void reset() {
222     foundColumn = false;
223     matchedColumn = false;
224   }
225 
226   /**
227    * Get whether entire row should be filtered if column is not found.
228    * @return true if row should be skipped if column not found, false if row
229    * should be let through anyways
230    */
231   public boolean getFilterIfMissing() {
232     return filterIfMissing;
233   }
234 
235   /**
236    * Set whether entire row should be filtered if column is not found.
237    * <p>
238    * If true, the entire row will be skipped if the column is not found.
239    * <p>
240    * If false, the row will pass if the column is not found.  This is default.
241    * @param filterIfMissing flag
242    */
243   public void setFilterIfMissing(boolean filterIfMissing) {
244     this.filterIfMissing = filterIfMissing;
245   }
246 
247   /**
248    * Get whether only the latest version of the column value should be compared.
249    * If true, the row will be returned if only the latest version of the column
250    * value matches. If false, the row will be returned if any version of the
251    * column value matches. The default is true.
252    * @return return value
253    */
254   public boolean getLatestVersionOnly() {
255     return latestVersionOnly;
256   }
257 
258   /**
259    * Set whether only the latest version of the column value should be compared.
260    * If true, the row will be returned if only the latest version of the column
261    * value matches. If false, the row will be returned if any version of the
262    * column value matches. The default is true.
263    * @param latestVersionOnly flag
264    */
265   public void setLatestVersionOnly(boolean latestVersionOnly) {
266     this.latestVersionOnly = latestVersionOnly;
267   }
268 
269   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
270     Preconditions.checkArgument(filterArguments.size() == 4 || filterArguments.size() == 6,
271                                 "Expected 4 or 6 but got: %s", filterArguments.size());
272     byte [] family = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
273     byte [] qualifier = ParseFilter.removeQuotesFromByteArray(filterArguments.get(1));
274     CompareOp compareOp = ParseFilter.createCompareOp(filterArguments.get(2));
275     ByteArrayComparable comparator = ParseFilter.createComparator(
276       ParseFilter.removeQuotesFromByteArray(filterArguments.get(3)));
277 
278     if (comparator instanceof RegexStringComparator ||
279         comparator instanceof SubstringComparator) {
280       if (compareOp != CompareOp.EQUAL &&
281           compareOp != CompareOp.NOT_EQUAL) {
282         throw new IllegalArgumentException ("A regexstring comparator and substring comparator " +
283                                             "can only be used with EQUAL and NOT_EQUAL");
284       }
285     }
286 
287     SingleColumnValueFilter filter = new SingleColumnValueFilter(family, qualifier,
288                                                                  compareOp, comparator);
289 
290     if (filterArguments.size() == 6) {
291       boolean filterIfMissing = ParseFilter.convertByteArrayToBoolean(filterArguments.get(4));
292       boolean latestVersionOnly = ParseFilter.convertByteArrayToBoolean(filterArguments.get(5));
293       filter.setFilterIfMissing(filterIfMissing);
294       filter.setLatestVersionOnly(latestVersionOnly);
295     }
296     return filter;
297   }
298 
299   FilterProtos.SingleColumnValueFilter convert() {
300     FilterProtos.SingleColumnValueFilter.Builder builder =
301       FilterProtos.SingleColumnValueFilter.newBuilder();
302     if (this.columnFamily != null) {
303       builder.setColumnFamily(ByteString.copyFrom(this.columnFamily));
304     }
305     if (this.columnQualifier != null) {
306       builder.setColumnQualifier(ByteString.copyFrom(this.columnQualifier));
307     }
308     HBaseProtos.CompareType compareOp = CompareType.valueOf(this.compareOp.name());
309     builder.setCompareOp(compareOp);
310     builder.setComparator(ProtobufUtil.toComparator(this.comparator));
311     builder.setFilterIfMissing(this.filterIfMissing);
312     builder.setLatestVersionOnly(this.latestVersionOnly);
313 
314     return builder.build();
315   }
316 
317   /**
318    * @return The filter serialized using pb
319    */
320   public byte [] toByteArray() {
321     return convert().toByteArray();
322   }
323 
324   /**
325    * @param pbBytes A pb serialized {@link SingleColumnValueFilter} instance
326    * @return An instance of {@link SingleColumnValueFilter} made from <code>bytes</code>
327    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
328    * @see #toByteArray
329    */
330   public static SingleColumnValueFilter parseFrom(final byte [] pbBytes)
331   throws DeserializationException {
332     FilterProtos.SingleColumnValueFilter proto;
333     try {
334       proto = FilterProtos.SingleColumnValueFilter.parseFrom(pbBytes);
335     } catch (InvalidProtocolBufferException e) {
336       throw new DeserializationException(e);
337     }
338 
339     final CompareOp compareOp =
340       CompareOp.valueOf(proto.getCompareOp().name());
341     final ByteArrayComparable comparator;
342     try {
343       comparator = ProtobufUtil.toComparator(proto.getComparator());
344     } catch (IOException ioe) {
345       throw new DeserializationException(ioe);
346     }
347 
348     return new SingleColumnValueFilter(proto.hasColumnFamily() ? proto.getColumnFamily()
349         .toByteArray() : null, proto.hasColumnQualifier() ? proto.getColumnQualifier()
350         .toByteArray() : null, compareOp, comparator, proto.getFilterIfMissing(), proto
351         .getLatestVersionOnly());
352   }
353 
354   /**
355    * @param other
356    * @return true if and only if the fields of the filter that are serialized
357    * are equal to the corresponding fields in other.  Used for testing.
358    */
359   boolean areSerializedFieldsEqual(Filter o) {
360     if (o == this) return true;
361     if (!(o instanceof SingleColumnValueFilter)) return false;
362 
363     SingleColumnValueFilter other = (SingleColumnValueFilter)o;
364     return Bytes.equals(this.getFamily(), other.getFamily())
365       && Bytes.equals(this.getQualifier(), other.getQualifier())
366       && this.compareOp.equals(other.compareOp)
367       && this.getComparator().areSerializedFieldsEqual(other.getComparator())
368       && this.getFilterIfMissing() == other.getFilterIfMissing()
369       && this.getLatestVersionOnly() == other.getLatestVersionOnly();
370   }
371 
372   /**
373    * The only CF this filter needs is given column family. So, it's the only essential
374    * column in whole scan. If filterIfMissing == false, all families are essential,
375    * because of possibility of skipping the rows without any data in filtered CF.
376    */
377   public boolean isFamilyEssential(byte[] name) {
378     return !this.filterIfMissing || Bytes.equals(name, this.columnFamily);
379   }
380 
381   @Override
382   public String toString() {
383     return String.format("%s (%s, %s, %s, %s)",
384         this.getClass().getSimpleName(), Bytes.toStringBinary(this.columnFamily),
385         Bytes.toStringBinary(this.columnQualifier), this.compareOp.name(),
386         Bytes.toStringBinary(this.comparator.getValue()));
387   }
388 }