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  /**
048   * Constructor.
049   * @param valueCompareOp  the compare op for value matching
050   * @param valueComparator the comparator for value matching
051   * @deprecated Since 2.0.0. Will be removed in 3.0.0. Use
052   *             {@link #ValueFilter(CompareOperator, ByteArrayComparable)}
053   */
054  public ValueFilter(final CompareOp valueCompareOp, final ByteArrayComparable valueComparator) {
055    super(valueCompareOp, valueComparator);
056  }
057
058  /**
059   * Constructor.
060   * @param valueCompareOp  the compare op for value matching
061   * @param valueComparator the comparator for value matching
062   */
063  public ValueFilter(final CompareOperator valueCompareOp,
064    final ByteArrayComparable valueComparator) {
065    super(valueCompareOp, valueComparator);
066  }
067
068  @Deprecated
069  @Override
070  public ReturnCode filterKeyValue(final Cell c) {
071    return filterCell(c);
072  }
073
074  @Override
075  public ReturnCode filterCell(final Cell c) {
076    if (compareValue(getCompareOperator(), this.comparator, c)) {
077      return ReturnCode.SKIP;
078    }
079    return ReturnCode.INCLUDE;
080  }
081
082  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
083    @SuppressWarnings("rawtypes") // for arguments
084    ArrayList arguments = CompareFilter.extractArguments(filterArguments);
085    CompareOperator compareOp = (CompareOperator) arguments.get(0);
086    ByteArrayComparable comparator = (ByteArrayComparable) arguments.get(1);
087    return new ValueFilter(compareOp, comparator);
088  }
089
090  /** Returns The filter serialized using pb */
091  @Override
092  public byte[] toByteArray() {
093    FilterProtos.ValueFilter.Builder builder = FilterProtos.ValueFilter.newBuilder();
094    builder.setCompareFilter(super.convert());
095    return builder.build().toByteArray();
096  }
097
098  /**
099   * Parse a serialized representation of {@link ValueFilter}
100   * @param pbBytes A pb serialized {@link ValueFilter} instance
101   * @return An instance of {@link ValueFilter} made from <code>bytes</code>
102   * @throws DeserializationException if an error occurred
103   * @see #toByteArray
104   */
105  public static ValueFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
106    FilterProtos.ValueFilter proto;
107    try {
108      proto = FilterProtos.ValueFilter.parseFrom(pbBytes);
109    } catch (InvalidProtocolBufferException e) {
110      throw new DeserializationException(e);
111    }
112    final CompareOperator valueCompareOp =
113      CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name());
114    ByteArrayComparable valueComparator = null;
115    try {
116      if (proto.getCompareFilter().hasComparator()) {
117        valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
118      }
119    } catch (IOException ioe) {
120      throw new DeserializationException(ioe);
121    }
122    return new ValueFilter(valueCompareOp, valueComparator);
123  }
124
125  /**
126   * Returns true if and only if the fields of the filter that are serialized are equal to the
127   * corresponding fields in other. Used for testing.
128   */
129  @Override
130  boolean areSerializedFieldsEqual(Filter o) {
131    if (o == this) {
132      return true;
133    }
134    if (!(o instanceof ValueFilter)) {
135      return false;
136    }
137    return super.areSerializedFieldsEqual(o);
138  }
139
140  @Override
141  public boolean equals(Object obj) {
142    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
143  }
144
145  @Override
146  public int hashCode() {
147    return super.hashCode();
148  }
149}