1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.filter;
21
22 import com.google.common.base.Preconditions;
23 import com.google.protobuf.ByteString;
24 import com.google.protobuf.InvalidProtocolBufferException;
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.exceptions.DeserializationException;
28 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29 import org.apache.hadoop.hbase.util.Bytes;
30
31 import java.util.ArrayList;
32
33
34
35
36 @InterfaceAudience.Public
37 @InterfaceStability.Stable
38 public class PrefixFilter extends FilterBase {
39 protected byte [] prefix = null;
40 protected boolean passedPrefix = false;
41
42 public PrefixFilter(final byte [] prefix) {
43 this.prefix = prefix;
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
56
57
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 static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
71 Preconditions.checkArgument(filterArguments.size() == 1,
72 "Expected 1 but got: %s", filterArguments.size());
73 byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
74 return new PrefixFilter(prefix);
75 }
76
77
78
79
80 public byte [] toByteArray() {
81 FilterProtos.PrefixFilter.Builder builder =
82 FilterProtos.PrefixFilter.newBuilder();
83 if (this.prefix != null) builder.setPrefix(ByteString.copyFrom(this.prefix));
84 return builder.build().toByteArray();
85 }
86
87
88
89
90
91
92
93 public static PrefixFilter parseFrom(final byte [] pbBytes)
94 throws DeserializationException {
95 FilterProtos.PrefixFilter proto;
96 try {
97 proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
98 } catch (InvalidProtocolBufferException e) {
99 throw new DeserializationException(e);
100 }
101 return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
102 }
103
104
105
106
107
108
109 boolean areSerializedFieldsEqual(Filter o) {
110 if (o == this) return true;
111 if (!(o instanceof PrefixFilter)) return false;
112
113 PrefixFilter other = (PrefixFilter)o;
114 return Bytes.equals(this.getPrefix(), other.getPrefix());
115 }
116
117 @Override
118 public String toString() {
119 return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
120 }
121 }