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