View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.filter;
20  
21  import java.util.Locale;
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  import com.google.protobuf.InvalidProtocolBufferException;
29  
30
31  /**
32   * This comparator is for use with SingleColumnValueFilter, for filtering based on
33   * the value of a given column. Use it to test if a given substring appears
34   * in a cell value in the column. The comparison is case insensitive.
35   * <p>
36   * Only EQUAL or NOT_EQUAL tests are valid with this comparator.
37   * <p>
38   * For example:
39   * <p>
40   * <pre>
41   * SingleColumnValueFilter scvf =
42   *   new SingleColumnValueFilter("col", CompareOp.EQUAL,
43   *     new SubstringComparator("substr"));
44   * </pre>
45   */
46  @InterfaceAudience.Public
47  @InterfaceStability.Stable
48  public class SubstringComparator extends ByteArrayComparable {
49
50    private String substr;
51
52    /**
53     * Constructor
54     * @param substr the substring
55     */
56    public SubstringComparator(String substr) {
57      super(Bytes.toBytes(substr.toLowerCase(Locale.ROOT)));
58      this.substr = substr.toLowerCase(Locale.ROOT);
59    }
60
61    @Override
62    public byte[] getValue() {
63      return Bytes.toBytes(substr);
64    }
65
66    @Override
67    public int compareTo(byte[] value, int offset, int length) {
68      return Bytes.toString(value, offset, length).toLowerCase(Locale.ROOT).contains(substr) ? 0
69          : 1;
70    }
71
72    /**
73     * @return The comparator serialized using pb
74     */
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     * @param pbBytes A pb serialized {@link SubstringComparator} instance
84     * @return An instance of {@link SubstringComparator} made from <code>bytes</code>
85     * @throws DeserializationException
86     * @see #toByteArray
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    * @param other
101    * @return true if and only if the fields of the comparator that are serialized
102    * are equal to the corresponding fields in other.  Used for testing.
103    */
104   boolean areSerializedFieldsEqual(ByteArrayComparable other) {
105     if (other == this) return true;
106     if (!(other instanceof SubstringComparator)) return false;
107
108     SubstringComparator comparator = (SubstringComparator)other;
109     return super.areSerializedFieldsEqual(comparator)
110       && this.substr.equals(comparator.substr);
111   }
112
113 }