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.HBaseTestingUtility;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.testclassification.MediumTests;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.junit.After;
027import org.junit.AfterClass;
028import org.junit.Assert;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036@Category(MediumTests.class)
037public class TestSmallReversedScanner {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestSmallReversedScanner.class);
042
043  public static final Logger LOG = LoggerFactory.getLogger(TestSmallReversedScanner.class);
044  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
045
046  private static final TableName TABLE_NAME = TableName.valueOf("testReversedSmall");
047  private static final byte[] COLUMN_FAMILY = Bytes.toBytes("columnFamily");
048
049  private static Table htable = null;
050
051  @BeforeClass
052  public static void setUpBeforeClass() throws Exception {
053    TEST_UTIL.startMiniCluster(1);
054
055    // create a table with 4 region: (-oo, b),[b,c),[c,d),[d,+oo)
056    byte[] bytes = Bytes.toBytes("bcd");
057    byte[][] splitKeys = new byte[bytes.length][];
058
059    for (int i = 0; i < bytes.length; i++) {
060      splitKeys[i] = new byte[] { bytes[i] };
061    }
062    htable = TEST_UTIL.createTable(TABLE_NAME, COLUMN_FAMILY, splitKeys);
063  }
064
065  @AfterClass
066  public static void tearDownAfterClass() throws Exception {
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  @After
071  public void tearDown() throws IOException {
072    TEST_UTIL.truncateTable(TABLE_NAME);
073  }
074
075  /**
076   * all rowKeys are fit in the last region.
077   * @throws IOException
078   */
079  @Test
080  public void testSmallReversedScan01() throws IOException {
081    String[][] keysCases = new String[][] {
082      { "d0", "d1", "d2", "d3" }, // all rowKeys fit in the 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      testSmallReversedScanInternal(keysCases[caseIndex]);
089      TEST_UTIL.truncateTable(TABLE_NAME);
090    }
091  }
092
093  private void testSmallReversedScanInternal(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.setSmall(true);
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   *  HBase has 4 regions, (-oo,b),[b,c),[c,d),[d,+oo), and only rowKey with byte[]={0x00} locate in region (-oo,b) .
120   *  test whether reversed small scanner will return infinity results with RowKey={0x00}.
121   * @throws IOException
122   */
123  @Test
124  public void testSmallReversedScan02() throws IOException {
125    Put put = new Put(new byte[] { (char) 0x00 });
126    put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(0));
127    htable.put(put);
128
129    Scan scan = new Scan();
130    scan.setCaching(1);
131    scan.setReversed(true);
132    scan.setSmall(true);
133
134    ResultScanner scanner = htable.getScanner(scan);
135    Result r;
136    int count = 1;
137    while ((r = scanner.next()) != null) {
138      Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(0));
139      Assert.assertArrayEquals(r.getRow(), new byte[] { (char) 0x00 });
140      Assert.assertTrue(--count >= 0);
141    }
142    Assert.assertEquals(0, count);
143  }
144}