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