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