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 java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.IOException;
26 import java.util.ArrayList;
27
28 import org.apache.hadoop.hbase.KeyValue;
29 import org.apache.hadoop.hbase.util.Bytes;
30
31 import com.google.common.base.Preconditions;
32
33
34
35
36 public class PrefixFilter extends FilterBase {
37 protected byte [] prefix = null;
38 protected boolean passedPrefix = false;
39 protected boolean filterRow = true;
40
41 public PrefixFilter(final byte [] prefix) {
42 this.prefix = prefix;
43 }
44
45 public PrefixFilter() {
46 super();
47 }
48
49 public byte[] getPrefix() {
50 return prefix;
51 }
52
53 public boolean filterRowKey(byte[] buffer, int offset, int length) {
54 if (buffer == null || this.prefix == null)
55 return true;
56 if (length < prefix.length)
57 return true;
58
59
60
61 int cmp = Bytes.compareTo(buffer, offset, this.prefix.length, this.prefix, 0,
62 this.prefix.length);
63 if(cmp > 0) {
64 passedPrefix = true;
65 }
66 filterRow = (cmp != 0);
67 return filterRow;
68 }
69
70 @Override
71 public ReturnCode filterKeyValue(KeyValue ignored) {
72 return filterRow ? ReturnCode.NEXT_ROW : ReturnCode.INCLUDE;
73 }
74
75 public boolean filterRow() {
76 return filterRow;
77 }
78
79 public void reset() {
80 filterRow = true;
81 }
82
83 public boolean filterAllRemaining() {
84 return passedPrefix;
85 }
86
87 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
88 Preconditions.checkArgument(filterArguments.size() == 1,
89 "Expected 1 but got: %s", filterArguments.size());
90 byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
91 return new PrefixFilter(prefix);
92 }
93
94 public void write(DataOutput out) throws IOException {
95 Bytes.writeByteArray(out, this.prefix);
96 }
97
98 public void readFields(DataInput in) throws IOException {
99 this.prefix = Bytes.readByteArray(in);
100 }
101
102 @Override
103 public String toString() {
104 return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
105 }
106 }