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 static org.junit.jupiter.api.Assertions.assertArrayEquals; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Scan.ReadType; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.junit.jupiter.api.AfterAll; 031import org.junit.jupiter.api.AfterEach; 032import org.junit.jupiter.api.BeforeAll; 033import org.junit.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038@Tag(MediumTests.TAG) 039public class TestPreadReversedScanner { 040 041 public static final Logger LOG = LoggerFactory.getLogger(TestPreadReversedScanner.class); 042 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 043 044 private static final TableName TABLE_NAME = TableName.valueOf("testPreadSmall"); 045 private static final byte[] COLUMN_FAMILY = Bytes.toBytes("columnFamily"); 046 047 private static Table htable = null; 048 049 @BeforeAll 050 public static void setUpBeforeClass() throws Exception { 051 TEST_UTIL.startMiniCluster(1); 052 053 // create a table with 4 region: (-oo, b),[b,c),[c,d),[d,+oo) 054 byte[] bytes = Bytes.toBytes("bcd"); 055 byte[][] splitKeys = new byte[bytes.length][]; 056 057 for (int i = 0; i < bytes.length; i++) { 058 splitKeys[i] = new byte[] { bytes[i] }; 059 } 060 htable = TEST_UTIL.createTable(TABLE_NAME, COLUMN_FAMILY, splitKeys); 061 } 062 063 @AfterAll 064 public static void tearDownAfterClass() throws Exception { 065 TEST_UTIL.shutdownMiniCluster(); 066 } 067 068 @AfterEach 069 public void tearDown() throws IOException { 070 TEST_UTIL.truncateTable(TABLE_NAME); 071 } 072 073 /** 074 * all rowKeys are fit in the last region. 075 */ 076 @Test 077 public void testPreadReversedScan01() throws IOException { 078 String[][] keysCases = new String[][] { { "d0", "d1", "d2", "d3" }, // all rowKeys fit in the 079 // last region. 080 { "a0", "a1", "a2", "a3" }, // all rowKeys fit in the first region. 081 { "a0", "b1", "c2", "d3" }, // each region with a rowKey 082 }; 083 084 for (int caseIndex = 0; caseIndex < keysCases.length; caseIndex++) { 085 testPreadReversedScanInternal(keysCases[caseIndex]); 086 TEST_UTIL.truncateTable(TABLE_NAME); 087 } 088 } 089 090 private void testPreadReversedScanInternal(String[] inputRowKeys) throws IOException { 091 int rowCount = inputRowKeys.length; 092 093 for (int i = 0; i < rowCount; i++) { 094 Put put = new Put(Bytes.toBytes(inputRowKeys[i])); 095 put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(i)); 096 htable.put(put); 097 } 098 099 Scan scan = new Scan(); 100 scan.setReversed(true); 101 scan.setReadType(ReadType.PREAD); 102 103 ResultScanner scanner = htable.getScanner(scan); 104 Result r; 105 int value = rowCount; 106 while ((r = scanner.next()) != null) { 107 assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(--value)); 108 assertArrayEquals(r.getRow(), Bytes.toBytes(inputRowKeys[value])); 109 } 110 111 assertEquals(0, value); 112 } 113 114 /** 115 * Corner case: 116 * <p/> 117 * HBase has 4 regions, (-oo,b),[b,c),[c,d),[d,+oo), and only rowKey with byte[]={0x00} locate in 118 * region (-oo,b) . test whether reversed pread scanner will return infinity results with 119 * RowKey={0x00}. 120 */ 121 @Test 122 public void testSmallReversedScan02() throws IOException { 123 Put put = new Put(new byte[] { (char) 0x00 }); 124 put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(0)); 125 htable.put(put); 126 127 Scan scan = new Scan(); 128 scan.setCaching(1); 129 scan.setReversed(true); 130 scan.setReadType(ReadType.PREAD); 131 132 ResultScanner scanner = htable.getScanner(scan); 133 Result r; 134 int count = 1; 135 while ((r = scanner.next()) != null) { 136 assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(0)); 137 assertArrayEquals(r.getRow(), new byte[] { (char) 0x00 }); 138 assertTrue(--count >= 0); 139 } 140 assertEquals(0, count); 141 } 142}