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 the key. It takes an operator (equal, greater, not equal,
034 * etc) and a byte [] comparator for the row, and column qualifier portions of a key.
035 * <p>
036 * This filter can be wrapped with {@link WhileMatchFilter} to add more control.
037 * <p>
038 * Multiple filters can be combined using {@link FilterList}.
039 * <p>
040 * If an already known row range needs to be scanned, use
041 * {@link org.apache.hadoop.hbase.CellScanner} start and stop rows directly rather than a filter.
042 */
043@InterfaceAudience.Public
044public class RowFilter extends CompareFilter {
045  private boolean filterOutRow = false;
046
047  /**
048   * Constructor.
049   * @param op            the compare op for row matching
050   * @param rowComparator the comparator for row matching
051   */
052  public RowFilter(final CompareOperator op, final ByteArrayComparable rowComparator) {
053    super(op, rowComparator);
054  }
055
056  @Override
057  public void reset() {
058    this.filterOutRow = false;
059  }
060
061  @Override
062  public ReturnCode filterCell(final Cell v) {
063    if (this.filterOutRow) {
064      return ReturnCode.NEXT_ROW;
065    }
066    return ReturnCode.INCLUDE;
067  }
068
069  @Override
070  public boolean filterRowKey(Cell firstRowCell) {
071    if (compareRow(getCompareOperator(), this.comparator, firstRowCell)) {
072      this.filterOutRow = true;
073    }
074    return this.filterOutRow;
075  }
076
077  @Override
078  public boolean filterRow() {
079    return this.filterOutRow;
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 RowFilter(compareOp, comparator);
088  }
089
090  /** Returns The filter serialized using pb */
091  @Override
092  public byte[] toByteArray() {
093    FilterProtos.RowFilter.Builder builder = FilterProtos.RowFilter.newBuilder();
094    builder.setCompareFilter(super.convert());
095    return builder.build().toByteArray();
096  }
097
098  /**
099   * Parse a serialized representation of {@link RowFilter}
100   * @param pbBytes A pb serialized {@link RowFilter} instance
101   * @return An instance of {@link RowFilter} made from <code>bytes</code>
102   * @throws DeserializationException if an error occurred
103   * @see #toByteArray
104   */
105  public static RowFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
106    FilterProtos.RowFilter proto;
107    try {
108      proto = FilterProtos.RowFilter.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 RowFilter(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 RowFilter)) {
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}