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.util.ArrayList;
021import org.apache.hadoop.hbase.ByteBufferExtendedCell;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.exceptions.DeserializationException;
024import org.apache.hadoop.hbase.util.ByteBufferUtils;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
029import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
030import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
033
034/**
035 * Pass results that have same row prefix.
036 */
037@InterfaceAudience.Public
038public class PrefixFilter extends FilterBase {
039  protected byte[] prefix = null;
040  protected boolean passedPrefix = false;
041  protected boolean filterRow = true;
042
043  public PrefixFilter(final byte[] prefix) {
044    this.prefix = prefix;
045  }
046
047  public byte[] getPrefix() {
048    return prefix;
049  }
050
051  @Override
052  public boolean filterRowKey(Cell firstRowCell) {
053    if (firstRowCell == null || this.prefix == null) return true;
054    if (filterAllRemaining()) return true;
055    int length = firstRowCell.getRowLength();
056    if (length < prefix.length) return true;
057    // if they are equal, return false => pass row
058    // else return true, filter row
059    // if we are passed the prefix, set flag
060    int cmp;
061    if (firstRowCell instanceof ByteBufferExtendedCell) {
062      cmp = ByteBufferUtils.compareTo(((ByteBufferExtendedCell) firstRowCell).getRowByteBuffer(),
063        ((ByteBufferExtendedCell) firstRowCell).getRowPosition(), this.prefix.length, this.prefix,
064        0, this.prefix.length);
065    } else {
066      cmp = Bytes.compareTo(firstRowCell.getRowArray(), firstRowCell.getRowOffset(),
067        this.prefix.length, this.prefix, 0, this.prefix.length);
068    }
069    if ((!isReversed() && cmp > 0) || (isReversed() && cmp < 0)) {
070      passedPrefix = true;
071    }
072    filterRow = (cmp != 0);
073    return filterRow;
074  }
075
076  @Deprecated
077  @Override
078  public ReturnCode filterKeyValue(final Cell c) {
079    return filterCell(c);
080  }
081
082  @Override
083  public ReturnCode filterCell(final Cell c) {
084    if (filterRow) return ReturnCode.NEXT_ROW;
085    return ReturnCode.INCLUDE;
086  }
087
088  @Override
089  public boolean filterRow() {
090    return filterRow;
091  }
092
093  @Override
094  public void reset() {
095    filterRow = true;
096  }
097
098  @Override
099  public boolean filterAllRemaining() {
100    return passedPrefix;
101  }
102
103  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
104    Preconditions.checkArgument(filterArguments.size() == 1, "Expected 1 but got: %s",
105      filterArguments.size());
106    byte[] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
107    return new PrefixFilter(prefix);
108  }
109
110  /** Returns The filter serialized using pb */
111  @Override
112  public byte[] toByteArray() {
113    FilterProtos.PrefixFilter.Builder builder = FilterProtos.PrefixFilter.newBuilder();
114    if (this.prefix != null) builder.setPrefix(UnsafeByteOperations.unsafeWrap(this.prefix));
115    return builder.build().toByteArray();
116  }
117
118  /**
119   * Parse a serialized representation of {@link PrefixFilter}
120   * @param pbBytes A pb serialized {@link PrefixFilter} instance
121   * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
122   * @throws DeserializationException if an error occurred
123   * @see #toByteArray
124   */
125  public static PrefixFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
126    FilterProtos.PrefixFilter proto;
127    try {
128      proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
129    } catch (InvalidProtocolBufferException e) {
130      throw new DeserializationException(e);
131    }
132    return new PrefixFilter(proto.hasPrefix() ? proto.getPrefix().toByteArray() : null);
133  }
134
135  /**
136   * Returns true if and only if the fields of the filter that are serialized are equal to the
137   * corresponding fields in other. Used for testing.
138   */
139  @Override
140  boolean areSerializedFieldsEqual(Filter o) {
141    if (o == this) {
142      return true;
143    }
144    if (!(o instanceof PrefixFilter)) {
145      return false;
146    }
147    PrefixFilter other = (PrefixFilter) o;
148    return Bytes.equals(this.getPrefix(), other.getPrefix());
149  }
150
151  @Override
152  public String toString() {
153    return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
154  }
155
156  @Override
157  public boolean equals(Object obj) {
158    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
159  }
160
161  @Override
162  public int hashCode() {
163    return Bytes.hashCode(this.getPrefix());
164  }
165}