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 com.google.common.base.Preconditions;
23  import com.google.protobuf.ByteString;
24  import com.google.protobuf.InvalidProtocolBufferException;
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.exceptions.DeserializationException;
28  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import java.util.ArrayList;
32  
33  /**
34   * A Filter that stops after the given row.  There is no "RowStopFilter" because
35   * the Scan spec allows you to specify a stop row.
36   *
37   * Use this filter to include the stop row, eg: [A,Z].
38   */
39  @InterfaceAudience.Public
40  @InterfaceStability.Stable
41  public class InclusiveStopFilter extends FilterBase {
42    private byte [] stopRowKey;
43    private boolean done = false;
44  
45    public InclusiveStopFilter(final byte [] stopRowKey) {
46      this.stopRowKey = stopRowKey;
47    }
48  
49    public byte[] getStopRowKey() {
50      return this.stopRowKey;
51    }
52  
53    public boolean filterRowKey(byte[] buffer, int offset, int length) {
54      if (buffer == null) {
55        //noinspection RedundantIfStatement
56        if (this.stopRowKey == null) {
57          return true; //filter...
58        }
59        return false;
60      }
61      // if stopRowKey is <= buffer, then true, filter row.
62      int cmp = Bytes.compareTo(stopRowKey, 0, stopRowKey.length,
63        buffer, offset, length);
64  
65      if(cmp < 0) {
66        done = true;
67      }
68      return done;
69    }
70  
71    public boolean filterAllRemaining() {
72      return done;
73    }
74  
75    public static Filter createFilterFromArguments (ArrayList<byte []> filterArguments) {
76      Preconditions.checkArgument(filterArguments.size() == 1,
77                                  "Expected 1 but got: %s", filterArguments.size());
78      byte [] stopRowKey = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
79      return new InclusiveStopFilter(stopRowKey);
80    }
81  
82    /**
83     * @return The filter serialized using pb
84     */
85    public byte [] toByteArray() {
86      FilterProtos.InclusiveStopFilter.Builder builder =
87        FilterProtos.InclusiveStopFilter.newBuilder();
88      if (this.stopRowKey != null) builder.setStopRowKey(ByteString.copyFrom(this.stopRowKey));
89      return builder.build().toByteArray();
90    }
91  
92    /**
93     * @param pbBytes A pb serialized {@link InclusiveStopFilter} instance
94     * @return An instance of {@link InclusiveStopFilter} made from <code>bytes</code>
95     * @throws DeserializationException
96     * @see #toByteArray
97     */
98    public static InclusiveStopFilter parseFrom(final byte [] pbBytes)
99    throws DeserializationException {
100     FilterProtos.InclusiveStopFilter proto;
101     try {
102       proto = FilterProtos.InclusiveStopFilter.parseFrom(pbBytes);
103     } catch (InvalidProtocolBufferException e) {
104       throw new DeserializationException(e);
105     }
106     return new InclusiveStopFilter(proto.hasStopRowKey()?proto.getStopRowKey().toByteArray():null);
107   }
108 
109   /**
110    * @param other
111    * @return true if and only if the fields of the filter that are serialized
112    * are equal to the corresponding fields in other.  Used for testing.
113    */
114   boolean areSerializedFieldsEqual(Filter o) {
115     if (o == this) return true;
116     if (!(o instanceof InclusiveStopFilter)) return false;
117 
118     InclusiveStopFilter other = (InclusiveStopFilter)o;
119     return Bytes.equals(this.getStopRowKey(), other.getStopRowKey());
120   }
121 
122   @Override
123   public String toString() {
124     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.stopRowKey);
125   }
126 }