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