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