001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import java.util.Set;
021import java.util.TreeSet;
022import junit.framework.TestCase;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.KeyValue;
025import org.apache.hadoop.hbase.testclassification.FilterTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.ClassRule;
029import org.junit.experimental.categories.Category;
030
031@SuppressWarnings("deprecation")
032@Category({FilterTests.class, SmallTests.class})
033public class TestFirstKeyValueMatchingQualifiersFilter extends TestCase {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037      HBaseClassTestRule.forClass(TestFirstKeyValueMatchingQualifiersFilter.class);
038
039  private static final byte[] ROW = Bytes.toBytes("test");
040  private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
041  private static final byte[] COLUMN_QUALIFIER_1 = Bytes.toBytes("foo");
042  private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
043  private static final byte[] COLUMN_QUALIFIER_3 = Bytes.toBytes("foo_3");
044  private static final byte[] VAL_1 = Bytes.toBytes("a");
045
046  /**
047   * Test the functionality of
048   * {@link FirstKeyValueMatchingQualifiersFilter#filterCell(org.apache.hadoop.hbase.Cell)}
049   *
050   * @throws Exception
051   */
052  public void testFirstKeyMatchingQualifierFilter() throws Exception {
053    Set<byte[]> quals = new TreeSet<>(Bytes.BYTES_COMPARATOR);
054    quals.add(COLUMN_QUALIFIER_1);
055    quals.add(COLUMN_QUALIFIER_2);
056    Filter filter = new FirstKeyValueMatchingQualifiersFilter(quals);
057
058    // Match in first attempt
059    KeyValue cell;
060    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
061    assertTrue("includeAndSetFlag",
062        filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
063    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
064    assertTrue("flagIsSetSkipToNextRow",
065        filter.filterCell(cell) == Filter.ReturnCode.NEXT_ROW);
066
067    // A mismatch in first attempt and match in second attempt.
068    filter.reset();
069    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_3, VAL_1);
070    System.out.println(filter.filterCell(cell));
071    assertTrue("includeFlagIsUnset",
072        filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
073    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
074    assertTrue("includeAndSetFlag",
075        filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
076    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
077    assertTrue("flagIsSetSkipToNextRow",
078        filter.filterCell(cell) == Filter.ReturnCode.NEXT_ROW);
079  }
080
081}