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.HBaseZeroCopyByteString;
24 import com.google.protobuf.InvalidProtocolBufferException;
25
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 import org.apache.hadoop.hbase.exceptions.DeserializationException;
29 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
30 import org.apache.hadoop.hbase.util.Bytes;
31
32 import java.util.ArrayList;
33
34
35
36
37 @InterfaceAudience.Public
38 @InterfaceStability.Stable
39 public class PrefixFilter extends FilterBase {
40 protected byte [] prefix = null;
41 protected boolean passedPrefix = false;
42 protected boolean filterRow = true;
43
44 public PrefixFilter(final byte [] prefix) {
45 this.prefix = prefix;
46 }
47
48 public byte[] getPrefix() {
49 return prefix;
50 }
51
52 public boolean filterRowKey(byte[] buffer, int offset, int length) {
53 if (buffer == null || this.prefix == null)
54 return true;
55 if (length < prefix.length)
56 return true;
57
58
59
60 int cmp = Bytes.compareTo(buffer, offset, this.prefix.length, this.prefix, 0,
61 this.prefix.length);
62 if ((!isReversed() && cmp > 0) || (isReversed() && cmp < 0)) {
63 passedPrefix = true;
64 }
65 filterRow = (cmp != 0);
66 return filterRow;
67 }
68
69 public boolean filterRow() {
70 return filterRow;
71 }
72
73 public void reset() {
74 filterRow = true;
75 }
76
77 public boolean filterAllRemaining() {
78 return passedPrefix;
79 }
80
81 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
82 Preconditions.checkArgument(filterArguments.size() == 1,
83 "Expected 1 but got: %s", filterArguments.size());
84 byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
85 return new PrefixFilter(prefix);
86 }
87
88
89
90
91 public byte [] toByteArray() {
92 FilterProtos.PrefixFilter.Builder builder =
93 FilterProtos.PrefixFilter.newBuilder();
94 if (this.prefix != null) builder.setPrefix(HBaseZeroCopyByteString.wrap(this.prefix));
95 return builder.build().toByteArray();
96 }
97
98
99
100
101
102
103
104 public static PrefixFilter parseFrom(final byte [] pbBytes)
105 throws DeserializationException {
106 FilterProtos.PrefixFilter proto;
107 try {
108 proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
109 } catch (InvalidProtocolBufferException e) {
110 throw new DeserializationException(e);
111 }
112 return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
113 }
114
115
116
117
118
119
120 boolean areSerializedFieldsEqual(Filter o) {
121 if (o == this) return true;
122 if (!(o instanceof PrefixFilter)) return false;
123
124 PrefixFilter other = (PrefixFilter)o;
125 return Bytes.equals(this.getPrefix(), other.getPrefix());
126 }
127
128 @Override
129 public String toString() {
130 return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
131 }
132 }