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.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.client.Admin;
025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.ResultScanner;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.jupiter.api.AfterAll;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.BeforeEach;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.junit.jupiter.api.TestInfo;
041
042@Tag(MediumTests.TAG)
043public class TestMaxResultsPerColumnFamily {
044
045  private static final byte[][] FAMILIES = { Bytes.toBytes("1"), Bytes.toBytes("2") };
046
047  private static final byte[][] VALUES =
048    { Bytes.toBytes("testValueOne"), Bytes.toBytes("testValueTwo") };
049
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051
052  @BeforeAll
053  public static void setUpBeforeClass() throws Exception {
054    UTIL.startMiniCluster(1);
055  }
056
057  @AfterAll
058  public static void tearDownAfterClass() throws Exception {
059    UTIL.shutdownMiniCluster();
060  }
061
062  private String name;
063
064  @BeforeEach
065  public void setTestName(TestInfo testInfo) {
066    this.name = testInfo.getTestMethod().get().getName();
067  }
068
069  @Test
070  public void testSetMaxResultsPerColumnFamilySimple() throws Exception {
071    TableName tableName = TableName.valueOf(name);
072    Admin admin = UTIL.getAdmin();
073    ColumnFamilyDescriptorBuilder cfBuilder0 =
074      ColumnFamilyDescriptorBuilder.newBuilder(FAMILIES[0]);
075
076    TableDescriptor tableDescriptor =
077      TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(cfBuilder0.build()).build();
078    admin.createTable(tableDescriptor);
079    try (Table table = UTIL.getConnection().getTable(tableName)) {
080
081      for (int i = 0; i < 30000; i++) {
082        byte[] ROW = Bytes.toBytes("" + i);
083        Put p = new Put(ROW);
084
085        p.addColumn(FAMILIES[0], Bytes.toBytes("" + 1), VALUES[1]);
086        p.addColumn(FAMILIES[0], Bytes.toBytes("" + 2), VALUES[0]);
087
088        table.put(p);
089      }
090    }
091
092    try (Table t = UTIL.getConnection().getTable(tableName)) {
093      int expected = 30000;
094
095      Scan limits = new Scan().setReadType(Scan.ReadType.PREAD);
096      limits.setMaxResultsPerColumnFamily(1);
097
098      int count1 = countScanRows(t, limits);
099      assertEquals(expected, count1);
100    }
101  }
102
103  static int countScanRows(Table t, Scan scan) throws Exception {
104
105    int count = 0;
106    try (ResultScanner scanner = t.getScanner(scan)) {
107      for (Result r : scanner) {
108        count++;
109      }
110    }
111    return count;
112  }
113}