1 /** 2 * Copyright 2010 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.filter; 21 22 import org.apache.hadoop.hbase.util.Bytes; 23 24 import java.io.DataInput; 25 import java.io.DataOutput; 26 import java.io.IOException; 27 28 /** 29 * This comparator is for use with SingleColumnValueFilter, for filtering based on 30 * the value of a given column. Use it to test if a given substring appears 31 * in a cell value in the column. The comparison is case insensitive. 32 * <p> 33 * Only EQUAL or NOT_EQUAL tests are valid with this comparator. 34 * <p> 35 * For example: 36 * <p> 37 * <pre> 38 * SingleColumnValueFilter scvf = 39 * new SingleColumnValueFilter("col", CompareOp.EQUAL, 40 * new SubstringComparator("substr")); 41 * </pre> 42 */ 43 public class SubstringComparator extends WritableByteArrayComparable { 44 45 private String substr; 46 47 /** Nullary constructor for Writable, do not use */ 48 public SubstringComparator() { 49 super(); 50 } 51 52 /** 53 * Constructor 54 * @param substr the substring 55 */ 56 public SubstringComparator(String substr) { 57 super(Bytes.toBytes(substr.toLowerCase())); 58 this.substr = substr.toLowerCase(); 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().contains(substr) ? 0 69 : 1; 70 } 71 72 @Override 73 public void readFields(DataInput in) throws IOException { 74 String substr = in.readUTF(); 75 this.value = Bytes.toBytes(substr); 76 this.substr = substr; 77 } 78 79 @Override 80 public void write(DataOutput out) throws IOException { 81 out.writeUTF(substr); 82 } 83 84 }