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