HBASE-10848 Filter SingleColumnValueFilter combined with NullComparator does not work (Fabien)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1583511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2014-04-01 01:53:01 +00:00
parent bc42dc04ac
commit 0f0d6c1754
3 changed files with 99 additions and 9 deletions

View File

@ -56,7 +56,7 @@ public class NullComparator extends ByteArrayComparable {
@Override
public int compareTo(byte[] value, int offset, int length) {
throw new UnsupportedOperationException();
return compareTo(value);
}
/**

View File

@ -0,0 +1,73 @@
/**
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package org.apache.hadoop.hbase.filter;
import org.apache.hadoop.hbase.SmallTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category(SmallTests.class)
public class TestNullComparator {
@Test
public void testNullValue()
{
// given
byte[] value = null;
NullComparator comparator = new NullComparator();
// when
int comp1 = comparator.compareTo(value);
int comp2 = comparator.compareTo(value, 5, 15);
// then
Assert.assertEquals(0, comp1);
Assert.assertEquals(0, comp2);
}
@Test
public void testNonNullValue() {
// given
byte[] value = new byte[] { 0, 1, 2, 3, 4, 5 };
NullComparator comparator = new NullComparator();
// when
int comp1 = comparator.compareTo(value);
int comp2 = comparator.compareTo(value, 1, 3);
// then
Assert.assertEquals(1, comp1);
Assert.assertEquals(1, comp2);
}
@Test
public void testEmptyValue() {
// given
byte[] value = new byte[] { 0 };
NullComparator comparator = new NullComparator();
// when
int comp1 = comparator.compareTo(value);
int comp2 = comparator.compareTo(value, 1, 3);
// then
Assert.assertEquals(1, comp1);
Assert.assertEquals(1, comp2);
}
}

View File

@ -18,23 +18,19 @@
*/
package org.apache.hadoop.hbase.filter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.regex.Pattern;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.junit.Assert.*;
/**
* Tests the value filter
*/
@ -56,6 +52,7 @@ public class TestSingleColumnValueFilter {
private static final Pattern QUICK_PATTERN = Pattern.compile("QuIcK", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Filter basicFilter;
Filter nullFilter;
Filter substrFilter;
Filter regexFilter;
Filter regexPatternFilter;
@ -63,6 +60,7 @@ public class TestSingleColumnValueFilter {
@Before
public void setUp() throws Exception {
basicFilter = basicFilterNew();
nullFilter = nullFilterNew();
substrFilter = substrFilterNew();
regexFilter = regexFilterNew();
regexPatternFilter = regexFilterNew(QUICK_PATTERN);
@ -73,6 +71,11 @@ public class TestSingleColumnValueFilter {
CompareOp.GREATER_OR_EQUAL, VAL_2);
}
private Filter nullFilterNew() {
return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, CompareOp.NOT_EQUAL,
new NullComparator());
}
private Filter substrFilterNew() {
return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
CompareOp.EQUAL,
@ -116,6 +119,17 @@ public class TestSingleColumnValueFilter {
assertFalse("basicFilterNotNull", filter.filterRow());
}
private void nullFilterTests(Filter filter) throws Exception {
((SingleColumnValueFilter) filter).setFilterIfMissing(true);
KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, FULLSTRING_1);
assertTrue("null1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
assertFalse("null1FilterRow", filter.filterRow());
filter.reset();
kv = new KeyValue(ROW, COLUMN_FAMILY, Bytes.toBytes("qual2"), FULLSTRING_2);
assertTrue("null2", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
assertTrue("null2FilterRow", filter.filterRow());
}
private void substrFilterTests(Filter filter)
throws Exception {
KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER,
@ -169,6 +183,7 @@ public class TestSingleColumnValueFilter {
@Test
public void testStop() throws Exception {
basicFilterTests((SingleColumnValueFilter) basicFilter);
nullFilterTests(nullFilter);
substrFilterTests(substrFilter);
regexFilterTests(regexFilter);
regexPatternFilterTests(regexPatternFilter);
@ -182,6 +197,8 @@ public class TestSingleColumnValueFilter {
public void testSerialization() throws Exception {
Filter newFilter = serializationTest(basicFilter);
basicFilterTests((SingleColumnValueFilter)newFilter);
newFilter = serializationTest(nullFilter);
nullFilterTests(newFilter);
newFilter = serializationTest(substrFilter);
substrFilterTests(newFilter);
newFilter = serializationTest(regexFilter);