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  
19  package org.apache.hadoop.hbase.io.encoding;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.CellScanner;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.MediumTests;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.HBaseAdmin;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.client.Result;
35  import org.apache.hadoop.hbase.client.ResultScanner;
36  import org.apache.hadoop.hbase.client.Scan;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Rule;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  import org.junit.rules.TestName;
44  
45  @Category(MediumTests.class)
46  public class TestPrefixTree {
47    private static final String row4 = "a-b-B-2-1402397300-1402416535";
48    private static final byte[] row4_bytes = Bytes.toBytes(row4);
49    private static final String row3 = "a-b-A-1-1402397227-1402415999";
50    private static final byte[] row3_bytes = Bytes.toBytes(row3);
51    private static final String row2 = "a-b-A-1-1402329600-1402396277";
52    private static final byte[] row2_bytes = Bytes.toBytes(row2);
53    private static final String row1 = "a-b-A-1";
54    private static final byte[] row1_bytes = Bytes.toBytes(row1);
55    public static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56    private final static byte[] fam = Bytes.toBytes("cf_1");
57    private final static byte[] qual1 = Bytes.toBytes("qf_1");
58    private final static byte[] qual2 = Bytes.toBytes("qf_2");
59    public static Configuration conf;
60  
61    @Rule
62    public final TestName TEST_NAME = new TestName();
63  
64    @BeforeClass
65    public static void setupBeforeClass() throws Exception {
66      // setup configuration
67      conf = TEST_UTIL.getConfiguration();
68      conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
69      conf.setBoolean("hbase.online.schema.update.enable", true);
70      conf.setInt("hbase.client.scanner.timeout.period", 600000);
71      TEST_UTIL.startMiniCluster(2);
72    }
73  
74    @AfterClass
75    public static void tearDownAfterClass() throws Exception {
76      TEST_UTIL.shutdownMiniCluster();
77    }
78  
79    @Test
80    public void testHBASE11728() throws Exception {
81      TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
82      HTable table = null;
83      try {
84        HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin();
85        HColumnDescriptor colDesc = new HColumnDescriptor(fam);
86        HTableDescriptor desc = new HTableDescriptor(tableName);
87        colDesc.setDataBlockEncoding(DataBlockEncoding.PREFIX_TREE);
88        colDesc.setTimeToLive(15552000);
89        desc.addFamily(colDesc);
90        hBaseAdmin.createTable(desc);
91        table = new HTable(conf, tableName);
92        Put put = new Put(Bytes.toBytes("a-b-0-0"));
93        put.add(fam, qual1, Bytes.toBytes("c1-value"));
94        table.put(put);
95        table.flushCommits();
96        put = new Put(row1_bytes);
97        put.add(fam, qual1, Bytes.toBytes("c1-value"));
98        table.put(put);
99        table.flushCommits();
100       put = new Put(row2_bytes);
101       put.add(fam, qual2, Bytes.toBytes("c2-value"));
102       table.put(put);
103       table.flushCommits();
104       put = new Put(row3_bytes);
105       put.add(fam, qual2, Bytes.toBytes("c2-value-2"));
106       table.put(put);
107       table.flushCommits();
108       put = new Put(row4_bytes);
109       put.add(fam, qual2, Bytes.toBytes("c2-value-3"));
110       table.put(put);
111       table.flushCommits();
112       hBaseAdmin.flush(tableName.getNameAsString());
113       String[] rows = new String[3];
114       rows[0] = row1;
115       rows[1] = row2;
116       rows[2] = row3;
117       Scan scan = new Scan();
118       scan.setStartRow(row1_bytes);
119       scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
120       ResultScanner scanner = table.getScanner(scan);
121       Result[] next = scanner.next(10);
122       assertEquals(3, next.length);
123       int i = 0;
124       for (Result res : next) {
125         CellScanner cellScanner = res.cellScanner();
126         while (cellScanner.advance()) {
127           assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
128               .current().getRowOffset(), cellScanner.current().getRowLength()));
129         }
130         i++;
131       }
132       scanner.close();
133       // Add column
134       scan = new Scan();
135       scan.addColumn(fam, qual2);
136       scan.setStartRow(row1_bytes);
137       scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
138       scanner = table.getScanner(scan);
139       next = scanner.next(10);
140       assertEquals(2, next.length);
141       i = 1;
142       for (Result res : next) {
143         CellScanner cellScanner = res.cellScanner();
144         while (cellScanner.advance()) {
145           assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
146               .current().getRowOffset(), cellScanner.current().getRowLength()));
147         }
148         i++;
149       }
150       scanner.close();
151       i = 1;
152       scan = new Scan();
153       scan.addColumn(fam, qual2);
154       scan.setStartRow(Bytes.toBytes("a-b-A-1-"));
155       scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
156       scanner = table.getScanner(scan);
157       next = scanner.next(10);
158       assertEquals(2, next.length);
159       for (Result res : next) {
160         CellScanner cellScanner = res.cellScanner();
161         while (cellScanner.advance()) {
162           assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
163               .current().getRowOffset(), cellScanner.current().getRowLength()));
164         }
165         i++;
166       }
167       scanner.close();
168       scan = new Scan();
169       scan.addColumn(fam, qual2);
170       scan.setStartRow(Bytes.toBytes("a-b-A-1-140239"));
171       scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
172       scanner = table.getScanner(scan);
173       next = scanner.next(10);
174       assertEquals(1, next.length);
175       scanner.close();
176     } finally {
177       table.close();
178     }
179   }
180 }