| /** |
| * Copyright 2007 The Apache Software Foundation |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you 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. |
| */ |
| package org.apache.hadoop.hbase.filter; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.DataInputStream; |
| import java.io.DataOutputStream; |
| |
| import org.apache.hadoop.hbase.KeyValue; |
| import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; |
| import org.apache.hadoop.hbase.util.Bytes; |
| |
| import junit.framework.TestCase; |
| |
| /** |
| * Tests the value filter |
| */ |
| public class TestSingleColumnValueFilter extends TestCase { |
| private static final byte[] ROW = Bytes.toBytes("test"); |
| private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test"); |
| private static final byte [] COLUMN_QUALIFIER = Bytes.toBytes("foo"); |
| private static final byte[] VAL_1 = Bytes.toBytes("a"); |
| private static final byte[] VAL_2 = Bytes.toBytes("ab"); |
| private static final byte[] VAL_3 = Bytes.toBytes("abc"); |
| private static final byte[] VAL_4 = Bytes.toBytes("abcd"); |
| private static final byte[] FULLSTRING_1 = |
| Bytes.toBytes("The quick brown fox jumps over the lazy dog."); |
| private static final byte[] FULLSTRING_2 = |
| Bytes.toBytes("The slow grey fox trips over the lazy dog."); |
| private static final String QUICK_SUBSTR = "quick"; |
| private static final String QUICK_REGEX = ".+quick.+"; |
| |
| Filter basicFilter; |
| Filter substrFilter; |
| Filter regexFilter; |
| |
| @Override |
| protected void setUp() throws Exception { |
| super.setUp(); |
| basicFilter = basicFilterNew(); |
| substrFilter = substrFilterNew(); |
| regexFilter = regexFilterNew(); |
| } |
| |
| private Filter basicFilterNew() { |
| return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, |
| CompareOp.GREATER_OR_EQUAL, VAL_2); |
| } |
| |
| private Filter substrFilterNew() { |
| return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, |
| CompareOp.EQUAL, |
| new SubstringComparator(QUICK_SUBSTR)); |
| } |
| |
| private Filter regexFilterNew() { |
| return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, |
| CompareOp.EQUAL, |
| new RegexStringComparator(QUICK_REGEX)); |
| } |
| |
| private void basicFilterTests(SingleColumnValueFilter filter) |
| throws Exception { |
| KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2); |
| assertTrue("basicFilter1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_3); |
| assertTrue("basicFilter2", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_4); |
| assertTrue("basicFilter3", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| assertFalse("basicFilterNotNull", filter.filterRow()); |
| filter.reset(); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1); |
| assertTrue("basicFilter4", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2); |
| assertTrue("basicFilter4", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW); |
| assertFalse("basicFilterAllRemaining", filter.filterAllRemaining()); |
| assertTrue("basicFilterNotNull", filter.filterRow()); |
| filter.reset(); |
| filter.setLatestVersionOnly(false); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1); |
| assertTrue("basicFilter5", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2); |
| assertTrue("basicFilter5", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| assertFalse("basicFilterNotNull", filter.filterRow()); |
| } |
| |
| private void substrFilterTests(Filter filter) |
| throws Exception { |
| KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, |
| FULLSTRING_1); |
| assertTrue("substrTrue", |
| filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, |
| FULLSTRING_2); |
| assertTrue("substrFalse", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| assertFalse("substrFilterAllRemaining", filter.filterAllRemaining()); |
| assertFalse("substrFilterNotNull", filter.filterRow()); |
| } |
| |
| private void regexFilterTests(Filter filter) |
| throws Exception { |
| KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, |
| FULLSTRING_1); |
| assertTrue("regexTrue", |
| filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, |
| FULLSTRING_2); |
| assertTrue("regexFalse", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); |
| assertFalse("regexFilterAllRemaining", filter.filterAllRemaining()); |
| assertFalse("regexFilterNotNull", filter.filterRow()); |
| } |
| |
| private Filter serializationTest(Filter filter) |
| throws Exception { |
| // Decompose filter to bytes. |
| ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| DataOutputStream out = new DataOutputStream(stream); |
| filter.write(out); |
| out.close(); |
| byte[] buffer = stream.toByteArray(); |
| |
| // Recompose filter. |
| DataInputStream in = |
| new DataInputStream(new ByteArrayInputStream(buffer)); |
| Filter newFilter = new SingleColumnValueFilter(); |
| newFilter.readFields(in); |
| |
| return newFilter; |
| } |
| |
| /** |
| * Tests identification of the stop row |
| * @throws Exception |
| */ |
| public void testStop() throws Exception { |
| basicFilterTests((SingleColumnValueFilter)basicFilter); |
| substrFilterTests(substrFilter); |
| regexFilterTests(regexFilter); |
| } |
| |
| /** |
| * Tests serialization |
| * @throws Exception |
| */ |
| public void testSerialization() throws Exception { |
| Filter newFilter = serializationTest(basicFilter); |
| basicFilterTests((SingleColumnValueFilter)newFilter); |
| newFilter = serializationTest(substrFilter); |
| substrFilterTests(newFilter); |
| newFilter = serializationTest(regexFilter); |
| regexFilterTests(newFilter); |
| } |
| } |