View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.filter;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  import java.io.DataInput;
27  import java.io.DataOutput;
28  import java.io.IOException;
29  import java.util.List;
30  import java.util.ArrayList;
31  
32  import com.google.common.base.Preconditions;
33  
34  /**
35   * A Filter that stops after the given row.  There is no "RowStopFilter" because
36   * the Scan spec allows you to specify a stop row.
37   *
38   * Use this filter to include the stop row, eg: [A,Z].
39   */
40  public class InclusiveStopFilter extends FilterBase {
41    private byte [] stopRowKey;
42    private boolean done = false;
43  
44    public InclusiveStopFilter() {
45      super();
46    }
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(KeyValue v) {
58      if (done) return ReturnCode.NEXT_ROW;
59      return ReturnCode.INCLUDE;
60    }
61  
62    public boolean filterRowKey(byte[] buffer, int offset, int length) {
63      if (buffer == null) {
64        //noinspection RedundantIfStatement
65        if (this.stopRowKey == null) {
66          return true; //filter...
67        }
68        return false;
69      }
70      // if stopRowKey is <= buffer, then true, filter row.
71      int cmp = Bytes.compareTo(stopRowKey, 0, stopRowKey.length,
72        buffer, offset, length);
73  
74      if(cmp < 0) {
75        done = true;
76      }
77      return done;
78    }
79  
80    public boolean filterAllRemaining() {
81      return done;
82    }
83  
84    public static Filter createFilterFromArguments (ArrayList<byte []> filterArguments) {
85      Preconditions.checkArgument(filterArguments.size() == 1,
86                                  "Expected 1 but got: %s", filterArguments.size());
87      byte [] stopRowKey = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
88      return new InclusiveStopFilter(stopRowKey);
89    }
90  
91    public void write(DataOutput out) throws IOException {
92      Bytes.writeByteArray(out, this.stopRowKey);
93    }
94  
95    public void readFields(DataInput in) throws IOException {
96      this.stopRowKey = Bytes.readByteArray(in);
97    }
98  
99    @Override
100   public String toString() {
101     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.stopRowKey);
102   }
103 }