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