1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.filter;
20
21 import com.google.protobuf.InvalidProtocolBufferException;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.exceptions.DeserializationException;
25 import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
26 import org.apache.hadoop.hbase.util.Bytes;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 @InterfaceAudience.Public
45 @InterfaceStability.Stable
46 public class SubstringComparator extends ByteArrayComparable {
47
48 private String substr;
49
50
51
52
53
54 public SubstringComparator(String substr) {
55 super(Bytes.toBytes(substr.toLowerCase()));
56 this.substr = substr.toLowerCase();
57 }
58
59 @Override
60 public byte[] getValue() {
61 return Bytes.toBytes(substr);
62 }
63
64 @Override
65 public int compareTo(byte[] value, int offset, int length) {
66 return Bytes.toString(value, offset, length).toLowerCase().contains(substr) ? 0
67 : 1;
68 }
69
70
71
72
73 public byte [] toByteArray() {
74 ComparatorProtos.SubstringComparator.Builder builder =
75 ComparatorProtos.SubstringComparator.newBuilder();
76 builder.setSubstr(this.substr);
77 return builder.build().toByteArray();
78 }
79
80
81
82
83
84
85
86 public static SubstringComparator parseFrom(final byte [] pbBytes)
87 throws DeserializationException {
88 ComparatorProtos.SubstringComparator proto;
89 try {
90 proto = ComparatorProtos.SubstringComparator.parseFrom(pbBytes);
91 } catch (InvalidProtocolBufferException e) {
92 throw new DeserializationException(e);
93 }
94 return new SubstringComparator(proto.getSubstr());
95 }
96
97
98
99
100
101
102 boolean areSerializedFieldsEqual(ByteArrayComparable other) {
103 if (other == this) return true;
104 if (!(other instanceof SubstringComparator)) return false;
105
106 SubstringComparator comparator = (SubstringComparator)other;
107 return super.areSerializedFieldsEqual(comparator)
108 && this.substr.equals(comparator.substr);
109 }
110
111 }