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.Cell;
022import org.apache.hadoop.hbase.CellComparator;
023import org.apache.hadoop.hbase.exceptions.DeserializationException;
024import org.apache.hadoop.hbase.util.Bytes;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
028import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
029import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
030
031import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
032
033/**
034 * A Filter that stops after the given row. There is no "RowStopFilter" because the Scan spec allows
035 * you to specify a stop row. Use this filter to include the stop row, eg: [A,Z].
036 */
037@InterfaceAudience.Public
038public class InclusiveStopFilter extends FilterBase {
039  private byte[] stopRowKey;
040  private boolean done = false;
041
042  public InclusiveStopFilter(final byte[] stopRowKey) {
043    this.stopRowKey = stopRowKey;
044  }
045
046  public byte[] getStopRowKey() {
047    return this.stopRowKey;
048  }
049
050  @Override
051  public ReturnCode filterCell(final Cell c) {
052    if (done) return ReturnCode.NEXT_ROW;
053    return ReturnCode.INCLUDE;
054  }
055
056  @Override
057  public boolean filterRowKey(Cell firstRowCell) {
058    // if stopRowKey is <= buffer, then true, filter row.
059    if (filterAllRemaining()) return true;
060    int cmp =
061      CellComparator.getInstance().compareRows(firstRowCell, stopRowKey, 0, stopRowKey.length);
062    done = reversed ? cmp < 0 : cmp > 0;
063    return done;
064  }
065
066  @Override
067  public boolean filterAllRemaining() {
068    return done;
069  }
070
071  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
072    Preconditions.checkArgument(filterArguments.size() == 1, "Expected 1 but got: %s",
073      filterArguments.size());
074    byte[] stopRowKey = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
075    return new InclusiveStopFilter(stopRowKey);
076  }
077
078  /**
079   * @return The filter serialized using pb
080   */
081  @Override
082  public byte[] toByteArray() {
083    FilterProtos.InclusiveStopFilter.Builder builder =
084      FilterProtos.InclusiveStopFilter.newBuilder();
085    if (this.stopRowKey != null)
086      builder.setStopRowKey(UnsafeByteOperations.unsafeWrap(this.stopRowKey));
087    return builder.build().toByteArray();
088  }
089
090  /**
091   * @param pbBytes A pb serialized {@link InclusiveStopFilter} instance
092   * @return An instance of {@link InclusiveStopFilter} made from <code>bytes</code> n * @see
093   *         #toByteArray
094   */
095  public static InclusiveStopFilter parseFrom(final byte[] pbBytes)
096    throws DeserializationException {
097    FilterProtos.InclusiveStopFilter proto;
098    try {
099      proto = FilterProtos.InclusiveStopFilter.parseFrom(pbBytes);
100    } catch (InvalidProtocolBufferException e) {
101      throw new DeserializationException(e);
102    }
103    return new InclusiveStopFilter(
104      proto.hasStopRowKey() ? proto.getStopRowKey().toByteArray() : null);
105  }
106
107  /**
108   * @param o the other filter to compare with
109   * @return true if and only if the fields of the filter that are serialized are equal to the
110   *         corresponding fields in other. Used for testing.
111   */
112  @Override
113  boolean areSerializedFieldsEqual(Filter o) {
114    if (o == this) return true;
115    if (!(o instanceof InclusiveStopFilter)) return false;
116
117    InclusiveStopFilter other = (InclusiveStopFilter) o;
118    return Bytes.equals(this.getStopRowKey(), other.getStopRowKey());
119  }
120
121  @Override
122  public String toString() {
123    return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.stopRowKey);
124  }
125
126  @Override
127  public boolean equals(Object obj) {
128    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
129  }
130
131  @Override
132  public int hashCode() {
133    return Bytes.hashCode(this.stopRowKey);
134  }
135}