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