1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.hfile;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.Cell;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HColumnDescriptor;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.SmallTests;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.client.Put;
38 import org.apache.hadoop.hbase.client.Scan;
39 import org.apache.hadoop.hbase.regionserver.BloomType;
40 import org.apache.hadoop.hbase.regionserver.HRegion;
41 import org.apache.hadoop.hbase.regionserver.InternalScanner;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.junit.AfterClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.junit.runner.RunWith;
47 import org.junit.runners.Parameterized;
48 import org.junit.runners.Parameterized.Parameters;
49
50
51
52
53 @RunWith(Parameterized.class)
54 @Category(SmallTests.class)
55 public class TestScannerSelectionUsingKeyRange {
56 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility().createLocalHTU();
57 private static TableName TABLE = TableName.valueOf("myTable");
58 private static String FAMILY = "myCF";
59 private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
60 private static final int NUM_ROWS = 8;
61 private static final int NUM_COLS_PER_ROW = 5;
62 private static final int NUM_FILES = 2;
63 private static final Map<Object, Integer> TYPE_COUNT = new HashMap<Object, Integer>(3);
64 static {
65 TYPE_COUNT.put(BloomType.ROWCOL, 0);
66 TYPE_COUNT.put(BloomType.ROW, 0);
67 TYPE_COUNT.put(BloomType.NONE, 0);
68 }
69
70 private BloomType bloomType;
71 private int expectedCount;
72
73 @Parameters
74 public static Collection<Object[]> parameters() {
75 List<Object[]> params = new ArrayList<Object[]>();
76 for (Object type : TYPE_COUNT.keySet()) {
77 params.add(new Object[] { type, TYPE_COUNT.get(type) });
78 }
79 return params;
80 }
81
82 public TestScannerSelectionUsingKeyRange(Object type, Object count) {
83 bloomType = (BloomType)type;
84 expectedCount = (Integer) count;
85 }
86
87 @AfterClass
88 public static void tearDownAfterClass() throws Exception {
89 TEST_UTIL.cleanupTestDir();
90 }
91
92 @Test
93 public void testScannerSelection() throws IOException {
94 Configuration conf = TEST_UTIL.getConfiguration();
95 conf.setInt("hbase.hstore.compactionThreshold", 10000);
96 HColumnDescriptor hcd = new HColumnDescriptor(FAMILY_BYTES).setBlockCacheEnabled(true)
97 .setBloomFilterType(bloomType);
98 HTableDescriptor htd = new HTableDescriptor(TABLE);
99 htd.addFamily(hcd);
100 HRegionInfo info = new HRegionInfo(TABLE);
101 HRegion region = HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(), conf, htd);
102
103 for (int iFile = 0; iFile < NUM_FILES; ++iFile) {
104 for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
105 Put put = new Put(Bytes.toBytes("row" + iRow));
106 for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
107 put.add(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
108 Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
109 }
110 region.put(put);
111 }
112 region.flushcache();
113 }
114
115 Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
116 CacheConfig cacheConf = new CacheConfig(conf);
117 LruBlockCache cache = (LruBlockCache) cacheConf.getBlockCache();
118 cache.clearCache();
119 InternalScanner scanner = region.getScanner(scan);
120 List<Cell> results = new ArrayList<Cell>();
121 while (scanner.next(results)) {
122 }
123 scanner.close();
124 assertEquals(0, results.size());
125 Set<String> accessedFiles = cache.getCachedFileNamesForTest();
126 assertEquals(expectedCount, accessedFiles.size());
127 region.close();
128 }
129 }