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  @Override
077  public ReturnCode filterCell(final Cell c) {
078    if (filterRow) return ReturnCode.NEXT_ROW;
079    return ReturnCode.INCLUDE;
080  }
081
082  @Override
083  public boolean filterRow() {
084    return filterRow;
085  }
086
087  @Override
088  public void reset() {
089    filterRow = true;
090  }
091
092  @Override
093  public boolean filterAllRemaining() {
094    return passedPrefix;
095  }
096
097  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
098    Preconditions.checkArgument(filterArguments.size() == 1, "Expected 1 but got: %s",
099      filterArguments.size());
100    byte[] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
101    return new PrefixFilter(prefix);
102  }
103
104  /** Returns The filter serialized using pb */
105  @Override
106  public byte[] toByteArray() {
107    FilterProtos.PrefixFilter.Builder builder = FilterProtos.PrefixFilter.newBuilder();
108    if (this.prefix != null) builder.setPrefix(UnsafeByteOperations.unsafeWrap(this.prefix));
109    return builder.build().toByteArray();
110  }
111
112  /**
113   * Parse a serialized representation of {@link PrefixFilter}
114   * @param pbBytes A pb serialized {@link PrefixFilter} instance
115   * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
116   * @throws DeserializationException if an error occurred
117   * @see #toByteArray
118   */
119  public static PrefixFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
120    FilterProtos.PrefixFilter proto;
121    try {
122      proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
123    } catch (InvalidProtocolBufferException e) {
124      throw new DeserializationException(e);
125    }
126    return new PrefixFilter(proto.hasPrefix() ? proto.getPrefix().toByteArray() : null);
127  }
128
129  /**
130   * Returns true if and only if the fields of the filter that are serialized are equal to the
131   * corresponding fields in other. Used for testing.
132   */
133  @Override
134  boolean areSerializedFieldsEqual(Filter o) {
135    if (o == this) {
136      return true;
137    }
138    if (!(o instanceof PrefixFilter)) {
139      return false;
140    }
141    PrefixFilter other = (PrefixFilter) o;
142    return Bytes.equals(this.getPrefix(), other.getPrefix());
143  }
144
145  @Override
146  public String toString() {
147    return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
148  }
149
150  @Override
151  public boolean equals(Object obj) {
152    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
153  }
154
155  @Override
156  public int hashCode() {
157    return Bytes.hashCode(this.getPrefix());
158  }
159}