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