View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.client;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.HColumnDescriptor;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.HTestConst;
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.regionserver.HRegion;
31  import org.apache.hadoop.hbase.regionserver.RegionScanner;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  /**
36   * Test scan/get offset and limit settings within one row through HRegion API.
37   */
38  @Category(SmallTests.class)
39  public class TestIntraRowPagination {
40  
41    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
42  
43    /**
44     * Test from client side for scan with maxResultPerCF set
45     *
46     * @throws Exception
47     */
48    @Test
49    public void testScanLimitAndOffset() throws Exception {
50      //byte [] TABLE = HTestConst.DEFAULT_TABLE_BYTES;
51      byte [][] ROWS = HTestConst.makeNAscii(HTestConst.DEFAULT_ROW_BYTES, 2);
52      byte [][] FAMILIES = HTestConst.makeNAscii(HTestConst.DEFAULT_CF_BYTES, 3);
53      byte [][] QUALIFIERS = HTestConst.makeNAscii(HTestConst.DEFAULT_QUALIFIER_BYTES, 10);
54  
55      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(HTestConst.DEFAULT_TABLE_BYTES));
56      HRegionInfo info = new HRegionInfo(HTestConst.DEFAULT_TABLE, null, null, false);
57      for (byte[] family : FAMILIES) {
58        HColumnDescriptor hcd = new HColumnDescriptor(family);
59        htd.addFamily(hcd);
60      }
61      HRegion region =
62          HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
63      try {
64        Put put;
65        Scan scan;
66        Result result;
67        boolean toLog = true;
68  
69        List<KeyValue> kvListExp = new ArrayList<KeyValue>();
70  
71        int storeOffset = 1;
72        int storeLimit = 3;
73        for (int r = 0; r < ROWS.length; r++) {
74          put = new Put(ROWS[r]);
75          for (int c = 0; c < FAMILIES.length; c++) {
76            for (int q = 0; q < QUALIFIERS.length; q++) {
77              KeyValue kv = new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1,
78                  HTestConst.DEFAULT_VALUE_BYTES);
79              put.add(kv);
80              if (storeOffset <= q && q < storeOffset + storeLimit) {
81                kvListExp.add(kv);
82              }
83            }
84          }
85          region.put(put);
86        }
87  
88        scan = new Scan();
89        scan.setRowOffsetPerColumnFamily(storeOffset);
90        scan.setMaxResultsPerColumnFamily(storeLimit);
91        RegionScanner scanner = region.getScanner(scan);
92        List<KeyValue> kvListScan = new ArrayList<KeyValue>();
93        List<KeyValue> results = new ArrayList<KeyValue>();
94        while (scanner.next(results) || !results.isEmpty()) {
95          kvListScan.addAll(results);
96          results.clear();
97        }
98        result = new Result(kvListScan);
99        TestScannersFromClientSide.verifyResult(result, kvListExp, toLog,
100           "Testing scan with storeOffset and storeLimit");
101     } finally {
102       region.close();
103     }
104   }
105 
106 }