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