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.DataOutput;
27  import java.io.IOException;
28  import java.io.DataInput;
29  import java.util.List;
30  
31  /**
32   * Pass results that have same row prefix.
33   */
34  public class PrefixFilter extends FilterBase {
35    protected byte [] prefix = null;
36    protected boolean passedPrefix = false;
37  
38    public PrefixFilter(final byte [] prefix) {
39      this.prefix = prefix;
40    }
41  
42    public PrefixFilter() {
43      super();
44    }
45  
46    public byte[] getPrefix() {
47      return prefix;
48    }
49  
50    public boolean filterRowKey(byte[] buffer, int offset, int length) {
51      if (buffer == null || this.prefix == null)
52        return true;
53      if (length < prefix.length)
54        return true;
55      // if they are equal, return false => pass row
56      // else return true, filter row
57      // if we are passed the prefix, set flag
58      int cmp = Bytes.compareTo(buffer, offset, this.prefix.length, this.prefix, 0,
59          this.prefix.length);
60      if(cmp > 0) {
61        passedPrefix = true;
62      }
63      return cmp != 0;
64    }
65  
66    public boolean filterAllRemaining() {
67      return passedPrefix;
68    }
69  
70    public void write(DataOutput out) throws IOException {
71      Bytes.writeByteArray(out, this.prefix);
72    }
73  
74    public void readFields(DataInput in) throws IOException {
75      this.prefix = Bytes.readByteArray(in);
76    }
77  }