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  
31  /**
32   * A Filter that stops after the given row.  There is no "RowStopFilter" because
33   * the Scan spec allows you to specify a stop row.
34   *
35   * Use this filter to include the stop row, eg: [A,Z].
36   */
37  public class InclusiveStopFilter extends FilterBase {
38    private byte [] stopRowKey;
39    private boolean done = false;
40  
41    public InclusiveStopFilter() {
42      super();
43    }
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 void write(DataOutput out) throws IOException {
76      Bytes.writeByteArray(out, this.stopRowKey);
77    }
78  
79    public void readFields(DataInput in) throws IOException {
80      this.stopRowKey = Bytes.readByteArray(in);
81    }
82  }