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