1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
71
72
73 @Test
74 public void testSmallReversedScan01() throws IOException {
75 String[][] keysCases = new String[][] {
76 { "d0", "d1", "d2", "d3" },
77 { "a0", "a1", "a2", "a3" },
78 { "a0", "b1", "c2", "d3" },
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
113
114
115
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 }