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