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 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   * This comparator is for use with SingleColumnValueFilter, for filtering based on
31   * the value of a given column. Use it to test if a given substring appears
32   * in a cell value in the column. The comparison is case insensitive.
33   * <p>
34   * Only EQUAL or NOT_EQUAL tests are valid with this comparator.
35   * <p>
36   * For example:
37   * <p>
38   * <pre>
39   * SingleColumnValueFilter scvf =
40   *   new SingleColumnValueFilter("col", CompareOp.EQUAL,
41   *     new SubstringComparator("substr"));
42   * </pre>
43   */
44  @InterfaceAudience.Public
45  @InterfaceStability.Stable
46  public class SubstringComparator extends ByteArrayComparable {
47  
48    private String substr;
49  
50    /**
51     * Constructor
52     * @param substr the substring
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     * @return The comparator serialized using pb
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     * @param pbBytes A pb serialized {@link SubstringComparator} instance
82     * @return An instance of {@link SubstringComparator} made from <code>bytes</code>
83     * @throws DeserializationException
84     * @see #toByteArray
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     * @param other
99     * @return true if and only if the fields of the comparator that are serialized
100    * are equal to the corresponding fields in other.  Used for testing.
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 }