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