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