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.assertArrayEquals;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.CellScanner;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HColumnDescriptor;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.client.Put;
38  import org.apache.hadoop.hbase.client.Result;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.regionserver.HRegion;
41  import org.apache.hadoop.hbase.regionserver.RegionScanner;
42  import org.apache.hadoop.hbase.testclassification.SmallTests;
43  import org.apache.hadoop.hbase.util.Bytes;
44  import org.junit.After;
45  import org.junit.Before;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  @Category(SmallTests.class)
50  public class TestPrefixTree {
51  
52    private static final String row4 = "a-b-B-2-1402397300-1402416535";
53    private static final byte[] row4_bytes = Bytes.toBytes(row4);
54    private static final String row3 = "a-b-A-1-1402397227-1402415999";
55    private static final byte[] row3_bytes = Bytes.toBytes(row3);
56    private static final String row2 = "a-b-A-1-1402329600-1402396277";
57    private static final byte[] row2_bytes = Bytes.toBytes(row2);
58    private static final String row1 = "a-b-A-1";
59    private static final byte[] row1_bytes = Bytes.toBytes(row1);
60  
61    private final static byte[] fam = Bytes.toBytes("cf_1");
62    private final static byte[] qual1 = Bytes.toBytes("qf_1");
63    private final static byte[] qual2 = Bytes.toBytes("qf_2");
64  
65    private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
66  
67    private HRegion region;
68  
69    @Before
70    public void setUp() throws Exception {
71      TableName tableName = TableName.valueOf(getClass().getSimpleName());
72      HTableDescriptor htd = new HTableDescriptor(tableName);
73      htd.addFamily(new HColumnDescriptor(fam).setDataBlockEncoding(DataBlockEncoding.PREFIX_TREE));
74      HRegionInfo info = new HRegionInfo(tableName, null, null, false);
75      Path path = testUtil.getDataTestDir(getClass().getSimpleName());
76      region = HRegion.createHRegion(info, path, testUtil.getConfiguration(), htd);
77    }
78  
79    @After
80    public void tearDown() throws Exception {
81      region.close(true);
82      testUtil.cleanupTestDir();
83    }
84  
85    @Test
86    public void testHBASE11728() throws Exception {
87      Put put = new Put(Bytes.toBytes("a-b-0-0"));
88      put.addColumn(fam, qual1, Bytes.toBytes("c1-value"));
89      region.put(put);
90      put = new Put(row1_bytes);
91      put.addColumn(fam, qual1, Bytes.toBytes("c1-value"));
92      region.put(put);
93      put = new Put(row2_bytes);
94      put.addColumn(fam, qual2, Bytes.toBytes("c2-value"));
95      region.put(put);
96      put = new Put(row3_bytes);
97      put.addColumn(fam, qual2, Bytes.toBytes("c2-value-2"));
98      region.put(put);
99      put = new Put(row4_bytes);
100     put.addColumn(fam, qual2, Bytes.toBytes("c2-value-3"));
101     region.put(put);
102     region.flush(true);
103     String[] rows = new String[3];
104     rows[0] = row1;
105     rows[1] = row2;
106     rows[2] = row3;
107     byte[][] val = new byte[3][];
108     val[0] = Bytes.toBytes("c1-value");
109     val[1] = Bytes.toBytes("c2-value");
110     val[2] = Bytes.toBytes("c2-value-2");
111     Scan scan = new Scan();
112     scan.setStartRow(row1_bytes);
113     scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
114 
115     RegionScanner scanner = region.getScanner(scan);
116     List<Cell> cells = new ArrayList<Cell>();
117     for (int i = 0; i < 3; i++) {
118       assertEquals(i < 2, scanner.next(cells));
119       CellScanner cellScanner = Result.create(cells).cellScanner();
120       while (cellScanner.advance()) {
121         assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
122             .current().getRowOffset(), cellScanner.current().getRowLength()));
123         assertEquals(Bytes.toString(val[i]), Bytes.toString(cellScanner.current().getValueArray(),
124           cellScanner.current().getValueOffset(), cellScanner.current().getValueLength()));
125       }
126       cells.clear();
127     }
128     scanner.close();
129 
130     // Add column
131     scan = new Scan();
132     scan.addColumn(fam, qual2);
133     scan.setStartRow(row1_bytes);
134     scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
135     scanner = region.getScanner(scan);
136     for (int i = 1; i < 3; i++) {
137       assertEquals(i < 2, scanner.next(cells));
138       CellScanner cellScanner = Result.create(cells).cellScanner();
139       while (cellScanner.advance()) {
140         assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
141             .current().getRowOffset(), cellScanner.current().getRowLength()));
142       }
143       cells.clear();
144     }
145     scanner.close();
146 
147     scan = new Scan();
148     scan.addColumn(fam, qual2);
149     scan.setStartRow(Bytes.toBytes("a-b-A-1-"));
150     scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
151     scanner = region.getScanner(scan);
152     for (int i = 1; i < 3; i++) {
153       assertEquals(i < 2, scanner.next(cells));
154       CellScanner cellScanner = Result.create(cells).cellScanner();
155       while (cellScanner.advance()) {
156         assertEquals(rows[i], Bytes.toString(cellScanner.current().getRowArray(), cellScanner
157             .current().getRowOffset(), cellScanner.current().getRowLength()));
158       }
159       cells.clear();
160     }
161     scanner.close();
162 
163     scan = new Scan();
164     scan.addColumn(fam, qual2);
165     scan.setStartRow(Bytes.toBytes("a-b-A-1-140239"));
166     scan.setStopRow(Bytes.toBytes("a-b-A-1:"));
167     scanner = region.getScanner(scan);
168     assertFalse(scanner.next(cells));
169     assertFalse(cells.isEmpty());
170     scanner.close();
171   }
172 
173   @Test
174   public void testHBASE12817() throws IOException {
175     for (int i = 0; i < 100; i++) {
176       region
177           .put(new Put(Bytes.toBytes("obj" + (2900 + i))).addColumn(fam, qual1, Bytes.toBytes(i)));
178     }
179     region.put(new Put(Bytes.toBytes("obj299")).addColumn(fam, qual1, Bytes.toBytes("whatever")));
180     region.put(new Put(Bytes.toBytes("obj29")).addColumn(fam, qual1, Bytes.toBytes("whatever")));
181     region.put(new Put(Bytes.toBytes("obj2")).addColumn(fam, qual1, Bytes.toBytes("whatever")));
182     region.put(new Put(Bytes.toBytes("obj3")).addColumn(fam, qual1, Bytes.toBytes("whatever")));
183     region.flush(true);
184     Scan scan = new Scan(Bytes.toBytes("obj29995"));
185     RegionScanner scanner = region.getScanner(scan);
186     List<Cell> cells = new ArrayList<Cell>();
187     assertFalse(scanner.next(cells));
188     assertArrayEquals(Bytes.toBytes("obj3"), Result.create(cells).getRow());
189   }
190 }