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  @Test
055  public void testScanLimitAndOffset() throws Exception {
056    // byte [] TABLE = HTestConst.DEFAULT_TABLE_BYTES;
057    byte[][] ROWS = HTestConst.makeNAscii(HTestConst.DEFAULT_ROW_BYTES, 2);
058    byte[][] FAMILIES = HTestConst.makeNAscii(HTestConst.DEFAULT_CF_BYTES, 3);
059    byte[][] QUALIFIERS = HTestConst.makeNAscii(HTestConst.DEFAULT_QUALIFIER_BYTES, 10);
060
061    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(HTestConst.DEFAULT_TABLE_BYTES));
062    HRegionInfo info = new HRegionInfo(HTestConst.DEFAULT_TABLE, null, null, false);
063    for (byte[] family : FAMILIES) {
064      HColumnDescriptor hcd = new HColumnDescriptor(family);
065      htd.addFamily(hcd);
066    }
067    HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(),
068      TEST_UTIL.getConfiguration(), htd);
069    try {
070      Put put;
071      Scan scan;
072      Result result;
073      boolean toLog = true;
074
075      List<Cell> kvListExp = new ArrayList<>();
076
077      int storeOffset = 1;
078      int storeLimit = 3;
079      for (int r = 0; r < ROWS.length; r++) {
080        put = new Put(ROWS[r]);
081        for (int c = 0; c < FAMILIES.length; c++) {
082          for (int q = 0; q < QUALIFIERS.length; q++) {
083            KeyValue kv =
084              new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1, HTestConst.DEFAULT_VALUE_BYTES);
085            put.add(kv);
086            if (storeOffset <= q && q < storeOffset + storeLimit) {
087              kvListExp.add(kv);
088            }
089          }
090        }
091        region.put(put);
092      }
093
094      scan = new Scan();
095      scan.setRowOffsetPerColumnFamily(storeOffset);
096      scan.setMaxResultsPerColumnFamily(storeLimit);
097      RegionScanner scanner = region.getScanner(scan);
098      List<Cell> kvListScan = new ArrayList<>();
099      List<Cell> results = new ArrayList<>();
100      while (scanner.next(results) || !results.isEmpty()) {
101        kvListScan.addAll(results);
102        results.clear();
103      }
104      result = Result.create(kvListScan);
105      TestScannersFromClientSide.verifyResult(result, kvListExp, toLog,
106        "Testing scan with storeOffset and storeLimit");
107    } finally {
108      HBaseTestingUtility.closeRegionAndWAL(region);
109    }
110  }
111
112}