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  @Deprecated
055  @Override
056  public ReturnCode filterKeyValue(final Cell c) {
057    return filterCell(c);
058  }
059
060  @Override
061  public ReturnCode filterCell(final Cell c) {
062    if (done) return ReturnCode.NEXT_ROW;
063    return ReturnCode.INCLUDE;
064  }
065
066  @Override
067  public boolean filterRowKey(Cell firstRowCell) {
068    // if stopRowKey is <= buffer, then true, filter row.
069    if (filterAllRemaining()) return true;
070    int cmp = CellComparator.getInstance().compareRows(firstRowCell, stopRowKey, 0, stopRowKey.length);
071    done = reversed ? cmp < 0 : cmp > 0;
072    return done;
073  }
074
075  @Override
076  public boolean filterAllRemaining() {
077    return done;
078  }
079
080  public static Filter createFilterFromArguments (ArrayList<byte []> filterArguments) {
081    Preconditions.checkArgument(filterArguments.size() == 1,
082                                "Expected 1 but got: %s", filterArguments.size());
083    byte [] stopRowKey = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
084    return new InclusiveStopFilter(stopRowKey);
085  }
086
087  /**
088   * @return The filter serialized using pb
089   */
090  @Override
091  public byte [] toByteArray() {
092    FilterProtos.InclusiveStopFilter.Builder builder =
093      FilterProtos.InclusiveStopFilter.newBuilder();
094    if (this.stopRowKey != null) builder.setStopRowKey(
095        UnsafeByteOperations.unsafeWrap(this.stopRowKey));
096    return builder.build().toByteArray();
097  }
098
099  /**
100   * @param pbBytes A pb serialized {@link InclusiveStopFilter} instance
101   * @return An instance of {@link InclusiveStopFilter} made from <code>bytes</code>
102   * @throws DeserializationException
103   * @see #toByteArray
104   */
105  public static InclusiveStopFilter parseFrom(final byte [] pbBytes)
106  throws DeserializationException {
107    FilterProtos.InclusiveStopFilter proto;
108    try {
109      proto = FilterProtos.InclusiveStopFilter.parseFrom(pbBytes);
110    } catch (InvalidProtocolBufferException e) {
111      throw new DeserializationException(e);
112    }
113    return new InclusiveStopFilter(proto.hasStopRowKey()?proto.getStopRowKey().toByteArray():null);
114  }
115
116  /**
117   * @param o the other filter to compare with
118   * @return true if and only if the fields of the filter that are serialized
119   * are equal to the corresponding fields in other.  Used for testing.
120   */
121  @Override
122  boolean areSerializedFieldsEqual(Filter o) {
123    if (o == this) return true;
124    if (!(o instanceof InclusiveStopFilter)) return false;
125
126    InclusiveStopFilter other = (InclusiveStopFilter)o;
127    return Bytes.equals(this.getStopRowKey(), other.getStopRowKey());
128  }
129
130  @Override
131  public String toString() {
132    return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.stopRowKey);
133  }
134
135  @Override
136  public boolean equals(Object obj) {
137    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
138  }
139
140  @Override
141  public int hashCode() {
142    return Bytes.hashCode(this.stopRowKey);
143  }
144}