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.Cell;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HColumnDescriptor;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.HTestConst;
28  import org.apache.hadoop.hbase.KeyValue;
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.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * Test scan/get offset and limit settings within one row through HRegion API.
38   */
39  @Category(SmallTests.class)
40  public class TestIntraRowPagination {
41  
42    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
43  
44    /**
45     * Test from client side for scan with maxResultPerCF set
46     *
47     * @throws Exception
48     */
49    @Test
50    public void testScanLimitAndOffset() throws Exception {
51      //byte [] TABLE = HTestConst.DEFAULT_TABLE_BYTES;
52      byte [][] ROWS = HTestConst.makeNAscii(HTestConst.DEFAULT_ROW_BYTES, 2);
53      byte [][] FAMILIES = HTestConst.makeNAscii(HTestConst.DEFAULT_CF_BYTES, 3);
54      byte [][] QUALIFIERS = HTestConst.makeNAscii(HTestConst.DEFAULT_QUALIFIER_BYTES, 10);
55  
56      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(HTestConst.DEFAULT_TABLE_BYTES));
57      HRegionInfo info = new HRegionInfo(HTestConst.DEFAULT_TABLE, null, null, false);
58      for (byte[] family : FAMILIES) {
59        HColumnDescriptor hcd = new HColumnDescriptor(family);
60        htd.addFamily(hcd);
61      }
62      HRegion region =
63          HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
64      try {
65        Put put;
66        Scan scan;
67        Result result;
68        boolean toLog = true;
69  
70        List<Cell> kvListExp = new ArrayList<Cell>();
71  
72        int storeOffset = 1;
73        int storeLimit = 3;
74        for (int r = 0; r < ROWS.length; r++) {
75          put = new Put(ROWS[r]);
76          for (int c = 0; c < FAMILIES.length; c++) {
77            for (int q = 0; q < QUALIFIERS.length; q++) {
78              KeyValue kv = new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1,
79                  HTestConst.DEFAULT_VALUE_BYTES);
80              put.add(kv);
81              if (storeOffset <= q && q < storeOffset + storeLimit) {
82                kvListExp.add(kv);
83              }
84            }
85          }
86          region.put(put);
87        }
88  
89        scan = new Scan();
90        scan.setRowOffsetPerColumnFamily(storeOffset);
91        scan.setMaxResultsPerColumnFamily(storeLimit);
92        RegionScanner scanner = region.getScanner(scan);
93        List<Cell> kvListScan = new ArrayList<Cell>();
94        List<Cell> results = new ArrayList<Cell>();
95        while (scanner.next(results) || !results.isEmpty()) {
96          kvListScan.addAll(results);
97          results.clear();
98        }
99        result = Result.create(kvListScan);
100       TestScannersFromClientSide.verifyResult(result, kvListExp, toLog,
101           "Testing scan with storeOffset and storeLimit");
102     } finally {
103       region.close();
104     }
105   }
106 
107 }