1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
32
33
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 }