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.KeyValue;
22  import org.apache.hadoop.hbase.SmallTests;
23  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  import static org.junit.Assert.*;
29  
30  import java.util.List;
31  import java.util.ArrayList;
32  
33  /**
34   * Tests for {@link SingleColumnValueExcludeFilter}. Because this filter
35   * extends {@link SingleColumnValueFilter}, only the added functionality is
36   * tested. That is, method filterKeyValue(KeyValue).
37   *
38   */
39  @Category(SmallTests.class)
40  public class TestSingleColumnValueExcludeFilter {
41    private static final byte[] ROW = Bytes.toBytes("test");
42    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
43    private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
44    private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
45    private static final byte[] VAL_1 = Bytes.toBytes("a");
46    private static final byte[] VAL_2 = Bytes.toBytes("ab");
47  
48    /**
49     * Test the overridden functionality of filterKeyValue(KeyValue)
50     * @throws Exception
51     */
52    @Test
53    public void testFilterKeyValue() throws Exception {
54      Filter filter = new SingleColumnValueExcludeFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
55          CompareOp.EQUAL, VAL_1);
56  
57      // A 'match' situation
58      List<KeyValue> kvs = new ArrayList<KeyValue>();
59      KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
60  
61      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
62      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1));
63      kvs.add (new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
64  
65      filter.filterRow(kvs);
66  
67      assertEquals("resultSize", kvs.size(), 2);
68      assertTrue("leftKV1", KeyValue.COMPARATOR.compare(kvs.get(0), kv) == 0);
69      assertTrue("leftKV2", KeyValue.COMPARATOR.compare(kvs.get(1), kv) == 0);
70      assertFalse("allRemainingWhenMatch", filter.filterAllRemaining());
71  
72      // A 'mismatch' situation
73      filter.reset();
74      // INCLUDE expected because test column has not yet passed
75      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
76      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
77      // Test column will pass (wont match), expect NEXT_ROW
78      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2);
79      assertTrue("testedMismatch", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
80      // After a mismatch (at least with LatestVersionOnly), subsequent columns are EXCLUDE
81      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
82      assertTrue("otherColumn", filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
83    }
84  
85  
86  }
87