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.MediumTests;
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, MediumTests.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
057      .getLogger(TestFilterWithScanLimits.class);
058
059  private static final TableName tableName = TableName.valueOf("scanWithLimit");
060  private static final String columnFamily = "f1";
061
062  @Test
063  public void testScanWithLimit() {
064    int kv_number = 0;
065    try {
066      Scan scan = new Scan();
067      // set batch number as 2, which means each Result should contain 2 KVs at most
068      scan.setBatch(2);
069      SingleColumnValueFilter filter = new SingleColumnValueFilter(
070          Bytes.toBytes(columnFamily), Bytes.toBytes("c5"),
071      CompareOperator .EQUAL, new SubstringComparator("2_c5"));
072
073      // add filter after batch defined
074      scan.setFilter(filter);
075      Table table = openTable(tableName);
076      ResultScanner scanner = table.getScanner(scan);
077      // Expect to get following row
078      // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>,
079      // row2 => <f1:c3, 2_c3>, <f1:c4, 2_c4>,
080      // row2 => <f1:c5, 2_c5>
081
082      for (Result result : scanner) {
083        for (Cell kv : result.listCells()) {
084          kv_number++;
085          LOG.debug(kv_number + ". kv: " + kv);
086        }
087      }
088
089      scanner.close();
090      table.close();
091    } catch (Exception e) {
092      // no correct result is expected
093      assertNotNull("No IncompatibleFilterException catched", e);
094    }
095    LOG.debug("check the fetched kv number");
096    assertEquals("We should not get result(s) returned.", 0, kv_number);
097  }
098
099  @BeforeClass
100  public static void prepareData() {
101    try {
102      createTable(tableName, columnFamily);
103      Table table = openTable(tableName);
104      List<Put> puts = new ArrayList<>();
105
106      // row1 => <f1:c1, 1_c1>, <f1:c2, 1_c2>, <f1:c3, 1_c3>, <f1:c4,1_c4>,
107      // <f1:c5, 1_c5>
108      // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>, <f1:c3, 2_c3>, <f1:c4,2_c4>,
109      // <f1:c5, 2_c5>
110      for (int i = 1; i < 4; i++) {
111        Put put = new Put(Bytes.toBytes("row" + i));
112        for (int j = 1; j < 6; j++) {
113          put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), Bytes.toBytes(i + "_c" + j));
114        }
115        puts.add(put);
116      }
117
118      table.put(puts);
119      table.close();
120    } catch (IOException e) {
121      assertNull("Exception found while putting data into table", e);
122    }
123  }
124
125}