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 static org.junit.Assert.assertTrue;
021
022import java.util.Set;
023import java.util.TreeSet;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.KeyValue;
026import org.apache.hadoop.hbase.testclassification.FilterTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.ClassRule;
030import org.junit.experimental.categories.Category;
031
032@SuppressWarnings("deprecation")
033@Category({ FilterTests.class, SmallTests.class })
034public class TestFirstKeyValueMatchingQualifiersFilter {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestFirstKeyValueMatchingQualifiersFilter.class);
039
040  private static final byte[] ROW = Bytes.toBytes("test");
041  private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
042  private static final byte[] COLUMN_QUALIFIER_1 = Bytes.toBytes("foo");
043  private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
044  private static final byte[] COLUMN_QUALIFIER_3 = Bytes.toBytes("foo_3");
045  private static final byte[] VAL_1 = Bytes.toBytes("a");
046
047  /**
048   * Test the functionality of
049   * {@link FirstKeyValueMatchingQualifiersFilter#filterCell(org.apache.hadoop.hbase.Cell)}
050   */
051  public void testFirstKeyMatchingQualifierFilter() throws Exception {
052    Set<byte[]> quals = new TreeSet<>(Bytes.BYTES_COMPARATOR);
053    quals.add(COLUMN_QUALIFIER_1);
054    quals.add(COLUMN_QUALIFIER_2);
055    Filter filter = new FirstKeyValueMatchingQualifiersFilter(quals);
056
057    // Match in first attempt
058    KeyValue cell;
059    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
060    assertTrue("includeAndSetFlag", filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
061    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
062    assertTrue("flagIsSetSkipToNextRow", filter.filterCell(cell) == Filter.ReturnCode.NEXT_ROW);
063
064    // A mismatch in first attempt and match in second attempt.
065    filter.reset();
066    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_3, VAL_1);
067    System.out.println(filter.filterCell(cell));
068    assertTrue("includeFlagIsUnset", filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
069    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
070    assertTrue("includeAndSetFlag", filter.filterCell(cell) == Filter.ReturnCode.INCLUDE);
071    cell = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_1, VAL_1);
072    assertTrue("flagIsSetSkipToNextRow", filter.filterCell(cell) == Filter.ReturnCode.NEXT_ROW);
073  }
074
075}