View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.filter;
19  
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestFirstKeyValueMatchingQualifiersFilter extends TestCase {
32    private static final byte[] ROW = Bytes.toBytes("test");
33    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
34    private static final byte[] COLUMN_QUALIFIER_1 = Bytes.toBytes("foo");
35    private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
36    private static final byte[] COLUMN_QUALIFIER_3 = Bytes.toBytes("foo_3");
37    private static final byte[] VAL_1 = Bytes.toBytes("a");
38  
39    /**
40     * Test the functionality of
41     * {@link FirstKeyValueMatchingQualifiersFilter#filterKeyValue(KeyValue)}
42     * 
43     * @throws Exception
44     */
45    public void testFirstKeyMatchingQualifierFilter() throws Exception {
46      Set<byte[]> quals = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
47      quals.add(COLUMN_QUALIFIER_1);
48      quals.add(COLUMN_QUALIFIER_2);
49      Filter filter = new FirstKeyValueMatchingQualifiersFilter(quals);
50  
51      // Match in first attempt
52      KeyValue kv;
53      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
54      assertTrue("includeAndSetFlag",
55          filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
56      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
57      assertTrue("flagIsSetSkipToNextRow",
58          filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
59  
60      // A mismatch in first attempt and match in second attempt.
61      filter.reset();
62      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_3, VAL_1);
63      System.out.println(filter.filterKeyValue(kv));
64      assertTrue("includeFlagIsUnset",
65          filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
66      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
67      assertTrue("includeAndSetFlag",
68          filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE);
69      kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
70      assertTrue("flagIsSetSkipToNextRow",
71          filter.filterKeyValue(kv) == Filter.ReturnCode.NEXT_ROW);
72    }
73  
74  }