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. n
077   */
078  @Test
079  public void testSmallReversedScan01() throws IOException {
080    String[][] keysCases = new String[][] { { "d0", "d1", "d2", "d3" }, // all rowKeys fit in the
081                                                                        // last region.
082      { "a0", "a1", "a2", "a3" }, // all rowKeys fit in the first region.
083      { "a0", "b1", "c2", "d3" }, // each region with a rowKey
084    };
085
086    for (int caseIndex = 0; caseIndex < keysCases.length; caseIndex++) {
087      testSmallReversedScanInternal(keysCases[caseIndex]);
088      TEST_UTIL.truncateTable(TABLE_NAME);
089    }
090  }
091
092  private void testSmallReversedScanInternal(String[] inputRowKeys) throws IOException {
093    int rowCount = inputRowKeys.length;
094
095    for (int i = 0; i < rowCount; i++) {
096      Put put = new Put(Bytes.toBytes(inputRowKeys[i]));
097      put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(i));
098      htable.put(put);
099    }
100
101    Scan scan = new Scan();
102    scan.setReversed(true);
103    scan.setSmall(true);
104
105    ResultScanner scanner = htable.getScanner(scan);
106    Result r;
107    int value = rowCount;
108    while ((r = scanner.next()) != null) {
109      Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(--value));
110      Assert.assertArrayEquals(r.getRow(), Bytes.toBytes(inputRowKeys[value]));
111    }
112
113    Assert.assertEquals(0, value);
114  }
115
116  /**
117   * Corner case: HBase has 4 regions, (-oo,b),[b,c),[c,d),[d,+oo), and only rowKey with
118   * byte[]={0x00} locate in region (-oo,b) . test whether reversed small scanner will return
119   * infinity results with RowKey={0x00}. n
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.setSmall(true);
131
132    ResultScanner scanner = htable.getScanner(scan);
133    Result r;
134    int count = 1;
135    while ((r = scanner.next()) != null) {
136      Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(0));
137      Assert.assertArrayEquals(r.getRow(), new byte[] { (char) 0x00 });
138      Assert.assertTrue(--count >= 0);
139    }
140    Assert.assertEquals(0, count);
141  }
142}