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.client;
019
020import java.util.ArrayList;
021import java.util.List;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HColumnDescriptor;
026import org.apache.hadoop.hbase.HRegionInfo;
027import org.apache.hadoop.hbase.HTableDescriptor;
028import org.apache.hadoop.hbase.HTestConst;
029import org.apache.hadoop.hbase.KeyValue;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.regionserver.HRegion;
032import org.apache.hadoop.hbase.regionserver.RegionScanner;
033import org.apache.hadoop.hbase.testclassification.ClientTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039/**
040 * Test scan/get offset and limit settings within one row through HRegion API.
041 */
042@Category({SmallTests.class, ClientTests.class})
043public class TestIntraRowPagination {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestIntraRowPagination.class);
048
049  private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050
051  /**
052   * Test from client side for scan with maxResultPerCF set
053   *
054   * @throws Exception
055   */
056  @Test
057  public void testScanLimitAndOffset() throws Exception {
058    //byte [] TABLE = HTestConst.DEFAULT_TABLE_BYTES;
059    byte [][] ROWS = HTestConst.makeNAscii(HTestConst.DEFAULT_ROW_BYTES, 2);
060    byte [][] FAMILIES = HTestConst.makeNAscii(HTestConst.DEFAULT_CF_BYTES, 3);
061    byte [][] QUALIFIERS = HTestConst.makeNAscii(HTestConst.DEFAULT_QUALIFIER_BYTES, 10);
062
063    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(HTestConst.DEFAULT_TABLE_BYTES));
064    HRegionInfo info = new HRegionInfo(HTestConst.DEFAULT_TABLE, null, null, false);
065    for (byte[] family : FAMILIES) {
066      HColumnDescriptor hcd = new HColumnDescriptor(family);
067      htd.addFamily(hcd);
068    }
069    HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(),
070        TEST_UTIL.getConfiguration(), htd);
071    try {
072      Put put;
073      Scan scan;
074      Result result;
075      boolean toLog = true;
076
077      List<Cell> kvListExp = new ArrayList<>();
078
079      int storeOffset = 1;
080      int storeLimit = 3;
081      for (int r = 0; r < ROWS.length; r++) {
082        put = new Put(ROWS[r]);
083        for (int c = 0; c < FAMILIES.length; c++) {
084          for (int q = 0; q < QUALIFIERS.length; q++) {
085            KeyValue kv = new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1,
086                HTestConst.DEFAULT_VALUE_BYTES);
087            put.add(kv);
088            if (storeOffset <= q && q < storeOffset + storeLimit) {
089              kvListExp.add(kv);
090            }
091          }
092        }
093        region.put(put);
094      }
095
096      scan = new Scan();
097      scan.setRowOffsetPerColumnFamily(storeOffset);
098      scan.setMaxResultsPerColumnFamily(storeLimit);
099      RegionScanner scanner = region.getScanner(scan);
100      List<Cell> kvListScan = new ArrayList<>();
101      List<Cell> results = new ArrayList<>();
102      while (scanner.next(results) || !results.isEmpty()) {
103        kvListScan.addAll(results);
104        results.clear();
105      }
106      result = Result.create(kvListScan);
107      TestScannersFromClientSide.verifyResult(result, kvListExp, toLog,
108          "Testing scan with storeOffset and storeLimit");
109    } finally {
110      HBaseTestingUtility.closeRegionAndWAL(region);
111    }
112  }
113
114}