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