View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.hadoop.hbase.HBaseTestCase;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HColumnDescriptor;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.Scan;
31  import org.apache.hadoop.hbase.io.compress.Compression;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @SuppressWarnings("deprecation")
38  @Category(SmallTests.class)
39  public class TestBlocksScanned extends HBaseTestCase {
40    private static byte [] TABLE = Bytes.toBytes("TestBlocksScanned");
41    private static byte [] FAMILY = Bytes.toBytes("family");
42    private static byte [] COL = Bytes.toBytes("col");
43    private static byte [] START_KEY = Bytes.toBytes("aaa");
44    private static byte [] END_KEY = Bytes.toBytes("zzz");
45    private static int BLOCK_SIZE = 70;
46  
47    private static HBaseTestingUtility TEST_UTIL = null;
48    private static HTableDescriptor TESTTABLEDESC = null;
49  
50     @Override
51     public void setUp() throws Exception {
52       super.setUp();
53  
54       TEST_UTIL = new HBaseTestingUtility();
55       TESTTABLEDESC = new HTableDescriptor(TableName.valueOf(TABLE));
56  
57       TESTTABLEDESC.addFamily(
58           new HColumnDescriptor(FAMILY)
59           .setMaxVersions(10)
60           .setBlockCacheEnabled(true)
61           .setBlocksize(BLOCK_SIZE)
62           .setCompressionType(Compression.Algorithm.NONE)
63       );
64     }
65  
66     @Test
67    public void testBlocksScanned() throws Exception {
68      HRegion r = createNewHRegion(TESTTABLEDESC, START_KEY, END_KEY,
69          TEST_UTIL.getConfiguration());
70      addContent(r, FAMILY, COL);
71      r.flushcache();
72  
73      // Do simple test of getting one row only first.
74      Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
75      scan.addColumn(FAMILY, COL);
76      scan.setMaxVersions(1);
77  
78      InternalScanner s = r.getScanner(scan);
79      List<KeyValue> results = new ArrayList<KeyValue>();
80      while (s.next(results));
81      s.close();
82  
83      int expectResultSize = 'z' - 'a';
84      Assert.assertEquals(expectResultSize, results.size());
85  
86      int kvPerBlock = (int) Math.ceil(BLOCK_SIZE / (double) results.get(0).getLength());
87      Assert.assertEquals(2, kvPerBlock);
88    }
89  
90  }