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  
30  /**
31   * This filter is used for selecting only those keys with columns that matches
32   * a particular prefix. For example, if prefix is 'an', it will pass keys will
33   * columns like 'and', 'anti' but not keys with columns like 'ball', 'act'.
34   */
35  public class ColumnPrefixFilter extends FilterBase {
36    protected byte [] prefix = null;
37  
38    public ColumnPrefixFilter() {
39      super();
40    }
41  
42    public ColumnPrefixFilter(final byte [] prefix) {
43      this.prefix = prefix;
44    }
45  
46    public byte[] getPrefix() {
47      return prefix;
48    }
49  
50    @Override
51    public ReturnCode filterKeyValue(KeyValue kv) {
52      if (this.prefix == null || kv.getBuffer() == null) {
53        return ReturnCode.INCLUDE;
54      } else {
55        return filterColumn(kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength());
56      }
57    }
58  
59    public ReturnCode filterColumn(byte[] buffer, int qualifierOffset, int qualifierLength) {
60      if (qualifierLength < prefix.length) {
61        int cmp = Bytes.compareTo(buffer, qualifierOffset, qualifierLength, this.prefix, 0,
62            qualifierLength);
63        if (cmp <= 0) {
64          return ReturnCode.SEEK_NEXT_USING_HINT;
65        } else {
66          return ReturnCode.NEXT_ROW;
67        }
68      } else {
69        int cmp = Bytes.compareTo(buffer, qualifierOffset, this.prefix.length, this.prefix, 0,
70            this.prefix.length);
71        if (cmp < 0) {
72          return ReturnCode.SEEK_NEXT_USING_HINT;
73        } else if (cmp > 0) {
74          return ReturnCode.NEXT_ROW;
75        } else {
76          return ReturnCode.INCLUDE;
77        }
78      }
79    }
80  
81    public void write(DataOutput out) throws IOException {
82      Bytes.writeByteArray(out, this.prefix);
83    }
84  
85    public void readFields(DataInput in) throws IOException {
86      this.prefix = Bytes.readByteArray(in);
87    }
88  
89    public KeyValue getNextKeyHint(KeyValue kv) {
90      return KeyValue.createFirstOnRow(
91          kv.getBuffer(), kv.getRowOffset(), kv.getRowLength(), kv.getBuffer(),
92          kv.getFamilyOffset(), kv.getFamilyLength(), prefix, 0, prefix.length);
93    }
94  }