001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.filter;
020
021import java.io.IOException;
022import java.util.ArrayList;
023
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CompareOperator;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.exceptions.DeserializationException;
028import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
030import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
031
032/**
033 * This filter is used to filter based on the column qualifier. It takes an
034 * operator (equal, greater, not equal, etc) and a byte [] comparator for the
035 * column qualifier portion of a key.
036 * <p>
037 * This filter can be wrapped with {@link WhileMatchFilter} and {@link SkipFilter}
038 * to add more control.
039 * <p>
040 * Multiple filters can be combined using {@link FilterList}.
041 * <p>
042 * If an already known column qualifier is looked for, 
043 * use {@link org.apache.hadoop.hbase.client.Get#addColumn}
044 * directly rather than a filter.
045 */
046@InterfaceAudience.Public
047public class QualifierFilter extends CompareFilter {
048  /**
049   * Constructor.
050   * @param op the compare op for column qualifier matching
051   * @param qualifierComparator the comparator for column qualifier matching
052   */
053  public QualifierFilter(final CompareOperator op,
054                         final ByteArrayComparable qualifierComparator) {
055    super(op, qualifierComparator);
056  }
057
058  @Override
059  public ReturnCode filterCell(final Cell c) {
060    if (compareQualifier(getCompareOperator(), this.comparator, c)) {
061      return ReturnCode.SKIP;
062    }
063    return ReturnCode.INCLUDE;
064  }
065
066  public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
067    ArrayList<?> arguments = CompareFilter.extractArguments(filterArguments);
068    CompareOperator compareOp = (CompareOperator)arguments.get(0);
069    ByteArrayComparable comparator = (ByteArrayComparable)arguments.get(1);
070    return new QualifierFilter(compareOp, comparator);
071  }
072
073  /**
074   * @return The filter serialized using pb
075   */
076  @Override
077  public byte [] toByteArray() {
078    FilterProtos.QualifierFilter.Builder builder =
079      FilterProtos.QualifierFilter.newBuilder();
080    builder.setCompareFilter(super.convert());
081    return builder.build().toByteArray();
082  }
083
084  /**
085   * @param pbBytes A pb serialized {@link QualifierFilter} instance
086   * @return An instance of {@link QualifierFilter} made from <code>bytes</code>
087   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
088   * @see #toByteArray
089   */
090  public static QualifierFilter parseFrom(final byte [] pbBytes)
091  throws DeserializationException {
092    FilterProtos.QualifierFilter proto;
093    try {
094      proto = FilterProtos.QualifierFilter.parseFrom(pbBytes);
095    } catch (InvalidProtocolBufferException e) {
096      throw new DeserializationException(e);
097    }
098    final CompareOperator valueCompareOp =
099      CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name());
100    ByteArrayComparable valueComparator = null;
101    try {
102      if (proto.getCompareFilter().hasComparator()) {
103        valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
104      }
105    } catch (IOException ioe) {
106      throw new DeserializationException(ioe);
107    }
108    return new QualifierFilter(valueCompareOp,valueComparator);
109  }
110
111  /**
112   * @return true if and only if the fields of the filter that are serialized
113   * are equal to the corresponding fields in other.  Used for testing.
114   */
115  @Override
116  boolean areSerializedFieldsEqual(Filter o) {
117    if (o == this) return true;
118    if (!(o instanceof QualifierFilter)) return false;
119
120    return super.areSerializedFieldsEqual(o);
121  }
122
123  @Override
124  public boolean equals(Object obj) {
125    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
126  }
127
128  @Override
129  public int hashCode() {
130    return super.hashCode();
131  }
132}