View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.testclassification.MediumTests;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.junit.After;
27  import org.junit.AfterClass;
28  import org.junit.Assert;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import java.io.IOException;
34  
35  @Category(MediumTests.class)
36  public class TestSmallReversedScanner {
37    public static final Log LOG = LogFactory.getLog(TestSmallReversedScanner.class);
38    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
39  
40    private static final TableName TABLE_NAME = TableName.valueOf("testReversedSmall");
41    private static final byte[] COLUMN_FAMILY = Bytes.toBytes("columnFamily");
42  
43    private static Table htable = null;
44  
45    @BeforeClass
46    public static void setUpBeforeClass() throws Exception {
47      TEST_UTIL.startMiniCluster(1);
48  
49      // create a table with 4 region: (-oo, b),[b,c),[c,d),[d,+oo)
50      byte[] bytes = Bytes.toBytes("bcd");
51      byte[][] splitKeys = new byte[bytes.length][];
52  
53      for (int i = 0; i < bytes.length; i++) {
54        splitKeys[i] = new byte[] { bytes[i] };
55      }
56      htable = TEST_UTIL.createTable(TABLE_NAME, COLUMN_FAMILY, splitKeys);
57    }
58  
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      TEST_UTIL.shutdownMiniCluster();
62    }
63  
64    @After
65    public void tearDown() throws IOException {
66      TEST_UTIL.deleteTableData(TABLE_NAME);
67    }
68  
69    /**
70     * all rowKeys are fit in the last region.
71     * @throws IOException
72     */
73    @Test
74    public void testSmallReversedScan01() throws IOException {
75      String[][] keysCases = new String[][] {
76              { "d0", "d1", "d2", "d3" }, // all rowKeys fit in the last region.
77              { "a0", "a1", "a2", "a3" }, // all rowKeys fit in the first region.
78              { "a0", "b1", "c2", "d3" }, // each region with a rowKey
79      };
80  
81      for (int caseIndex = 0; caseIndex < keysCases.length; caseIndex++) {
82        testSmallReversedScanInternal(keysCases[caseIndex]);
83        TEST_UTIL.deleteTableData(TABLE_NAME);
84      }
85    }
86  
87    private void testSmallReversedScanInternal(String[] inputRowKeys) throws IOException {
88      int rowCount = inputRowKeys.length;
89  
90      for (int i = 0; i < rowCount; i++) {
91        Put put = new Put(Bytes.toBytes(inputRowKeys[i]));
92        put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(i));
93        htable.put(put);
94      }
95  
96      Scan scan = new Scan();
97      scan.setReversed(true);
98      scan.setSmall(true);
99  
100     ResultScanner scanner = htable.getScanner(scan);
101     Result r;
102     int value = rowCount;
103     while ((r = scanner.next()) != null) {
104       Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(--value));
105       Assert.assertArrayEquals(r.getRow(), Bytes.toBytes(inputRowKeys[value]));
106     }
107 
108     Assert.assertEquals(value, 0);
109   }
110 
111   /**
112    * Corner case:
113    *  HBase has 4 regions, (-oo,b),[b,c),[c,d),[d,+oo), and only rowKey with byte[]={0x00} locate in region (-oo,b) .
114    *  test whether reversed small scanner will return infinity results with RowKey={0x00}.
115    * @throws IOException
116    */
117   @Test
118   public void testSmallReversedScan02() throws IOException {
119     Put put = new Put(new byte[] { (char) 0x00 });
120     put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(0));
121     htable.put(put);
122 
123     Scan scan = new Scan();
124     scan.setCaching(1);
125     scan.setReversed(true);
126     scan.setSmall(true);
127 
128     ResultScanner scanner = htable.getScanner(scan);
129     Result r;
130     int count = 1;
131     while ((r = scanner.next()) != null) {
132       Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(0));
133       Assert.assertArrayEquals(r.getRow(), new byte[] { (char) 0x00 });
134       Assert.assertTrue(--count >= 0);
135     }
136     Assert.assertEquals(count, 0);
137   }
138 }