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 org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  import com.google.protobuf.InvalidProtocolBufferException;
28  
29  
30  /**
31   * This comparator is for use with SingleColumnValueFilter, for filtering based on
32   * the value of a given column. Use it to test if a given substring appears
33   * in a cell value in the column. The comparison is case insensitive.
34   * <p>
35   * Only EQUAL or NOT_EQUAL tests are valid with this comparator.
36   * <p>
37   * For example:
38   * <p>
39   * <pre>
40   * SingleColumnValueFilter scvf =
41   *   new SingleColumnValueFilter("col", CompareOp.EQUAL,
42   *     new SubstringComparator("substr"));
43   * </pre>
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Stable
47  public class SubstringComparator extends ByteArrayComparable {
48  
49    private String substr;
50  
51    /**
52     * Constructor
53     * @param substr the substring
54     */
55    public SubstringComparator(String substr) {
56      super(Bytes.toBytes(substr.toLowerCase()));
57      this.substr = substr.toLowerCase();
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().contains(substr) ? 0
68          : 1;
69    }
70  
71    /**
72     * @return The comparator serialized using pb
73     */
74    public byte [] toByteArray() {
75      ComparatorProtos.SubstringComparator.Builder builder =
76        ComparatorProtos.SubstringComparator.newBuilder();
77      builder.setSubstr(this.substr);
78      return builder.build().toByteArray();
79    }
80  
81    /**
82     * @param pbBytes A pb serialized {@link SubstringComparator} instance
83     * @return An instance of {@link SubstringComparator} made from <code>bytes</code>
84     * @throws DeserializationException
85     * @see #toByteArray
86     */
87    public static SubstringComparator parseFrom(final byte [] pbBytes)
88    throws DeserializationException {
89      ComparatorProtos.SubstringComparator proto;
90      try {
91        proto = ComparatorProtos.SubstringComparator.parseFrom(pbBytes);
92      } catch (InvalidProtocolBufferException e) {
93        throw new DeserializationException(e);
94      }
95      return new SubstringComparator(proto.getSubstr());
96    }
97  
98    /**
99     * @param other
100    * @return true if and only if the fields of the comparator that are serialized
101    * are equal to the corresponding fields in other.  Used for testing.
102    */
103   boolean areSerializedFieldsEqual(ByteArrayComparable other) {
104     if (other == this) return true;
105     if (!(other instanceof SubstringComparator)) return false;
106 
107     SubstringComparator comparator = (SubstringComparator)other;
108     return super.areSerializedFieldsEqual(comparator)
109       && this.substr.equals(comparator.substr);
110   }
111 
112 }