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 import java.util.List;
30 import java.util.ArrayList;
31
32 import com.google.common.base.Preconditions;
33
34
35
36
37 public class PrefixFilter extends FilterBase {
38 protected byte [] prefix = null;
39 protected boolean passedPrefix = false;
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 return cmp != 0;
67 }
68
69 public boolean filterAllRemaining() {
70 return passedPrefix;
71 }
72
73 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
74 Preconditions.checkArgument(filterArguments.size() == 1,
75 "Expected 1 but got: %s", filterArguments.size());
76 byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
77 return new PrefixFilter(prefix);
78 }
79
80 public void write(DataOutput out) throws IOException {
81 Bytes.writeByteArray(out, this.prefix);
82 }
83
84 public void readFields(DataInput in) throws IOException {
85 this.prefix = Bytes.readByteArray(in);
86 }
87
88 @Override
89 public String toString() {
90 return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
91 }
92 }