001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.filter;
021
022import java.util.ArrayList;
023
024import org.apache.hadoop.hbase.ByteBufferExtendedCell;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.exceptions.DeserializationException;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
029import org.apache.hadoop.hbase.util.ByteBufferUtils;
030import org.apache.hadoop.hbase.util.Bytes;
031
032import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
034import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
035
036/**
037 * Pass results that have same row prefix.
038 */
039@InterfaceAudience.Public
040public class PrefixFilter extends FilterBase {
041  protected byte [] prefix = null;
042  protected boolean passedPrefix = false;
043  protected boolean filterRow = true;
044
045  public PrefixFilter(final byte [] prefix) {
046    this.prefix = prefix;
047  }
048
049  public byte[] getPrefix() {
050    return prefix;
051  }
052
053  @Override
054  public boolean filterRowKey(Cell firstRowCell) {
055    if (firstRowCell == null || this.prefix == null)
056      return true;
057    if (filterAllRemaining()) return true;
058    int length = firstRowCell.getRowLength();
059    if (length < prefix.length) return true;
060    // if they are equal, return false => pass row
061    // else return true, filter row
062    // if we are passed the prefix, set flag
063    int cmp;
064    if (firstRowCell instanceof ByteBufferExtendedCell) {
065      cmp = ByteBufferUtils.compareTo(((ByteBufferExtendedCell) firstRowCell).getRowByteBuffer(),
066          ((ByteBufferExtendedCell) firstRowCell).getRowPosition(), this.prefix.length,
067          this.prefix, 0, this.prefix.length);
068    } else {
069      cmp = Bytes.compareTo(firstRowCell.getRowArray(), firstRowCell.getRowOffset(),
070          this.prefix.length, this.prefix, 0, this.prefix.length);
071    }
072    if ((!isReversed() && cmp > 0) || (isReversed() && cmp < 0)) {
073      passedPrefix = true;
074    }
075    filterRow = (cmp != 0);
076    return filterRow;
077  }
078
079  @Override
080  public ReturnCode filterCell(final Cell c) {
081    if (filterRow) return ReturnCode.NEXT_ROW;
082    return ReturnCode.INCLUDE;
083  }
084
085  @Override
086  public boolean filterRow() {
087    return filterRow;
088  }
089
090  @Override
091  public void reset() {
092    filterRow = true;
093  }
094
095  @Override
096  public boolean filterAllRemaining() {
097    return passedPrefix;
098  }
099
100  public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
101    Preconditions.checkArgument(filterArguments.size() == 1,
102                                "Expected 1 but got: %s", filterArguments.size());
103    byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
104    return new PrefixFilter(prefix);
105  }
106
107  /**
108   * @return The filter serialized using pb
109   */
110  @Override
111  public byte [] toByteArray() {
112    FilterProtos.PrefixFilter.Builder builder =
113      FilterProtos.PrefixFilter.newBuilder();
114    if (this.prefix != null) builder.setPrefix(UnsafeByteOperations.unsafeWrap(this.prefix));
115    return builder.build().toByteArray();
116  }
117
118  /**
119   * @param pbBytes A pb serialized {@link PrefixFilter} instance
120   * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
121   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
122   * @see #toByteArray
123   */
124  public static PrefixFilter parseFrom(final byte [] pbBytes)
125  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   * @param o the other filter to compare with
137   * @return true if and only if the fields of the filter that are serialized
138   * are equal to the corresponding fields in other.  Used for testing.
139   */
140  @Override
141  boolean areSerializedFieldsEqual(Filter o) {
142    if (o == this) return true;
143    if (!(o instanceof PrefixFilter)) return false;
144
145    PrefixFilter other = (PrefixFilter)o;
146    return Bytes.equals(this.getPrefix(), other.getPrefix());
147  }
148
149  @Override
150  public String toString() {
151    return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
152  }
153
154  @Override
155  public boolean equals(Object obj) {
156    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
157  }
158
159  @Override
160  public int hashCode() {
161    return Bytes.hashCode(this.getPrefix());
162  }
163}