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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HBaseTestingUtility;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.*;
024import org.apache.hadoop.hbase.filter.FilterList.Operator;
025import org.apache.hadoop.hbase.testclassification.FilterTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.AfterClass;
029import org.junit.Assert;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in
041 * TestFilterList and TestFilterFromRegionSide instead.
042 */
043@Category({ MediumTests.class, FilterTests.class })
044public class TestFilterListOnMini {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestFilterListOnMini.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestFilterListOnMini.class);
051  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052
053  @Rule
054  public TestName name = new TestName();
055
056  @BeforeClass
057  public static void setUpBeforeClass() throws Exception {
058    TEST_UTIL.startMiniCluster(1);
059  }
060
061  @AfterClass
062  public static void tearDownAfterClass() throws Exception {
063    TEST_UTIL.shutdownMiniCluster();
064  }
065
066  @Test
067  public void testFiltersWithOR() throws Exception {
068    TableName tn = TableName.valueOf(name.getMethodName());
069    Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" });
070    byte[] CF1 = Bytes.toBytes("cf1");
071    byte[] CF2 = Bytes.toBytes("cf2");
072    Put put1 = new Put(Bytes.toBytes("0"));
073    put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0));
074    table.put(put1);
075    Put put2 = new Put(Bytes.toBytes("0"));
076    put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0));
077    table.put(put2);
078    FamilyFilter filterCF1 =
079        new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF1));
080    FamilyFilter filterCF2 =
081        new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF2));
082    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
083    filterList.addFilter(filterCF1);
084    filterList.addFilter(filterCF2);
085    Scan scan = new Scan();
086    scan.setFilter(filterList);
087    ResultScanner scanner = table.getScanner(scan);
088    LOG.info("Filter list: " + filterList);
089    for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
090      Assert.assertEquals(2, rr.size());
091    }
092  }
093
094  /**
095   * Test case for HBASE-21620
096   */
097  @Test
098  public void testColumnPrefixFilterConcatWithOR() throws Exception {
099    TableName tn = TableName.valueOf(name.getMethodName());
100    byte[] cf1 = Bytes.toBytes("f1");
101    byte[] row = Bytes.toBytes("row");
102    byte[] value = Bytes.toBytes("value");
103    String[] columns = new String[]{
104      "1544768273917010001_lt",
105      "1544768273917010001_w_1",
106      "1544768723910010001_ca_1",
107      "1544768723910010001_lt",
108      "1544768723910010001_ut_1",
109      "1544768723910010001_w_5",
110      "1544769779710010001_lt",
111      "1544769779710010001_w_5",
112      "1544769883529010001_lt",
113      "1544769883529010001_w_5",
114      "1544769915805010001_lt",
115      "1544769915805010001_w_5",
116      "1544779883529010001_lt",
117      "1544770422942010001_lt",
118      "1544770422942010001_w_5"
119    };
120    Table table = TEST_UTIL.createTable(tn, cf1);
121    for (int i = 0; i < columns.length; i++) {
122      Put put = new Put(row).addColumn(cf1, Bytes.toBytes(columns[i]), value);
123      table.put(put);
124    }
125    Scan scan = new Scan();
126    scan.withStartRow(row).withStopRow(row, true)
127        .setFilter(new FilterList(Operator.MUST_PASS_ONE,
128            new ColumnPrefixFilter(Bytes.toBytes("1544770422942010001_")),
129            new ColumnPrefixFilter(Bytes.toBytes("1544769883529010001_"))));
130    ResultScanner scanner = table.getScanner(scan);
131    Result result;
132    int resultCount = 0;
133    int cellCount = 0;
134    while ((result = scanner.next()) != null) {
135      cellCount += result.listCells().size();
136      resultCount++;
137    }
138    Assert.assertEquals(resultCount, 1);
139    Assert.assertEquals(cellCount, 4);
140  }
141}