View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.hadoop.hbase.util.ByteStringer;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import com.google.common.base.Preconditions;
33  import com.google.protobuf.InvalidProtocolBufferException;
34  
35  /**
36   * A Filter that stops after the given row.  There is no "RowStopFilter" because
37   * the Scan spec allows you to specify a stop row.
38   *
39   * Use this filter to include the stop row, eg: [A,Z].
40   */
41  @InterfaceAudience.Public
42  @InterfaceStability.Stable
43  public class InclusiveStopFilter extends FilterBase {
44    private byte [] stopRowKey;
45    private boolean done = false;
46  
47    public InclusiveStopFilter(final byte [] stopRowKey) {
48      this.stopRowKey = stopRowKey;
49    }
50  
51    public byte[] getStopRowKey() {
52      return this.stopRowKey;
53    }
54  
55    @Override
56    public ReturnCode filterKeyValue(Cell v) {
57      if (done) return ReturnCode.NEXT_ROW;
58      return ReturnCode.INCLUDE;
59    }
60  
61    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
62    // See HBASE-12068
63    @Override
64    public Cell transformCell(Cell v) {
65      return v;
66    }
67  
68    @Override
69    public boolean filterRowKey(byte[] buffer, int offset, int length) {
70      if (buffer == null) {
71        //noinspection RedundantIfStatement
72        if (this.stopRowKey == null) {
73          return true; //filter...
74        }
75        return false;
76      }
77      // if stopRowKey is <= buffer, then true, filter row.
78      int cmp = Bytes.compareTo(stopRowKey, 0, stopRowKey.length,
79        buffer, offset, length);
80  
81      done = reversed ? cmp > 0 : cmp < 0;
82      return done;
83    }
84  
85    @Override
86    public boolean filterAllRemaining() {
87      return done;
88    }
89  
90    public static Filter createFilterFromArguments (ArrayList<byte []> filterArguments) {
91      Preconditions.checkArgument(filterArguments.size() == 1,
92                                  "Expected 1 but got: %s", filterArguments.size());
93      byte [] stopRowKey = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
94      return new InclusiveStopFilter(stopRowKey);
95    }
96  
97    /**
98     * @return The filter serialized using pb
99     */
100   @Override
101   public byte [] toByteArray() {
102     FilterProtos.InclusiveStopFilter.Builder builder =
103       FilterProtos.InclusiveStopFilter.newBuilder();
104     if (this.stopRowKey != null) builder.setStopRowKey(ByteStringer.wrap(this.stopRowKey));
105     return builder.build().toByteArray();
106   }
107 
108   /**
109    * @param pbBytes A pb serialized {@link InclusiveStopFilter} instance
110    * @return An instance of {@link InclusiveStopFilter} made from <code>bytes</code>
111    * @throws DeserializationException
112    * @see #toByteArray
113    */
114   public static InclusiveStopFilter parseFrom(final byte [] pbBytes)
115   throws DeserializationException {
116     FilterProtos.InclusiveStopFilter proto;
117     try {
118       proto = FilterProtos.InclusiveStopFilter.parseFrom(pbBytes);
119     } catch (InvalidProtocolBufferException e) {
120       throw new DeserializationException(e);
121     }
122     return new InclusiveStopFilter(proto.hasStopRowKey()?proto.getStopRowKey().toByteArray():null);
123   }
124 
125   /**
126    * @param other
127    * @return true if and only if the fields of the filter that are serialized
128    * are equal to the corresponding fields in other.  Used for testing.
129    */
130   @Override
131   boolean areSerializedFieldsEqual(Filter o) {
132     if (o == this) return true;
133     if (!(o instanceof InclusiveStopFilter)) return false;
134 
135     InclusiveStopFilter other = (InclusiveStopFilter)o;
136     return Bytes.equals(this.getStopRowKey(), other.getStopRowKey());
137   }
138 
139   @Override
140   public String toString() {
141     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.stopRowKey);
142   }
143 
144   @Override
145   public boolean equals(Object obj) {
146     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
147   }
148 
149   @Override
150   public int hashCode() {
151     return Bytes.hashCode(this.stopRowKey);
152   }
153 }