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
77
78 @Override
79 public Cell transformCell(Cell v) {
80 return v;
81 }
82
83 public boolean filterRow() {
84 return filterRow;
85 }
86
87 public void reset() {
88 filterRow = true;
89 }
90
91 public boolean filterAllRemaining() {
92 return passedPrefix;
93 }
94
95 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
96 Preconditions.checkArgument(filterArguments.size() == 1,
97 "Expected 1 but got: %s", filterArguments.size());
98 byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
99 return new PrefixFilter(prefix);
100 }
101
102
103
104
105 public byte [] toByteArray() {
106 FilterProtos.PrefixFilter.Builder builder =
107 FilterProtos.PrefixFilter.newBuilder();
108 if (this.prefix != null) builder.setPrefix(ByteStringer.wrap(this.prefix));
109 return builder.build().toByteArray();
110 }
111
112
113
114
115
116
117
118 public static PrefixFilter parseFrom(final byte [] pbBytes)
119 throws DeserializationException {
120 FilterProtos.PrefixFilter proto;
121 try {
122 proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
123 } catch (InvalidProtocolBufferException e) {
124 throw new DeserializationException(e);
125 }
126 return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
127 }
128
129
130
131
132
133
134 boolean areSerializedFieldsEqual(Filter o) {
135 if (o == this) return true;
136 if (!(o instanceof PrefixFilter)) return false;
137
138 PrefixFilter other = (PrefixFilter)o;
139 return Bytes.equals(this.getPrefix(), other.getPrefix());
140 }
141
142 @Override
143 public String toString() {
144 return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
145 }
146 }