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.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNull;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.CompareOperator;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Result;
033import org.apache.hadoop.hbase.client.ResultScanner;
034import org.apache.hadoop.hbase.client.Scan;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.testclassification.FilterTests;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046/**
047 * Test if Filter is incompatible with scan-limits
048 */
049@Category({ FilterTests.class, LargeTests.class })
050public class TestFilterWithScanLimits extends FilterTestingCluster {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestFilterWithScanLimits.class);
055
056  private static final Logger LOG = LoggerFactory.getLogger(TestFilterWithScanLimits.class);
057
058  private static final TableName tableName = TableName.valueOf("scanWithLimit");
059  private static final String columnFamily = "f1";
060
061  @Test
062  public void testScanWithLimit() {
063    int kv_number = 0;
064    try {
065      Scan scan = new Scan();
066      // set batch number as 2, which means each Result should contain 2 KVs at most
067      scan.setBatch(2);
068      SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),
069        Bytes.toBytes("c5"), CompareOperator.EQUAL, new SubstringComparator("2_c5"));
070
071      // add filter after batch defined
072      scan.setFilter(filter);
073      Table table = openTable(tableName);
074      ResultScanner scanner = table.getScanner(scan);
075      // Expect to get following row
076      // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>,
077      // row2 => <f1:c3, 2_c3>, <f1:c4, 2_c4>,
078      // row2 => <f1:c5, 2_c5>
079
080      for (Result result : scanner) {
081        for (Cell kv : result.listCells()) {
082          kv_number++;
083          LOG.debug(kv_number + ". kv: " + kv);
084        }
085      }
086
087      scanner.close();
088      table.close();
089    } catch (Exception e) {
090      // no correct result is expected
091      assertNotNull("No IncompatibleFilterException catched", e);
092    }
093    LOG.debug("check the fetched kv number");
094    assertEquals("We should not get result(s) returned.", 0, kv_number);
095  }
096
097  @BeforeClass
098  public static void prepareData() {
099    try {
100      createTable(tableName, columnFamily);
101      Table table = openTable(tableName);
102      List<Put> puts = new ArrayList<>();
103
104      // row1 => <f1:c1, 1_c1>, <f1:c2, 1_c2>, <f1:c3, 1_c3>, <f1:c4,1_c4>,
105      // <f1:c5, 1_c5>
106      // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>, <f1:c3, 2_c3>, <f1:c4,2_c4>,
107      // <f1:c5, 2_c5>
108      for (int i = 1; i < 4; i++) {
109        Put put = new Put(Bytes.toBytes("row" + i));
110        for (int j = 1; j < 6; j++) {
111          put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), Bytes.toBytes(i + "_c" + j));
112        }
113        puts.add(put);
114      }
115
116      table.put(puts);
117      table.close();
118    } catch (IOException e) {
119      assertNull("Exception found while putting data into table", e);
120    }
121  }
122
123}